From d62f022c92fcc3ebe2269273628943a0089683fd Mon Sep 17 00:00:00 2001 From: Richard-Qin-X Date: Thu, 13 Aug 2026 06:19:30 +0800 Subject: [CodeGen] Align MIR parser and printer ownership --- backend/include/rcc/CodeGen/MIRParser/MIParser.h | 107 ++++++++++++++++++ backend/include/rcc/CodeGen/MIRParser/MIRParser.h | 130 ++++++++++++++++++++++ 2 files changed, 237 insertions(+) create mode 100644 backend/include/rcc/CodeGen/MIRParser/MIParser.h create mode 100644 backend/include/rcc/CodeGen/MIRParser/MIRParser.h (limited to 'backend/include/rcc/CodeGen/MIRParser') diff --git a/backend/include/rcc/CodeGen/MIRParser/MIParser.h b/backend/include/rcc/CodeGen/MIRParser/MIParser.h new file mode 100644 index 0000000..0b888e5 --- /dev/null +++ b/backend/include/rcc/CodeGen/MIRParser/MIParser.h @@ -0,0 +1,107 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RCC_CODEGEN_MIRPARSER_MIPARSER_H +#define RCC_CODEGEN_MIRPARSER_MIPARSER_H + +#include "rcc/CodeGen/MachineIRTypes.h" +#include "rcc/CodeGen/MachineMetadata.h" +#include "rcc/CodeGen/Register.h" +#include "rcc/Support/Error.h" +#include "rcc/Support/TextDiagnostic.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace rcc { + +class MachineFunction; +class TargetDescription; + +namespace mir { + +struct MIVirtualRegisterSlot final { + std::uint32_t TextID = 0; + Register Reg; +}; + +struct MIFrameObjectSlot final { + std::uint32_t TextID = 0; + FrameIndex Index; + std::optional Name; +}; + +struct MIConstantPoolSlot final { + std::uint32_t TextID = 0; + ConstantPoolIndex Index; +}; + +struct MIJumpTableSlot final { + std::uint32_t TextID = 0; + JumpTableIndex Index; +}; + +struct MIMetadataSlot final { + std::uint32_t TextID = 0; + MachineMetadataReference Reference; +}; + +struct MISyncScopeSlot final { + std::string Name; + SyncScopeID ID; +}; + +struct MIParserContext final { + std::vector VirtualRegisters; + std::vector StackObjects; + std::vector FixedStackObjects; + std::vector ConstantPoolItems; + std::vector JumpTables; + std::vector Metadata; + std::vector SyncScopes; + std::function HasIRBlock; + std::function(std::string_view)> InternSyncScope; + std::function(std::uint32_t)> PointerSizeInBits; +}; + +struct MIParserOptions final { + std::size_t MaximumTokens = std::size_t{1} << 24U; + std::size_t MaximumTokenLength = std::size_t{1} << 24U; + std::size_t MaximumBlocks = std::size_t{1} << 20U; + std::size_t MaximumInstructions = std::size_t{1} << 24U; + std::size_t MaximumOperandsPerInstruction = std::size_t{1} << 20U; + std::size_t MaximumMemoryOperandsPerInstruction = std::size_t{1} << 20U; + bool VerifyAfterParse = true; +}; + +class MIParser final { +public: + MIParser(MachineFunction &Function, const TargetDescription &Target, std::string SourceName, + std::string SourceText, MIParserContext Context = {}, MIParserOptions Options = {}); + MIParser(const MIParser &) = delete; + MIParser &operator=(const MIParser &) = delete; + MIParser(MIParser &&) noexcept; + MIParser &operator=(MIParser &&) noexcept; + ~MIParser(); + + [[nodiscard]] Expected parse(); + + [[nodiscard]] const TextSource &source() const noexcept; + [[nodiscard]] std::span diagnostics() const noexcept; + [[nodiscard]] bool failed() const noexcept; + +private: + class Impl; + std::unique_ptr Implementation; +}; + +} // namespace mir +} // namespace rcc + +#endif // RCC_CODEGEN_MIRPARSER_MIPARSER_H diff --git a/backend/include/rcc/CodeGen/MIRParser/MIRParser.h b/backend/include/rcc/CodeGen/MIRParser/MIRParser.h new file mode 100644 index 0000000..798a747 --- /dev/null +++ b/backend/include/rcc/CodeGen/MIRParser/MIRParser.h @@ -0,0 +1,130 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RCC_CODEGEN_MIRPARSER_MIRPARSER_H +#define RCC_CODEGEN_MIRPARSER_MIRPARSER_H + +#include "rcc/CodeGen/CompilationUnit.h" +#include "rcc/CodeGen/MIRParser/MIParser.h" +#include "rcc/CodeGen/StableID.h" +#include "rcc/MIR/EmbeddedIR.h" +#include "rcc/Support/TextDiagnostic.h" +#include "rcc/Support/YAML.h" + +#include +#include +#include +#include +#include +#include + +namespace rcc { + +class TargetDescription; + +namespace mir { + +struct MIRFunctionDocumentInfo final { + MachineFunctionStableID Function; + std::optional NoPHIs; + std::optional IsSSA; + std::optional NoVRegs; + std::optional HasFakeUses; + + friend bool operator==(const MIRFunctionDocumentInfo &Left, + const MIRFunctionDocumentInfo &Right) = default; +}; + +class MIRDocument final { +public: + MIRDocument(); + explicit MIRDocument(CompilationUnit Unit); + MIRDocument(const MIRDocument &) = delete; + MIRDocument &operator=(const MIRDocument &) = delete; + MIRDocument(MIRDocument &&) noexcept; + MIRDocument &operator=(MIRDocument &&) noexcept; + ~MIRDocument(); + + [[nodiscard]] CompilationUnit &unit() noexcept { + return Unit; + } + + [[nodiscard]] const CompilationUnit &unit() const noexcept { + return Unit; + } + + [[nodiscard]] const EmbeddedIRReferenceIndex *embeddedIR() const noexcept { + return EmbeddedIR ? &*EmbeddedIR : nullptr; + } + + [[nodiscard]] std::span functionDocumentInfo() const noexcept { + return FunctionInfo; + } + + [[nodiscard]] std::span syncScopes() const noexcept { + return SyncScopes; + } + +private: + CompilationUnit Unit; + std::optional EmbeddedIR; + std::vector FunctionInfo; + std::vector SyncScopes; + + friend class MIRDocumentParser; +}; + +struct MIRDocumentParserOptions final { + yaml::ParseOptions YAML; + MIParserOptions MachineInstructions; + std::string DataLayout; + bool VerifyMachineFunctions = true; +}; + +class MIRDocumentParseResult final { +public: + MIRDocumentParseResult(const MIRDocumentParseResult &) = delete; + MIRDocumentParseResult &operator=(const MIRDocumentParseResult &) = delete; + MIRDocumentParseResult(MIRDocumentParseResult &&) noexcept; + MIRDocumentParseResult &operator=(MIRDocumentParseResult &&) noexcept; + ~MIRDocumentParseResult(); + + [[nodiscard]] bool succeeded() const noexcept { + return Document.has_value() && Diagnostics.empty(); + } + + [[nodiscard]] MIRDocument *document() noexcept { + return Document ? &*Document : nullptr; + } + + [[nodiscard]] const MIRDocument *document() const noexcept { + return Document ? &*Document : nullptr; + } + + [[nodiscard]] const TextSource &source() const noexcept { + return Source; + } + + [[nodiscard]] std::span diagnostics() const noexcept { + return Diagnostics; + } + +private: + MIRDocumentParseResult(std::string SourceName, std::string SourceText) + : Source(std::move(SourceName), std::move(SourceText)) {} + + TextSource Source; + std::optional Document; + std::vector Diagnostics; + + friend class MIRDocumentParser; +}; + +[[nodiscard]] MIRDocumentParseResult parseMIRDocument(const TargetDescription &Target, + std::string SourceName, + std::string SourceText, + MIRDocumentParserOptions Options = {}); + +} // namespace mir +} // namespace rcc + +#endif // RCC_CODEGEN_MIRPARSER_MIRPARSER_H -- cgit v1.2.3