From 787f3a7a20958bfa6f231abccc617f4d8de25c4d Mon Sep 17 00:00:00 2001 From: Richard-Qin-X Date: Sat, 1 Aug 2026 17:36:40 +0800 Subject: [CodeGen] Add MIR function and block attributes --- .../rcc/CodeGen/MachineFunctionAttributes.h | 100 +++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 backend/include/rcc/CodeGen/MachineFunctionAttributes.h (limited to 'backend/include/rcc/CodeGen/MachineFunctionAttributes.h') diff --git a/backend/include/rcc/CodeGen/MachineFunctionAttributes.h b/backend/include/rcc/CodeGen/MachineFunctionAttributes.h new file mode 100644 index 0000000..4344640 --- /dev/null +++ b/backend/include/rcc/CodeGen/MachineFunctionAttributes.h @@ -0,0 +1,100 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RCC_CODEGEN_MACHINEFUNCTIONATTRIBUTES_H +#define RCC_CODEGEN_MACHINEFUNCTIONATTRIBUTES_H + +#include "rcc/CodeGen/MachineIRTypes.h" +#include "rcc/Support/Error.h" + +#include +#include +#include + +namespace rcc { + +enum class MachineFunctionAttribute : std::uint8_t { + ExposesReturnsTwice, + HasWinCFI, + HasFakeUses, + CallsEHReturn, + CallsUnwindInit, + HasEHContTarget, + HasEHScopes, + HasEHFunclets, + IsOutlined, + DebugInstrRef, + TracksDebugUserValues, + Count, +}; + +[[nodiscard]] std::string_view +getMachineFunctionAttributeName(MachineFunctionAttribute Attribute) noexcept; + +class MachineFunctionAttributes final { +public: + constexpr MachineFunctionAttributes() noexcept = default; + + [[nodiscard]] static Expected fromRawFlags(std::uint16_t RawFlags); + + [[nodiscard]] constexpr bool has(MachineFunctionAttribute Attribute) const noexcept { + if (!isValidAttribute(Attribute)) + return false; + return (Flags & flagFor(Attribute)) != 0; + } + + constexpr void set(MachineFunctionAttribute Attribute) noexcept { + if (isValidAttribute(Attribute)) + Flags |= flagFor(Attribute); + } + + constexpr void clear(MachineFunctionAttribute Attribute) noexcept { + if (isValidAttribute(Attribute)) + Flags &= static_cast(~flagFor(Attribute)); + } + + [[nodiscard]] constexpr std::uint16_t rawFlags() const noexcept { + return Flags; + } + + [[nodiscard]] std::optional alignment() const noexcept { + return FunctionAlignment; + } + + void setAlignment(Alignment NewAlignment) noexcept { + FunctionAlignment = NewAlignment; + } + + void clearAlignment() noexcept { + FunctionAlignment.reset(); + } + + [[nodiscard]] Expected verify() const; + + friend bool operator==(const MachineFunctionAttributes &Left, + const MachineFunctionAttributes &Right) = default; + +private: + static constexpr std::uint16_t AttributeCount = + static_cast(MachineFunctionAttribute::Count); + static_assert(AttributeCount < 16); + + static constexpr std::uint16_t ValidFlags = + static_cast((std::uint16_t{1} << AttributeCount) - 1); + + [[nodiscard]] static constexpr bool + isValidAttribute(MachineFunctionAttribute Attribute) noexcept { + return static_cast(Attribute) < AttributeCount; + } + + [[nodiscard]] static constexpr std::uint16_t + flagFor(MachineFunctionAttribute Attribute) noexcept { + return static_cast(std::uint16_t{1} << static_cast(Attribute)); + } + + std::uint16_t Flags = 0; + std::optional FunctionAlignment; +}; + +} // namespace rcc + +#endif // RCC_CODEGEN_MACHINEFUNCTIONATTRIBUTES_H -- cgit v1.2.3