diff options
| author | Richard-Qin-X <richard.qin.001@gmail.com> | 2026-08-13 06:19:30 +0800 |
|---|---|---|
| committer | Richard-Qin-X <richard.qin.001@gmail.com> | 2026-08-13 06:19:30 +0800 |
| commit | d62f022c92fcc3ebe2269273628943a0089683fd (patch) | |
| tree | 92d13e4888b095cca71a0ac8b9b9eb99cc845596 /backend/include | |
| parent | 236082a0fe4dcd0362c5c74923a93e3db6a01291 (diff) | |
| download | rcc-project-d62f022c92fcc3ebe2269273628943a0089683fd.tar.gz rcc-project-d62f022c92fcc3ebe2269273628943a0089683fd.tar.xz | |
[CodeGen] Align MIR parser and printer ownership
Diffstat (limited to 'backend/include')
| -rw-r--r-- | backend/include/rcc/CodeGen/MIRParser/MIParser.h (renamed from backend/include/rcc/MIR/MIParser.h) | 6 | ||||
| -rw-r--r-- | backend/include/rcc/CodeGen/MIRParser/MIRParser.h (renamed from backend/include/rcc/MIR/MIRDocument.h) | 19 | ||||
| -rw-r--r-- | backend/include/rcc/CodeGen/MIRPrinter.h (renamed from backend/include/rcc/MIR/MIPrinter.h) | 18 | ||||
| -rw-r--r-- | backend/include/rcc/CodeGen/MIRYamlMapping.h | 144 | ||||
| -rw-r--r-- | backend/include/rcc/MIR/MIDebugSyntax.h | 26 | ||||
| -rw-r--r-- | backend/include/rcc/MIR/MILexer.h | 181 | ||||
| -rw-r--r-- | backend/include/rcc/Tooling/ArtifactStore.h | 2 | ||||
| -rw-r--r-- | backend/include/rcc/Tooling/PipelineDriver.h | 2 |
8 files changed, 167 insertions, 231 deletions
diff --git a/backend/include/rcc/MIR/MIParser.h b/backend/include/rcc/CodeGen/MIRParser/MIParser.h index b660f47..0b888e5 100644 --- a/backend/include/rcc/MIR/MIParser.h +++ b/backend/include/rcc/CodeGen/MIRParser/MIParser.h @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-3.0-or-later -#ifndef RCC_MIR_MIPARSER_H -#define RCC_MIR_MIPARSER_H +#ifndef RCC_CODEGEN_MIRPARSER_MIPARSER_H +#define RCC_CODEGEN_MIRPARSER_MIPARSER_H #include "rcc/CodeGen/MachineIRTypes.h" #include "rcc/CodeGen/MachineMetadata.h" @@ -104,4 +104,4 @@ private: } // namespace mir } // namespace rcc -#endif // RCC_MIR_MIPARSER_H +#endif // RCC_CODEGEN_MIRPARSER_MIPARSER_H diff --git a/backend/include/rcc/MIR/MIRDocument.h b/backend/include/rcc/CodeGen/MIRParser/MIRParser.h index 6041657..798a747 100644 --- a/backend/include/rcc/MIR/MIRDocument.h +++ b/backend/include/rcc/CodeGen/MIRParser/MIRParser.h @@ -1,13 +1,12 @@ // SPDX-License-Identifier: GPL-3.0-or-later -#ifndef RCC_MIR_MIRDOCUMENT_H -#define RCC_MIR_MIRDOCUMENT_H +#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/MIR/MIParser.h" -#include "rcc/MIR/MIPrinter.h" #include "rcc/Support/TextDiagnostic.h" #include "rcc/Support/YAML.h" @@ -125,17 +124,7 @@ private: std::string SourceText, MIRDocumentParserOptions Options = {}); -struct MIRDocumentPrinterOptions final { - MIPrintMode Mode = MIPrintMode::Complete; - bool EmitEmbeddedIR = true; - std::string DataLayout; -}; - -[[nodiscard]] Expected<std::string> printMIRDocument(const MIRDocument &Document, - const TargetDescription &Target, - MIRDocumentPrinterOptions Options = {}); - } // namespace mir } // namespace rcc -#endif // RCC_MIR_MIRDOCUMENT_H +#endif // RCC_CODEGEN_MIRPARSER_MIRPARSER_H diff --git a/backend/include/rcc/MIR/MIPrinter.h b/backend/include/rcc/CodeGen/MIRPrinter.h index 663859e..bb87509 100644 --- a/backend/include/rcc/MIR/MIPrinter.h +++ b/backend/include/rcc/CodeGen/MIRPrinter.h @@ -1,9 +1,9 @@ // SPDX-License-Identifier: GPL-3.0-or-later -#ifndef RCC_MIR_MIPRINTER_H -#define RCC_MIR_MIPRINTER_H +#ifndef RCC_CODEGEN_MIRPRINTER_H +#define RCC_CODEGEN_MIRPRINTER_H -#include "rcc/MIR/MIParser.h" +#include "rcc/CodeGen/MIRParser/MIRParser.h" #include "rcc/Support/Error.h" #include <string> @@ -29,7 +29,17 @@ struct MIPrinterOptions final { const MIParserContext &Context = {}, MIPrinterOptions Options = {}); +struct MIRDocumentPrinterOptions final { + MIPrintMode Mode = MIPrintMode::Complete; + bool EmitEmbeddedIR = true; + std::string DataLayout; +}; + +[[nodiscard]] Expected<std::string> printMIRDocument(const MIRDocument &Document, + const TargetDescription &Target, + MIRDocumentPrinterOptions Options = {}); + } // namespace mir } // namespace rcc -#endif // RCC_MIR_MIPRINTER_H +#endif // RCC_CODEGEN_MIRPRINTER_H diff --git a/backend/include/rcc/CodeGen/MIRYamlMapping.h b/backend/include/rcc/CodeGen/MIRYamlMapping.h new file mode 100644 index 0000000..82880ca --- /dev/null +++ b/backend/include/rcc/CodeGen/MIRYamlMapping.h @@ -0,0 +1,144 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RCC_CODEGEN_MIRYAMLMAPPING_H +#define RCC_CODEGEN_MIRYAMLMAPPING_H + +#include "rcc/CodeGen/MIRParser/MIRParser.h" +#include "rcc/CodeGen/MachineDebugInfo.h" +#include "rcc/CodeGen/MachineFrameInfo.h" +#include "rcc/CodeGen/MachineJumpTableInfo.h" +#include "rcc/CodeGen/MachineMetadata.h" +#include "rcc/CodeGen/TargetMIRTextInfo.h" +#include "rcc/MIR/LLVMConstantSyntax.h" +#include "rcc/Support/Error.h" + +#include <cstdint> +#include <optional> +#include <span> +#include <string> +#include <string_view> +#include <utility> +#include <vector> + +namespace rcc { + +class MachineBasicBlock; +class MachineFunction; +class MachineInstr; + +namespace mir { + +struct PendingJumpTable final { + JumpTableIndex Index; + std::uint32_t TextID = 0; + JumpTableEntryEncoding Encoding = JumpTableEntryEncoding::BlockAddress; + std::uint8_t EntrySize = 8; + std::vector<std::string> Blocks; +}; + +class MIRDocumentParser final { +public: + MIRDocumentParser(const TargetDescription &Target, std::string SourceName, std::string SourceText, + MIRDocumentParserOptions Options) + : Target(Target), Options(std::move(Options)), + Result(std::move(SourceName), std::move(SourceText)) {} + + [[nodiscard]] MIRDocumentParseResult run(); + +private: + [[nodiscard]] bool fail(const TextRange &Range, std::string Message, + std::string Category = "mir-yaml"); + [[nodiscard]] bool importError(const TextRange &Range, const Error &Failure, + std::string Category = "mir-yaml"); + [[nodiscard]] const yaml::Node *node(yaml::NodeID ID) const noexcept { + return Stream->node(ID); + } + [[nodiscard]] const yaml::Node *mappingValue(const yaml::Node &Mapping, + std::string_view Key) const noexcept; + [[nodiscard]] bool requireKind(const yaml::Node &Node, yaml::NodeKind Kind, + std::string_view Description); + [[nodiscard]] bool validateKeys(const yaml::Node &Mapping, std::span<const std::string_view> Keys, + std::string_view Description); + [[nodiscard]] bool scalar(const yaml::Node &Node, std::string_view &Value, + std::string_view Description); + [[nodiscard]] bool boolean(const yaml::Node &Node, bool &Value); + [[nodiscard]] bool unsignedValue(const yaml::Node &Node, std::uint64_t &Value, + std::string_view Description); + [[nodiscard]] bool signedValue(const yaml::Node &Node, std::int64_t &Value, + std::string_view Description); + [[nodiscard]] bool alignment(const yaml::Node &Node, Alignment &Value, bool AllowZero); + [[nodiscard]] bool physicalRegister(const yaml::Node &Node, PhysicalRegister &Reg); + [[nodiscard]] bool physicalRegister(std::string_view Text, const TextRange &Range, + PhysicalRegister &Reg); + [[nodiscard]] bool virtualRegister(std::string_view Text, const TextRange &Range, + MachineFunction &Function, MIParserContext &Context, + Register &Reg); + [[nodiscard]] Expected<MachineMetadataReference> + resolveMetadataReference(const MachineFunction &Function, std::uint32_t Slot) const; + [[nodiscard]] bool parseVariableDebugInfo(const yaml::Node &Entry, MachineFunction &Function, + MachineVariableDebugInfo &DebugInfo); + [[nodiscard]] bool parseFunction(const yaml::Node &Root, MIRDocument &Document); + [[nodiscard]] bool parseFunctionScalars(const yaml::Node &Root, MachineFunction &Function, + MIRFunctionDocumentInfo &DocumentInfo); + [[nodiscard]] bool recomputeProperties(const yaml::Node &Root, MachineFunction &Function, + const MIRFunctionDocumentInfo &DocumentInfo); + [[nodiscard]] bool parseRegisters(const yaml::Node &Root, MachineFunction &Function, + MIParserContext &Context); + [[nodiscard]] bool parseMetadata(const yaml::Node &Root, MachineFunction &Function, + MIParserContext &Context); + [[nodiscard]] bool parseStackObjects(const yaml::Node &Root, MachineFunction &Function, + MIParserContext &Context); + [[nodiscard]] bool parseConstants(const yaml::Node &Root, MachineFunction &Function, + MIParserContext &Context); + [[nodiscard]] bool parseFrameState(const yaml::Node &Root, MachineFunction &Function, + const MIParserContext &Context); + [[nodiscard]] bool reserveJumpTables(const yaml::Node &Root, MachineFunction &Function, + MIParserContext &Context, + std::vector<PendingJumpTable> &Pending); + [[nodiscard]] bool defineJumpTables(MachineFunction &Function, + std::span<const PendingJumpTable> Pending); + [[nodiscard]] bool parseBody(const yaml::Node &Root, MachineFunction &Function, + MIParserContext &Context); + [[nodiscard]] TextRange scalarDiagnosticRange(const yaml::Node &Scalar, + const TextRange &ScalarRange) const noexcept; + [[nodiscard]] bool parseFunctionRecords(const yaml::Node &Root, MachineFunction &Function); + [[nodiscard]] bool parseTargetState(const yaml::Node &Root, MachineFunction &Function); + [[nodiscard]] bool convertTargetNode(const yaml::Node &Source, MIRTargetNode &Destination); + [[nodiscard]] bool validateDocumentReferences(const MachineFunction &Function); + [[nodiscard]] std::string_view effectiveDataLayout() const noexcept; + [[nodiscard]] LLVMConstantSyntaxOptions pointerLayoutOptions() const noexcept; + + [[nodiscard]] MachineBasicBlock *blockAt(MachineFunction &Function, + std::uint32_t Number) const noexcept; + [[nodiscard]] MachineInstr *instructionAt(MachineFunction &Function, std::uint32_t Block, + std::uint32_t Offset) const noexcept; + [[nodiscard]] bool parseBlockReference(std::string_view Text, const TextRange &Range, + MachineFunction &Function, MachineBasicBlock *&Block); + [[nodiscard]] bool parseFrameReference(const yaml::Node &Node, const MIParserContext &Context, + FrameIndex &Index); + [[nodiscard]] bool parseSaveRestorePoints(const yaml::Node &Node, MachineFunction &Function, + std::vector<MachineFrameSaveRestorePoint> &Points); + + const TargetDescription &Target; + MIRDocumentParserOptions Options; + MIRDocumentParseResult Result; + const yaml::Stream *Stream = nullptr; + MIRDocument *ActiveDocument = nullptr; + MachineFunction *ActiveFunction = nullptr; +}; + +[[nodiscard]] Expected<void> verifyMIRDocumentReferences(const MIRDocument &Document, + const MachineFunction &Function, + bool UseEmbeddedIR); + +[[nodiscard]] std::optional<std::uint64_t> +parseDIExpressionOpcodeName(std::string_view Name) noexcept; +[[nodiscard]] std::optional<std::uint64_t> +parseDIExpressionAttributeEncodingName(std::string_view Name) noexcept; +[[nodiscard]] std::string invalidDIExpressionElementMessage(std::string_view Name); +[[nodiscard]] std::string formatDIExpression(std::span<const std::uint64_t> Elements); + +} // namespace mir +} // namespace rcc + +#endif // RCC_CODEGEN_MIRYAMLMAPPING_H diff --git a/backend/include/rcc/MIR/MIDebugSyntax.h b/backend/include/rcc/MIR/MIDebugSyntax.h deleted file mode 100644 index 4952128..0000000 --- a/backend/include/rcc/MIR/MIDebugSyntax.h +++ /dev/null @@ -1,26 +0,0 @@ -// SPDX-License-Identifier: GPL-3.0-or-later - -#ifndef RCC_MIR_MIDEBUGSYNTAX_H -#define RCC_MIR_MIDEBUGSYNTAX_H - -#include <cstdint> -#include <optional> -#include <span> -#include <string> -#include <string_view> - -namespace rcc::mir { - -[[nodiscard]] std::optional<std::uint64_t> -parseDIExpressionOpcodeName(std::string_view Name) noexcept; - -[[nodiscard]] std::optional<std::uint64_t> -parseDIExpressionAttributeEncodingName(std::string_view Name) noexcept; - -[[nodiscard]] std::string invalidDIExpressionElementMessage(std::string_view Name); - -[[nodiscard]] std::string formatDIExpression(std::span<const std::uint64_t> Elements); - -} // namespace rcc::mir - -#endif // RCC_MIR_MIDEBUGSYNTAX_H diff --git a/backend/include/rcc/MIR/MILexer.h b/backend/include/rcc/MIR/MILexer.h deleted file mode 100644 index f54f255..0000000 --- a/backend/include/rcc/MIR/MILexer.h +++ /dev/null @@ -1,181 +0,0 @@ -// SPDX-License-Identifier: GPL-3.0-or-later - -#ifndef RCC_MIR_MILEXER_H -#define RCC_MIR_MILEXER_H - -#include "rcc/Support/TextDiagnostic.h" - -#include <cstddef> -#include <cstdint> -#include <memory> -#include <span> -#include <string> -#include <string_view> -#include <utility> -#include <variant> - -namespace rcc::mir { - -enum class MITokenKind : std::uint8_t { - EndOfFile, - Error, - Newline, - - Comma, - Equal, - Underscore, - Colon, - DoubleColon, - Dot, - Exclaim, - LeftParen, - RightParen, - LeftBrace, - RightBrace, - Plus, - Minus, - Less, - Greater, - - Keyword, - MetadataKeyword, - Identifier, - NamedRegister, - NamedVirtualRegister, - MachineBasicBlockLabel, - MachineBasicBlock, - StackObject, - FixedStackObject, - NamedGlobalValue, - GlobalValue, - ExternalSymbol, - MCSymbol, - IntegerLiteral, - FloatingPointLiteral, - HexLiteral, - VirtualRegister, - ConstantPoolItem, - JumpTableIndex, - NamedIRBlock, - IRBlock, - NamedIRValue, - IRValue, - QuotedIRValue, - SubRegisterIndex, - StringConstant, -}; - -[[nodiscard]] std::string_view getMITokenKindName(MITokenKind Kind) noexcept; -[[nodiscard]] bool isMIKeyword(std::string_view Spelling) noexcept; -[[nodiscard]] bool isMIMetadataKeyword(std::string_view Spelling) noexcept; - -class MIToken final { -public: - [[nodiscard]] MITokenKind kind() const noexcept { - return Kind; - } - - [[nodiscard]] const TextRange &range() const noexcept { - return Range; - } - - [[nodiscard]] std::string_view spelling() const noexcept { - return Spelling; - } - - [[nodiscard]] bool hasStringValue() const noexcept { - return !std::holds_alternative<std::monostate>(StringValue); - } - - [[nodiscard]] std::string_view stringValue() const noexcept; - - [[nodiscard]] bool hasIntegerValue() const noexcept { - return !IntegerValue.empty(); - } - - [[nodiscard]] std::string_view integerValue() const noexcept { - return IntegerValue; - } - - [[nodiscard]] bool is(MITokenKind Expected) const noexcept { - return Kind == Expected; - } - - [[nodiscard]] bool isKeyword(std::string_view Expected) const noexcept { - return Kind == MITokenKind::Keyword && stringValue() == Expected; - } - - [[nodiscard]] bool isMetadataKeyword(std::string_view Expected) const noexcept { - return Kind == MITokenKind::MetadataKeyword && stringValue() == Expected; - } - - [[nodiscard]] bool isNewlineOrEnd() const noexcept { - return Kind == MITokenKind::Newline || Kind == MITokenKind::EndOfFile; - } - - [[nodiscard]] bool isErrorOrEnd() const noexcept { - return Kind == MITokenKind::Error || Kind == MITokenKind::EndOfFile; - } - - [[nodiscard]] bool isRegister() const noexcept; - [[nodiscard]] bool isRegisterFlag() const noexcept; - [[nodiscard]] bool isMemoryOperandFlag() const noexcept; - -private: - using StringValueStorage = std::variant<std::monostate, std::string_view, std::string>; - - MIToken(MITokenKind Kind, TextRange Range, std::string_view Spelling, - StringValueStorage StringValue = {}, std::string_view IntegerValue = {}) - : Kind(Kind), Range(Range), Spelling(Spelling), StringValue(std::move(StringValue)), - IntegerValue(IntegerValue) {} - - MITokenKind Kind = MITokenKind::Error; - TextRange Range; - std::string_view Spelling; - StringValueStorage StringValue; - std::string_view IntegerValue; - - friend class MILexer; -}; - -struct MILexerOptions final { - std::size_t MaximumTokenLength = std::size_t{1} << 24U; - std::size_t MaximumTokens = std::size_t{1} << 24U; -}; - -class MILexer final { -public: - MILexer(std::string SourceName, std::string SourceText, MILexerOptions Options = {}); - MILexer(const MILexer &) = delete; - MILexer &operator=(const MILexer &) = delete; - MILexer(MILexer &&) noexcept; - MILexer &operator=(MILexer &&) noexcept; - ~MILexer(); - - [[nodiscard]] MIToken next(); - - [[nodiscard]] const TextSource &source() const noexcept; - [[nodiscard]] std::span<const TextDiagnostic> diagnostics() const noexcept; - [[nodiscard]] bool failed() const noexcept; - [[nodiscard]] std::size_t tokenCount() const noexcept; - -private: - [[nodiscard]] static MIToken makeToken(MITokenKind Kind, TextRange Range, - std::string_view Spelling, - std::string_view IntegerValue = {}); - [[nodiscard]] static MIToken makeStringToken(MITokenKind Kind, TextRange Range, - std::string_view Spelling, - std::string_view StringValue, - std::string_view IntegerValue = {}); - [[nodiscard]] static MIToken makeOwnedStringToken(MITokenKind Kind, TextRange Range, - std::string_view Spelling, - std::string StringValue, - std::string_view IntegerValue = {}); - - class Impl; - std::unique_ptr<Impl> Implementation; -}; - -} // namespace rcc::mir - -#endif // RCC_MIR_MILEXER_H diff --git a/backend/include/rcc/Tooling/ArtifactStore.h b/backend/include/rcc/Tooling/ArtifactStore.h index a30f572..f11cd6c 100644 --- a/backend/include/rcc/Tooling/ArtifactStore.h +++ b/backend/include/rcc/Tooling/ArtifactStore.h @@ -3,7 +3,7 @@ #ifndef RCC_TOOLING_ARTIFACTSTORE_H #define RCC_TOOLING_ARTIFACTSTORE_H -#include "rcc/MIR/MIRDocument.h" +#include "rcc/CodeGen/MIRParser/MIRParser.h" #include "rcc/Support/Error.h" #include "rcc/Tooling/BackendManifest.h" diff --git a/backend/include/rcc/Tooling/PipelineDriver.h b/backend/include/rcc/Tooling/PipelineDriver.h index 849b2f7..5fa4fba 100644 --- a/backend/include/rcc/Tooling/PipelineDriver.h +++ b/backend/include/rcc/Tooling/PipelineDriver.h @@ -3,8 +3,8 @@ #ifndef RCC_TOOLING_PIPELINEDRIVER_H #define RCC_TOOLING_PIPELINEDRIVER_H +#include "rcc/CodeGen/MIRParser/MIRParser.h" #include "rcc/CodeGen/MachinePassManager.h" -#include "rcc/MIR/MIRDocument.h" #include "rcc/Tooling/BackendInvocation.h" #include <cstdint> |
