Debian Codegraph @

Source/Core/Common/Assembler/AssemblerTables.h

raw symbols SCIP
1 // Copyright 2023 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <array>
7 #include <cstddef>
8 #include <optional>
9 #include <string_view>
10 #include <vector>
11
12 #include "Common/Assembler/AssemblerShared.h"
13 #include "Common/Assembler/CaseInsensitiveDict.h"
14 #include "Common/CommonTypes.h"
15
16 namespace Common::GekkoAssembler::detail
17 {
18 ///////////////////
19 // PARSER TABLES //
20 ///////////////////
21 enum class ParseAlg
22 {
23 None,
24 Op1,
25 NoneOrOp1,
26 Op1Off1,
27 Op2,
28 Op1Or2,
29 Op3,
30 Op2Or3,
31 Op4,
32 Op5,
33 Op1Off1Op2,
34 };
35
36 struct ParseInfo
37 {
38 size_t mnemonic_index;
39 ParseAlg parse_algorithm;
40 };
41
42 // Mapping of SPRG names to values
43 extern const CaseInsensitiveDict<u32, '_'> sprg_map;
44 // Mapping of directive names to an enumeration
45 extern const CaseInsensitiveDict<GekkoDirective> directives_map;
46 // Mapping of normal Gekko mnemonics to their index and argument form
47 extern const CaseInsensitiveDict<ParseInfo, '.', '_'> mnemonic_tokens;
48 // Mapping of extended Gekko mnemonics to their index and argument form
49 extern const CaseInsensitiveDict<ParseInfo, '.', '_', '+', '-'> extended_mnemonic_tokens;
50
51 //////////////////////
52 // ASSEMBLER TABLES //
53 //////////////////////
54 constexpr size_t MAX_OPERANDS = 5;
55
56 struct OperandList
57 {
58 std::array<Tagged<Interval, u32>, MAX_OPERANDS> list;
59 u32 count;
60 bool overfill;
61
62 constexpr u32 operator[](size_t index) const { return ValueOf(list[index]); }
63 constexpr u32& operator[](size_t index) { return ValueOf(list[index]); }
64
65 void Insert(size_t before, u32 val);
66
67 template <typename It>
68 void Copy(It begin, It end)
69 {
70 count = 0;
71 for (auto& i : list)
72 {
73 if (begin == end)
74 {
75 break;
76 }
77 i = *begin;
78 begin++;
79 count++;
80 }
81 overfill = begin != end;
82 }
83 };
84
85 struct OperandDesc
86 {
87 u32 mask;
88 struct
89 {
90 u32 shift : 31;
91 bool is_signed : 1;
92 };
93 u32 MaxVal() const;
94 u32 MinVal() const;
95 u32 TruncBits() const;
96
97 bool Fits(u32 val) const;
98 u32 Fit(u32 val) const;
99 };
100
101 // MnemonicDesc holds the machine-code template for mnemonics
102 struct MnemonicDesc
103 {
104 // Initial value for a given mnemonic (opcode, func code, LK, AA, OE)
105 const u32 initial_value;
106 const u32 operand_count;
107 // Masks for operands
108 std::array<OperandDesc, MAX_OPERANDS> operand_masks;
109 };
110
111 // ExtendedMnemonicDesc holds the name of the mnemonic it transforms to as well as a
112 // transformer callback to translate the operands into the correct form for the base mnemonic
113 struct ExtendedMnemonicDesc
114 {
115 size_t mnemonic_index;
116 void (*transform_operands)(OperandList&);
117 };
118
119 static constexpr size_t NUM_MNEMONICS = static_cast<size_t>(GekkoMnemonic::LastMnemonic) + 1;
120 static constexpr size_t NUM_EXT_MNEMONICS =
121 static_cast<size_t>(ExtendedGekkoMnemonic::LastMnemonic) + 1;
122 static constexpr size_t VARIANT_PERMUTATIONS = 4;
123
124 // Table for mapping mnemonic+variants to their descriptors
125 extern const std::array<MnemonicDesc, NUM_MNEMONICS * VARIANT_PERMUTATIONS> mnemonics;
126 // Table for mapping extended mnemonic+variants to their descriptors
127 extern const std::array<ExtendedMnemonicDesc, NUM_EXT_MNEMONICS * VARIANT_PERMUTATIONS>
128 extended_mnemonics;
129
130 //////////////////
131 // LEXER TABLES //
132 //////////////////
133
134 // In place of the reliace on std::regex, DFAs will be defined for matching sufficiently complex
135 // tokens This gives an extra benefit of providing reasons for match failures
136 using TransitionF = bool (*)(char c);
137 using DfaEdge = std::pair<TransitionF, size_t>;
138 struct DfaNode
139 {
140 std::vector<DfaEdge> edges;
141 // If nullopt: this is a final node
142 // If string: invalid reason
143 std::optional<std::string_view> match_failure_reason;
144 };
145
146 // Floating point strings that will be accepted by std::stof/std::stod
147 // regex: [\+-]?(\d+(\.\d+)?|\.\d+)(e[\+-]?\d+)?
148 extern const std::vector<DfaNode> float_dfa;
149 // C-style strings
150 // regex: "([^\\\n]|\\([0-7]{1,3}|x[0-9a-fA-F]+|[^x0-7\n]))*"
151 extern const std::vector<DfaNode> string_dfa;
152 } // namespace Common::GekkoAssembler::detail