SILICON
Loading...
Searching...
No Matches
isaArchitecture.cpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "isaArchitecture.hpp"
19
20#include <algorithm>
21#include <cctype>
22#include <format>
23#include <ranges>
24#include <stdexcept>
25#include <utility>
26
27#include <sisl/sisl.hpp>
28
29namespace SILICON::project {
30
31bool isArchitecturePathSlug(const std::string_view name)
32{
33 const auto letter = [](const char ch) {
34 return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
35 };
36 const auto digit = [](const char ch) { return ch >= '0' && ch <= '9'; };
37 if (name.empty() || !(letter(name.front()) || name.front() == '_'))
38 return false;
39 return std::ranges::all_of(name.substr(1), [&](const char ch) {
40 return letter(ch) || digit(ch) || ch == '_' || ch == '-';
41 });
42}
43
44bool isValidArchitectureName(const std::string_view name)
45{
46 if (!isArchitecturePathSlug(name))
47 return false;
48 try {
49 [[maybe_unused]] const auto isa =
50 sisl::Isa::load_string(std::format("arch {} = {{}};", name));
51 } catch (const sisl::Error&) {
52 return false;
53 }
54 return true;
55}
56
57namespace {
58
59 std::optional<std::pair<std::size_t, std::size_t>>
60 declarationNameRange(const std::string_view source)
61 {
62 std::size_t pos = 0;
63 const auto skipTrivia = [&]() -> bool {
64 for (;;) {
65 while (pos < source.size()
66 && std::isspace(static_cast<unsigned char>(source[pos])))
67 ++pos;
68 if (source.substr(pos).starts_with("//")) {
69 const auto end = source.find('\n', pos + 2);
70 pos = end == std::string_view::npos ? source.size() : end + 1;
71 } else if (source.substr(pos).starts_with("/*")) {
72 const auto end = source.find("*/", pos + 2);
73 if (end == std::string_view::npos)
74 return false;
75 pos = end + 2;
76 } else {
77 return true;
78 }
79 }
80 };
81 if (!skipTrivia())
82 return std::nullopt;
83 if (!source.substr(pos).starts_with("arch"))
84 return std::nullopt;
85 pos += 4;
86 if (pos == source.size()
87 || !(std::isspace(static_cast<unsigned char>(source[pos]))
88 || source.substr(pos).starts_with("//")
89 || source.substr(pos).starts_with("/*")))
90 return std::nullopt;
91 if (!skipTrivia())
92 return std::nullopt;
93 const auto start = pos;
94 while (pos < source.size()) {
95 const char ch = source[pos];
96 if (!((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')
97 || (ch >= '0' && ch <= '9') || ch == '_' || ch == '-'))
98 break;
99 ++pos;
100 }
101 const auto name = source.substr(start, pos - start);
102 return isValidArchitectureName(name) ? std::optional(std::pair{start, pos - start})
103 : std::nullopt;
104 }
105
106} // namespace
107
108std::optional<std::string> declaredArchitectureName(const std::string_view source)
109{
110 const auto range = declarationNameRange(source);
111 return range ? std::optional(std::string(source.substr(range->first, range->second)))
112 : std::nullopt;
113}
114
115std::string renameArchitectureDeclaration(const std::string_view source,
116 const std::string_view oldName,
117 const std::string_view newName)
118{
119 std::string result(source);
120 if (const auto range = declarationNameRange(source);
121 range && source.substr(range->first, range->second) == oldName)
122 result.replace(range->first, range->second, newName);
123 return result;
124}
125
126void validateArchitectureDeclaration(const std::string_view directoryName,
127 const std::string_view source)
128{
129 const auto declared = declaredArchitectureName(source);
130 if (declared && *declared != directoryName)
131 throw std::invalid_argument(
132 std::format("architecture declaration '{}' does not match directory '{}'",
133 *declared, directoryName));
134}
135
136std::string architectureDirectory(const std::string_view name)
137{
138 return std::format("isa/{}/", name);
139}
140
141ArchitectureBuildResult buildArchitecture(const std::optional<std::string> expectedName,
142 const std::string_view source)
143{
145 if (expectedName) {
146 try {
147 validateArchitectureDeclaration(*expectedName, source);
148 } catch (const std::exception& error) {
149 result.error = error.what();
150 return result;
151 }
152 }
153 try {
154 result.isa = sisl::Isa::load_string(source);
155 } catch (const sisl::Error& error) {
156 result.diagnostics = error.diagnostics();
157 if (result.diagnostics.empty())
158 result.error = error.what();
159 } catch (const std::exception& error) {
160 result.error = error.what();
161 }
162 return result;
163}
164
165} // namespace SILICON::project
std::optional< std::string > declaredArchitectureName(const std::string_view source)
std::string renameArchitectureDeclaration(const std::string_view source, const std::string_view oldName, const std::string_view newName)
void validateArchitectureDeclaration(const std::string_view directoryName, const std::string_view source)
ArchitectureBuildResult buildArchitecture(const std::optional< std::string > expectedName, const std::string_view source)
bool isValidArchitectureName(const std::string_view name)
bool isArchitecturePathSlug(const std::string_view name)
std::string architectureDirectory(const std::string_view name)
std::vector< sisl::Diagnostic > diagnostics