v3d: Add pack/unpack/decode support for fields with a "- 1" modifier.
[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->has_default = true;
361 field->default_value = strtoul(atts[i + 1], &p, 0);
362 } else if (strcmp(atts[i], "minus_one") == 0) {
363 assert(strcmp(atts[i + 1], "true") == 0);
364 field->minus_one = true;
365 }
366 }
367
368 if (size)
369 field->end = field->start + size - 1;
370
371 return field;
372 }
373
374 static struct v3d_value *
375 create_value(struct parser_context *ctx, const char **atts)
376 {
377 struct v3d_value *value = xzalloc(sizeof(*value));
378
379 for (int i = 0; atts[i]; i += 2) {
380 if (strcmp(atts[i], "name") == 0)
381 value->name = xstrdup(atts[i + 1]);
382 else if (strcmp(atts[i], "value") == 0)
383 value->value = strtoul(atts[i + 1], NULL, 0);
384 }
385
386 return value;
387 }
388
389 static void
390 create_and_append_field(struct parser_context *ctx,
391 const char **atts)
392 {
393 if (ctx->group->nfields == ctx->group->fields_size) {
394 ctx->group->fields_size = MAX2(ctx->group->fields_size * 2, 2);
395 ctx->group->fields =
396 (struct v3d_field **) realloc(ctx->group->fields,
397 sizeof(ctx->group->fields[0]) *
398 ctx->group->fields_size);
399 }
400
401 ctx->group->fields[ctx->group->nfields++] = create_field(ctx, atts);
402 }
403
404 static void
405 set_group_opcode(struct v3d_group *group, const char **atts)
406 {
407 char *p;
408 int i;
409
410 for (i = 0; atts[i]; i += 2) {
411 if (strcmp(atts[i], "code") == 0)
412 group->opcode = strtoul(atts[i + 1], &p, 0);
413 }
414 return;
415 }
416
417 static void
418 start_element(void *data, const char *element_name, const char **atts)
419 {
420 struct parser_context *ctx = data;
421 int i;
422 const char *name = NULL;
423 const char *ver = NULL;
424
425 ctx->loc.line_number = XML_GetCurrentLineNumber(ctx->parser);
426
427 for (i = 0; atts[i]; i += 2) {
428 if (strcmp(atts[i], "name") == 0)
429 name = atts[i + 1];
430 else if (strcmp(atts[i], "gen") == 0)
431 ver = atts[i + 1];
432 }
433
434 if (strcmp(element_name, "vcxml") == 0) {
435 if (ver == NULL)
436 fail(&ctx->loc, "no ver given");
437
438 int major, minor;
439 int n = sscanf(ver, "%d.%d", &major, &minor);
440 if (n == 0)
441 fail(&ctx->loc, "invalid ver given: %s", ver);
442 if (n == 1)
443 minor = 0;
444
445 ctx->spec->ver = major * 10 + minor;
446 } else if (strcmp(element_name, "packet") == 0 ||
447 strcmp(element_name, "struct") == 0) {
448 ctx->group = create_group(ctx, name, atts, NULL);
449
450 if (strcmp(element_name, "packet") == 0)
451 set_group_opcode(ctx->group, atts);
452 } else if (strcmp(element_name, "register") == 0) {
453 ctx->group = create_group(ctx, name, atts, NULL);
454 get_register_offset(atts, &ctx->group->register_offset);
455 } else if (strcmp(element_name, "group") == 0) {
456 struct v3d_group *previous_group = ctx->group;
457 while (previous_group->next)
458 previous_group = previous_group->next;
459
460 struct v3d_group *group = create_group(ctx, "", atts,
461 ctx->group);
462 previous_group->next = group;
463 ctx->group = group;
464 } else if (strcmp(element_name, "field") == 0) {
465 create_and_append_field(ctx, atts);
466 } else if (strcmp(element_name, "enum") == 0) {
467 ctx->enoom = create_enum(ctx, name, atts);
468 } else if (strcmp(element_name, "value") == 0) {
469 ctx->values[ctx->nvalues++] = create_value(ctx, atts);
470 assert(ctx->nvalues < ARRAY_SIZE(ctx->values));
471 }
472
473 }
474
475 static void
476 end_element(void *data, const char *name)
477 {
478 struct parser_context *ctx = data;
479 struct v3d_spec *spec = ctx->spec;
480
481 if (strcmp(name, "packet") == 0 ||
482 strcmp(name, "struct") == 0 ||
483 strcmp(name, "register") == 0) {
484 struct v3d_group *group = ctx->group;
485
486 ctx->group = ctx->group->parent;
487
488 if (strcmp(name, "packet") == 0) {
489 spec->commands[spec->ncommands++] = group;
490
491 /* V3D packet XML has the packet contents with offsets
492 * starting from the first bit after the opcode, to
493 * match the spec. Shift the fields up now.
494 */
495 for (int i = 0; i < group->nfields; i++) {
496 group->fields[i]->start += 8;
497 group->fields[i]->end += 8;
498 }
499 }
500 else if (strcmp(name, "struct") == 0)
501 spec->structs[spec->nstructs++] = group;
502 else if (strcmp(name, "register") == 0)
503 spec->registers[spec->nregisters++] = group;
504
505 assert(spec->ncommands < ARRAY_SIZE(spec->commands));
506 assert(spec->nstructs < ARRAY_SIZE(spec->structs));
507 assert(spec->nregisters < ARRAY_SIZE(spec->registers));
508 } else if (strcmp(name, "group") == 0) {
509 ctx->group = ctx->group->parent;
510 } else if (strcmp(name, "field") == 0) {
511 assert(ctx->group->nfields > 0);
512 struct v3d_field *field = ctx->group->fields[ctx->group->nfields - 1];
513 size_t size = ctx->nvalues * sizeof(ctx->values[0]);
514 field->inline_enum.values = xzalloc(size);
515 field->inline_enum.nvalues = ctx->nvalues;
516 memcpy(field->inline_enum.values, ctx->values, size);
517 ctx->nvalues = 0;
518 } else if (strcmp(name, "enum") == 0) {
519 struct v3d_enum *e = ctx->enoom;
520 size_t size = ctx->nvalues * sizeof(ctx->values[0]);
521 e->values = xzalloc(size);
522 e->nvalues = ctx->nvalues;
523 memcpy(e->values, ctx->values, size);
524 ctx->nvalues = 0;
525 ctx->enoom = NULL;
526 spec->enums[spec->nenums++] = e;
527 }
528 }
529
530 static void
531 character_data(void *data, const XML_Char *s, int len)
532 {
533 }
534
535 static uint32_t zlib_inflate(const void *compressed_data,
536 uint32_t compressed_len,
537 void **out_ptr)
538 {
539 struct z_stream_s zstream;
540 void *out;
541
542 memset(&zstream, 0, sizeof(zstream));
543
544 zstream.next_in = (unsigned char *)compressed_data;
545 zstream.avail_in = compressed_len;
546
547 if (inflateInit(&zstream) != Z_OK)
548 return 0;
549
550 out = malloc(4096);
551 zstream.next_out = out;
552 zstream.avail_out = 4096;
553
554 do {
555 switch (inflate(&zstream, Z_SYNC_FLUSH)) {
556 case Z_STREAM_END:
557 goto end;
558 case Z_OK:
559 break;
560 default:
561 inflateEnd(&zstream);
562 return 0;
563 }
564
565 if (zstream.avail_out)
566 break;
567
568 out = realloc(out, 2*zstream.total_out);
569 if (out == NULL) {
570 inflateEnd(&zstream);
571 return 0;
572 }
573
574 zstream.next_out = (unsigned char *)out + zstream.total_out;
575 zstream.avail_out = zstream.total_out;
576 } while (1);
577 end:
578 inflateEnd(&zstream);
579 *out_ptr = out;
580 return zstream.total_out;
581 }
582
583 struct v3d_spec *
584 v3d_spec_load(const struct v3d_device_info *devinfo)
585 {
586 struct parser_context ctx;
587 void *buf;
588 uint8_t *text_data = NULL;
589 uint32_t text_offset = 0, text_length = 0, total_length;
590
591 for (int i = 0; i < ARRAY_SIZE(genxml_files_table); i++) {
592 if (genxml_files_table[i].gen_10 == devinfo->ver) {
593 text_offset = genxml_files_table[i].offset;
594 text_length = genxml_files_table[i].length;
595 break;
596 }
597 }
598
599 if (text_length == 0) {
600 fprintf(stderr, "unable to find gen (%u) data\n", devinfo->ver);
601 return NULL;
602 }
603
604 memset(&ctx, 0, sizeof ctx);
605 ctx.parser = XML_ParserCreate(NULL);
606 XML_SetUserData(ctx.parser, &ctx);
607 if (ctx.parser == NULL) {
608 fprintf(stderr, "failed to create parser\n");
609 return NULL;
610 }
611
612 XML_SetElementHandler(ctx.parser, start_element, end_element);
613 XML_SetCharacterDataHandler(ctx.parser, character_data);
614
615 ctx.spec = xzalloc(sizeof(*ctx.spec));
616
617 total_length = zlib_inflate(compress_genxmls,
618 sizeof(compress_genxmls),
619 (void **) &text_data);
620 assert(text_offset + text_length <= total_length);
621
622 buf = XML_GetBuffer(ctx.parser, text_length);
623 memcpy(buf, &text_data[text_offset], text_length);
624
625 if (XML_ParseBuffer(ctx.parser, text_length, true) == 0) {
626 fprintf(stderr,
627 "Error parsing XML at line %ld col %ld byte %ld/%u: %s\n",
628 XML_GetCurrentLineNumber(ctx.parser),
629 XML_GetCurrentColumnNumber(ctx.parser),
630 XML_GetCurrentByteIndex(ctx.parser), text_length,
631 XML_ErrorString(XML_GetErrorCode(ctx.parser)));
632 XML_ParserFree(ctx.parser);
633 free(text_data);
634 return NULL;
635 }
636
637 XML_ParserFree(ctx.parser);
638 free(text_data);
639
640 return ctx.spec;
641 }
642
643 struct v3d_group *
644 v3d_spec_find_instruction(struct v3d_spec *spec, const uint8_t *p)
645 {
646 uint8_t opcode = *p;
647
648 for (int i = 0; i < spec->ncommands; i++) {
649 struct v3d_group *group = spec->commands[i];
650
651 if (opcode != group->opcode)
652 continue;
653
654 /* If there's a "sub-id" field, make sure that it matches the
655 * instruction being decoded.
656 */
657 struct v3d_field *subid = NULL;
658 for (int j = 0; j < group->nfields; j++) {
659 struct v3d_field *field = group->fields[j];
660 if (strcmp(field->name, "sub-id") == 0) {
661 subid = field;
662 break;
663 }
664 }
665
666 if (subid && (__gen_unpack_uint(p, subid->start, subid->end) !=
667 subid->default_value)) {
668 continue;
669 }
670
671 return group;
672 }
673
674 return NULL;
675 }
676
677 /** Returns the size of a V3D packet. */
678 int
679 v3d_group_get_length(struct v3d_group *group)
680 {
681 int last_bit = 0;
682 for (int i = 0; i < group->nfields; i++) {
683 struct v3d_field *field = group->fields[i];
684
685 last_bit = MAX2(last_bit, field->end);
686 }
687 return last_bit / 8 + 1;
688 }
689
690 void
691 v3d_field_iterator_init(struct v3d_field_iterator *iter,
692 struct v3d_group *group,
693 const uint8_t *p,
694 bool print_colors)
695 {
696 memset(iter, 0, sizeof(*iter));
697
698 iter->group = group;
699 iter->p = p;
700 iter->print_colors = print_colors;
701 }
702
703 static const char *
704 v3d_get_enum_name(struct v3d_enum *e, uint64_t value)
705 {
706 for (int i = 0; i < e->nvalues; i++) {
707 if (e->values[i]->value == value) {
708 return e->values[i]->name;
709 }
710 }
711 return NULL;
712 }
713
714 static bool
715 iter_more_fields(const struct v3d_field_iterator *iter)
716 {
717 return iter->field_iter < iter->group->nfields;
718 }
719
720 static uint32_t
721 iter_group_offset_bits(const struct v3d_field_iterator *iter,
722 uint32_t group_iter)
723 {
724 return iter->group->group_offset + (group_iter *
725 iter->group->group_size);
726 }
727
728 static bool
729 iter_more_groups(const struct v3d_field_iterator *iter)
730 {
731 if (iter->group->variable) {
732 return iter_group_offset_bits(iter, iter->group_iter + 1) <
733 (v3d_group_get_length(iter->group) * 8);
734 } else {
735 return (iter->group_iter + 1) < iter->group->group_count ||
736 iter->group->next != NULL;
737 }
738 }
739
740 static void
741 iter_advance_group(struct v3d_field_iterator *iter)
742 {
743 if (iter->group->variable)
744 iter->group_iter++;
745 else {
746 if ((iter->group_iter + 1) < iter->group->group_count) {
747 iter->group_iter++;
748 } else {
749 iter->group = iter->group->next;
750 iter->group_iter = 0;
751 }
752 }
753
754 iter->field_iter = 0;
755 }
756
757 static bool
758 iter_advance_field(struct v3d_field_iterator *iter)
759 {
760 while (!iter_more_fields(iter)) {
761 if (!iter_more_groups(iter))
762 return false;
763
764 iter_advance_group(iter);
765 }
766
767 iter->field = iter->group->fields[iter->field_iter++];
768 if (iter->field->name)
769 strncpy(iter->name, iter->field->name, sizeof(iter->name));
770 else
771 memset(iter->name, 0, sizeof(iter->name));
772 iter->offset = iter_group_offset_bits(iter, iter->group_iter) / 8 +
773 iter->field->start / 8;
774 iter->struct_desc = NULL;
775
776 return true;
777 }
778
779 bool
780 v3d_field_iterator_next(struct v3d_field_iterator *iter)
781 {
782 if (!iter_advance_field(iter))
783 return false;
784
785 const char *enum_name = NULL;
786
787 int group_member_offset =
788 iter_group_offset_bits(iter, iter->group_iter);
789 int s = group_member_offset + iter->field->start;
790 int e = group_member_offset + iter->field->end;
791
792 assert(!iter->field->minus_one ||
793 iter->field->type.kind == V3D_TYPE_INT ||
794 iter->field->type.kind == V3D_TYPE_UINT);
795
796 switch (iter->field->type.kind) {
797 case V3D_TYPE_UNKNOWN:
798 case V3D_TYPE_INT: {
799 uint32_t value = __gen_unpack_sint(iter->p, s, e);
800 if (iter->field->minus_one)
801 value++;
802 snprintf(iter->value, sizeof(iter->value), "%d", value);
803 enum_name = v3d_get_enum_name(&iter->field->inline_enum, value);
804 break;
805 }
806 case V3D_TYPE_UINT: {
807 uint32_t value = __gen_unpack_uint(iter->p, s, e);
808 if (iter->field->minus_one)
809 value++;
810 snprintf(iter->value, sizeof(iter->value), "%u", value);
811 enum_name = v3d_get_enum_name(&iter->field->inline_enum, value);
812 break;
813 }
814 case V3D_TYPE_BOOL: {
815 const char *true_string =
816 iter->print_colors ? "\e[0;35mtrue\e[0m" : "true";
817 snprintf(iter->value, sizeof(iter->value), "%s",
818 __gen_unpack_uint(iter->p, s, e) ?
819 true_string : "false");
820 break;
821 }
822 case V3D_TYPE_FLOAT:
823 snprintf(iter->value, sizeof(iter->value), "%f",
824 __gen_unpack_float(iter->p, s, e));
825 break;
826 case V3D_TYPE_ADDRESS:
827 case V3D_TYPE_OFFSET:
828 snprintf(iter->value, sizeof(iter->value), "0x%08"PRIx64,
829 __gen_unpack_uint(iter->p, s, e) << (31 - (e - s)));
830 break;
831 case V3D_TYPE_STRUCT:
832 snprintf(iter->value, sizeof(iter->value), "<struct %s>",
833 iter->field->type.v3d_struct->name);
834 iter->struct_desc =
835 v3d_spec_find_struct(iter->group->spec,
836 iter->field->type.v3d_struct->name);
837 break;
838 case V3D_TYPE_SFIXED:
839 snprintf(iter->value, sizeof(iter->value), "%f",
840 __gen_unpack_sfixed(iter->p, s, e,
841 iter->field->type.f));
842 break;
843 case V3D_TYPE_UFIXED:
844 snprintf(iter->value, sizeof(iter->value), "%f",
845 __gen_unpack_ufixed(iter->p, s, e,
846 iter->field->type.f));
847 break;
848 case V3D_TYPE_MBO:
849 break;
850 case V3D_TYPE_ENUM: {
851 uint32_t value = __gen_unpack_uint(iter->p, s, e);
852 snprintf(iter->value, sizeof(iter->value), "%d", value);
853 enum_name = v3d_get_enum_name(iter->field->type.v3d_enum, value);
854 break;
855 }
856 }
857
858 if (strlen(iter->group->name) == 0) {
859 int length = strlen(iter->name);
860 snprintf(iter->name + length, sizeof(iter->name) - length,
861 "[%i]", iter->group_iter);
862 }
863
864 if (enum_name) {
865 int length = strlen(iter->value);
866 snprintf(iter->value + length, sizeof(iter->value) - length,
867 " (%s)", enum_name);
868 }
869
870 return true;
871 }
872
873 void
874 v3d_print_group(FILE *outfile, struct v3d_group *group,
875 uint64_t offset, const uint8_t *p, bool color)
876 {
877 struct v3d_field_iterator iter;
878
879 v3d_field_iterator_init(&iter, group, p, color);
880 while (v3d_field_iterator_next(&iter)) {
881 fprintf(outfile, " %s: %s\n", iter.name, iter.value);
882 if (iter.struct_desc) {
883 uint64_t struct_offset = offset + iter.offset;
884 v3d_print_group(outfile, iter.struct_desc,
885 struct_offset,
886 &p[iter.offset], color);
887 }
888 }
889 }