17ec681f3Smrg/*
27ec681f3Smrg * Copyright © 2020 Google, Inc.
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
207ec681f3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
217ec681f3Smrg * SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg
247ec681f3Smrg#ifndef _DECODE_H_
257ec681f3Smrg#define _DECODE_H_
267ec681f3Smrg
277ec681f3Smrg#include <isaspec-isa.h>
287ec681f3Smrg#include <stdbool.h>
297ec681f3Smrg#include <stdint.h>
307ec681f3Smrg
317ec681f3Smrg/*
327ec681f3Smrg * Defines the tables which are generated from xml for disassembly
337ec681f3Smrg */
347ec681f3Smrg
357ec681f3Smrgstruct decode_scope;
367ec681f3Smrgstruct isa_bitset;
377ec681f3Smrg
387ec681f3Smrg/**
397ec681f3Smrg * Table of enum values
407ec681f3Smrg */
417ec681f3Smrgstruct isa_enum {
427ec681f3Smrg	unsigned num_values;
437ec681f3Smrg	struct {
447ec681f3Smrg		unsigned val;
457ec681f3Smrg		const char *display;
467ec681f3Smrg	} values[];
477ec681f3Smrg};
487ec681f3Smrg
497ec681f3Smrg/**
507ec681f3Smrg * An expression used to for conditional overrides, derived fields, etc
517ec681f3Smrg */
527ec681f3Smrgtypedef uint64_t (*isa_expr_t)(struct decode_scope *scope);
537ec681f3Smrg
547ec681f3Smrg/**
557ec681f3Smrg * Used by generated expr functions
567ec681f3Smrg */
577ec681f3Smrguint64_t isa_decode_field(struct decode_scope *scope, const char *field_name);
587ec681f3Smrg
597ec681f3Smrg/**
607ec681f3Smrg * For bitset fields, there are some cases where we want to "remap" field
617ec681f3Smrg * names, essentially allowing one to parameterize a nested bitset when
627ec681f3Smrg * it resolves fields in an enclosing bitset.
637ec681f3Smrg */
647ec681f3Smrgstruct isa_field_params {
657ec681f3Smrg	unsigned num_params;
667ec681f3Smrg	struct {
677ec681f3Smrg		const char *name;
687ec681f3Smrg		const char *as;
697ec681f3Smrg	} params[];
707ec681f3Smrg};
717ec681f3Smrg
727ec681f3Smrg/**
737ec681f3Smrg * Description of a single field within a bitset case.
747ec681f3Smrg */
757ec681f3Smrgstruct isa_field {
767ec681f3Smrg	const char *name;
777ec681f3Smrg	isa_expr_t expr;       /* for virtual "derived" fields */
787ec681f3Smrg	unsigned low;
797ec681f3Smrg	unsigned high;
807ec681f3Smrg	enum {
817ec681f3Smrg		/* Basic types: */
827ec681f3Smrg		TYPE_BRANCH,   /* branch target, like INT but optional labeling*/
837ec681f3Smrg		TYPE_INT,
847ec681f3Smrg		TYPE_UINT,
857ec681f3Smrg		TYPE_HEX,
867ec681f3Smrg		TYPE_OFFSET,   /* Like INT but formated with +/- or omitted if ==0 */
877ec681f3Smrg		TYPE_UOFFSET,  /* Like UINT but formated with + or omitted if ==0 */
887ec681f3Smrg		TYPE_FLOAT,
897ec681f3Smrg		TYPE_BOOL,
907ec681f3Smrg		TYPE_ENUM,
917ec681f3Smrg
927ec681f3Smrg		/* To assert a certain value in a given range of bits.. not
937ec681f3Smrg		 * used for pattern matching, but allows an override to specify
947ec681f3Smrg		 * that a certain bitpattern in some "unused" bits is expected
957ec681f3Smrg		 */
967ec681f3Smrg		TYPE_ASSERT,
977ec681f3Smrg
987ec681f3Smrg		/* For fields that are decoded with another bitset hierarchy: */
997ec681f3Smrg		TYPE_BITSET,
1007ec681f3Smrg	} type;
1017ec681f3Smrg	union {
1027ec681f3Smrg		const struct isa_bitset **bitsets;  /* if type==BITSET */
1037ec681f3Smrg		bitmask_t val;                      /* if type==ASSERT */
1047ec681f3Smrg		const struct isa_enum *enums;       /* if type==ENUM */
1057ec681f3Smrg		const char *display;                /* if type==BOOL */
1067ec681f3Smrg	};
1077ec681f3Smrg
1087ec681f3Smrg	/**
1097ec681f3Smrg	 * type==BITSET fields can also optionally provide remapping for
1107ec681f3Smrg	 * field names
1117ec681f3Smrg	 */
1127ec681f3Smrg	const struct isa_field_params *params;
1137ec681f3Smrg};
1147ec681f3Smrg
1157ec681f3Smrg/**
1167ec681f3Smrg * A bitset consists of N "cases", with the last one (with case->expr==NULL)
1177ec681f3Smrg * being the default.
1187ec681f3Smrg *
1197ec681f3Smrg * When resolving a field, display template string, etc, all the cases with
1207ec681f3Smrg * an expression that evaluates to non-zero are consider, falling back to
1217ec681f3Smrg * the last (default) case.
1227ec681f3Smrg */
1237ec681f3Smrgstruct isa_case {
1247ec681f3Smrg	isa_expr_t expr;
1257ec681f3Smrg	const char *display;
1267ec681f3Smrg	unsigned num_fields;
1277ec681f3Smrg	struct isa_field fields[];
1287ec681f3Smrg};
1297ec681f3Smrg
1307ec681f3Smrg/**
1317ec681f3Smrg * An individual bitset, the leaves of a bitset inheritance hiearchy will
1327ec681f3Smrg * have the match and mask to match a single instruction (or arbitrary
1337ec681f3Smrg * bit-pattern) against.
1347ec681f3Smrg */
1357ec681f3Smrgstruct isa_bitset {
1367ec681f3Smrg	const struct isa_bitset *parent;
1377ec681f3Smrg	const char *name;
1387ec681f3Smrg	struct {
1397ec681f3Smrg		unsigned min;
1407ec681f3Smrg		unsigned max;
1417ec681f3Smrg	} gen;
1427ec681f3Smrg	bitmask_t match;
1437ec681f3Smrg	bitmask_t dontcare;
1447ec681f3Smrg	bitmask_t mask;
1457ec681f3Smrg	unsigned num_cases;
1467ec681f3Smrg	const struct isa_case *cases[];
1477ec681f3Smrg};
1487ec681f3Smrg
1497ec681f3Smrg#endif /* _DECODE_H_ */
150