diff options
| author | Richard-Qin-X <richard.qin.001@gmail.com> | 2026-08-09 18:31:49 +0800 |
|---|---|---|
| committer | Richard-Qin-X <richard.qin.001@gmail.com> | 2026-08-09 18:31:49 +0800 |
| commit | 379ff50dcc7ccf977379aee68fa9d10f69065d23 (patch) | |
| tree | e0fac3952d46d0bbcaea32bcfc6087c95fff2992 /backend/lib/MC/MCSection.cpp | |
| parent | 495c675771f43cfa12ab7a426a538be72fec1805 (diff) | |
| download | rcc-project-379ff50dcc7ccf977379aee68fa9d10f69065d23.tar.gz rcc-project-379ff50dcc7ccf977379aee68fa9d10f69065d23.tar.xz | |
[MC] Implement RV64GC assembly emission
Diffstat (limited to 'backend/lib/MC/MCSection.cpp')
| -rw-r--r-- | backend/lib/MC/MCSection.cpp | 60 |
1 files changed, 60 insertions, 0 deletions
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 <cstdint> +#include <string_view> + +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<unsigned char>(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<void> 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<bool>(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 |
