| 1 |
|
| 2 |
|
| 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 |
|
| 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 |
|
| 43 |
extern const CaseInsensitiveDict<u32, '_'> sprg_map; |
| 44 |
|
| 45 |
extern const CaseInsensitiveDict<GekkoDirective> directives_map; |
| 46 |
|
| 47 |
extern const CaseInsensitiveDict<ParseInfo, '.', '_'> mnemonic_tokens; |
| 48 |
|
| 49 |
extern const CaseInsensitiveDict<ParseInfo, '.', '_', '+', '-'> extended_mnemonic_tokens; |
| 50 |
|
| 51 |
|
| 52 |
|
| 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 |
|
| 102 |
struct MnemonicDesc |
| 103 |
{ |
| 104 |
|
| 105 |
const u32 initial_value; |
| 106 |
const u32 operand_count; |
| 107 |
|
| 108 |
std::array<OperandDesc, MAX_OPERANDS> operand_masks; |
| 109 |
}; |
| 110 |
|
| 111 |
|
| 112 |
|
| 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 |
|
| 125 |
extern const std::array<MnemonicDesc, NUM_MNEMONICS * VARIANT_PERMUTATIONS> mnemonics; |
| 126 |
|
| 127 |
extern const std::array<ExtendedMnemonicDesc, NUM_EXT_MNEMONICS * VARIANT_PERMUTATIONS> |
| 128 |
extended_mnemonics; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
using TransitionF = bool (*)(char c); |
| 137 |
using DfaEdge = std::pair<TransitionF, size_t>; |
| 138 |
struct DfaNode |
| 139 |
{ |
| 140 |
std::vector<DfaEdge> edges; |
| 141 |
|
| 142 |
|
| 143 |
std::optional<std::string_view> match_failure_reason; |
| 144 |
}; |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
extern const std::vector<DfaNode> float_dfa; |
| 149 |
|
| 150 |
|
| 151 |
extern const std::vector<DfaNode> string_dfa; |
| 152 |
} |