From c759cebef15bdac2ef160065efd116fe6ed540a7 Mon Sep 17 00:00:00 2001 From: Richard-Qin-X Date: Sat, 1 Aug 2026 05:27:13 +0800 Subject: [CodeGen] Build the core MachineIR object model Add arena-owned, address-stable MachineFunction, MachineBasicBlock, and MachineInstr nodes with intrusive layout and instruction lists. Model every required MachineOperand category, reciprocal ties, source state, and immutable per-instruction memory references. Implement physical and virtual register def-use chains whose back-links survive operand vector moves, plus vreg properties, tombstones, live-ins, reserved registers, and atomic CFG updates. Exercise ownership, reallocation, unlink/relink, cross-function rejection, and sanitizer paths. --- backend/include/rcc/CodeGen/MachineInstr.h | 164 +++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 backend/include/rcc/CodeGen/MachineInstr.h (limited to 'backend/include/rcc/CodeGen/MachineInstr.h') diff --git a/backend/include/rcc/CodeGen/MachineInstr.h b/backend/include/rcc/CodeGen/MachineInstr.h new file mode 100644 index 0000000..376273f --- /dev/null +++ b/backend/include/rcc/CodeGen/MachineInstr.h @@ -0,0 +1,164 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RCC_CODEGEN_MACHINEINSTR_H +#define RCC_CODEGEN_MACHINEINSTR_H + +#include "rcc/CodeGen/MachineIRTypes.h" +#include "rcc/CodeGen/MachineOperand.h" +#include "rcc/CodeGen/StableID.h" +#include "rcc/Support/Error.h" + +#include +#include +#include +#include +#include +#include + +namespace rcc { + +class MachineBasicBlock; +class MachineFunction; +class MachineMemOperand; +class MachineRegisterInfo; + +class MachineInstr final { +public: + MachineInstr(const MachineInstr &) = delete; + MachineInstr &operator=(const MachineInstr &) = delete; + MachineInstr(MachineInstr &&) = delete; + MachineInstr &operator=(MachineInstr &&) = delete; + ~MachineInstr(); + + [[nodiscard]] Opcode opcode() const noexcept { + return InstructionOpcode; + } + + [[nodiscard]] Expected setOpcode(Opcode NewOpcode); + + [[nodiscard]] MachineInstrStableID stableID() const noexcept { + return StableID; + } + + [[nodiscard]] MachineFunction &function() noexcept { + return *OwnerFunction; + } + + [[nodiscard]] const MachineFunction &function() const noexcept { + return *OwnerFunction; + } + + [[nodiscard]] MachineBasicBlock *parent() noexcept { + return ParentBlock; + } + + [[nodiscard]] const MachineBasicBlock *parent() const noexcept { + return ParentBlock; + } + + [[nodiscard]] MachineInstr *previous() noexcept { + return PreviousInstruction; + } + + [[nodiscard]] const MachineInstr *previous() const noexcept { + return PreviousInstruction; + } + + [[nodiscard]] MachineInstr *next() noexcept { + return NextInstruction; + } + + [[nodiscard]] const MachineInstr *next() const noexcept { + return NextInstruction; + } + + [[nodiscard]] bool isLinked() const noexcept { + return ParentBlock != nullptr; + } + + [[nodiscard]] std::size_t operandCount() const noexcept { + return Operands.size(); + } + + [[nodiscard]] MachineOperand *operandAt(std::size_t Index) noexcept; + [[nodiscard]] const MachineOperand *operandAt(std::size_t Index) const noexcept; + [[nodiscard]] std::span operands() const noexcept { + return Operands; + } + + void reserveOperands(std::size_t Capacity); + [[nodiscard]] Expected addOperand(MachineOperand Operand); + [[nodiscard]] Expected insertOperand(std::size_t Index, MachineOperand Operand); + [[nodiscard]] Expected eraseOperand(std::size_t Index); + + [[nodiscard]] Expected tieOperands(std::size_t DefIndex, std::size_t UseIndex); + [[nodiscard]] Expected untieOperand(std::size_t Index); + + [[nodiscard]] Expected addMemOperand(const MachineMemOperand &MemOperand); + [[nodiscard]] Expected eraseMemOperand(std::size_t Index); + [[nodiscard]] std::span memOperands() const noexcept { + return MemOperands; + } + + [[nodiscard]] MachineInstrFlags flags() const noexcept { + return Flags; + } + + void setFlags(MachineInstrFlags NewFlags) noexcept { + Flags = NewFlags; + } + + [[nodiscard]] const std::optional &sourceLocation() const noexcept { + return Location; + } + + void setSourceLocation(SourceLocation NewLocation) { + Location = std::move(NewLocation); + } + + void clearSourceLocation() noexcept { + Location.reset(); + } + + [[nodiscard]] std::optional origin() const noexcept { + return Origin; + } + + void setOrigin(OriginID NewOrigin) noexcept { + Origin = NewOrigin; + } + + void clearOrigin() noexcept { + Origin.reset(); + } + +private: + MachineInstr(MachineFunction &OwnerFunction, Opcode InstructionOpcode, + MachineInstrStableID StableID); + + [[nodiscard]] MachineRegisterInfo *linkedRegisterInfo() noexcept; + [[nodiscard]] Expected attachToBlock(MachineBasicBlock &Block); + void detachFromBlock() noexcept; + void adjustTiesForInsertion(std::size_t Index) noexcept; + void adjustTiesForErasure(std::size_t Index) noexcept; + + MachineFunction *OwnerFunction; + Opcode InstructionOpcode; + MachineInstrStableID StableID; + MachineBasicBlock *ParentBlock = nullptr; + MachineInstr *PreviousInstruction = nullptr; + MachineInstr *NextInstruction = nullptr; + std::vector Operands; + std::vector MemOperands; + MachineInstrFlags Flags; + std::optional Location; + std::optional Origin; + + friend class MachineArena; + friend class MachineBasicBlock; + friend class MachineFunction; +}; + +} // namespace rcc + +#endif // RCC_CODEGEN_MACHINEINSTR_H -- cgit v1.2.3