v3d: Create XML fields for min_ver and max_ver of a packet/struct/enum.
[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 const struct v3d_device_info *devinfo;
62 int foo;
63 struct location loc;
64
65 struct v3d_group *group;
66 struct v3d_enum *enoom;
67
68 int nvalues;
69 struct v3d_value *values[256];
70
71 struct v3d_spec *spec;
72
73 int parse_depth;
74 int parse_skip_depth;
75 };
76
77 const char *
78 v3d_group_get_name(struct v3d_group *group)
79 {
80 return group->name;
81 }
82
83 uint8_t
84 v3d_group_get_opcode(struct v3d_group *group)
85 {
86 return group->opcode;
87 }
88
89 struct v3d_group *
90 v3d_spec_find_struct(struct v3d_spec *spec, const char *name)
91 {
92 for (int i = 0; i < spec->nstructs; i++)
93 if (strcmp(spec->structs[i]->name, name) == 0)
94 return spec->structs[i];
95
96 return NULL;
97 }
98
99 struct v3d_group *
100 v3d_spec_find_register(struct v3d_spec *spec, uint32_t offset)
101 {
102 for (int i = 0; i < spec->nregisters; i++)
103 if (spec->registers[i]->register_offset == offset)
104 return spec->registers[i];
105
106 return NULL;
107 }
108
109 struct v3d_group *
110 v3d_spec_find_register_by_name(struct v3d_spec *spec, const char *name)
111 {
112 for (int i = 0; i < spec->nregisters; i++) {
113 if (strcmp(spec->registers[i]->name, name) == 0)
114 return spec->registers[i];
115 }
116
117 return NULL;
118 }
119
120 struct v3d_enum *
121 v3d_spec_find_enum(struct v3d_spec *spec, const char *name)
122 {
123 for (int i = 0; i < spec->nenums; i++)
124 if (strcmp(spec->enums[i]->name, name) == 0)
125 return spec->enums[i];
126
127 return NULL;
128 }
129
130 static void __attribute__((noreturn))
131 fail(struct location *loc, const char *msg, ...)
132 {
133 va_list ap;
134
135 va_start(ap, msg);
136 fprintf(stderr, "%s:%d: error: ",
137 loc->filename, loc->line_number);
138 vfprintf(stderr, msg, ap);
139 fprintf(stderr, "\n");
140 va_end(ap);
141 exit(EXIT_FAILURE);
142 }
143
144 static void *
145 fail_on_null(void *p)
146 {
147 if (p == NULL) {
148 fprintf(stderr, "aubinator: out of memory\n");
149 exit(EXIT_FAILURE);
150 }
151
152 return p;
153 }
154
155 static char *
156 xstrdup(const char *s)
157 {
158 return fail_on_null(strdup(s));
159 }
160
161 static void *
162 zalloc(size_t s)
163 {
164 return calloc(s, 1);
165 }
166
167 static void *
168 xzalloc(size_t s)
169 {
170 return fail_on_null(zalloc(s));
171 }
172
173 /* We allow fields to have either a bit index, or append "b" for a byte index.
174 */
175 static bool
176 is_byte_offset(const char *value)
177 {
178 return value[strlen(value) - 1] == 'b';
179 }
180
181 static void
182 get_group_offset_count(const char **atts, uint32_t *offset, uint32_t *count,
183 uint32_t *size, bool *variable)
184 {
185 char *p;
186 int i;
187
188 for (i = 0; atts[i]; i += 2) {
189 if (strcmp(atts[i], "count") == 0) {
190 *count = strtoul(atts[i + 1], &p, 0);
191 if (*count == 0)
192 *variable = true;
193 } else if (strcmp(atts[i], "start") == 0) {
194 *offset = strtoul(atts[i + 1], &p, 0);
195 } else if (strcmp(atts[i], "size") == 0) {
196 *size = strtoul(atts[i + 1], &p, 0);
197 }
198 }
199 return;
200 }
201
202 static struct v3d_group *
203 create_group(struct parser_context *ctx,
204 const char *name,
205 const char **atts,
206 struct v3d_group *parent)
207 {
208 struct v3d_group *group;
209
210 group = xzalloc(sizeof(*group));
211 if (name)
212 group->name = xstrdup(name);
213
214 group->spec = ctx->spec;
215 group->group_offset = 0;
216 group->group_count = 0;
217 group->variable = false;
218
219 if (parent) {
220 group->parent = parent;
221 get_group_offset_count(atts,
222 &group->group_offset,
223 &group->group_count,
224 &group->group_size,
225 &group->variable);
226 }
227
228 return group;
229 }
230
231 static struct v3d_enum *
232 create_enum(struct parser_context *ctx, const char *name, const char **atts)
233 {
234 struct v3d_enum *e;
235
236 e = xzalloc(sizeof(*e));
237 if (name)
238 e->name = xstrdup(name);
239
240 e->nvalues = 0;
241
242 return e;
243 }
244
245 static void
246 get_register_offset(const char **atts, uint32_t *offset)
247 {
248 char *p;
249 int i;
250
251 for (i = 0; atts[i]; i += 2) {
252 if (strcmp(atts[i], "num") == 0)
253 *offset = strtoul(atts[i + 1], &p, 0);
254 }
255 return;
256 }
257
258 static void
259 get_start_end_pos(int *start, int *end)
260 {
261 /* start value has to be mod with 32 as we need the relative
262 * start position in the first DWord. For the end position, add
263 * the length of the field to the start position to get the
264 * relative postion in the 64 bit address.
265 */
266 if (*end - *start > 32) {
267 int len = *end - *start;
268 *start = *start % 32;
269 *end = *start + len;
270 } else {
271 *start = *start % 32;
272 *end = *end % 32;
273 }
274
275 return;
276 }
277
278 static inline uint64_t
279 mask(int start, int end)
280 {
281 uint64_t v;
282
283 v = ~0ULL >> (63 - end + start);
284
285 return v << start;
286 }
287
288 static inline uint64_t
289 field(uint64_t value, int start, int end)
290 {
291 get_start_end_pos(&start, &end);
292 return (value & mask(start, end)) >> (start);
293 }
294
295 static inline uint64_t
296 field_address(uint64_t value, int start, int end)
297 {
298 /* no need to right shift for address/offset */
299 get_start_end_pos(&start, &end);
300 return (value & mask(start, end));
301 }
302
303 static struct v3d_type
304 string_to_type(struct parser_context *ctx, const char *s)
305 {
306 int i, f;
307 struct v3d_group *g;
308 struct v3d_enum *e;
309
310 if (strcmp(s, "int") == 0)
311 return (struct v3d_type) { .kind = V3D_TYPE_INT };
312 else if (strcmp(s, "uint") == 0)
313 return (struct v3d_type) { .kind = V3D_TYPE_UINT };
314 else if (strcmp(s, "bool") == 0)
315 return (struct v3d_type) { .kind = V3D_TYPE_BOOL };
316 else if (strcmp(s, "float") == 0)
317 return (struct v3d_type) { .kind = V3D_TYPE_FLOAT };
318 else if (strcmp(s, "address") == 0)
319 return (struct v3d_type) { .kind = V3D_TYPE_ADDRESS };
320 else if (strcmp(s, "offset") == 0)
321 return (struct v3d_type) { .kind = V3D_TYPE_OFFSET };
322 else if (sscanf(s, "u%d.%d", &i, &f) == 2)
323 return (struct v3d_type) { .kind = V3D_TYPE_UFIXED, .i = i, .f = f };
324 else if (sscanf(s, "s%d.%d", &i, &f) == 2)
325 return (struct v3d_type) { .kind = V3D_TYPE_SFIXED, .i = i, .f = f };
326 else if (g = v3d_spec_find_struct(ctx->spec, s), g != NULL)
327 return (struct v3d_type) { .kind = V3D_TYPE_STRUCT, .v3d_struct = g };
328 else if (e = v3d_spec_find_enum(ctx->spec, s), e != NULL)
329 return (struct v3d_type) { .kind = V3D_TYPE_ENUM, .v3d_enum = e };
330 else if (strcmp(s, "mbo") == 0)
331 return (struct v3d_type) { .kind = V3D_TYPE_MBO };
332 else
333 fail(&ctx->loc, "invalid type: %s", s);
334 }
335
336 static struct v3d_field *
337 create_field(struct parser_context *ctx, const char **atts)
338 {
339 struct v3d_field *field;
340 char *p;
341 int i;
342 uint32_t size = 0;
343
344 field = xzalloc(sizeof(*field));
345
346 for (i = 0; atts[i]; i += 2) {
347 if (strcmp(atts[i], "name") == 0)
348 field->name = xstrdup(atts[i + 1]);
349 else if (strcmp(atts[i], "start") == 0) {
350 field->start = strtoul(atts[i + 1], &p, 0);
351 if (is_byte_offset(atts[i + 1]))
352 field->start *= 8;
353 } else if (strcmp(atts[i], "end") == 0) {
354 field->end = strtoul(atts[i + 1], &p, 0) - 1;
355 if (is_byte_offset(atts[i + 1]))
356 field->end *= 8;
357 } else if (strcmp(atts[i], "size") == 0) {
358 size = strtoul(atts[i + 1], &p, 0);
359 if (is_byte_offset(atts[i + 1]))
360 size *= 8;
361 } else if (strcmp(atts[i], "type") == 0)
362 field->type = string_to_type(ctx, atts[i + 1]);
363 else if (strcmp(atts[i], "default") == 0) {
364 field->has_default = true;
365 field->default_value = strtoul(atts[i + 1], &p, 0);
366 } else if (strcmp(atts[i], "minus_one") == 0) {
367 assert(strcmp(atts[i + 1], "true") == 0);
368 field->minus_one = true;
369 }
370 }
371
372 if (size)
373 field->end = field->start + size - 1;
374
375 return field;
376 }
377
378 static struct v3d_value *
379 create_value(struct parser_context *ctx, const char **atts)
380 {
381 struct v3d_value *value = xzalloc(sizeof(*value));
382
383 for (int i = 0; atts[i]; i += 2) {
384 if (strcmp(atts[i], "name") == 0)
385 value->name = xstrdup(atts[i + 1]);
386 else if (strcmp(atts[i], "value") == 0)
387 value->value = strtoul(atts[i + 1], NULL, 0);
388 }
389
390 return value;
391 }
392
393 static void
394 create_and_append_field(struct parser_context *ctx,
395 const char **atts)
396 {
397 if (ctx->group->nfields == ctx->group->fields_size) {
398 ctx->group->fields_size = MAX2(ctx->group->fields_size * 2, 2);
399 ctx->group->fields =
400 (struct v3d_field **) realloc(ctx->group->fields,
401 sizeof(ctx->group->fields[0]) *
402 ctx->group->fields_size);
403 }
404
405 ctx->group->fields[ctx->group->nfields++] = create_field(ctx, atts);
406 }
407
408 static void
409 set_group_opcode(struct v3d_group *group, const char **atts)
410 {
411 char *p;
412 int i;
413
414 for (i = 0; atts[i]; i += 2) {
415 if (strcmp(atts[i], "code") == 0)
416 group->opcode = strtoul(atts[i + 1], &p, 0);
417 }
418 return;
419 }
420
421 static bool
422 ver_in_range(int ver, int min_ver, int max_ver)
423 {
424 return ((min_ver == 0 || ver >= min_ver) &&
425 (max_ver == 0 || ver <= max_ver));
426 }
427
428 static bool
429 skip_if_ver_mismatch(struct parser_context *ctx, int min_ver, int max_ver)
430 {
431 if (!ctx->parse_skip_depth && !ver_in_range(ctx->devinfo->ver,
432 min_ver, max_ver)) {
433 assert(ctx->parse_depth != 0);
434 ctx->parse_skip_depth = ctx->parse_depth;
435 }
436
437 return ctx->parse_skip_depth;
438 }
439
440 static void
441 start_element(void *data, const char *element_name, const char **atts)
442 {
443 struct parser_context *ctx = data;
444 int i;
445 const char *name = NULL;
446 const char *ver = NULL;
447 int min_ver = 0;
448 int max_ver = 0;
449
450 ctx->loc.line_number = XML_GetCurrentLineNumber(ctx->parser);
451
452 for (i = 0; atts[i]; i += 2) {
453 if (strcmp(atts[i], "name") == 0)
454 name = atts[i + 1];
455 else if (strcmp(atts[i], "gen") == 0)
456 ver = atts[i + 1];
457 else if (strcmp(atts[i], "min_ver") == 0)
458 min_ver = strtoul(atts[i + 1], NULL, 0);
459 else if (strcmp(atts[i], "max_ver") == 0)
460 max_ver = strtoul(atts[i + 1], NULL, 0);
461 }
462
463 if (skip_if_ver_mismatch(ctx, min_ver, max_ver))
464 goto skip;
465
466 if (strcmp(element_name, "vcxml") == 0) {
467 if (ver == NULL)
468 fail(&ctx->loc, "no ver given");
469
470 int major, minor;
471 int n = sscanf(ver, "%d.%d", &major, &minor);
472 if (n == 0)
473 fail(&ctx->loc, "invalid ver given: %s", ver);
474 if (n == 1)
475 minor = 0;
476
477 ctx->spec->ver = major * 10 + minor;
478 } else if (strcmp(element_name, "packet") == 0 ||
479 strcmp(element_name, "struct") == 0) {
480 ctx->group = create_group(ctx, name, atts, NULL);
481
482 if (strcmp(element_name, "packet") == 0)
483 set_group_opcode(ctx->group, atts);
484 } else if (strcmp(element_name, "register") == 0) {
485 ctx->group = create_group(ctx, name, atts, NULL);
486 get_register_offset(atts, &ctx->group->register_offset);
487 } else if (strcmp(element_name, "group") == 0) {
488 struct v3d_group *previous_group = ctx->group;
489 while (previous_group->next)
490 previous_group = previous_group->next;
491
492 struct v3d_group *group = create_group(ctx, "", atts,
493 ctx->group);
494 previous_group->next = group;
495 ctx->group = group;
496 } else if (strcmp(element_name, "field") == 0) {
497 create_and_append_field(ctx, atts);
498 } else if (strcmp(element_name, "enum") == 0) {
499 ctx->enoom = create_enum(ctx, name, atts);
500 } else if (strcmp(element_name, "value") == 0) {
501 ctx->values[ctx->nvalues++] = create_value(ctx, atts);
502 assert(ctx->nvalues < ARRAY_SIZE(ctx->values));
503 }
504
505 skip:
506 ctx->parse_depth++;
507 }
508
509 static void
510 end_element(void *data, const char *name)
511 {
512 struct parser_context *ctx = data;
513 struct v3d_spec *spec = ctx->spec;
514
515 ctx->parse_depth--;
516
517 if (ctx->parse_skip_depth) {
518 if (ctx->parse_skip_depth == ctx->parse_depth)
519 ctx->parse_skip_depth = 0;
520 return;
521 }
522
523 if (strcmp(name, "packet") == 0 ||
524 strcmp(name, "struct") == 0 ||
525 strcmp(name, "register") == 0) {
526 struct v3d_group *group = ctx->group;
527
528 ctx->group = ctx->group->parent;
529
530 if (strcmp(name, "packet") == 0) {
531 spec->commands[spec->ncommands++] = group;
532
533 /* V3D packet XML has the packet contents with offsets
534 * starting from the first bit after the opcode, to
535 * match the spec. Shift the fields up now.
536 */
537 for (int i = 0; i < group->nfields; i++) {
538 group->fields[i]->start += 8;
539 group->fields[i]->end += 8;
540 }
541 }
542 else if (strcmp(name, "struct") == 0)
543 spec->structs[spec->nstructs++] = group;
544 else if (strcmp(name, "register") == 0)
545 spec->registers[spec->nregisters++] = group;
546
547 assert(spec->ncommands < ARRAY_SIZE(spec->commands));
548 assert(spec->nstructs < ARRAY_SIZE(spec->structs));
549 assert(spec->nregisters < ARRAY_SIZE(spec->registers));
550 } else if (strcmp(name, "group") == 0) {
551 ctx->group = ctx->group->parent;
552 } else if (strcmp(name, "field") == 0) {
553 assert(ctx->group->nfields > 0);
554 struct v3d_field *field = ctx->group->fields[ctx->group->nfields - 1];
555 size_t size = ctx->nvalues * sizeof(ctx->values[0]);
556 field->inline_enum.values = xzalloc(size);
557 field->inline_enum.nvalues = ctx->nvalues;
558 memcpy(field->inline_enum.values, ctx->values, size);
559 ctx->nvalues = 0;
560 } else if (strcmp(name, "enum") == 0) {
561 struct v3d_enum *e = ctx->enoom;
562 size_t size = ctx->nvalues * sizeof(ctx->values[0]);
563 e->values = xzalloc(size);
564 e->nvalues = ctx->nvalues;
565 memcpy(e->values, ctx->values, size);
566 ctx->nvalues = 0;
567 ctx->enoom = NULL;
568 spec->enums[spec->nenums++] = e;
569 }
570 }
571
572 static void
573 character_data(void *data, const XML_Char *s, int len)
574 {
575 }
576
577 static uint32_t zlib_inflate(const void *compressed_data,
578 uint32_t compressed_len,
579 void **out_ptr)
580 {
581 struct z_stream_s zstream;
582 void *out;
583
584 memset(&zstream, 0, sizeof(zstream));
585
586 zstream.next_in = (unsigned char *)compressed_data;
587 zstream.avail_in = compressed_len;
588
589 if (inflateInit(&zstream) != Z_OK)
590 return 0;
591
592 out = malloc(4096);
593 zstream.next_out = out;
594 zstream.avail_out = 4096;
595
596 do {
597 switch (inflate(&zstream, Z_SYNC_FLUSH)) {
598 case Z_STREAM_END:
599 goto end;
600 case Z_OK:
601 break;
602 default:
603 inflateEnd(&zstream);
604 return 0;
605 }
606
607 if (zstream.avail_out)
608 break;
609
610 out = realloc(out, 2*zstream.total_out);
611 if (out == NULL) {
612 inflateEnd(&zstream);
613 return 0;
614 }
615
616 zstream.next_out = (unsigned char *)out + zstream.total_out;
617 zstream.avail_out = zstream.total_out;
618 } while (1);
619 end:
620 inflateEnd(&zstream);
621 *out_ptr = out;
622 return zstream.total_out;
623 }
624
625 struct v3d_spec *
626 v3d_spec_load(const struct v3d_device_info *devinfo)
627 {
628 struct parser_context ctx;
629 void *buf;
630 uint8_t *text_data = NULL;
631 uint32_t text_offset = 0, text_length = 0, total_length;
632
633 for (int i = 0; i < ARRAY_SIZE(genxml_files_table); i++) {
634 if (genxml_files_table[i].gen_10 == devinfo->ver) {
635 text_offset = genxml_files_table[i].offset;
636 text_length = genxml_files_table[i].length;
637 break;
638 }
639 }
640
641 if (text_length == 0) {
642 fprintf(stderr, "unable to find gen (%u) data\n", devinfo->ver);
643 return NULL;
644 }
645
646 memset(&ctx, 0, sizeof ctx);
647 ctx.parser = XML_ParserCreate(NULL);
648 ctx.devinfo = devinfo;
649 XML_SetUserData(ctx.parser, &ctx);
650 if (ctx.parser == NULL) {
651 fprintf(stderr, "failed to create parser\n");
652 return NULL;
653 }
654
655 XML_SetElementHandler(ctx.parser, start_element, end_element);
656 XML_SetCharacterDataHandler(ctx.parser, character_data);
657
658 ctx.spec = xzalloc(sizeof(*ctx.spec));
659
660 total_length = zlib_inflate(compress_genxmls,
661 sizeof(compress_genxmls),
662 (void **) &text_data);
663 assert(text_offset + text_length <= total_length);
664
665 buf = XML_GetBuffer(ctx.parser, text_length);
666 memcpy(buf, &text_data[text_offset], text_length);
667
668 if (XML_ParseBuffer(ctx.parser, text_length, true) == 0) {
669 fprintf(stderr,
670 "Error parsing XML at line %ld col %ld byte %ld/%u: %s\n",
671 XML_GetCurrentLineNumber(ctx.parser),
672 XML_GetCurrentColumnNumber(ctx.parser),
673 XML_GetCurrentByteIndex(ctx.parser), text_length,
674 XML_ErrorString(XML_GetErrorCode(ctx.parser)));
675 XML_ParserFree(ctx.parser);
676 free(text_data);
677 return NULL;
678 }
679
680 XML_ParserFree(ctx.parser);
681 free(text_data);
682
683 return ctx.spec;
684 }
685
686 struct v3d_group *
687 v3d_spec_find_instruction(struct v3d_spec *spec, const uint8_t *p)
688 {
689 uint8_t opcode = *p;
690
691 for (int i = 0; i < spec->ncommands; i++) {
692 struct v3d_group *group = spec->commands[i];
693
694 if (opcode != group->opcode)
695 continue;
696
697 /* If there's a "sub-id" field, make sure that it matches the
698 * instruction being decoded.
699 */
700 struct v3d_field *subid = NULL;
701 for (int j = 0; j < group->nfields; j++) {
702 struct v3d_field *field = group->fields[j];
703 if (strcmp(field->name, "sub-id") == 0) {
704 subid = field;
705 break;
706 }
707 }
708
709 if (subid && (__gen_unpack_uint(p, subid->start, subid->end) !=
710 subid->default_value)) {
711 continue;
712 }
713
714 return group;
715 }
716
717 return NULL;
718 }
719
720 /** Returns the size of a V3D packet. */
721 int
722 v3d_group_get_length(struct v3d_group *group)
723 {
724 int last_bit = 0;
725 for (int i = 0; i < group->nfields; i++) {
726 struct v3d_field *field = group->fields[i];
727
728 last_bit = MAX2(last_bit, field->end);
729 }
730 return last_bit / 8 + 1;
731 }
732
733 void
734 v3d_field_iterator_init(struct v3d_field_iterator *iter,
735 struct v3d_group *group,
736 const uint8_t *p,
737 bool print_colors)
738 {
739 memset(iter, 0, sizeof(*iter));
740
741 iter->group = group;
742 iter->p = p;
743 iter->print_colors = print_colors;
744 }
745
746 static const char *
747 v3d_get_enum_name(struct v3d_enum *e, uint64_t value)
748 {
749 for (int i = 0; i < e->nvalues; i++) {
750 if (e->values[i]->value == value) {
751 return e->values[i]->name;
752 }
753 }
754 return NULL;
755 }
756
757 static bool
758 iter_more_fields(const struct v3d_field_iterator *iter)
759 {
760 return iter->field_iter < iter->group->nfields;
761 }
762
763 static uint32_t
764 iter_group_offset_bits(const struct v3d_field_iterator *iter,
765 uint32_t group_iter)
766 {
767 return iter->group->group_offset + (group_iter *
768 iter->group->group_size);
769 }
770
771 static bool
772 iter_more_groups(const struct v3d_field_iterator *iter)
773 {
774 if (iter->group->variable) {
775 return iter_group_offset_bits(iter, iter->group_iter + 1) <
776 (v3d_group_get_length(iter->group) * 8);
777 } else {
778 return (iter->group_iter + 1) < iter->group->group_count ||
779 iter->group->next != NULL;
780 }
781 }
782
783 static void
784 iter_advance_group(struct v3d_field_iterator *iter)
785 {
786 if (iter->group->variable)
787 iter->group_iter++;
788 else {
789 if ((iter->group_iter + 1) < iter->group->group_count) {
790 iter->group_iter++;
791 } else {
792 iter->group = iter->group->next;
793 iter->group_iter = 0;
794 }
795 }
796
797 iter->field_iter = 0;
798 }
799
800 static bool
801 iter_advance_field(struct v3d_field_iterator *iter)
802 {
803 while (!iter_more_fields(iter)) {
804 if (!iter_more_groups(iter))
805 return false;
806
807 iter_advance_group(iter);
808 }
809
810 iter->field = iter->group->fields[iter->field_iter++];
811 if (iter->field->name)
812 strncpy(iter->name, iter->field->name, sizeof(iter->name));
813 else
814 memset(iter->name, 0, sizeof(iter->name));
815 iter->offset = iter_group_offset_bits(iter, iter->group_iter) / 8 +
816 iter->field->start / 8;
817 iter->struct_desc = NULL;
818
819 return true;
820 }
821
822 bool
823 v3d_field_iterator_next(struct v3d_field_iterator *iter)
824 {
825 if (!iter_advance_field(iter))
826 return false;
827
828 const char *enum_name = NULL;
829
830 int group_member_offset =
831 iter_group_offset_bits(iter, iter->group_iter);
832 int s = group_member_offset + iter->field->start;
833 int e = group_member_offset + iter->field->end;
834
835 assert(!iter->field->minus_one ||
836 iter->field->type.kind == V3D_TYPE_INT ||
837 iter->field->type.kind == V3D_TYPE_UINT);
838
839 switch (iter->field->type.kind) {
840 case V3D_TYPE_UNKNOWN:
841 case V3D_TYPE_INT: {
842 uint32_t value = __gen_unpack_sint(iter->p, s, e);
843 if (iter->field->minus_one)
844 value++;
845 snprintf(iter->value, sizeof(iter->value), "%d", value);
846 enum_name = v3d_get_enum_name(&iter->field->inline_enum, value);
847 break;
848 }
849 case V3D_TYPE_UINT: {
850 uint32_t value = __gen_unpack_uint(iter->p, s, e);
851 if (iter->field->minus_one)
852 value++;
853 snprintf(iter->value, sizeof(iter->value), "%u", value);
854 enum_name = v3d_get_enum_name(&iter->field->inline_enum, value);
855 break;
856 }
857 case V3D_TYPE_BOOL: {
858 const char *true_string =
859 iter->print_colors ? "\e[0;35mtrue\e[0m" : "true";
860 snprintf(iter->value, sizeof(iter->value), "%s",
861 __gen_unpack_uint(iter->p, s, e) ?
862 true_string : "false");
863 break;
864 }
865 case V3D_TYPE_FLOAT:
866 snprintf(iter->value, sizeof(iter->value), "%f",
867 __gen_unpack_float(iter->p, s, e));
868 break;
869 case V3D_TYPE_ADDRESS:
870 case V3D_TYPE_OFFSET:
871 snprintf(iter->value, sizeof(iter->value), "0x%08"PRIx64,
872 __gen_unpack_uint(iter->p, s, e) << (31 - (e - s)));
873 break;
874 case V3D_TYPE_STRUCT:
875 snprintf(iter->value, sizeof(iter->value), "<struct %s>",
876 iter->field->type.v3d_struct->name);
877 iter->struct_desc =
878 v3d_spec_find_struct(iter->group->spec,
879 iter->field->type.v3d_struct->name);
880 break;
881 case V3D_TYPE_SFIXED:
882 snprintf(iter->value, sizeof(iter->value), "%f",
883 __gen_unpack_sfixed(iter->p, s, e,
884 iter->field->type.f));
885 break;
886 case V3D_TYPE_UFIXED:
887 snprintf(iter->value, sizeof(iter->value), "%f",
888 __gen_unpack_ufixed(iter->p, s, e,
889 iter->field->type.f));
890 break;
891 case V3D_TYPE_MBO:
892 break;
893 case V3D_TYPE_ENUM: {
894 uint32_t value = __gen_unpack_uint(iter->p, s, e);
895 snprintf(iter->value, sizeof(iter->value), "%d", value);
896 enum_name = v3d_get_enum_name(iter->field->type.v3d_enum, value);
897 break;
898 }
899 }
900
901 if (strlen(iter->group->name) == 0) {
902 int length = strlen(iter->name);
903 snprintf(iter->name + length, sizeof(iter->name) - length,
904 "[%i]", iter->group_iter);
905 }
906
907 if (enum_name) {
908 int length = strlen(iter->value);
909 snprintf(iter->value + length, sizeof(iter->value) - length,
910 " (%s)", enum_name);
911 }
912
913 return true;
914 }
915
916 void
917 v3d_print_group(FILE *outfile, struct v3d_group *group,
918 uint64_t offset, const uint8_t *p, bool color)
919 {
920 struct v3d_field_iterator iter;
921
922 v3d_field_iterator_init(&iter, group, p, color);
923 while (v3d_field_iterator_next(&iter)) {
924 fprintf(outfile, " %s: %s\n", iter.name, iter.value);
925 if (iter.struct_desc) {
926 uint64_t struct_offset = offset + iter.offset;
927 v3d_print_group(outfile, iter.struct_desc,
928 struct_offset,
929 &p[iter.offset], color);
930 }
931 }
932 }