Debian Codegraph @

Source/Core/Common/Assembler/GekkoParser.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 <optional>
7 #include <string>
8 #include <string_view>
9
10 #include "Common/Assembler/AssemblerShared.h"
11 #include "Common/Assembler/GekkoLexer.h"
12 #include "Common/CommonTypes.h"
13
14 namespace Common::GekkoAssembler::detail
15 {
16 class ParsePlugin;
17
18 struct ParseState
19 {
20 ParseState(std::string_view input_str, ParsePlugin& plugin);
21
22 bool HasToken(TokenType tp) const;
23 void ParseToken(TokenType tp);
24 void EmitErrorHere(std::string&& message);
25
26 Lexer lexer;
27 ParsePlugin& plugin;
28
29 std::optional<AssemblerError> error;
30 bool eof;
31 };
32
33 enum class AsmOp
34 {
35 Or,
36 Xor,
37 And,
38 Lsh,
39 Rsh,
40 Add,
41 Sub,
42 Mul,
43 Div,
44 Neg,
45 Not
46 };
47
48 enum class Terminal
49 {
50 Hex,
51 Dec,
52 Oct,
53 Bin,
54 Flt,
55 Str,
56 Id,
57 GPR,
58 FPR,
59 SPR,
60 CRField,
61 Lt,
62 Gt,
63 Eq,
64 So,
65 Dot,
66 NumLabFwd,
67 NumLabBwd,
68 };
69
70 enum class ParenType
71 {
72 Normal,
73 RelConv,
74 };
75
76 // Overridable plugin class supporting a series of skeleton functions which get called when
77 // the parser parses a given point of interest
78 class ParsePlugin
79 {
80 public:
81 ParsePlugin() : m_owner(nullptr) {}
82 virtual ~ParsePlugin() = default;
83
84 void SetOwner(ParseState* o) { m_owner = o; }
85 void ForwardError(AssemblerError&& err) { m_owner_error = std::move(err); }
86 std::optional<AssemblerError>& Error() { return m_owner_error; }
87
88 virtual void PostParseAction() {}
89
90 // Nonterminal callouts
91 // Pre occurs prior to the head nonterminal being parsed
92 // Post occurs after the nonterminal has been fully parsed
93 virtual void OnDirectivePre(GekkoDirective directive) {}
94 virtual void OnDirectivePost(GekkoDirective directive) {}
95 virtual void OnInstructionPre(const ParseInfo& mnemonic_info, bool extended) {}
96 virtual void OnInstructionPost(const ParseInfo& mnemonic_info, bool extended) {}
97 virtual void OnOperandPre() {}
98 virtual void OnOperandPost() {}
99 virtual void OnResolvedExprPre() {}
100 virtual void OnResolvedExprPost() {}
101
102 // Operator callouts
103 // All occur after the relevant operands have been parsed
104 virtual void OnOperator(AsmOp operation) {}
105
106 // Individual token callouts
107 // All occur prior to the token being parsed
108 // Due to ambiguity of some tokens, an explicit operation is provided
109 virtual void OnTerminal(Terminal type, const AssemblerToken& val) {}
110 virtual void OnHiaddr(std::string_view id) {}
111 virtual void OnLoaddr(std::string_view id) {}
112 virtual void OnOpenParen(ParenType type) {}
113 virtual void OnCloseParen(ParenType type) {}
114 virtual void OnError() {}
115 virtual void OnLabelDecl(std::string_view name) {}
116 virtual void OnNumericLabelDecl(std::string_view name, u32 parse_num) {}
117 virtual void OnVarDecl(std::string_view name) {}
118
119 protected:
120 ParseState* m_owner;
121 std::optional<AssemblerError> m_owner_error;
122 };
123
124 // Parse the provided input with a plugin to handle what to do with certain points of interest
125 // e.g. Convert to an IR for generating final machine code, picking up syntactical information
126 void ParseWithPlugin(ParsePlugin* plugin, std::string_view input);
127 } // namespace Common::GekkoAssembler::detail