| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <algorithm> |
| 7 |
#include <array> |
| 8 |
#include <deque> |
| 9 |
#include <optional> |
| 10 |
#include <span> |
| 11 |
#include <string_view> |
| 12 |
#include <vector> |
| 13 |
|
| 14 |
#include "Common/Assembler/AssemblerShared.h" |
| 15 |
#include "Common/Assembler/AssemblerTables.h" |
| 16 |
#include "Common/CommonTypes.h" |
| 17 |
|
| 18 |
namespace Common::GekkoAssembler::detail |
| 19 |
{ |
| 20 |
void ConvertStringLiteral(std::string_view literal, std::vector<u8>* out_vec); |
| 21 |
|
| 22 |
enum class TokenType |
| 23 |
{ |
| 24 |
Invalid, |
| 25 |
Identifier, |
| 26 |
StringLit, |
| 27 |
HexadecimalLit, |
| 28 |
DecimalLit, |
| 29 |
OctalLit, |
| 30 |
BinaryLit, |
| 31 |
FloatLit, |
| 32 |
GPR, |
| 33 |
FPR, |
| 34 |
CRField, |
| 35 |
SPR, |
| 36 |
Lt, |
| 37 |
Gt, |
| 38 |
Eq, |
| 39 |
So, |
| 40 |
NumLabFwd, |
| 41 |
NumLabBwd, |
| 42 |
|
| 43 |
Eol, |
| 44 |
Eof, |
| 45 |
|
| 46 |
Dot, |
| 47 |
Colon, |
| 48 |
Comma, |
| 49 |
Lparen, |
| 50 |
Rparen, |
| 51 |
Pipe, |
| 52 |
Caret, |
| 53 |
Ampersand, |
| 54 |
Lsh, |
| 55 |
Rsh, |
| 56 |
Plus, |
| 57 |
Minus, |
| 58 |
Star, |
| 59 |
Slash, |
| 60 |
Tilde, |
| 61 |
Grave, |
| 62 |
At, |
| 63 |
|
| 64 |
OperatorBegin = Dot, |
| 65 |
LastToken = At, |
| 66 |
}; |
| 67 |
|
| 68 |
std::string_view TokenTypeToStr(TokenType); |
| 69 |
|
| 70 |
struct AssemblerToken |
| 71 |
{ |
| 72 |
TokenType token_type; |
| 73 |
std::string_view token_val; |
| 74 |
std::string_view invalid_reason; |
| 75 |
|
| 76 |
Interval invalid_region; |
| 77 |
|
| 78 |
std::string_view TypeStr() const; |
| 79 |
std::string_view ValStr() const; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
template <typename T> |
| 84 |
std::optional<T> EvalToken() const; |
| 85 |
}; |
| 86 |
|
| 87 |
struct CursorPosition |
| 88 |
{ |
| 89 |
size_t index = 0; |
| 90 |
size_t line = 0; |
| 91 |
size_t col = 0; |
| 92 |
}; |
| 93 |
|
| 94 |
class Lexer |
| 95 |
{ |
| 96 |
public: |
| 97 |
enum class IdentifierMatchRule |
| 98 |
{ |
| 99 |
Typical, |
| 100 |
Mnemonic, |
| 101 |
Directive, |
| 102 |
}; |
| 103 |
|
| 104 |
public: |
| 105 |
explicit Lexer(std::string_view str) |
| 106 |
: m_lex_string(str), m_match_rule(IdentifierMatchRule::Typical) |
| 107 |
{ |
| 108 |
} |
| 109 |
|
| 110 |
size_t LineNumber() const; |
| 111 |
size_t ColNumber() const; |
| 112 |
std::string_view CurrentLine() const; |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
void SetIdentifierMatchRule(IdentifierMatchRule set); |
| 117 |
const Tagged<CursorPosition, AssemblerToken>& LookaheadTagRef(size_t num_fwd) const; |
| 118 |
AssemblerToken Lookahead() const; |
| 119 |
const AssemblerToken& LookaheadRef() const; |
| 120 |
TokenType LookaheadType() const; |
| 121 |
|
| 122 |
|
| 123 |
AssemblerToken LookaheadFloat() const; |
| 124 |
void Eat(); |
| 125 |
void EatAndReset(); |
| 126 |
|
| 127 |
template <size_t N> |
| 128 |
void LookaheadTaggedN(std::array<Tagged<CursorPosition, AssemblerToken>, N>* tokens_out) const |
| 129 |
{ |
| 130 |
const size_t filled_amt = std::min(m_lexed_tokens.size(), N); |
| 131 |
|
| 132 |
std::copy_n(m_lexed_tokens.begin(), filled_amt, tokens_out->begin()); |
| 133 |
|
| 134 |
std::generate_n(tokens_out->begin() + filled_amt, N - filled_amt, [this] { |
| 135 |
CursorPosition p = m_pos; |
| 136 |
return m_lexed_tokens.emplace_back(p, LexSingle()); |
| 137 |
}); |
| 138 |
} |
| 139 |
|
| 140 |
template <size_t N> |
| 141 |
void LookaheadN(std::array<AssemblerToken, N>* tokens_out) const |
| 142 |
{ |
| 143 |
const size_t filled_amt = std::min(m_lexed_tokens.size(), N); |
| 144 |
|
| 145 |
auto _it = m_lexed_tokens.begin(); |
| 146 |
std::generate_n(tokens_out->begin(), filled_amt, [&_it] { return ValueOf(*_it++); }); |
| 147 |
|
| 148 |
std::generate_n(tokens_out->begin() + filled_amt, N - filled_amt, [this] { |
| 149 |
CursorPosition p = m_pos; |
| 150 |
return ValueOf(m_lexed_tokens.emplace_back(p, LexSingle())); |
| 151 |
}); |
| 152 |
} |
| 153 |
|
| 154 |
template <size_t N> |
| 155 |
void EatN() |
| 156 |
{ |
| 157 |
size_t consumed = 0; |
| 158 |
while (m_lexed_tokens.size() > 0 && consumed < N) |
| 159 |
{ |
| 160 |
m_lexed_tokens.pop_front(); |
| 161 |
consumed++; |
| 162 |
} |
| 163 |
for (size_t i = consumed; i < N; i++) |
| 164 |
{ |
| 165 |
LexSingle(); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
private: |
| 170 |
std::optional<std::string_view> RunDfa(std::span<const DfaNode> dfa) const; |
| 171 |
void SkipWs() const; |
| 172 |
void FeedbackTokens() const; |
| 173 |
bool IdentifierHeadExtra(char h) const; |
| 174 |
bool IdentifierExtra(char c) const; |
| 175 |
void ScanStart() const; |
| 176 |
void ScanFinish() const; |
| 177 |
std::string_view ScanFinishOut() const; |
| 178 |
char Peek() const; |
| 179 |
const Lexer& Step() const; |
| 180 |
TokenType LexStringLit(std::string_view& invalid_reason, Interval& invalid_region) const; |
| 181 |
TokenType ClassifyAlnum() const; |
| 182 |
AssemblerToken LexSingle() const; |
| 183 |
|
| 184 |
std::string_view m_lex_string; |
| 185 |
mutable CursorPosition m_pos; |
| 186 |
mutable CursorPosition m_scan_pos; |
| 187 |
mutable std::deque<Tagged<CursorPosition, AssemblerToken>> m_lexed_tokens; |
| 188 |
IdentifierMatchRule m_match_rule; |
| 189 |
}; |
| 190 |
} |