v3d: Use /* */ instead of () for enum names in CLIF output.
[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 /* Make sure that we picked an XML that matched our version.
471 */
472 assert(ver_in_range(ctx->devinfo->ver, min_ver, max_ver));
473
474 int major, minor;
475 int n = sscanf(ver, "%d.%d", &major, &minor);
476 if (n == 0)
477 fail(&ctx->loc, "invalid ver given: %s", ver);
478 if (n == 1)
479 minor = 0;
480
481 ctx->spec->ver = major * 10 + minor;
482 } else if (strcmp(element_name, "packet") == 0 ||
483 strcmp(element_name, "struct") == 0) {
484 ctx->group = create_group(ctx, name, atts, NULL);
485
486 if (strcmp(element_name, "packet") == 0)
487 set_group_opcode(ctx->group, atts);
488 } else if (strcmp(element_name, "register") == 0) {
489 ctx->group = create_group(ctx, name, atts, NULL);
490 get_register_offset(atts, &ctx->group->register_offset);
491 } else if (strcmp(element_name, "group") == 0) {
492 struct v3d_group *previous_group = ctx->group;
493 while (previous_group->next)
494 previous_group = previous_group->next;
495
496 struct v3d_group *group = create_group(ctx, "", atts,
497 ctx->group);
498 previous_group->next = group;
499 ctx->group = group;
500 } else if (strcmp(element_name, "field") == 0) {
501 create_and_append_field(ctx, atts);
502 } else if (strcmp(element_name, "enum") == 0) {
503 ctx->enoom = create_enum(ctx, name, atts);
504 } else if (strcmp(element_name, "value") == 0) {
505 ctx->values[ctx->nvalues++] = create_value(ctx, atts);
506 assert(ctx->nvalues < ARRAY_SIZE(ctx->values));
507 }
508
509 skip:
510 ctx->parse_depth++;
511 }
512
513 static void
514 end_element(void *data, const char *name)
515 {
516 struct parser_context *ctx = data;
517 struct v3d_spec *spec = ctx->spec;
518
519 ctx->parse_depth--;
520
521 if (ctx->parse_skip_depth) {
522 if (ctx->parse_skip_depth == ctx->parse_depth)
523 ctx->parse_skip_depth = 0;
524 return;
525 }
526
527 if (strcmp(name, "packet") == 0 ||
528 strcmp(name, "struct") == 0 ||
529 strcmp(name, "register") == 0) {
530 struct v3d_group *group = ctx->group;
531
532 ctx->group = ctx->group->parent;
533
534 if (strcmp(name, "packet") == 0) {
535 spec->commands[spec->ncommands++] = group;
536
537 /* V3D packet XML has the packet contents with offsets
538 * starting from the first bit after the opcode, to
539 * match the spec. Shift the fields up now.
540 */
541 for (int i = 0; i < group->nfields; i++) {
542 group->fields[i]->start += 8;
543 group->fields[i]->end += 8;
544 }
545 }
546 else if (strcmp(name, "struct") == 0)
547 spec->structs[spec->nstructs++] = group;
548 else if (strcmp(name, "register") == 0)
549 spec->registers[spec->nregisters++] = group;
550
551 assert(spec->ncommands < ARRAY_SIZE(spec->commands));
552 assert(spec->nstructs < ARRAY_SIZE(spec->structs));
553 assert(spec->nregisters < ARRAY_SIZE(spec->registers));
554 } else if (strcmp(name, "group") == 0) {
555 ctx->group = ctx->group->parent;
556 } else if (strcmp(name, "field") == 0) {
557 assert(ctx->group->nfields > 0);
558 struct v3d_field *field = ctx->group->fields[ctx->group->nfields - 1];
559 size_t size = ctx->nvalues * sizeof(ctx->values[0]);
560 field->inline_enum.values = xzalloc(size);
561 field->inline_enum.nvalues = ctx->nvalues;
562 memcpy(field->inline_enum.values, ctx->values, size);
563 ctx->nvalues = 0;
564 } else if (strcmp(name, "enum") == 0) {
565 struct v3d_enum *e = ctx->enoom;
566 size_t size = ctx->nvalues * sizeof(ctx->values[0]);
567 e->values = xzalloc(size);
568 e->nvalues = ctx->nvalues;
569 memcpy(e->values, ctx->values, size);
570 ctx->nvalues = 0;
571 ctx->enoom = NULL;
572 spec->enums[spec->nenums++] = e;
573 }
574 }
575
576 static void
577 character_data(void *data, const XML_Char *s, int len)
578 {
579 }
580
581 static uint32_t zlib_inflate(const void *compressed_data,
582 uint32_t compressed_len,
583 void **out_ptr)
584 {
585 struct z_stream_s zstream;
586 void *out;
587
588 memset(&zstream, 0, sizeof(zstream));
589
590 zstream.next_in = (unsigned char *)compressed_data;
591 zstream.avail_in = compressed_len;
592
593 if (inflateInit(&zstream) != Z_OK)
594 return 0;
595
596 out = malloc(4096);
597 zstream.next_out = out;
598 zstream.avail_out = 4096;
599
600 do {
601 switch (inflate(&zstream, Z_SYNC_FLUSH)) {
602 case Z_STREAM_END:
603 goto end;
604 case Z_OK:
605 break;
606 default:
607 inflateEnd(&zstream);
608 return 0;
609 }
610
611 if (zstream.avail_out)
612 break;
613
614 out = realloc(out, 2*zstream.total_out);
615 if (out == NULL) {
616 inflateEnd(&zstream);
617 return 0;
618 }
619
620 zstream.next_out = (unsigned char *)out + zstream.total_out;
621 zstream.avail_out = zstream.total_out;
622 } while (1);
623 end:
624 inflateEnd(&zstream);
625 *out_ptr = out;
626 return zstream.total_out;
627 }
628
629 struct v3d_spec *
630 v3d_spec_load(const struct v3d_device_info *devinfo)
631 {
632 struct parser_context ctx;
633 void *buf;
634 uint8_t *text_data = NULL;
635 uint32_t text_offset = 0, text_length = 0, total_length;
636
637 for (int i = 0; i < ARRAY_SIZE(genxml_files_table); i++) {
638 if (i != 0) {
639 assert(genxml_files_table[i - 1].gen_10 <
640 genxml_files_table[i].gen_10);
641 }
642
643 if (genxml_files_table[i].gen_10 <= devinfo->ver) {
644 text_offset = genxml_files_table[i].offset;
645 text_length = genxml_files_table[i].length;
646 }
647 }
648
649 if (text_length == 0) {
650 fprintf(stderr, "unable to find gen (%u) data\n", devinfo->ver);
651 return NULL;
652 }
653
654 memset(&ctx, 0, sizeof ctx);
655 ctx.parser = XML_ParserCreate(NULL);
656 ctx.devinfo = devinfo;
657 XML_SetUserData(ctx.parser, &ctx);
658 if (ctx.parser == NULL) {
659 fprintf(stderr, "failed to create parser\n");
660 return NULL;
661 }
662
663 XML_SetElementHandler(ctx.parser, start_element, end_element);
664 XML_SetCharacterDataHandler(ctx.parser, character_data);
665
666 ctx.spec = xzalloc(sizeof(*ctx.spec));
667
668 total_length = zlib_inflate(compress_genxmls,
669 sizeof(compress_genxmls),
670 (void **) &text_data);
671 assert(text_offset + text_length <= total_length);
672
673 buf = XML_GetBuffer(ctx.parser, text_length);
674 memcpy(buf, &text_data[text_offset], text_length);
675
676 if (XML_ParseBuffer(ctx.parser, text_length, true) == 0) {
677 fprintf(stderr,
678 "Error parsing XML at line %ld col %ld byte %ld/%u: %s\n",
679 XML_GetCurrentLineNumber(ctx.parser),
680 XML_GetCurrentColumnNumber(ctx.parser),
681 XML_GetCurrentByteIndex(ctx.parser), text_length,
682 XML_ErrorString(XML_GetErrorCode(ctx.parser)));
683 XML_ParserFree(ctx.parser);
684 free(text_data);
685 return NULL;
686 }
687
688 XML_ParserFree(ctx.parser);
689 free(text_data);
690
691 return ctx.spec;
692 }
693
694 struct v3d_group *
695 v3d_spec_find_instruction(struct v3d_spec *spec, const uint8_t *p)
696 {
697 uint8_t opcode = *p;
698
699 for (int i = 0; i < spec->ncommands; i++) {
700 struct v3d_group *group = spec->commands[i];
701
702 if (opcode != group->opcode)
703 continue;
704
705 /* If there's a "sub-id" field, make sure that it matches the
706 * instruction being decoded.
707 */
708 struct v3d_field *subid = NULL;
709 for (int j = 0; j < group->nfields; j++) {
710 struct v3d_field *field = group->fields[j];
711 if (strcmp(field->name, "sub-id") == 0) {
712 subid = field;
713 break;
714 }
715 }
716
717 if (subid && (__gen_unpack_uint(p, subid->start, subid->end) !=
718 subid->default_value)) {
719 continue;
720 }
721
722 return group;
723 }
724
725 return NULL;
726 }
727
728 /** Returns the size of a V3D packet. */
729 int
730 v3d_group_get_length(struct v3d_group *group)
731 {
732 int last_bit = 0;
733 for (int i = 0; i < group->nfields; i++) {
734 struct v3d_field *field = group->fields[i];
735
736 last_bit = MAX2(last_bit, field->end);
737 }
738 return last_bit / 8 + 1;
739 }
740
741 void
742 v3d_field_iterator_init(struct v3d_field_iterator *iter,
743 struct v3d_group *group,
744 const uint8_t *p,
745 bool print_colors)
746 {
747 memset(iter, 0, sizeof(*iter));
748
749 iter->group = group;
750 iter->p = p;
751 iter->print_colors = print_colors;
752 }
753
754 static const char *
755 v3d_get_enum_name(struct v3d_enum *e, uint64_t value)
756 {
757 for (int i = 0; i < e->nvalues; i++) {
758 if (e->values[i]->value == value) {
759 return e->values[i]->name;
760 }
761 }
762 return NULL;
763 }
764
765 static bool
766 iter_more_fields(const struct v3d_field_iterator *iter)
767 {
768 return iter->field_iter < iter->group->nfields;
769 }
770
771 static uint32_t
772 iter_group_offset_bits(const struct v3d_field_iterator *iter,
773 uint32_t group_iter)
774 {
775 return iter->group->group_offset + (group_iter *
776 iter->group->group_size);
777 }
778
779 static bool
780 iter_more_groups(const struct v3d_field_iterator *iter)
781 {
782 if (iter->group->variable) {
783 return iter_group_offset_bits(iter, iter->group_iter + 1) <
784 (v3d_group_get_length(iter->group) * 8);
785 } else {
786 return (iter->group_iter + 1) < iter->group->group_count ||
787 iter->group->next != NULL;
788 }
789 }
790
791 static void
792 iter_advance_group(struct v3d_field_iterator *iter)
793 {
794 if (iter->group->variable)
795 iter->group_iter++;
796 else {
797 if ((iter->group_iter + 1) < iter->group->group_count) {
798 iter->group_iter++;
799 } else {
800 iter->group = iter->group->next;
801 iter->group_iter = 0;
802 }
803 }
804
805 iter->field_iter = 0;
806 }
807
808 static bool
809 iter_advance_field(struct v3d_field_iterator *iter)
810 {
811 while (!iter_more_fields(iter)) {
812 if (!iter_more_groups(iter))
813 return false;
814
815 iter_advance_group(iter);
816 }
817
818 iter->field = iter->group->fields[iter->field_iter++];
819 if (iter->field->name)
820 strncpy(iter->name, iter->field->name, sizeof(iter->name));
821 else
822 memset(iter->name, 0, sizeof(iter->name));
823 iter->offset = iter_group_offset_bits(iter, iter->group_iter) / 8 +
824 iter->field->start / 8;
825 iter->struct_desc = NULL;
826
827 return true;
828 }
829
830 bool
831 v3d_field_iterator_next(struct v3d_field_iterator *iter)
832 {
833 if (!iter_advance_field(iter))
834 return false;
835
836 const char *enum_name = NULL;
837
838 int group_member_offset =
839 iter_group_offset_bits(iter, iter->group_iter);
840 int s = group_member_offset + iter->field->start;
841 int e = group_member_offset + iter->field->end;
842
843 assert(!iter->field->minus_one ||
844 iter->field->type.kind == V3D_TYPE_INT ||
845 iter->field->type.kind == V3D_TYPE_UINT);
846
847 switch (iter->field->type.kind) {
848 case V3D_TYPE_UNKNOWN:
849 case V3D_TYPE_INT: {
850 uint32_t value = __gen_unpack_sint(iter->p, s, e);
851 if (iter->field->minus_one)
852 value++;
853 snprintf(iter->value, sizeof(iter->value), "%d", value);
854 enum_name = v3d_get_enum_name(&iter->field->inline_enum, value);
855 break;
856 }
857 case V3D_TYPE_UINT: {
858 uint32_t value = __gen_unpack_uint(iter->p, s, e);
859 if (iter->field->minus_one)
860 value++;
861 if (strcmp(iter->field->name, "Vec size") == 0 && value == 0)
862 value = 1 << (e - s);
863 snprintf(iter->value, sizeof(iter->value), "%u", value);
864 enum_name = v3d_get_enum_name(&iter->field->inline_enum, value);
865 break;
866 }
867 case V3D_TYPE_BOOL: {
868 const char *true_string =
869 iter->print_colors ? "\e[0;35mtrue\e[0m" : "true";
870 snprintf(iter->value, sizeof(iter->value), "%s",
871 __gen_unpack_uint(iter->p, s, e) ?
872 true_string : "false");
873 break;
874 }
875 case V3D_TYPE_FLOAT:
876 snprintf(iter->value, sizeof(iter->value), "%f",
877 __gen_unpack_float(iter->p, s, e));
878 break;
879 case V3D_TYPE_ADDRESS:
880 case V3D_TYPE_OFFSET:
881 snprintf(iter->value, sizeof(iter->value), "0x%08"PRIx64,
882 __gen_unpack_uint(iter->p, s, e) << (31 - (e - s)));
883 break;
884 case V3D_TYPE_STRUCT:
885 snprintf(iter->value, sizeof(iter->value), "<struct %s>",
886 iter->field->type.v3d_struct->name);
887 iter->struct_desc =
888 v3d_spec_find_struct(iter->group->spec,
889 iter->field->type.v3d_struct->name);
890 break;
891 case V3D_TYPE_SFIXED:
892 snprintf(iter->value, sizeof(iter->value), "%f",
893 __gen_unpack_sfixed(iter->p, s, e,
894 iter->field->type.f));
895 break;
896 case V3D_TYPE_UFIXED:
897 snprintf(iter->value, sizeof(iter->value), "%f",
898 __gen_unpack_ufixed(iter->p, s, e,
899 iter->field->type.f));
900 break;
901 case V3D_TYPE_MBO:
902 break;
903 case V3D_TYPE_ENUM: {
904 uint32_t value = __gen_unpack_uint(iter->p, s, e);
905 snprintf(iter->value, sizeof(iter->value), "%d", value);
906 enum_name = v3d_get_enum_name(iter->field->type.v3d_enum, value);
907 break;
908 }
909 }
910
911 if (strlen(iter->group->name) == 0) {
912 int length = strlen(iter->name);
913 snprintf(iter->name + length, sizeof(iter->name) - length,
914 "[%i]", iter->group_iter);
915 }
916
917 if (enum_name) {
918 int length = strlen(iter->value);
919 snprintf(iter->value + length, sizeof(iter->value) - length,
920 " /* %s */", enum_name);
921 }
922
923 return true;
924 }
925
926 void
927 v3d_print_group(FILE *outfile, struct v3d_group *group,
928 uint64_t offset, const uint8_t *p, bool color)
929 {
930 struct v3d_field_iterator iter;
931
932 v3d_field_iterator_init(&iter, group, p, color);
933 while (v3d_field_iterator_next(&iter)) {
934 fprintf(outfile, " %s: %s\n", iter.name, iter.value);
935 if (iter.struct_desc) {
936 uint64_t struct_offset = offset + iter.offset;
937 v3d_print_group(outfile, iter.struct_desc,
938 struct_offset,
939 &p[iter.offset], color);
940 }
941 }
942 }