From e16de95dce32f077361577377d5c52fc4ad78339 Mon Sep 17 00:00:00 2001 From: Richard-Qin-X Date: Fri, 7 Aug 2026 02:15:09 +0800 Subject: [RISCV] Add generated RV64GC register description Add an RCC-owned build-time target description generator and the initial RV64GC register model. Keep ABI aliases separate from canonical MIR register spelling. --- backend/tools/DescriptionGenerator/CMakeLists.txt | 39 +++++++++ .../DescriptionGeneratorMain.cpp | 97 ++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 backend/tools/DescriptionGenerator/CMakeLists.txt create mode 100644 backend/tools/DescriptionGenerator/DescriptionGeneratorMain.cpp (limited to 'backend/tools/DescriptionGenerator') diff --git a/backend/tools/DescriptionGenerator/CMakeLists.txt b/backend/tools/DescriptionGenerator/CMakeLists.txt new file mode 100644 index 0000000..9bc3f26 --- /dev/null +++ b/backend/tools/DescriptionGenerator/CMakeLists.txt @@ -0,0 +1,39 @@ +# SPDX-License-Identifier: GPL-3.0-or-later + +set( + RCCBackendDescriptionGeneratorCoreSources + "${RCC_BACKEND_SOURCE_DIR}/lib/Target/Description/TargetDescriptionGenerator.cpp" +) +set( + RCCBackendDescriptionGeneratorCoreHeaders + "${RCC_BACKEND_SOURCE_DIR}/lib/Target/Description/TargetDescriptionGenerator.h" +) + +add_library( + RCCBackendDescriptionGeneratorCore + STATIC + ${RCCBackendDescriptionGeneratorCoreSources} +) + +rcc_backend_configure_target(RCCBackendDescriptionGeneratorCore) + +target_include_directories( + RCCBackendDescriptionGeneratorCore + PUBLIC "$" +) +target_link_libraries(RCCBackendDescriptionGeneratorCore PUBLIC RCCBackend::Support) + +add_executable(RCCBackendDescriptionGenerator DescriptionGeneratorMain.cpp) +rcc_backend_configure_target(RCCBackendDescriptionGenerator) +target_link_libraries(RCCBackendDescriptionGenerator PRIVATE RCCBackendDescriptionGeneratorCore) +set_target_properties(RCCBackendDescriptionGenerator PROPERTIES OUTPUT_NAME rcc-backend-descgen) + +rcc_backend_register_format_files( + "${CMAKE_CURRENT_SOURCE_DIR}/DescriptionGeneratorMain.cpp" + ${RCCBackendDescriptionGeneratorCoreSources} + ${RCCBackendDescriptionGeneratorCoreHeaders} +) +rcc_backend_register_tidy_files( + "${CMAKE_CURRENT_SOURCE_DIR}/DescriptionGeneratorMain.cpp" + ${RCCBackendDescriptionGeneratorCoreSources} +) diff --git a/backend/tools/DescriptionGenerator/DescriptionGeneratorMain.cpp b/backend/tools/DescriptionGenerator/DescriptionGeneratorMain.cpp new file mode 100644 index 0000000..11890f6 --- /dev/null +++ b/backend/tools/DescriptionGenerator/DescriptionGeneratorMain.cpp @@ -0,0 +1,97 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "TargetDescriptionGenerator.h" +#include "rcc/Support/TextDiagnostic.h" + +#include +#include +#include +#include +#include +#include + +namespace { + +struct CommandLine final { + std::string InputPath; + std::string HeaderPath; + std::string TablesPath; +}; + +[[nodiscard]] bool parseCommandLine(int ArgumentCount, char *Arguments[], CommandLine &Result) { + for (int Index = 1; Index < ArgumentCount; ++Index) { + const std::string_view Argument(Arguments[Index]); + if (Argument == "--input" || Argument == "--header" || Argument == "--tables") { + if (Index + 1 >= ArgumentCount) { + std::cerr << "rcc-backend-descgen: missing value for " << Argument << '\n'; + return false; + } + std::string *Destination = nullptr; + if (Argument == "--input") + Destination = &Result.InputPath; + else if (Argument == "--header") + Destination = &Result.HeaderPath; + else + Destination = &Result.TablesPath; + *Destination = Arguments[++Index]; + continue; + } + std::cerr << "rcc-backend-descgen: unknown argument '" << Argument << "'\n"; + return false; + } + if (!Result.InputPath.empty() && !Result.HeaderPath.empty() && !Result.TablesPath.empty()) + return true; + std::cerr << "usage: rcc-backend-descgen --input --header --tables \n"; + return false; +} + +[[nodiscard]] bool writeFile(const std::filesystem::path &Path, std::string_view Contents) { + std::error_code Error; + std::filesystem::create_directories(Path.parent_path(), Error); + if (Error) { + std::cerr << "rcc-backend-descgen: cannot create output directory for '" << Path.string() + << "': " << Error.message() << '\n'; + return false; + } + std::ofstream Output(Path, std::ios::binary | std::ios::trunc); + if (!Output) { + std::cerr << "rcc-backend-descgen: cannot open output '" << Path.string() << "'\n"; + return false; + } + Output.write(Contents.data(), static_cast(Contents.size())); + if (Output) { + Output.close(); + return true; + } + std::cerr << "rcc-backend-descgen: cannot write output '" << Path.string() << "'\n"; + return false; +} + +} // namespace + +int main(int ArgumentCount, char *Arguments[]) { + CommandLine Command; + if (!parseCommandLine(ArgumentCount, Arguments, Command)) + return 1; + + std::ifstream Input(Command.InputPath, std::ios::binary); + if (!Input) { + std::cerr << "rcc-backend-descgen: cannot open input '" << Command.InputPath << "'\n"; + return 1; + } + const std::string SourceText{std::istreambuf_iterator(Input), + std::istreambuf_iterator()}; + const rcc::targetgen::DescriptionGenerationResult Result = + rcc::targetgen::generateDescription(Command.InputPath, SourceText); + if (!Result.succeeded()) { + const rcc::TextSource Source(Command.InputPath, SourceText); + for (const rcc::TextDiagnostic &Diagnostic : Result.Diagnostics) + std::cerr << rcc::formatTextDiagnostic(Diagnostic, Source) << '\n'; + return 1; + } + + return writeFile(Command.HeaderPath, Result.Output->RegisterHeader) && + writeFile(Command.TablesPath, Result.Output->TargetTables) + ? 0 + : 1; +} -- cgit v1.2.3