1 /*
2  * Copyright © 2020 Google, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 #include <assert.h>
25 #include <inttypes.h>
26 #include <stdbool.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 
32 #include "util/bitset.h"
33 #include "util/compiler.h"
34 #include "util/half_float.h"
35 #include "util/hash_table.h"
36 #include "util/ralloc.h"
37 #include "util/u_debug.h"
38 #include "util/u_math.h"
39 
40 #include "decode.h"
41 #include "isa.h"
42 
43 /**
44  * The set of leaf node bitsets in the bitset hiearchy which defines all
45  * the possible instructions.
46  *
47  * TODO maybe we want to pass this in as parameter so this same decoder
48  * can work with multiple different instruction sets.
49  */
50 extern const struct isa_bitset *__instruction[];
51 
52 struct decode_state;
53 
54 /**
55  * Decode scope.  When parsing a field that is itself a bitset, we push a
56  * new scope to the stack.  A nested bitset is allowed to resolve fields
57  * from an enclosing scope (needed, for example, to decode src register
58  * bitsets, where half/fullness is determined by fields outset if bitset
59  * in the instruction containing the bitset.
60  *
61  * But the field being resolved could be a derived field, or different
62  * depending on an override at a higher level of the stack, requiring
63  * expression evaluation which could in turn reference variables which
64  * triggers a recursive field lookup.  But those lookups should not start
65  * from the top of the stack, but instead the current stack level.  This
66  * prevents a field from accidentally resolving to different values
67  * depending on the starting point of the lookup.  (Not only causing
68  * confusion, but this is behavior we don't want to depend on if we
69  * wanted to optimize things by caching field lookup results.)
70  */
71 struct decode_scope {
72 	/**
73 	 * Enclosing scope
74 	 */
75 	struct decode_scope *parent;
76 
77 	/**
78 	 * Current bitset value being decoded
79 	 */
80 	bitmask_t val;
81 
82 	/**
83 	 * Current bitset.
84 	 */
85 	const struct isa_bitset *bitset;
86 
87 	/**
88 	 * Field name remapping.
89 	 */
90 	const struct isa_field_params *params;
91 
92 	/**
93 	 * Pointer back to decode state, for convenience.
94 	 */
95 	struct decode_state *state;
96 
97 	/**
98 	 * Cache expression evaluation results.  Expressions for overrides can
99 	 * be repeatedly evaluated for each field being resolved.  And each
100 	 * field reference to a derived field (potentially from another expr)
101 	 * would require re-evaluation.  But for a given scope, each evaluation
102 	 * of an expression gives the same result.  So we can cache to speed
103 	 * things up.
104 	 *
105 	 * TODO we could maybe be clever and assign a unique idx to each expr
106 	 * and use a direct lookup table?  Would be a bit more clever if it was
107 	 * smart enough to allow unrelated expressions that are never involved
108 	 * in a given scope to have overlapping cache lookup idx's.
109 	 */
110 	struct hash_table *cache;
111 };
112 
113 /**
114  * Current decode state
115  */
116 struct decode_state {
117 	const struct isa_decode_options *options;
118 	FILE *out;
119 
120 	/**
121 	 * Current instruction being decoded:
122 	 */
123 	unsigned n;
124 
125 	/**
126 	 * Number of instructions being decoded
127 	 */
128 	unsigned num_instr;
129 
130 	/**
131 	 * Column number of current line
132 	 */
133 	unsigned line_column;
134 
135 	/**
136 	 * Bitset of instructions that are branch targets (if options->branch_labels
137 	 * is enabled)
138 	 */
139 	BITSET_WORD *branch_targets;
140 
141 	/**
142 	 * We allow a limited amount of expression evaluation recursion, but
143 	 * not recursive evaluation of any given expression, to prevent infinite
144 	 * recursion.
145 	 */
146 	int expr_sp;
147 	isa_expr_t expr_stack[8];
148 
149 	/**
150 	 * Current topmost/innermost level of scope used for decoding fields,
151 	 * including derived fields which may in turn rely on decoding other
152 	 * fields, potentially from a lower/out level in the stack.
153 	 */
154 	struct decode_scope *scope;
155 
156 	/**
157 	 * A small fixed upper limit on # of decode errors to capture per-
158 	 * instruction seems reasonable.
159 	 */
160 	unsigned num_errors;
161 	char *errors[4];
162 };
163 
164 static void
print(struct decode_state * state,const char * fmt,...)165 print(struct decode_state *state, const char *fmt, ...)
166 {
167 	char *buffer;
168 	va_list args;
169 	int ret;
170 
171 	va_start(args, fmt);
172 	ret = vasprintf(&buffer, fmt, args);
173 	va_end(args);
174 
175 	if (ret != -1) {
176 		const size_t len = strlen(buffer);
177 
178 		for (size_t i = 0; i < len; i++) {
179 			const char c = buffer[i];
180 
181 			fputc(c, state->out);
182 			state->line_column++;
183 
184 			if (c == '\n') {
185 				state->line_column = 0;
186 			}
187       }
188 
189 		free(buffer);
190 
191 		return;
192 	}
193 }
194 
195 static void display(struct decode_scope *scope);
196 static void decode_error(struct decode_state *state, const char *fmt, ...) _util_printf_format(2,3);
197 
198 static void
decode_error(struct decode_state * state,const char * fmt,...)199 decode_error(struct decode_state *state, const char *fmt, ...)
200 {
201 	if (!state->options->show_errors) {
202 		return;
203 	}
204 
205 	if (state->num_errors == ARRAY_SIZE(state->errors)) {
206 		/* too many errors, bail */
207 		return;
208 	}
209 
210 	va_list ap;
211 	va_start(ap, fmt);
212 	vasprintf(&state->errors[state->num_errors++], fmt, ap);
213 	va_end(ap);
214 }
215 
216 static unsigned
flush_errors(struct decode_state * state)217 flush_errors(struct decode_state *state)
218 {
219 	unsigned num_errors = state->num_errors;
220 	if (num_errors > 0)
221 		print(state, "\t; ");
222 	for (unsigned i = 0; i < num_errors; i++) {
223 		print(state, "%s%s", (i > 0) ? ", " : "", state->errors[i]);
224 		free(state->errors[i]);
225 	}
226 	state->num_errors = 0;
227 	return num_errors;
228 }
229 
230 
231 static bool
push_expr(struct decode_state * state,isa_expr_t expr)232 push_expr(struct decode_state *state, isa_expr_t expr)
233 {
234 	for (int i = state->expr_sp - 1; i > 0; i--) {
235 		if (state->expr_stack[i] == expr) {
236 			return false;
237 		}
238 	}
239 	state->expr_stack[state->expr_sp++] = expr;
240 	return true;
241 }
242 
243 static void
pop_expr(struct decode_state * state)244 pop_expr(struct decode_state *state)
245 {
246 	assert(state->expr_sp > 0);
247 	state->expr_sp--;
248 }
249 
250 static struct decode_scope *
push_scope(struct decode_state * state,const struct isa_bitset * bitset,bitmask_t val)251 push_scope(struct decode_state *state, const struct isa_bitset *bitset, bitmask_t val)
252 {
253 	struct decode_scope *scope = rzalloc_size(state, sizeof(*scope));
254 
255 	BITSET_COPY(scope->val.bitset, val.bitset);
256 	scope->bitset = bitset;
257 	scope->parent = state->scope;
258 	scope->state  = state;
259 
260 	state->scope = scope;
261 
262 	return scope;
263 }
264 
265 static void
pop_scope(struct decode_scope * scope)266 pop_scope(struct decode_scope *scope)
267 {
268 	assert(scope->state->scope == scope);  /* must be top of stack */
269 
270 	scope->state->scope = scope->parent;
271 	ralloc_free(scope);
272 }
273 
274 /**
275  * Evaluate an expression, returning it's resulting value
276  */
277 static uint64_t
evaluate_expr(struct decode_scope * scope,isa_expr_t expr)278 evaluate_expr(struct decode_scope *scope, isa_expr_t expr)
279 {
280 	if (scope->cache) {
281 		struct hash_entry *entry = _mesa_hash_table_search(scope->cache, expr);
282 		if (entry) {
283 			return *(uint64_t *)entry->data;
284 		}
285 	} else {
286 		scope->cache = _mesa_pointer_hash_table_create(scope);
287 	}
288 
289 	if (!push_expr(scope->state, expr))
290 		return 0;
291 
292 	uint64_t ret = expr(scope);
293 
294 	pop_expr(scope->state);
295 
296 	uint64_t *retp = ralloc_size(scope->cache, sizeof(*retp));
297 	*retp = ret;
298 	_mesa_hash_table_insert(scope->cache, expr, retp);
299 
300 	return ret;
301 }
302 
303 /**
304  * Find the bitset in NULL terminated bitset hiearchy root table which
305  * matches against 'val'
306  */
307 static const struct isa_bitset *
find_bitset(struct decode_state * state,const struct isa_bitset ** bitsets,bitmask_t val)308 find_bitset(struct decode_state *state, const struct isa_bitset **bitsets,
309 		bitmask_t val)
310 {
311 	const struct isa_bitset *match = NULL;
312 	for (int n = 0; bitsets[n]; n++) {
313 		if (state->options->gpu_id > bitsets[n]->gen.max)
314 			continue;
315 		if (state->options->gpu_id < bitsets[n]->gen.min)
316 			continue;
317 
318 		// m = (val & bitsets[n]->mask) & ~bitsets[n]->dontcare;
319 		bitmask_t m = { 0 };
320 		bitmask_t not_dontcare;
321 
322 		BITSET_AND(m.bitset, val.bitset, bitsets[n]->mask.bitset);
323 
324 		BITSET_COPY(not_dontcare.bitset, bitsets[n]->dontcare.bitset);
325 		BITSET_NOT(not_dontcare.bitset);
326 
327 		BITSET_AND(m.bitset, m.bitset, not_dontcare.bitset);
328 
329 		if (!BITSET_EQUAL(m.bitset, bitsets[n]->match.bitset)) {
330 			continue;
331 		}
332 
333 		/* We should only have exactly one match
334 		 *
335 		 * TODO more complete/formal way to validate that any given
336 		 * bit pattern will only have a single match?
337 		 */
338 		if (match) {
339 			decode_error(state, "bitset conflict: %s vs %s", match->name,
340 					bitsets[n]->name);
341 			return NULL;
342 		}
343 
344 		match = bitsets[n];
345 	}
346 
347 	if (match) {
348 		bitmask_t m = { 0 };
349 		BITSET_AND(m.bitset, match->dontcare.bitset, val.bitset);
350 
351 		if (BITSET_COUNT(m.bitset)) {
352 			decode_error(state, "dontcare bits in %s: %"BITSET_FORMAT,
353 					match->name, BITSET_VALUE(m.bitset));
354 		}
355 	}
356 
357 	return match;
358 }
359 
360 static const struct isa_field *
find_field(struct decode_scope * scope,const struct isa_bitset * bitset,const char * name,size_t name_len)361 find_field(struct decode_scope *scope, const struct isa_bitset *bitset,
362 		const char *name, size_t name_len)
363 {
364 	for (unsigned i = 0; i < bitset->num_cases; i++) {
365 		const struct isa_case *c = bitset->cases[i];
366 
367 		if (c->expr) {
368 			struct decode_state *state = scope->state;
369 
370 			/* When resolving a field for evaluating an expression,
371 			 * temporarily assume the expression evaluates to true.
372 			 * This allows <override/>'s to speculatively refer to
373 			 * fields defined within the override:
374 			 */
375 			isa_expr_t cur_expr = NULL;
376 			if (state->expr_sp > 0)
377 				cur_expr = state->expr_stack[state->expr_sp - 1];
378 			if ((cur_expr != c->expr) && !evaluate_expr(scope, c->expr))
379 				continue;
380 		}
381 
382 		for (unsigned i = 0; i < c->num_fields; i++) {
383 			if (!strncmp(name, c->fields[i].name, name_len) &&
384 			   (c->fields[i].name[name_len] == '\0')) {
385 				return &c->fields[i];
386 			}
387 		}
388 	}
389 
390 	if (bitset->parent) {
391 		const struct isa_field *f = find_field(scope, bitset->parent, name, name_len);
392 		if (f) {
393 			return f;
394 		}
395 	}
396 
397 	return NULL;
398 }
399 
400 static bitmask_t
extract_field(struct decode_scope * scope,const struct isa_field * field)401 extract_field(struct decode_scope *scope, const struct isa_field *field)
402 {
403    bitmask_t val, mask;
404 
405    BITSET_COPY(val.bitset, scope->val.bitset);
406    BITSET_ZERO(mask.bitset);
407 
408    BITSET_SET_RANGE(mask.bitset, field->low, field->high);
409    BITSET_AND(val.bitset, val.bitset, mask.bitset);
410    BITSET_SHR(val.bitset, field->low);
411 
412    return val;
413 }
414 
415 /**
416  * Find the display template for a given bitset, recursively searching
417  * parents in the bitset hierarchy.
418  */
419 static const char *
find_display(struct decode_scope * scope,const struct isa_bitset * bitset)420 find_display(struct decode_scope *scope, const struct isa_bitset *bitset)
421 {
422 	for (unsigned i = 0; i < bitset->num_cases; i++) {
423 		const struct isa_case *c = bitset->cases[i];
424 		if (c->expr && !evaluate_expr(scope, c->expr))
425 			continue;
426 		/* since this is the chosen case, it seems like a good place
427 		 * to check asserted bits:
428 		 */
429 		for (unsigned j = 0; j < c->num_fields; j++) {
430 			if (c->fields[j].type == TYPE_ASSERT) {
431 				const struct isa_field *f = &c->fields[j];
432 				bitmask_t val;
433 
434 				val = extract_field(scope, f);
435 				if (!BITSET_EQUAL(val.bitset, f->val.bitset)) {
436 					decode_error(scope->state, "WARNING: unexpected "
437 							"bits[%u:%u] in %s: %"BITSET_FORMAT" vs %"BITSET_FORMAT,
438 							f->low, f->high, bitset->name,
439 							BITSET_VALUE(val.bitset), BITSET_VALUE(f->val.bitset));
440 				}
441 			}
442 		}
443 		if (!c->display)
444 			continue;
445 		return c->display;
446 	}
447 
448 	/**
449 	 * If we didn't find something check up the bitset hierarchy.
450 	 */
451 	if (bitset->parent) {
452 		return find_display(scope, bitset->parent);
453 	}
454 
455 	return NULL;
456 }
457 
458 /**
459  * Decode a field that is itself another bitset type
460  */
461 static void
display_bitset_field(struct decode_scope * scope,const struct isa_field * field,bitmask_t val)462 display_bitset_field(struct decode_scope *scope, const struct isa_field *field, bitmask_t val)
463 {
464 	const struct isa_bitset *b = find_bitset(scope->state, field->bitsets, val);
465 	if (!b) {
466 		decode_error(scope->state, "no match: FIELD: '%s.%s': %"BITSET_FORMAT,
467 				scope->bitset->name, field->name, BITSET_VALUE(val.bitset));
468 		return;
469 	}
470 
471 	struct decode_scope *nested_scope =
472 			push_scope(scope->state, b, val);
473 	nested_scope->params = field->params;
474 	display(nested_scope);
475 	pop_scope(nested_scope);
476 }
477 
478 static void
display_enum_field(struct decode_scope * scope,const struct isa_field * field,bitmask_t val)479 display_enum_field(struct decode_scope *scope, const struct isa_field *field, bitmask_t val)
480 {
481 	const struct isa_enum *e = field->enums;
482 	const uint64_t ui = bitmask_to_uint64_t(val);
483 
484 	for (unsigned i = 0; i < e->num_values; i++) {
485 		if (e->values[i].val == ui) {
486 			print(scope->state, "%s", e->values[i].display);
487 			return;
488 		}
489 	}
490 
491 	print(scope->state, "%u", (unsigned)ui);
492 }
493 
494 static const struct isa_field *
resolve_field(struct decode_scope * scope,const char * field_name,size_t field_name_len,bitmask_t * valp)495 resolve_field(struct decode_scope *scope, const char *field_name, size_t field_name_len, bitmask_t *valp)
496 {
497 	if (!scope) {
498 		/* We've reached the bottom of the stack! */
499 		return NULL;
500 	}
501 
502 	const struct isa_field *field =
503 			find_field(scope, scope->bitset, field_name, field_name_len);
504 
505 	if (!field && scope->params) {
506 		for (unsigned i = 0; i < scope->params->num_params; i++) {
507 			if (!strncmp(field_name, scope->params->params[i].as, field_name_len) &&
508 			   (scope->params->params[i].as[field_name_len] == '\0')) {
509 				const char *param_name = scope->params->params[i].name;
510 				return resolve_field(scope->parent, param_name, strlen(param_name), valp);
511 			}
512 		}
513 	}
514 
515 	if (!field) {
516 		return NULL;
517 	}
518 
519 	/* extract out raw field value: */
520 	if (field->expr) {
521 		uint64_t val = evaluate_expr(scope, field->expr);
522 
523 		*valp = uint64_t_to_bitmask(val);
524 	} else {
525 		*valp = extract_field(scope, field);
526 	}
527 
528 	return field;
529 }
530 
531 /* This is also used from generated expr functions */
532 uint64_t
isa_decode_field(struct decode_scope * scope,const char * field_name)533 isa_decode_field(struct decode_scope *scope, const char *field_name)
534 {
535 	bitmask_t val;
536 	const struct isa_field *field = resolve_field(scope, field_name, strlen(field_name), &val);
537 	if (!field) {
538 		decode_error(scope->state, "no field '%s'", field_name);
539 		return 0;
540 	}
541 
542 	return bitmask_to_uint64_t(val);
543 }
544 
545 static void
display_field(struct decode_scope * scope,const char * field_name)546 display_field(struct decode_scope *scope, const char *field_name)
547 {
548 	const struct isa_decode_options *options = scope->state->options;
549 	struct decode_state *state = scope->state;
550 	size_t field_name_len = strlen(field_name);
551 	int num_align = 0;
552 
553 	/* alignment handling */
554 	const char *align = strstr(field_name, ":align=");
555 
556 	if (align) {
557 		const char *value = strstr(align, "=") + 1;
558 
559 		field_name_len = align - field_name;
560 		num_align = atoi(value);
561 	}
562 
563 	/* Special case 'NAME' maps to instruction/bitset name: */
564 	if (!strncmp("NAME", field_name, field_name_len)) {
565 		if (options->field_cb) {
566 			options->field_cb(options->cbdata, field_name, &(struct isa_decode_value){
567 				.str = scope->bitset->name,
568 			});
569 		}
570 
571 		while (scope->state->line_column < num_align)
572 			print(state, " ");
573 
574 		print(scope->state, "%s", scope->bitset->name);
575 
576 		return;
577 	}
578 
579 	bitmask_t v;
580 	const struct isa_field *field = resolve_field(scope, field_name, field_name_len, &v);
581 	if (!field) {
582 		decode_error(scope->state, "no field '%.*s'", (int)field_name_len, field_name);
583 		return;
584 	}
585 
586 	uint64_t val = bitmask_to_uint64_t(v);
587 
588 	if (options->field_cb) {
589 		options->field_cb(options->cbdata, field_name, &(struct isa_decode_value){
590 			.num = val,
591 		});
592 	}
593 
594 	unsigned width = 1 + field->high - field->low;
595 
596 	while (scope->state->line_column < num_align)
597 		print(state, " ");
598 
599 	switch (field->type) {
600 	/* Basic types: */
601 	case TYPE_BRANCH:
602 		if (scope->state->options->branch_labels) {
603 			int offset = util_sign_extend(val, width) + scope->state->n;
604 			if (offset < scope->state->num_instr) {
605 				print(scope->state, "l%d", offset);
606 				BITSET_SET(scope->state->branch_targets, offset);
607 				break;
608 			}
609 		}
610 		FALLTHROUGH;
611 	case TYPE_INT:
612 		print(scope->state, "%"PRId64, util_sign_extend(val, width));
613 		break;
614 	case TYPE_UINT:
615 		print(scope->state, "%"PRIu64, val);
616 		break;
617 	case TYPE_HEX:
618 		// TODO format # of digits based on field width?
619 		print(scope->state, "%"PRIx64, val);
620 		break;
621 	case TYPE_OFFSET:
622 		if (val != 0) {
623 			print(scope->state, "%+"PRId64, util_sign_extend(val, width));
624 		}
625 		break;
626 	case TYPE_UOFFSET:
627 		if (val != 0) {
628 			print(scope->state, "+%"PRIu64, val);
629 		}
630 		break;
631 	case TYPE_FLOAT:
632 		if (width == 16) {
633 			print(scope->state, "%f", _mesa_half_to_float(val));
634 		} else {
635 			assert(width == 32);
636 			print(scope->state, "%f", uif(val));
637 		}
638 		break;
639 	case TYPE_BOOL:
640 		if (field->display) {
641 			if (val) {
642 				print(scope->state, "%s", field->display);
643 			}
644 		} else {
645 			print(scope->state, "%u", (unsigned)val);
646 		}
647 		break;
648 	case TYPE_ENUM:
649 		display_enum_field(scope, field, v);
650 		break;
651 
652 	case TYPE_ASSERT:
653 		/* assert fields are not for display */
654 		assert(0);
655 		break;
656 
657 	/* For fields that are decoded with another bitset hierarchy: */
658 	case TYPE_BITSET:
659 		display_bitset_field(scope, field, v);
660 		break;
661 	default:
662 		decode_error(scope->state, "Bad field type: %d (%s)",
663 				field->type, field->name);
664 	}
665 }
666 
667 static void
display(struct decode_scope * scope)668 display(struct decode_scope *scope)
669 {
670 	const struct isa_bitset *bitset = scope->bitset;
671 	const char *display = find_display(scope, bitset);
672 
673 	if (!display) {
674 		decode_error(scope->state, "%s: no display template", bitset->name);
675 		return;
676 	}
677 
678 	const char *p = display;
679 
680 	while (*p != '\0') {
681 		if (*p == '{') {
682 			const char *e = ++p;
683 			while (*e != '}') {
684 				e++;
685 			}
686 
687 			char *field_name = strndup(p, e-p);
688 			display_field(scope, field_name);
689 			free(field_name);
690 
691 			p = e;
692 		} else {
693 			fputc(*p, scope->state->out);
694 			scope->state->line_column++;
695 		}
696 		p++;
697 	}
698 }
699 
700 static void
decode(struct decode_state * state,void * bin,int sz)701 decode(struct decode_state *state, void *bin, int sz)
702 {
703 	BITSET_WORD *instrs = bin;
704 	unsigned errors = 0;   /* number of consecutive unmatched instructions */
705 
706 	assert(sz % BITMASK_WORDS == 0);
707 
708 	for (state->n = 0; state->n < state->num_instr; state->n++) {
709 		bitmask_t instr = { 0 };
710 
711 		next_instruction(&instr, &instrs[state->n * BITMASK_WORDS]);
712       state->line_column = 0;
713 
714 		if (state->options->max_errors && (errors > state->options->max_errors)) {
715 			break;
716 		}
717 
718 		if (state->options->branch_labels &&
719 				BITSET_TEST(state->branch_targets, state->n)) {
720 			if (state->options->instr_cb) {
721 				state->options->instr_cb(state->options->cbdata,
722 						state->n, instr.bitset);
723 			}
724 			print(state, "l%d:\n", state->n);
725 		}
726 
727 		if (state->options->instr_cb) {
728 			state->options->instr_cb(state->options->cbdata, state->n, instr.bitset);
729 		}
730 
731 		const struct isa_bitset *b = find_bitset(state, __instruction, instr);
732 		if (!b) {
733 			print(state, "no match: %"BITSET_FORMAT"\n", BITSET_VALUE(instr.bitset));
734 			errors++;
735 			continue;
736 		}
737 
738 		struct decode_scope *scope = push_scope(state, b, instr);
739 
740 		display(scope);
741 		if (flush_errors(state)) {
742 			errors++;
743 		} else {
744 			errors = 0;
745 		}
746 		print(state, "\n");
747 
748 		pop_scope(scope);
749 
750 		if (state->options->stop) {
751 			break;
752 		}
753 	}
754 }
755 
756 void
isa_decode(void * bin,int sz,FILE * out,const struct isa_decode_options * options)757 isa_decode(void *bin, int sz, FILE *out, const struct isa_decode_options *options)
758 {
759 	const struct isa_decode_options default_options = {
760 		.branch_labels = options ? options->branch_labels : false
761 	};
762 	struct decode_state *state;
763 
764 	if (!options)
765 		options = &default_options;
766 
767 	util_cpu_detect();  /* needed for _mesa_half_to_float() */
768 
769 	state = rzalloc_size(NULL, sizeof(*state));
770 	state->options = options;
771 	state->num_instr = sz / (BITMASK_WORDS * sizeof(BITSET_WORD));
772 
773 	if (state->options->branch_labels) {
774 		state->branch_targets = rzalloc_size(state,
775 				sizeof(BITSET_WORD) * BITSET_WORDS(state->num_instr));
776 
777 		/* Do a pre-pass to find all the branch targets: */
778 		state->out = fopen("/dev/null", "w");
779 		state->options = &default_options;   /* skip hooks for prepass */
780 		decode(state, bin, sz);
781 		fclose(state->out);
782 		if (options) {
783 			state->options = options;
784 		}
785 	}
786 
787 	state->out = out;
788 
789 	decode(state, bin, sz);
790 
791 	ralloc_free(state);
792 }
793