From 379ff50dcc7ccf977379aee68fa9d10f69065d23 Mon Sep 17 00:00:00 2001 From: Richard-Qin-X Date: Sun, 9 Aug 2026 18:31:49 +0800 Subject: [MC] Implement RV64GC assembly emission --- backend/lib/MC/MCSection.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 backend/lib/MC/MCSection.cpp (limited to 'backend/lib/MC/MCSection.cpp') diff --git a/backend/lib/MC/MCSection.cpp b/backend/lib/MC/MCSection.cpp new file mode 100644 index 0000000..21ba410 --- /dev/null +++ b/backend/lib/MC/MCSection.cpp @@ -0,0 +1,60 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "rcc/MC/MCSection.h" + +#include +#include + +namespace rcc::mc { +namespace { + +[[nodiscard]] bool isValidAssemblyName(std::string_view Name) noexcept { + if (Name.empty()) + return false; + for (const char RawCharacter : Name) { + const auto Character = static_cast(RawCharacter); + if (Character == 0 || Character == '\n' || Character == '\r' || Character == 0x7fU) + return false; + } + return true; +} + +[[nodiscard]] bool isPowerOfTwo(std::uint64_t Value) noexcept { + return Value != 0 && (Value & (Value - 1U)) == 0; +} + +} // namespace + +Expected MCSectionDesc::verify() const { + if (!isValidAssemblyName(Name)) + return makeUnexpected(ErrorCode::InvalidArgument, "MC section has an invalid name"); + if (!isPowerOfTwo(Alignment)) { + return makeUnexpected(ErrorCode::InvalidArgument, + "MC section alignment must be a nonzero power of two"); + } + if (Flags.has(MCSectionFlag::Strings) && !Flags.has(MCSectionFlag::Merge)) { + return makeUnexpected(ErrorCode::InvalidArgument, "string MC sections must also be mergeable"); + } + if (Flags.has(MCSectionFlag::Merge) && EntrySize == 0) { + return makeUnexpected(ErrorCode::InvalidArgument, + "mergeable MC sections require a nonzero entry size"); + } + if ((Kind == MCSectionKind::TLSData || Kind == MCSectionKind::TLSBSS) && + !Flags.has(MCSectionFlag::TLS)) { + return makeUnexpected(ErrorCode::InvalidArgument, + "TLS MC sections must carry the TLS section flag"); + } + if (Flags.has(MCSectionFlag::Group) != static_cast(Group)) { + return makeUnexpected(ErrorCode::InvalidArgument, + "MC section group flag and group symbol must agree"); + } + if (Group && Group->name().empty()) { + return makeUnexpected(ErrorCode::InvalidArgument, "MC section group symbol must have a name"); + } + if (UniqueID && *UniqueID == 0) { + return makeUnexpected(ErrorCode::InvalidArgument, "unique MC section identity must be nonzero"); + } + return {}; +} + +} // namespace rcc::mc -- cgit v1.2.3