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#ifndef _DECODE_H_
25#define _DECODE_H_
26
27#include <isaspec-isa.h>
28#include <stdbool.h>
29#include <stdint.h>
30
31/*
32 * Defines the tables which are generated from xml for disassembly
33 */
34
35struct decode_scope;
36struct isa_bitset;
37
38/**
39 * Table of enum values
40 */
41struct isa_enum {
42	unsigned num_values;
43	struct {
44		unsigned val;
45		const char *display;
46	} values[];
47};
48
49/**
50 * An expression used to for conditional overrides, derived fields, etc
51 */
52typedef uint64_t (*isa_expr_t)(struct decode_scope *scope);
53
54/**
55 * Used by generated expr functions
56 */
57uint64_t isa_decode_field(struct decode_scope *scope, const char *field_name);
58
59/**
60 * For bitset fields, there are some cases where we want to "remap" field
61 * names, essentially allowing one to parameterize a nested bitset when
62 * it resolves fields in an enclosing bitset.
63 */
64struct isa_field_params {
65	unsigned num_params;
66	struct {
67		const char *name;
68		const char *as;
69	} params[];
70};
71
72/**
73 * Description of a single field within a bitset case.
74 */
75struct isa_field {
76	const char *name;
77	isa_expr_t expr;       /* for virtual "derived" fields */
78	unsigned low;
79	unsigned high;
80	enum {
81		/* Basic types: */
82		TYPE_BRANCH,   /* branch target, like INT but optional labeling*/
83		TYPE_INT,
84		TYPE_UINT,
85		TYPE_HEX,
86		TYPE_OFFSET,   /* Like INT but formated with +/- or omitted if ==0 */
87		TYPE_UOFFSET,  /* Like UINT but formated with + or omitted if ==0 */
88		TYPE_FLOAT,
89		TYPE_BOOL,
90		TYPE_ENUM,
91
92		/* To assert a certain value in a given range of bits.. not
93		 * used for pattern matching, but allows an override to specify
94		 * that a certain bitpattern in some "unused" bits is expected
95		 */
96		TYPE_ASSERT,
97
98		/* For fields that are decoded with another bitset hierarchy: */
99		TYPE_BITSET,
100	} type;
101	union {
102		const struct isa_bitset **bitsets;  /* if type==BITSET */
103		bitmask_t val;                      /* if type==ASSERT */
104		const struct isa_enum *enums;       /* if type==ENUM */
105		const char *display;                /* if type==BOOL */
106	};
107
108	/**
109	 * type==BITSET fields can also optionally provide remapping for
110	 * field names
111	 */
112	const struct isa_field_params *params;
113};
114
115/**
116 * A bitset consists of N "cases", with the last one (with case->expr==NULL)
117 * being the default.
118 *
119 * When resolving a field, display template string, etc, all the cases with
120 * an expression that evaluates to non-zero are consider, falling back to
121 * the last (default) case.
122 */
123struct isa_case {
124	isa_expr_t expr;
125	const char *display;
126	unsigned num_fields;
127	struct isa_field fields[];
128};
129
130/**
131 * An individual bitset, the leaves of a bitset inheritance hiearchy will
132 * have the match and mask to match a single instruction (or arbitrary
133 * bit-pattern) against.
134 */
135struct isa_bitset {
136	const struct isa_bitset *parent;
137	const char *name;
138	struct {
139		unsigned min;
140		unsigned max;
141	} gen;
142	bitmask_t match;
143	bitmask_t dontcare;
144	bitmask_t mask;
145	unsigned num_cases;
146	const struct isa_case *cases[];
147};
148
149#endif /* _DECODE_H_ */
150