/home/runner/work/LabNeura/LabNeura/src/labneura/eda/mapping/technology/technology_mapping.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include "labneura/eda/netlist.h" |
2 | | |
3 | | #include <algorithm> |
4 | | #include <cctype> |
5 | | #include <optional> |
6 | | #include <map> |
7 | | #include <regex> |
8 | | #include <stdexcept> |
9 | | #include <string> |
10 | | #include <string_view> |
11 | | #include <unordered_map> |
12 | | #include <utility> |
13 | | #include <vector> |
14 | | |
15 | | namespace labneura::eda { |
16 | | namespace { |
17 | | |
18 | | class LibertyFunctionParser { |
19 | | public: |
20 | | LibertyFunctionParser( |
21 | | std::string_view expression, |
22 | | const std::unordered_map<std::string, bool>& values) |
23 | 15.1k | : expression_(expression), values_(values) {} |
24 | | |
25 | 15.1k | std::optional<bool> parse() { |
26 | 15.1k | const auto value = parse_or(); |
27 | 15.1k | skip_space(); |
28 | 15.1k | if (!value || position_ != expression_.size()) return std::nullopt; |
29 | 13.5k | return value; |
30 | 15.1k | } |
31 | | |
32 | | private: |
33 | 391k | void skip_space() { |
34 | 408k | while (position_ < expression_.size() && |
35 | 408k | std::isspace(static_cast<unsigned char>(expression_[position_]))) { |
36 | 17.0k | ++position_; |
37 | 17.0k | } |
38 | 391k | } |
39 | | |
40 | 37.2k | std::optional<bool> parse_or() { |
41 | 37.2k | auto value = parse_xor(); |
42 | 45.7k | while (value) { |
43 | 44.0k | skip_space(); |
44 | 44.0k | if (position_ == expression_.size() || |
45 | 44.0k | (expression_[position_] != '|' && |
46 | 35.5k | expression_[position_] != '+')) { |
47 | 35.5k | break; |
48 | 35.5k | } |
49 | 8.51k | ++position_; |
50 | 8.51k | const auto rhs = parse_xor(); |
51 | 8.51k | if (!rhs) return std::nullopt; |
52 | 8.51k | value = *value || *rhs; |
53 | 8.51k | } |
54 | 37.2k | return value; |
55 | 37.2k | } |
56 | | |
57 | 45.7k | std::optional<bool> parse_xor() { |
58 | 45.7k | auto value = parse_and(); |
59 | 45.7k | while (value) { |
60 | 44.0k | skip_space(); |
61 | 44.0k | if (position_ == expression_.size() || |
62 | 44.0k | expression_[position_] != '^') { |
63 | 44.0k | break; |
64 | 44.0k | } |
65 | 0 | ++position_; |
66 | 0 | const auto rhs = parse_and(); |
67 | 0 | if (!rhs) return std::nullopt; |
68 | 0 | value = *value != *rhs; |
69 | 0 | } |
70 | 45.7k | return value; |
71 | 45.7k | } |
72 | | |
73 | 45.7k | std::optional<bool> parse_and() { |
74 | 45.7k | auto value = parse_unary(); |
75 | 63.2k | while (value) { |
76 | 61.6k | skip_space(); |
77 | 61.6k | if (position_ == expression_.size() || |
78 | 61.6k | (expression_[position_] != '&' && |
79 | 48.0k | expression_[position_] != '*')) { |
80 | 44.0k | break; |
81 | 44.0k | } |
82 | 17.5k | ++position_; |
83 | 17.5k | const auto rhs = parse_unary(); |
84 | 17.5k | if (!rhs) return std::nullopt; |
85 | 17.5k | value = *value && *rhs; |
86 | 17.5k | } |
87 | 45.7k | return value; |
88 | 45.7k | } |
89 | | |
90 | 79.2k | std::optional<bool> parse_unary() { |
91 | 79.2k | skip_space(); |
92 | 79.2k | if (position_ == expression_.size()) return std::nullopt; |
93 | 79.2k | if (expression_[position_] == '!' || expression_[position_] == '~') { |
94 | 15.9k | ++position_; |
95 | 15.9k | const auto value = parse_unary(); |
96 | 15.9k | if (!value) return std::nullopt; |
97 | 15.9k | return !*value; |
98 | 15.9k | } |
99 | 63.2k | auto value = parse_primary(); |
100 | 63.2k | while (value) { |
101 | 61.6k | skip_space(); |
102 | 61.6k | if (position_ == expression_.size() || |
103 | 61.6k | expression_[position_] != '\'') { |
104 | 61.6k | break; |
105 | 61.6k | } |
106 | 0 | ++position_; |
107 | 0 | value = !*value; |
108 | 0 | } |
109 | 63.2k | return value; |
110 | 79.2k | } |
111 | | |
112 | 63.2k | std::optional<bool> parse_primary() { |
113 | 63.2k | skip_space(); |
114 | 63.2k | if (position_ == expression_.size()) return std::nullopt; |
115 | 63.2k | if (expression_[position_] == '(') { |
116 | 22.0k | ++position_; |
117 | 22.0k | auto value = parse_or(); |
118 | 22.0k | skip_space(); |
119 | 22.0k | if (!value || position_ == expression_.size() || |
120 | 22.0k | expression_[position_] != ')') { |
121 | 0 | return std::nullopt; |
122 | 0 | } |
123 | 22.0k | ++position_; |
124 | 22.0k | return value; |
125 | 22.0k | } |
126 | 41.2k | if (expression_[position_] == '0' || expression_[position_] == '1') { |
127 | 0 | return expression_[position_++] == '1'; |
128 | 0 | } |
129 | 41.2k | const auto start = position_; |
130 | 103k | while (position_ < expression_.size()) { |
131 | 102k | const auto character = |
132 | 102k | static_cast<unsigned char>(expression_[position_]); |
133 | 102k | if (!std::isalnum(character) && expression_[position_] != '_') break; |
134 | 62.7k | ++position_; |
135 | 62.7k | } |
136 | 41.2k | if (start == position_) return std::nullopt; |
137 | 41.2k | const auto value = values_.find( |
138 | 41.2k | std::string(expression_.substr(start, position_ - start))); |
139 | 41.2k | if (value == values_.end()) return std::nullopt; |
140 | 39.5k | return value->second; |
141 | 41.2k | } |
142 | | |
143 | | std::string_view expression_; |
144 | | const std::unordered_map<std::string, bool>& values_; |
145 | | std::size_t position_ = 0; |
146 | | }; |
147 | | |
148 | | struct LogicGate { |
149 | | std::string family; |
150 | | std::size_t input_count; |
151 | | }; |
152 | | |
153 | 828 | std::optional<LogicGate> logic_gate(const std::string& type) { |
154 | 828 | static const std::regex gate( |
155 | 828 | R"(^(AND|NAND|OR|NOR|XOR|XNOR)([2-9][0-9]*)$)"); |
156 | 828 | std::smatch match; |
157 | 828 | if (std::regex_match(type, match, gate)) { |
158 | 477 | return LogicGate{match[1].str(), |
159 | 477 | static_cast<std::size_t>(std::stoul(match[2].str()))}; |
160 | 477 | } |
161 | 351 | if (type == "INV") return LogicGate{"INV", 1}; |
162 | 240 | if (type == "BUF") return LogicGate{"BUF", 1}; |
163 | 223 | return std::nullopt; |
164 | 240 | } |
165 | | |
166 | | bool matches_logic_function(const CellPin& output, const LogicGate& gate, |
167 | 8.62k | const std::vector<const CellPin*>& inputs) { |
168 | 8.62k | if (inputs.size() != gate.input_count || gate.input_count > 16 || |
169 | 8.62k | output.get_function().empty()) { |
170 | 234 | return false; |
171 | 234 | } |
172 | 8.39k | const std::size_t combinations = std::size_t{1} << gate.input_count; |
173 | 16.4k | for (std::size_t combination = 0; combination < combinations; |
174 | 15.1k | ++combination) { |
175 | 15.1k | std::unordered_map<std::string, bool> values; |
176 | 47.4k | for (std::size_t index = 0; index < inputs.size(); ++index) { |
177 | 32.3k | values.emplace(inputs[index]->get_name(), |
178 | 32.3k | (combination & (std::size_t{1} << index)) != 0); |
179 | 32.3k | } |
180 | 15.1k | const auto actual = |
181 | 15.1k | LibertyFunctionParser(output.get_function(), values).parse(); |
182 | 15.1k | if (!actual) return false; |
183 | | |
184 | 13.5k | bool expected = false; |
185 | 13.5k | if (gate.family == "AND" || gate.family == "NAND") { |
186 | 4.32k | expected = combination == combinations - 1; |
187 | 4.32k | if (gate.family == "NAND") expected = !expected; |
188 | 9.20k | } else if (gate.family == "OR" || gate.family == "NOR") { |
189 | 3.23k | expected = combination != 0; |
190 | 3.23k | if (gate.family == "NOR") expected = !expected; |
191 | 5.96k | } else if (gate.family == "XOR" || gate.family == "XNOR") { |
192 | 3.95k | auto bits = combination; |
193 | 6.78k | while (bits != 0) { |
194 | 2.82k | expected = !expected; |
195 | 2.82k | bits &= bits - 1; |
196 | 2.82k | } |
197 | 3.95k | if (gate.family == "XNOR") expected = !expected; |
198 | 3.95k | } else if (gate.family == "INV") { |
199 | 1.68k | expected = combination == 0; |
200 | 1.68k | } else { |
201 | 329 | expected = combination != 0; |
202 | 329 | } |
203 | 13.5k | if (*actual != expected) return false; |
204 | 13.5k | } |
205 | 1.27k | return true; |
206 | 8.39k | } |
207 | | |
208 | 43.3k | std::vector<const CellPin*> input_pins(const StandardCell& cell) { |
209 | 43.3k | std::vector<const CellPin*> inputs; |
210 | 178k | for (const auto& pin : cell.get_pins()) { |
211 | 178k | if (pin.get_direction() == "input") inputs.push_back(&pin); |
212 | 178k | } |
213 | 43.3k | return inputs; |
214 | 43.3k | } |
215 | | |
216 | | std::string normalized(std::string value); |
217 | | |
218 | | bool conventional_logic_cell_name(const StandardCell& cell, |
219 | 1.02k | const LogicGate& gate) { |
220 | 1.02k | auto fragment = normalized(gate.family); |
221 | 1.02k | if (gate.input_count > 1) fragment += std::to_string(gate.input_count); |
222 | 1.02k | const auto name = normalized(cell.get_name()); |
223 | 1.02k | const auto position = name.find(fragment); |
224 | 1.02k | if (position == std::string::npos) return false; |
225 | 779 | if (position > 0 && name[position - 1] != '_') return false; |
226 | 438 | const auto end = position + fragment.size(); |
227 | 438 | return end == name.size() || name[end] == '_'; |
228 | 779 | } |
229 | | |
230 | 17.2k | std::string normalized(std::string value) { |
231 | 17.2k | std::transform(value.begin(), value.end(), value.begin(), |
232 | 415k | [](unsigned char ch) { |
233 | 415k | return static_cast<char>(std::tolower(ch)); |
234 | 415k | }); |
235 | 17.2k | return value; |
236 | 17.2k | } |
237 | | |
238 | 33 | std::string cell_family(const std::string& type) { |
239 | 33 | const auto lower_type = normalized(type); |
240 | 33 | if (lower_type == "inv") return "inv"; |
241 | 33 | if (lower_type == "buf") return "buf"; |
242 | 33 | if (lower_type == "mux2") return "mux2"; |
243 | 24 | if (lower_type == "dff") return "dfxtp"; |
244 | 13 | if (lower_type == "dffr") return "dfrtp"; |
245 | 12 | if (lower_type == "dffs") return "dfstp"; |
246 | 11 | if (lower_type == "dffsr") return "dfbbp"; |
247 | 10 | if (lower_type == "tribuf") return "ebufn"; |
248 | 8 | if (lower_type == "latch") return "dlxtp"; |
249 | 8 | if (lower_type == "tiehi" || lower_type == "tielo") return "conb"; |
250 | 0 | for (const auto* family : {"and", "nand", "or", "nor", "xor", "xnor", |
251 | 0 | "maj"}) { |
252 | 0 | if (lower_type.rfind(family, 0) == 0) return family; |
253 | 0 | } |
254 | 0 | return {}; |
255 | 0 | } |
256 | | |
257 | | std::optional<StandardCell> find_cell_for_generic_type( |
258 | 136 | const StandardCellLibrary& library, const std::string& type) { |
259 | 136 | if (const auto* exact = library.find_cell(type)) return *exact; |
260 | 136 | if (const auto gate = logic_gate(type)) { |
261 | 103 | const StandardCell* best = nullptr; |
262 | 103 | bool best_has_conventional_name = false; |
263 | 103 | bool best_has_single_output = false; |
264 | 103 | bool best_is_unit_drive = false; |
265 | 42.8k | for (const auto& cell : library.get_cells()) { |
266 | 42.8k | const auto inputs = input_pins(cell); |
267 | 42.8k | if (inputs.size() != gate->input_count) continue; |
268 | 7.75k | const bool has_matching_output = std::any_of( |
269 | 7.75k | cell.get_pins().begin(), cell.get_pins().end(), |
270 | 24.4k | [&](const CellPin& pin) { |
271 | 24.4k | return pin.get_direction() == "output" && |
272 | 24.4k | matches_logic_function(pin, *gate, inputs); |
273 | 24.4k | }); |
274 | 7.75k | if (!has_matching_output) continue; |
275 | | |
276 | 1.02k | const auto candidate_name = normalized(cell.get_name()); |
277 | 1.02k | const bool has_conventional_name = |
278 | 1.02k | conventional_logic_cell_name(cell, *gate); |
279 | 1.02k | const auto output_count = static_cast<std::size_t>(std::count_if( |
280 | 1.02k | cell.get_pins().begin(), cell.get_pins().end(), |
281 | 2.81k | [](const CellPin& pin) { |
282 | 2.81k | return pin.get_direction() == "output"; |
283 | 2.81k | })); |
284 | 1.02k | const bool has_single_output = output_count == 1; |
285 | 1.02k | const bool unit_drive = |
286 | 1.02k | candidate_name.size() >= 2 && |
287 | 1.02k | candidate_name.compare(candidate_name.size() - 2, 2, "_1") == 0; |
288 | 1.02k | const bool better = |
289 | 1.02k | !best || |
290 | 1.02k | (has_conventional_name && !best_has_conventional_name) || |
291 | 1.02k | (has_conventional_name == best_has_conventional_name && |
292 | 865 | has_single_output && !best_has_single_output) || |
293 | 1.02k | (has_conventional_name == best_has_conventional_name && |
294 | 865 | has_single_output == best_has_single_output && unit_drive && |
295 | 865 | !best_is_unit_drive) || |
296 | 1.02k | (has_conventional_name == best_has_conventional_name && |
297 | 803 | has_single_output == best_has_single_output && |
298 | 803 | unit_drive == best_is_unit_drive && cell.get_area() && |
299 | 803 | (!best->get_area() || |
300 | 33 | *cell.get_area() < *best->get_area())); |
301 | 1.02k | if (better) { |
302 | 240 | best = &cell; |
303 | 240 | best_has_conventional_name = has_conventional_name; |
304 | 240 | best_has_single_output = has_single_output; |
305 | 240 | best_is_unit_drive = unit_drive; |
306 | 240 | } |
307 | 1.02k | } |
308 | 103 | if (best) return *best; |
309 | 1 | return std::nullopt; |
310 | 103 | } |
311 | 33 | static const std::regex generic_gate( |
312 | 33 | R"(^(AND|NAND|OR|NOR|XOR|XNOR|MAJ)([2-9][0-9]*)$)"); |
313 | 33 | static const std::regex generic_unary(R"(^(INV|BUF|MUX2|DFF|DFFR|DFFS|DFFSR|LATCH|TRIBUF|TIEHI|TIELO)$)"); |
314 | 33 | std::smatch generic_match; |
315 | 33 | if (!std::regex_match(type, generic_match, generic_gate) && |
316 | 33 | !std::regex_match(type, generic_unary)) { |
317 | 0 | return std::nullopt; |
318 | 0 | } |
319 | 33 | const auto family = cell_family(type); |
320 | 33 | if (family.empty()) return std::nullopt; |
321 | | |
322 | 33 | std::string digit_count; |
323 | 134 | for (const auto character : type) { |
324 | 134 | if (std::isdigit(static_cast<unsigned char>(character))) { |
325 | 9 | digit_count.push_back(character); |
326 | 9 | } |
327 | 134 | } |
328 | 33 | const auto name_fragment = |
329 | 33 | type == "MUX2" || type.rfind("DFF", 0) == 0 || type == "LATCH" || |
330 | 33 | type == "TRIBUF" |
331 | 33 | ? family |
332 | 33 | : family + digit_count; |
333 | 33 | const StandardCell* best = nullptr; |
334 | 33 | bool best_is_unit_drive = false; |
335 | 14.1k | for (const auto& cell : library.get_cells()) { |
336 | 14.1k | const auto candidate_name = normalized(cell.get_name()); |
337 | 14.1k | const auto family_position = candidate_name.find(name_fragment); |
338 | 14.1k | if (family_position == std::string::npos) continue; |
339 | 203 | if (family_position > 0 && |
340 | 203 | candidate_name[family_position - 1] != '_') { |
341 | 84 | continue; |
342 | 84 | } |
343 | 119 | const auto family_end = family_position + name_fragment.size(); |
344 | 119 | if (family_end < candidate_name.size() && |
345 | 119 | candidate_name[family_end] != '_') { |
346 | 27 | continue; |
347 | 27 | } |
348 | | |
349 | 92 | std::size_t input_count = 0; |
350 | 92 | std::size_t output_count = 0; |
351 | 313 | for (const auto& pin : cell.get_pins()) { |
352 | 313 | if (pin.get_direction() == "input") ++input_count; |
353 | 313 | if (pin.get_direction() == "output") ++output_count; |
354 | 313 | } |
355 | 92 | std::size_t expected_inputs = 0; |
356 | 92 | if (type.rfind("AND", 0) == 0 || type.rfind("OR", 0) == 0 || |
357 | 92 | type.rfind("NAND", 0) == 0 || type.rfind("NOR", 0) == 0 || |
358 | 92 | type.rfind("XOR", 0) == 0 || type.rfind("XNOR", 0) == 0 || |
359 | 92 | type.rfind("MAJ", 0) == 0) { |
360 | 0 | expected_inputs = static_cast<std::size_t>(std::stoul(digit_count)); |
361 | 92 | } else if (type == "MUX2") { |
362 | 36 | expected_inputs = 3; |
363 | 56 | } else if (type == "DFF" || type == "LATCH" || type == "TRIBUF") { |
364 | 41 | expected_inputs = 2; |
365 | 41 | } else if (type == "DFFR" || type == "DFFS") { |
366 | 6 | expected_inputs = 3; |
367 | 9 | } else if (type == "DFFSR") { |
368 | 1 | expected_inputs = 4; |
369 | 8 | } else if (type == "INV" || type == "BUF") { |
370 | 0 | expected_inputs = 1; |
371 | 0 | } |
372 | 92 | if (input_count != expected_inputs || output_count == 0) continue; |
373 | 92 | const bool unit_drive = candidate_name.size() >= 2 && |
374 | 92 | candidate_name.compare( |
375 | 92 | candidate_name.size() - 2, 2, "_1") == 0; |
376 | 92 | if (!best || (unit_drive && !best_is_unit_drive) || |
377 | 92 | (unit_drive == best_is_unit_drive && cell.get_area() && |
378 | 59 | (!best->get_area() || |
379 | 33 | *cell.get_area() < *best->get_area()))) { |
380 | 33 | best = &cell; |
381 | 33 | best_is_unit_drive = unit_drive; |
382 | 33 | } |
383 | 92 | } |
384 | 33 | if (best) return *best; |
385 | 0 | return std::nullopt; |
386 | 33 | } |
387 | | |
388 | 349 | std::string generic_alias(const std::string& type) { |
389 | 349 | if (type == "AND2_X1") return "AND2"; |
390 | 349 | if (type == "OR2_X1") return "OR2"; |
391 | 349 | if (type == "XOR2_X1") return "XOR2"; |
392 | 349 | if (type == "INV_X1") return "INV"; |
393 | 349 | if (type == "BUF_X1") return "BUF"; |
394 | 349 | if (type == "MUX2_X1") return "MUX2"; |
395 | 349 | if (type == "TIEHI_X1") return "TIEHI"; |
396 | 349 | if (type == "TIELO_X1") return "TIELO"; |
397 | 349 | return type; |
398 | 349 | } |
399 | | |
400 | 349 | bool is_generic_type(const std::string& type) { |
401 | 349 | static const std::regex generic_gate( |
402 | 349 | R"(^(AND|NAND|OR|NOR|XOR|XNOR|MAJ)([2-9][0-9]*)$)"); |
403 | 349 | static const std::regex generic_unary(R"(^(INV|BUF|MUX2|DFF|DFFR|DFFS|DFFSR|LATCH|TRIBUF|TIEHI|TIELO)$)"); |
404 | 349 | return std::regex_match(type, generic_gate) || |
405 | 349 | std::regex_match(type, generic_unary); |
406 | 349 | } |
407 | | |
408 | 346 | std::string output_pin(const StandardCell& cell, const std::string& type) { |
409 | 346 | if (const auto gate = logic_gate(type)) { |
410 | 251 | const auto inputs = input_pins(cell); |
411 | 718 | for (const auto& pin : cell.get_pins()) { |
412 | 718 | if (pin.get_direction() == "output" && |
413 | 718 | matches_logic_function(pin, *gate, inputs)) { |
414 | 251 | return pin.get_name(); |
415 | 251 | } |
416 | 718 | } |
417 | 0 | throw std::runtime_error( |
418 | 0 | "standard cell '" + cell.get_name() + |
419 | 0 | "' has no output matching generic cell '" + type + "'"); |
420 | 251 | } |
421 | 95 | const std::string expected_function = |
422 | 95 | type == "TIEHI" ? "1" : type == "TIELO" ? "0" : ""; |
423 | 304 | for (const auto& pin : cell.get_pins()) { |
424 | 304 | if (pin.get_direction() == "output" && |
425 | 304 | (expected_function.empty() || |
426 | 100 | pin.get_function() == expected_function)) { |
427 | 95 | return pin.get_name(); |
428 | 95 | } |
429 | 304 | } |
430 | 0 | throw std::runtime_error("standard cell '" + cell.get_name() + |
431 | 0 | "' has no matching output pin"); |
432 | 95 | } |
433 | | |
434 | 467 | std::optional<std::size_t> generic_input_index(const std::string& pin) { |
435 | 467 | if (pin.size() == 1 && pin[0] >= 'A' && pin[0] <= 'Z') { |
436 | 467 | return static_cast<std::size_t>(pin[0] - 'A'); |
437 | 467 | } |
438 | 0 | if (pin.rfind("IN", 0) == 0 && pin.size() > 2) { |
439 | 0 | try { |
440 | 0 | const auto one_based = std::stoul(pin.substr(2)); |
441 | 0 | if (one_based > 26) return one_based - 1; |
442 | 0 | } catch (const std::invalid_argument&) { |
443 | 0 | return std::nullopt; |
444 | 0 | } catch (const std::out_of_range&) { |
445 | 0 | return std::nullopt; |
446 | 0 | } |
447 | 0 | } |
448 | 0 | return std::nullopt; |
449 | 0 | } |
450 | | |
451 | | } // namespace |
452 | | |
453 | | Module tech_map(const Module& generic_module, |
454 | 48 | const StandardCellLibrary& cell_library) { |
455 | 48 | std::vector<Instance> instances; |
456 | 48 | instances.reserve(generic_module.get_instances().size()); |
457 | 48 | std::map<std::string, std::optional<StandardCell>> cell_cache; |
458 | | |
459 | 349 | for (const auto& instance : generic_module.get_instances()) { |
460 | 349 | const auto generic_type = instance.get_cell_type(); |
461 | 349 | const auto mapped_type = generic_alias(generic_type); |
462 | 349 | if (mapped_type != generic_type) { |
463 | 0 | if (const auto* exact = cell_library.find_cell(generic_type)) { |
464 | 0 | instances.emplace_back(instance.get_name(), generic_type, |
465 | 0 | instance.get_connections(), *exact); |
466 | 0 | continue; |
467 | 0 | } |
468 | 0 | } |
469 | 349 | const bool generic_cell = is_generic_type(mapped_type); |
470 | 349 | if (!generic_cell) { |
471 | 2 | if (const auto* exact = cell_library.find_cell(generic_type)) { |
472 | 0 | instances.emplace_back(instance.get_name(), generic_type, |
473 | 0 | instance.get_connections(), *exact); |
474 | 0 | continue; |
475 | 0 | } |
476 | 2 | } |
477 | 349 | if (!generic_cell) { |
478 | 2 | instances.emplace_back(instance.get_name(), generic_type, |
479 | 2 | instance.get_connections(), instance.get_cell()); |
480 | 2 | continue; |
481 | 2 | } |
482 | 347 | auto cached = cell_cache.find(mapped_type); |
483 | 347 | if (cached == cell_cache.end()) { |
484 | 136 | cached = cell_cache |
485 | 136 | .emplace(mapped_type, |
486 | 136 | find_cell_for_generic_type(cell_library, mapped_type)) |
487 | 136 | .first; |
488 | 136 | } |
489 | 347 | const auto& cell = cached->second; |
490 | 347 | if (!cell) { |
491 | 1 | throw std::runtime_error( |
492 | 1 | "no standard cell matching generic cell '" + generic_type + |
493 | 1 | "' was found in Liberty library '" + cell_library.get_name() + |
494 | 1 | "'"); |
495 | 1 | } |
496 | | |
497 | 346 | std::vector<PinConnection> connections; |
498 | 346 | const auto gate = logic_gate(mapped_type); |
499 | 346 | const auto candidate_inputs = gate ? input_pins(*cell) |
500 | 346 | : std::vector<const CellPin*>{}; |
501 | 1.02k | for (const auto& connection : instance.get_connections()) { |
502 | 1.02k | if (connection.get_pin() == "Y") { |
503 | 346 | connections.emplace_back( |
504 | 346 | output_pin(*cell, mapped_type), connection.get_net()); |
505 | 675 | } else if (gate) { |
506 | 467 | const auto index = |
507 | 467 | generic_input_index(connection.get_pin()); |
508 | 467 | if (!index || *index >= candidate_inputs.size()) { |
509 | 0 | throw std::runtime_error( |
510 | 0 | "generic cell '" + generic_type + |
511 | 0 | "' has invalid input pin '" + |
512 | 0 | connection.get_pin() + "'"); |
513 | 0 | } |
514 | 467 | connections.emplace_back( |
515 | 467 | candidate_inputs[*index]->get_name(), |
516 | 467 | connection.get_net()); |
517 | 467 | } else { |
518 | 208 | const auto* pin = cell->find_pin(connection.get_pin()); |
519 | 208 | if (!pin || pin->get_direction() != "input") { |
520 | 0 | throw std::runtime_error( |
521 | 0 | "cell '" + cell->get_name() + |
522 | 0 | "' has no input pin corresponding to generic pin '" + |
523 | 0 | connection.get_pin() + "'"); |
524 | 0 | } |
525 | 208 | connections.emplace_back(pin->get_name(), connection.get_net()); |
526 | 208 | } |
527 | 1.02k | } |
528 | 346 | instances.emplace_back(instance.get_name(), cell->get_name(), |
529 | 346 | std::move(connections), *cell); |
530 | 346 | } |
531 | 47 | std::vector<Module> child_modules; |
532 | 47 | child_modules.reserve(generic_module.get_child_modules().size()); |
533 | 47 | for (const auto& child : generic_module.get_child_modules()) { |
534 | 2 | child_modules.push_back(tech_map(child, cell_library)); |
535 | 2 | } |
536 | 47 | return Module(generic_module.get_name(), generic_module.get_ports(), |
537 | 47 | generic_module.get_nets(), std::move(instances), |
538 | 47 | generic_module.get_assignments(), generic_module.is_behavioral(), |
539 | 47 | generic_module.get_hierarchy_instance_name(), |
540 | 47 | std::move(child_modules)); |
541 | 48 | } |
542 | | |
543 | | Module tech_map(const Project& project, const StandardCellLibrary& cell_library, |
544 | | const std::string& top, |
545 | 1 | const std::map<std::string, std::string>& parameters) { |
546 | 1 | return tech_map(generic_map(project, top, parameters), cell_library); |
547 | 1 | } |
548 | | |
549 | | } // namespace labneura::eda |