aboutsummaryrefslogtreecommitdiff
path: root/backend/include
diff options
context:
space:
mode:
authorRichard-Qin-X <richard.qin.001@gmail.com>2026-08-16 15:39:45 +0800
committerRichard-Qin-X <richard.qin.001@gmail.com>2026-08-16 15:39:45 +0800
commit4f7b41c3c711d6c6fa4e422f3eec8961c91eea7a (patch)
treea825927a4a3ef91ec6e3489aa1f96e63788ff476 /backend/include
parenta39d8f6f4ed2ad0f8435b73bd6b2a35769c6fead (diff)
downloadrcc-project-4f7b41c3c711d6c6fa4e422f3eec8961c91eea7a.tar.gz
rcc-project-4f7b41c3c711d6c6fa4e422f3eec8961c91eea7a.tar.xz
tablegen: Generate GlobalISel match tables
Diffstat (limited to 'backend/include')
-rw-r--r--backend/include/rcc/CodeGen/GlobalISel/GIMatchTableExecutor.h70
-rw-r--r--backend/include/rcc/CodeGen/GlobalISel/InstructionSelector.h4
-rw-r--r--backend/include/rcc/TableGen/Record.h38
3 files changed, 110 insertions, 2 deletions
diff --git a/backend/include/rcc/CodeGen/GlobalISel/GIMatchTableExecutor.h b/backend/include/rcc/CodeGen/GlobalISel/GIMatchTableExecutor.h
new file mode 100644
index 0000000..807cb4a
--- /dev/null
+++ b/backend/include/rcc/CodeGen/GlobalISel/GIMatchTableExecutor.h
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+#ifndef RCC_CODEGEN_GLOBALISEL_GIMATCHTABLEEXECUTOR_H
+#define RCC_CODEGEN_GLOBALISEL_GIMATCHTABLEEXECUTOR_H
+
+#include "rcc/CodeGen/MachineIRTypes.h"
+#include "rcc/Support/Error.h"
+
+#include <cstdint>
+#include <span>
+#include <string_view>
+
+namespace rcc {
+
+class MachineFunction;
+class MachineInstr;
+class TargetDescription;
+
+enum class GIMatchTypeKind : std::uint8_t {
+ Any,
+ Scalar,
+ Pointer,
+};
+
+enum class GIMatchTableOpcode : std::uint8_t {
+ CheckOpcode,
+ CheckOperandType,
+ CheckOperandBank,
+ CheckOperandClass,
+ MutateOpcode,
+ AppendImmediate,
+ Complete,
+};
+
+struct GIMatchTableInstruction final {
+ GIMatchTableOpcode Opcode = GIMatchTableOpcode::Complete;
+ std::uint16_t OperandIndex = 0;
+ std::uint32_t Value = 0;
+ std::uint32_t Auxiliary = 0;
+ std::int64_t Immediate = 0;
+};
+
+struct GIMatchTableRule final {
+ std::string_view Name;
+ std::uint32_t Priority = 0;
+ std::uint32_t FirstInstruction = 0;
+ std::uint32_t InstructionCount = 0;
+};
+
+struct GIMatchTable final {
+ std::span<const GIMatchTableInstruction> Instructions;
+ std::span<const GIMatchTableRule> Rules;
+};
+
+class GIMatchTableExecutor {
+public:
+ [[nodiscard]] Expected<bool> match(const GIMatchTable &Table, const MachineInstr &Instruction,
+ const MachineFunction &Function,
+ const TargetDescription &Target) const;
+
+ // A false result means that no table rule matched. The executor completes all
+ // predicates before applying the selected rule to MachineIR.
+ [[nodiscard]] Expected<bool> select(const GIMatchTable &Table, MachineInstr &Instruction,
+ MachineFunction &Function,
+ const TargetDescription &Target) const;
+};
+
+} // namespace rcc
+
+#endif // RCC_CODEGEN_GLOBALISEL_GIMATCHTABLEEXECUTOR_H
diff --git a/backend/include/rcc/CodeGen/GlobalISel/InstructionSelector.h b/backend/include/rcc/CodeGen/GlobalISel/InstructionSelector.h
index 4bea4e7..36fca8d 100644
--- a/backend/include/rcc/CodeGen/GlobalISel/InstructionSelector.h
+++ b/backend/include/rcc/CodeGen/GlobalISel/InstructionSelector.h
@@ -3,7 +3,7 @@
#ifndef RCC_CODEGEN_GLOBALISEL_INSTRUCTIONSELECTOR_H
#define RCC_CODEGEN_GLOBALISEL_INSTRUCTIONSELECTOR_H
-#include "rcc/CodeGen/MachineIRTypes.h"
+#include "rcc/CodeGen/GlobalISel/GIMatchTableExecutor.h"
#include "rcc/Support/Error.h"
namespace rcc {
@@ -12,7 +12,7 @@ class MachineFunction;
class MachineInstr;
class TargetDescription;
-class InstructionSelector {
+class InstructionSelector : public GIMatchTableExecutor {
public:
virtual ~InstructionSelector() = default;
diff --git a/backend/include/rcc/TableGen/Record.h b/backend/include/rcc/TableGen/Record.h
index 50ac4c9..94308c7 100644
--- a/backend/include/rcc/TableGen/Record.h
+++ b/backend/include/rcc/TableGen/Record.h
@@ -205,6 +205,43 @@ struct SourceInstruction final {
TextRange Range;
};
+struct SourceSelectionTypePredicate final {
+ std::string Kind;
+ std::uint32_t Bits = 0;
+ TextRange Range;
+};
+
+struct SourceSelectionOperandTypePredicate final {
+ std::uint32_t OperandIndex = 0;
+ SourceSelectionTypePredicate Type;
+ TextRange Range;
+};
+
+struct SourceSelectionOperandBankPredicate final {
+ std::uint32_t OperandIndex = 0;
+ std::string BankSymbol;
+ TextRange Range;
+};
+
+struct SourceSelectionOperandClassPredicate final {
+ std::uint32_t OperandIndex = 0;
+ std::string ClassSymbol;
+ TextRange Range;
+};
+
+struct SourceSelectionRule final {
+ std::string Symbol;
+ std::uint32_t Priority = 0;
+ std::string MatchOpcodeSymbol;
+ std::optional<SourceSelectionTypePredicate> ResultType;
+ std::vector<SourceSelectionOperandTypePredicate> OperandTypes;
+ std::vector<SourceSelectionOperandBankPredicate> OperandBanks;
+ std::vector<SourceSelectionOperandClassPredicate> OperandClasses;
+ std::string ResultOpcodeSymbol;
+ std::vector<std::int64_t> AppendedImmediates;
+ TextRange Range;
+};
+
struct DescriptionAST final {
SourceTarget Target;
std::vector<SourceRegisterUnit> RegisterUnits;
@@ -220,6 +257,7 @@ struct DescriptionAST final {
std::vector<SourceEncodingCategory> EncodingCategories;
std::vector<SourceAssemblyFormat> AssemblyFormats;
std::vector<SourceInstruction> Instructions;
+ std::vector<SourceSelectionRule> SelectionRules;
};
[[nodiscard]] std::string lowerASCII(std::string_view Value);