diff options
| author | Richard-Qin-X <richard.qin.001@gmail.com> | 2026-08-07 02:15:09 +0800 |
|---|---|---|
| committer | Richard-Qin-X <richard.qin.001@gmail.com> | 2026-08-07 02:15:09 +0800 |
| commit | e16de95dce32f077361577377d5c52fc4ad78339 (patch) | |
| tree | ea563652a95e6e32eb86ce68845a2005d83a85d5 /backend/tools | |
| parent | 76d89700953ddd81cf31ab8f260eddf67e93aa85 (diff) | |
| download | rcc-project-e16de95dce32f077361577377d5c52fc4ad78339.tar.gz rcc-project-e16de95dce32f077361577377d5c52fc4ad78339.tar.xz | |
[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.
Diffstat (limited to 'backend/tools')
| -rw-r--r-- | backend/tools/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | backend/tools/DescriptionGenerator/CMakeLists.txt | 39 | ||||
| -rw-r--r-- | backend/tools/DescriptionGenerator/DescriptionGeneratorMain.cpp | 97 |
3 files changed, 139 insertions, 0 deletions
diff --git a/backend/tools/CMakeLists.txt b/backend/tools/CMakeLists.txt new file mode 100644 index 0000000..67d7273 --- /dev/null +++ b/backend/tools/CMakeLists.txt @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-3.0-or-later + +add_subdirectory(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 "$<BUILD_INTERFACE:${RCC_BACKEND_SOURCE_DIR}/lib/Target/Description>" +) +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 <filesystem> +#include <fstream> +#include <iostream> +#include <iterator> +#include <string> +#include <string_view> + +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 <file> --header <file> --tables <file>\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<std::streamsize>(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<char>(Input), + std::istreambuf_iterator<char>()}; + 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; +} |
