From 183e2354e9fdd107be420cbb185c1d239434aa48 Mon Sep 17 00:00:00 2001 From: Richard-Qin-X Date: Tue, 4 Aug 2026 05:34:15 +0800 Subject: [MIR] Add machine instruction parser and printer Parse MachineFunction bodies in two passes and serialize the same MachineIR model in complete or readable form. Extend instruction attachments and memory operands required by the fixed LLVM MIR grammar, and cover deterministic parse-print round trips. --- backend/include/rcc/CodeGen/MachineInstr.h | 59 +++++++++++++++ backend/include/rcc/CodeGen/MachineMemOperand.h | 23 ++++++ backend/include/rcc/MIR/MIParser.h | 95 +++++++++++++++++++++++++ backend/include/rcc/MIR/MIPrinter.h | 35 +++++++++ 4 files changed, 212 insertions(+) create mode 100644 backend/include/rcc/MIR/MIParser.h create mode 100644 backend/include/rcc/MIR/MIPrinter.h (limited to 'backend/include') diff --git a/backend/include/rcc/CodeGen/MachineInstr.h b/backend/include/rcc/CodeGen/MachineInstr.h index 69d7dd7..e7294b4 100644 --- a/backend/include/rcc/CodeGen/MachineInstr.h +++ b/backend/include/rcc/CodeGen/MachineInstr.h @@ -13,6 +13,8 @@ #include #include #include +#include +#include #include #include @@ -154,6 +156,56 @@ public: DebugInstrNumber = 0; } + [[nodiscard]] std::optional preInstrSymbol() const noexcept; + [[nodiscard]] Expected setPreInstrSymbol(std::string Symbol); + void clearPreInstrSymbol() noexcept { + PreInstrSymbol.reset(); + } + + [[nodiscard]] std::optional postInstrSymbol() const noexcept; + [[nodiscard]] Expected setPostInstrSymbol(std::string Symbol); + void clearPostInstrSymbol() noexcept { + PostInstrSymbol.reset(); + } + + [[nodiscard]] const std::optional &heapAllocMarker() const noexcept { + return HeapAllocMarker; + } + [[nodiscard]] Expected setHeapAllocMarker(MachineMetadataReference Marker); + void clearHeapAllocMarker() noexcept { + HeapAllocMarker.reset(); + } + + [[nodiscard]] const std::optional &pcSections() const noexcept { + return PCSections; + } + [[nodiscard]] Expected setPCSections(MachineMetadataReference Sections); + void clearPCSections() noexcept { + PCSections.reset(); + } + + [[nodiscard]] const std::optional &mmra() const noexcept { + return MMRA; + } + [[nodiscard]] Expected setMMRA(MachineMetadataReference Metadata); + void clearMMRA() noexcept { + MMRA.reset(); + } + + [[nodiscard]] std::optional cfiType() const noexcept { + return CFIType; + } + [[nodiscard]] Expected setCFIType(std::uint64_t Type); + void clearCFIType() noexcept { + CFIType.reset(); + } + + [[nodiscard]] std::optional deactivationSymbol() const noexcept; + [[nodiscard]] Expected setDeactivationSymbol(std::string Symbol); + void clearDeactivationSymbol() noexcept { + DeactivationSymbol.reset(); + } + private: MachineInstr(MachineFunction &OwnerFunction, Opcode InstructionOpcode, MachineInstrStableID StableID); @@ -177,6 +229,13 @@ private: std::optional DebugLocation; std::optional Origin; std::uint32_t DebugInstrNumber = 0; + std::optional PreInstrSymbol; + std::optional PostInstrSymbol; + std::optional HeapAllocMarker; + std::optional PCSections; + std::optional MMRA; + std::optional CFIType; + std::optional DeactivationSymbol; friend class MachineArena; friend class MachineBasicBlock; diff --git a/backend/include/rcc/CodeGen/MachineMemOperand.h b/backend/include/rcc/CodeGen/MachineMemOperand.h index b6cb07f..a9e19a6 100644 --- a/backend/include/rcc/CodeGen/MachineMemOperand.h +++ b/backend/include/rcc/CodeGen/MachineMemOperand.h @@ -5,6 +5,7 @@ #include "rcc/CodeGen/LowLevelType.h" #include "rcc/CodeGen/MachineIRTypes.h" +#include "rcc/CodeGen/MachineMetadata.h" #include #include @@ -138,6 +139,14 @@ public: JumpTable, GlobalSymbol, ExternalSymbol, + IRValue, + Stack, + GOT, + ConstantPoolPseudo, + JumpTablePseudo, + CallEntryGlobal, + CallEntryExternal, + Custom, }; MemoryBase() = default; @@ -147,6 +156,14 @@ public: [[nodiscard]] static MemoryBase jumpTable(JumpTableIndex Index); [[nodiscard]] static MemoryBase globalSymbol(std::string Name); [[nodiscard]] static MemoryBase externalSymbol(std::string Name); + [[nodiscard]] static MemoryBase irValue(std::string Name); + [[nodiscard]] static MemoryBase stack(); + [[nodiscard]] static MemoryBase got(); + [[nodiscard]] static MemoryBase constantPoolPseudo(); + [[nodiscard]] static MemoryBase jumpTablePseudo(); + [[nodiscard]] static MemoryBase callEntryGlobal(std::string Name); + [[nodiscard]] static MemoryBase callEntryExternal(std::string Name); + [[nodiscard]] static MemoryBase custom(std::string Name); [[nodiscard]] Kind kind() const noexcept { return BaseKind; @@ -177,11 +194,17 @@ struct MachineMemOperandDesc final { SyncScopeID SyncScope; MemorySize Size; Alignment Align; + std::optional BaseAlign; std::uint32_t AddressSpace = 0; MemoryBase Base; std::int64_t Offset = 0; std::optional OffsetRange; std::optional AliasSource; + std::optional TBAA; + std::optional AliasScope; + std::optional NoAlias; + std::optional NoAliasAddressSpace; + std::optional RangeMetadata; std::uint16_t TargetFlags = 0; friend bool operator==(const MachineMemOperandDesc &Left, diff --git a/backend/include/rcc/MIR/MIParser.h b/backend/include/rcc/MIR/MIParser.h new file mode 100644 index 0000000..978e928 --- /dev/null +++ b/backend/include/rcc/MIR/MIParser.h @@ -0,0 +1,95 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RCC_MIR_MIPARSER_H +#define RCC_MIR_MIPARSER_H + +#include "rcc/CodeGen/MachineIRTypes.h" +#include "rcc/CodeGen/MachineMetadata.h" +#include "rcc/Support/Error.h" +#include "rcc/Support/TextDiagnostic.h" + +#include +#include +#include +#include +#include +#include +#include + +namespace rcc { + +class MachineFunction; +class TargetDescription; + +namespace mir { + +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 StackObjects; + std::vector FixedStackObjects; + std::vector ConstantPoolItems; + std::vector JumpTables; + std::vector Metadata; + std::vector SyncScopes; + std::uint32_t DefaultPointerSizeInBits = 0; +}; + +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; +}; + +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_MIR_MIPARSER_H diff --git a/backend/include/rcc/MIR/MIPrinter.h b/backend/include/rcc/MIR/MIPrinter.h new file mode 100644 index 0000000..663859e --- /dev/null +++ b/backend/include/rcc/MIR/MIPrinter.h @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RCC_MIR_MIPRINTER_H +#define RCC_MIR_MIPRINTER_H + +#include "rcc/MIR/MIParser.h" +#include "rcc/Support/Error.h" + +#include + +namespace rcc { + +class MachineFunction; +class TargetDescription; + +namespace mir { + +enum class MIPrintMode : unsigned char { + Complete, + Readable, +}; + +struct MIPrinterOptions final { + MIPrintMode Mode = MIPrintMode::Complete; +}; + +[[nodiscard]] Expected printMachineFunctionBody(const MachineFunction &Function, + const TargetDescription &Target, + const MIParserContext &Context = {}, + MIPrinterOptions Options = {}); + +} // namespace mir +} // namespace rcc + +#endif // RCC_MIR_MIPRINTER_H -- cgit v1.2.3