From cfdbcc469f9691bd9dce3b0b86982a63d86ca6b7 Mon Sep 17 00:00:00 2001 From: Richard-Qin-X Date: Sat, 1 Aug 2026 14:50:04 +0800 Subject: [CodeGen] Add target state and compilation status --- backend/lib/CodeGen/TargetFunctionState.cpp | 82 +++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 backend/lib/CodeGen/TargetFunctionState.cpp (limited to 'backend/lib/CodeGen/TargetFunctionState.cpp') diff --git a/backend/lib/CodeGen/TargetFunctionState.cpp b/backend/lib/CodeGen/TargetFunctionState.cpp new file mode 100644 index 0000000..56ff363 --- /dev/null +++ b/backend/lib/CodeGen/TargetFunctionState.cpp @@ -0,0 +1,82 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "rcc/CodeGen/TargetFunctionState.h" + +#include +#include +#include +#include +#include + +namespace rcc { + +TargetFunctionState::TargetFunctionState(TargetFunctionState &&) noexcept = default; +TargetFunctionState &TargetFunctionState::operator=(TargetFunctionState &&) noexcept = default; +TargetFunctionState::~TargetFunctionState() = default; + +std::optional TargetFunctionState::typeID() const noexcept { + if (Storage == nullptr) + return std::nullopt; + return Storage->typeID(); +} + +std::optional TargetFunctionState::schemaVersion() const noexcept { + if (Storage == nullptr) + return std::nullopt; + return Storage->schemaVersion(); +} + +Expected TargetFunctionState::verify() const { + if (Storage == nullptr) + return makeUnexpected(ErrorCode::VerificationError, "target function state is empty"); + if (Storage->typeID().value() == 0 || Storage->schemaVersion() == 0) { + return makeUnexpected(ErrorCode::VerificationError, + "target function state identity or schema version is invalid"); + } + return Storage->verify(); +} + +Expected +TargetFunctionState::clone(const MachineFunctionCloneContext &CloneContext) const { + auto Verification = verify(); + if (!Verification) + return std::unexpected(Verification.error()); + auto ClonedStorage = Storage->cloneStorage(CloneContext); + if (!ClonedStorage) + return std::unexpected(ClonedStorage.error()); + TargetFunctionState Result(std::move(*ClonedStorage)); + auto CloneVerification = Result.verify(); + if (!CloneVerification) + return std::unexpected(CloneVerification.error()); + return Result; +} + +Expected TargetFunctionState::print() const { + auto Verification = verify(); + if (!Verification) + return std::unexpected(Verification.error()); + return Storage->print(); +} + +Expected TargetFunctionState::encode() const { + auto Verification = verify(); + if (!Verification) + return std::unexpected(Verification.error()); + auto Payload = Storage->encodePayload(); + if (!Payload) + return std::unexpected(Payload.error()); + return EncodedTargetFunctionState{Storage->typeID(), Storage->schemaVersion(), + std::move(*Payload)}; +} + +bool TargetFunctionState::equals(const TargetFunctionState &Other) const noexcept { + if (Storage == nullptr || Other.Storage == nullptr) + return Storage == nullptr && Other.Storage == nullptr; + if (Storage->typeID() != Other.Storage->typeID() || + Storage->localTypeToken() != Other.Storage->localTypeToken()) { + return false; + } + return Storage->equals(*Other.Storage); +} + +} // namespace rcc -- cgit v1.2.3