yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
abc.cc
Go to the documentation of this file.
1 /*
2  * yosys -- Yosys Open SYnthesis Suite
3  *
4  * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  *
18  */
19 
20 // [[CITE]] ABC
21 // Berkeley Logic Synthesis and Verification Group, ABC: A System for Sequential Synthesis and Verification
22 // http://www.eecs.berkeley.edu/~alanmi/abc/
23 
24 // [[CITE]] Berkeley Logic Interchange Format (BLIF)
25 // University of California. Berkeley. July 28, 1992
26 // http://www.ece.cmu.edu/~ee760/760docs/blif.pdf
27 
28 // [[CITE]] Kahn's Topological sorting algorithm
29 // Kahn, Arthur B. (1962), "Topological sorting of large networks", Communications of the ACM 5 (11): 558–562, doi:10.1145/368996.369025
30 // http://en.wikipedia.org/wiki/Topological_sorting
31 
32 #define ABC_COMMAND_LIB "strash; scorr -v; ifraig -v; retime -v {D}; strash; dch -vf; map -v {D}"
33 #define ABC_COMMAND_CTR "strash; scorr -v; ifraig -v; retime -v {D}; strash; dch -vf; map -v {D}; buffer -v; upsize -v {D}; dnsize -v {D}; stime -p"
34 #define ABC_COMMAND_LUT "strash; scorr -v; ifraig -v; retime -v; strash; dch -vf; if -v"
35 #define ABC_COMMAND_DFL "strash; scorr -v; ifraig -v; retime -v; strash; dch -vf; map -v"
36 
37 #define ABC_FAST_COMMAND_LIB "retime -v {D}; map -v {D}"
38 #define ABC_FAST_COMMAND_CTR "retime -v {D}; map -v {D}; buffer -v; upsize -v {D}; dnsize -v {D}; stime -p"
39 #define ABC_FAST_COMMAND_LUT "retime -v; if -v"
40 #define ABC_FAST_COMMAND_DFL "retime -v; map -v"
41 
42 #include "kernel/register.h"
43 #include "kernel/sigtools.h"
44 #include "kernel/cost.h"
45 #include "kernel/log.h"
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <cerrno>
50 #include <sstream>
51 #include <climits>
52 
53 #ifndef _WIN32
54 # include <unistd.h>
55 # include <dirent.h>
56 #endif
57 
58 #include "blifparse.h"
59 
62 
63 enum class gate_type_t {
64  G_NONE,
65  G_FF,
66  G_BUF,
67  G_NOT,
68  G_AND,
69  G_NAND,
70  G_OR,
71  G_NOR,
72  G_XOR,
73  G_XNOR,
74  G_MUX,
75  G_AOI3,
76  G_OAI3,
77  G_AOI4,
78  G_OAI4
79 };
80 
81 #define G(_name) gate_type_t::G_ ## _name
82 
83 struct gate_t
84 {
85  int id;
87  int in1, in2, in3, in4;
88  bool is_port;
90 };
91 
95 std::vector<gate_t> signal_list;
96 std::map<RTLIL::SigBit, int> signal_map;
97 
100 
101 int map_signal(RTLIL::SigBit bit, gate_type_t gate_type = G(NONE), int in1 = -1, int in2 = -1, int in3 = -1, int in4 = -1)
102 {
103  assign_map.apply(bit);
104 
105  if (signal_map.count(bit) == 0) {
106  gate_t gate;
107  gate.id = signal_list.size();
108  gate.type = G(NONE);
109  gate.in1 = -1;
110  gate.in2 = -1;
111  gate.in3 = -1;
112  gate.in4 = -1;
113  gate.is_port = false;
114  gate.bit = bit;
115  signal_list.push_back(gate);
116  signal_map[bit] = gate.id;
117  }
118 
119  gate_t &gate = signal_list[signal_map[bit]];
120 
121  if (gate_type != G(NONE))
122  gate.type = gate_type;
123  if (in1 >= 0)
124  gate.in1 = in1;
125  if (in2 >= 0)
126  gate.in2 = in2;
127  if (in3 >= 0)
128  gate.in3 = in3;
129  if (in4 >= 0)
130  gate.in4 = in4;
131 
132  return gate.id;
133 }
134 
136 {
137  for (auto &bit : assign_map(sig))
138  if (bit.wire != NULL && signal_map.count(bit) > 0)
139  signal_list[signal_map[bit]].is_port = true;
140 }
141 
142 void extract_cell(RTLIL::Cell *cell, bool keepff)
143 {
144  if (cell->type == "$_DFF_N_" || cell->type == "$_DFF_P_")
145  {
146  if (clk_polarity != (cell->type == "$_DFF_P_"))
147  return;
148  if (clk_sig != assign_map(cell->getPort("\\C")))
149  return;
150 
151  RTLIL::SigSpec sig_d = cell->getPort("\\D");
152  RTLIL::SigSpec sig_q = cell->getPort("\\Q");
153 
154  if (keepff)
155  for (auto &c : sig_q.chunks())
156  if (c.wire != NULL)
157  c.wire->attributes["\\keep"] = 1;
158 
159  assign_map.apply(sig_d);
160  assign_map.apply(sig_q);
161 
162  map_signal(sig_q, G(FF), map_signal(sig_d));
163 
164  module->remove(cell);
165  return;
166  }
167 
168  if (cell->type.in("$_BUF_", "$_NOT_"))
169  {
170  RTLIL::SigSpec sig_a = cell->getPort("\\A");
171  RTLIL::SigSpec sig_y = cell->getPort("\\Y");
172 
173  assign_map.apply(sig_a);
174  assign_map.apply(sig_y);
175 
176  map_signal(sig_y, cell->type == "$_BUF_" ? G(BUF) : G(NOT), map_signal(sig_a));
177 
178  module->remove(cell);
179  return;
180  }
181 
182  if (cell->type.in("$_AND_", "$_NAND_", "$_OR_", "$_NOR_", "$_XOR_", "$_XNOR_"))
183  {
184  RTLIL::SigSpec sig_a = cell->getPort("\\A");
185  RTLIL::SigSpec sig_b = cell->getPort("\\B");
186  RTLIL::SigSpec sig_y = cell->getPort("\\Y");
187 
188  assign_map.apply(sig_a);
189  assign_map.apply(sig_b);
190  assign_map.apply(sig_y);
191 
192  int mapped_a = map_signal(sig_a);
193  int mapped_b = map_signal(sig_b);
194 
195  if (cell->type == "$_AND_")
196  map_signal(sig_y, G(AND), mapped_a, mapped_b);
197  else if (cell->type == "$_NAND_")
198  map_signal(sig_y, G(NAND), mapped_a, mapped_b);
199  else if (cell->type == "$_OR_")
200  map_signal(sig_y, G(OR), mapped_a, mapped_b);
201  else if (cell->type == "$_NOR_")
202  map_signal(sig_y, G(NOR), mapped_a, mapped_b);
203  else if (cell->type == "$_XOR_")
204  map_signal(sig_y, G(XOR), mapped_a, mapped_b);
205  else if (cell->type == "$_XNOR_")
206  map_signal(sig_y, G(XNOR), mapped_a, mapped_b);
207  else
208  log_abort();
209 
210  module->remove(cell);
211  return;
212  }
213 
214  if (cell->type == "$_MUX_")
215  {
216  RTLIL::SigSpec sig_a = cell->getPort("\\A");
217  RTLIL::SigSpec sig_b = cell->getPort("\\B");
218  RTLIL::SigSpec sig_s = cell->getPort("\\S");
219  RTLIL::SigSpec sig_y = cell->getPort("\\Y");
220 
221  assign_map.apply(sig_a);
222  assign_map.apply(sig_b);
223  assign_map.apply(sig_s);
224  assign_map.apply(sig_y);
225 
226  int mapped_a = map_signal(sig_a);
227  int mapped_b = map_signal(sig_b);
228  int mapped_s = map_signal(sig_s);
229 
230  map_signal(sig_y, G(MUX), mapped_a, mapped_b, mapped_s);
231 
232  module->remove(cell);
233  return;
234  }
235 
236  if (cell->type.in("$_AOI3_", "$_OAI3_"))
237  {
238  RTLIL::SigSpec sig_a = cell->getPort("\\A");
239  RTLIL::SigSpec sig_b = cell->getPort("\\B");
240  RTLIL::SigSpec sig_c = cell->getPort("\\C");
241  RTLIL::SigSpec sig_y = cell->getPort("\\Y");
242 
243  assign_map.apply(sig_a);
244  assign_map.apply(sig_b);
245  assign_map.apply(sig_c);
246  assign_map.apply(sig_y);
247 
248  int mapped_a = map_signal(sig_a);
249  int mapped_b = map_signal(sig_b);
250  int mapped_c = map_signal(sig_c);
251 
252  map_signal(sig_y, cell->type == "$_AOI3_" ? G(AOI3) : G(OAI3), mapped_a, mapped_b, mapped_c);
253 
254  module->remove(cell);
255  return;
256  }
257 
258  if (cell->type.in("$_AOI4_", "$_OAI4_"))
259  {
260  RTLIL::SigSpec sig_a = cell->getPort("\\A");
261  RTLIL::SigSpec sig_b = cell->getPort("\\B");
262  RTLIL::SigSpec sig_c = cell->getPort("\\C");
263  RTLIL::SigSpec sig_d = cell->getPort("\\D");
264  RTLIL::SigSpec sig_y = cell->getPort("\\Y");
265 
266  assign_map.apply(sig_a);
267  assign_map.apply(sig_b);
268  assign_map.apply(sig_c);
269  assign_map.apply(sig_d);
270  assign_map.apply(sig_y);
271 
272  int mapped_a = map_signal(sig_a);
273  int mapped_b = map_signal(sig_b);
274  int mapped_c = map_signal(sig_c);
275  int mapped_d = map_signal(sig_d);
276 
277  map_signal(sig_y, cell->type == "$_AOI4_" ? G(AOI4) : G(OAI4), mapped_a, mapped_b, mapped_c, mapped_d);
278 
279  module->remove(cell);
280  return;
281  }
282 }
283 
284 std::string remap_name(RTLIL::IdString abc_name)
285 {
286  std::stringstream sstr;
287  sstr << "$abc$" << map_autoidx << "$" << abc_name.substr(1);
288  return sstr.str();
289 }
290 
291 void dump_loop_graph(FILE *f, int &nr, std::map<int, std::set<int>> &edges, std::set<int> &workpool, std::vector<int> &in_counts)
292 {
293  if (f == NULL)
294  return;
295 
296  log("Dumping loop state graph to slide %d.\n", ++nr);
297 
298  fprintf(f, "digraph \"slide%d\" {\n", nr);
299  fprintf(f, " label=\"slide%d\";\n", nr);
300  fprintf(f, " rankdir=\"TD\";\n");
301 
302  std::set<int> nodes;
303  for (auto &e : edges) {
304  nodes.insert(e.first);
305  for (auto n : e.second)
306  nodes.insert(n);
307  }
308 
309  for (auto n : nodes)
310  fprintf(f, " n%d [label=\"%s\\nid=%d, count=%d\"%s];\n", n, log_signal(signal_list[n].bit),
311  n, in_counts[n], workpool.count(n) ? ", shape=box" : "");
312 
313  for (auto &e : edges)
314  for (auto n : e.second)
315  fprintf(f, " n%d -> n%d;\n", e.first, n);
316 
317  fprintf(f, "}\n");
318 }
319 
321 {
322  // http://en.wikipedia.org/wiki/Topological_sorting
323  // (Kahn, Arthur B. (1962), "Topological sorting of large networks")
324 
325  std::map<int, std::set<int>> edges;
326  std::vector<int> in_edges_count(signal_list.size());
327  std::set<int> workpool;
328 
329  FILE *dot_f = NULL;
330  int dot_nr = 0;
331 
332  // uncomment for troubleshooting the loop detection code
333  // dot_f = fopen("test.dot", "w");
334 
335  for (auto &g : signal_list) {
336  if (g.type == G(NONE) || g.type == G(FF)) {
337  workpool.insert(g.id);
338  } else {
339  if (g.in1 >= 0) {
340  edges[g.in1].insert(g.id);
341  in_edges_count[g.id]++;
342  }
343  if (g.in2 >= 0 && g.in2 != g.in1) {
344  edges[g.in2].insert(g.id);
345  in_edges_count[g.id]++;
346  }
347  if (g.in3 >= 0 && g.in3 != g.in2 && g.in3 != g.in1) {
348  edges[g.in3].insert(g.id);
349  in_edges_count[g.id]++;
350  }
351  if (g.in4 >= 0 && g.in4 != g.in3 && g.in4 != g.in2 && g.in4 != g.in1) {
352  edges[g.in4].insert(g.id);
353  in_edges_count[g.id]++;
354  }
355  }
356  }
357 
358  dump_loop_graph(dot_f, dot_nr, edges, workpool, in_edges_count);
359 
360  while (workpool.size() > 0)
361  {
362  int id = *workpool.begin();
363  workpool.erase(id);
364 
365  // log("Removing non-loop node %d from graph: %s\n", id, log_signal(signal_list[id].bit));
366 
367  for (int id2 : edges[id]) {
368  log_assert(in_edges_count[id2] > 0);
369  if (--in_edges_count[id2] == 0)
370  workpool.insert(id2);
371  }
372  edges.erase(id);
373 
374  dump_loop_graph(dot_f, dot_nr, edges, workpool, in_edges_count);
375 
376  while (workpool.size() == 0)
377  {
378  if (edges.size() == 0)
379  break;
380 
381  int id1 = edges.begin()->first;
382 
383  for (auto &edge_it : edges) {
384  int id2 = edge_it.first;
385  RTLIL::Wire *w1 = signal_list[id1].bit.wire;
386  RTLIL::Wire *w2 = signal_list[id2].bit.wire;
387  if (w1 == NULL)
388  id1 = id2;
389  else if (w2 == NULL)
390  continue;
391  else if (w1->name[0] == '$' && w2->name[0] == '\\')
392  id1 = id2;
393  else if (w1->name[0] == '\\' && w2->name[0] == '$')
394  continue;
395  else if (edges[id1].size() < edges[id2].size())
396  id1 = id2;
397  else if (edges[id1].size() > edges[id2].size())
398  continue;
399  else if (w2->name.str() < w1->name.str())
400  id1 = id2;
401  }
402 
403  if (edges[id1].size() == 0) {
404  edges.erase(id1);
405  continue;
406  }
407 
408  log_assert(signal_list[id1].bit.wire != NULL);
409 
410  std::stringstream sstr;
411  sstr << "$abcloop$" << (autoidx++);
412  RTLIL::Wire *wire = module->addWire(sstr.str());
413 
414  bool first_line = true;
415  for (int id2 : edges[id1]) {
416  if (first_line)
417  log("Breaking loop using new signal %s: %s -> %s\n", log_signal(RTLIL::SigSpec(wire)),
418  log_signal(signal_list[id1].bit), log_signal(signal_list[id2].bit));
419  else
420  log(" %*s %s -> %s\n", int(strlen(log_signal(RTLIL::SigSpec(wire)))), "",
421  log_signal(signal_list[id1].bit), log_signal(signal_list[id2].bit));
422  first_line = false;
423  }
424 
425  int id3 = map_signal(RTLIL::SigSpec(wire));
426  signal_list[id1].is_port = true;
427  signal_list[id3].is_port = true;
428  log_assert(id3 == int(in_edges_count.size()));
429  in_edges_count.push_back(0);
430  workpool.insert(id3);
431 
432  for (int id2 : edges[id1]) {
433  if (signal_list[id2].in1 == id1)
434  signal_list[id2].in1 = id3;
435  if (signal_list[id2].in2 == id1)
436  signal_list[id2].in2 = id3;
437  if (signal_list[id2].in3 == id1)
438  signal_list[id2].in3 = id3;
439  if (signal_list[id2].in4 == id1)
440  signal_list[id2].in4 = id3;
441  }
442  edges[id1].swap(edges[id3]);
443 
444  module->connect(RTLIL::SigSig(signal_list[id3].bit, signal_list[id1].bit));
445  dump_loop_graph(dot_f, dot_nr, edges, workpool, in_edges_count);
446  }
447  }
448 
449  if (dot_f != NULL)
450  fclose(dot_f);
451 }
452 
453 std::string add_echos_to_abc_cmd(std::string str)
454 {
455  std::string new_str, token;
456  for (size_t i = 0; i < str.size(); i++) {
457  token += str[i];
458  if (str[i] == ';') {
459  while (i+1 < str.size() && str[i+1] == ' ')
460  i++;
461  if (!new_str.empty())
462  new_str += "echo; ";
463  new_str += "echo + " + token + " " + token + " ";
464  token.clear();
465  }
466  }
467 
468  if (!token.empty()) {
469  if (!new_str.empty())
470  new_str += "echo; echo + " + token + "; ";
471  new_str += token;
472  }
473 
474  return new_str;
475 }
476 
477 std::string fold_abc_cmd(std::string str)
478 {
479  std::string token, new_str = " ";
480  int char_counter = 10;
481 
482  for (size_t i = 0; i <= str.size(); i++) {
483  if (i < str.size())
484  token += str[i];
485  if (i == str.size() || str[i] == ';') {
486  if (char_counter + token.size() > 75)
487  new_str += "\n ", char_counter = 14;
488  new_str += token, char_counter += token.size();
489  token.clear();
490  }
491  }
492 
493  return new_str;
494 }
495 
497 {
498  bool got_cr;
500  std::string linebuf;
501 
503  {
504  got_cr = false;
505  escape_seq_state = 0;
506  }
507 
508  void next_char(char ch)
509  {
510  if (escape_seq_state == 0 && ch == '\033') {
511  escape_seq_state = 1;
512  return;
513  }
514  if (escape_seq_state == 1) {
515  escape_seq_state = ch == '[' ? 2 : 0;
516  return;
517  }
518  if (escape_seq_state == 2) {
519  if ((ch < '0' || '9' < ch) && ch != ';')
520  escape_seq_state = 0;
521  return;
522  }
523  escape_seq_state = 0;
524  if (ch == '\r') {
525  got_cr = true;
526  return;
527  }
528  if (ch == '\n') {
529  log("ABC: %s\n", linebuf.c_str());
530  got_cr = false, linebuf.clear();
531  return;
532  }
533  if (got_cr)
534  got_cr = false, linebuf.clear();
535  linebuf += ch;
536  }
537 
538  void next_line(const std::string &line)
539  {
540  for (char ch : line)
541  next_char(ch);
542  }
543 };
544 
545 void abc_module(RTLIL::Design *design, RTLIL::Module *current_module, std::string script_file, std::string exe_file,
546  std::string liberty_file, std::string constr_file, bool cleanup, int lut_mode, bool dff_mode, std::string clk_str,
547  bool keepff, std::string delay_target, bool fast_mode)
548 {
549  module = current_module;
550  map_autoidx = autoidx++;
551 
552  signal_map.clear();
553  signal_list.clear();
554  assign_map.set(module);
555 
556  clk_polarity = true;
557  clk_sig = RTLIL::SigSpec();
558 
559  std::string tempdir_name = "/tmp/yosys-abc-XXXXXX";
560  if (!cleanup)
561  tempdir_name[0] = tempdir_name[4] = '_';
562  tempdir_name = make_temp_dir(tempdir_name);
563  log_header("Extracting gate netlist of module `%s' to `%s/input.blif'..\n", module->name.c_str(), tempdir_name.c_str());
564 
565  std::string abc_script = stringf("read_blif %s/input.blif; ", tempdir_name.c_str());
566 
567  if (!liberty_file.empty()) {
568  abc_script += stringf("read_lib -w %s; ", liberty_file.c_str());
569  if (!constr_file.empty())
570  abc_script += stringf("read_constr -v %s; ", constr_file.c_str());
571  } else
572  if (lut_mode)
573  abc_script += stringf("read_lut %s/lutdefs.txt; ", tempdir_name.c_str());
574  else
575  abc_script += stringf("read_library %s/stdcells.genlib; ", tempdir_name.c_str());
576 
577  if (!script_file.empty()) {
578  if (script_file[0] == '+') {
579  for (size_t i = 1; i < script_file.size(); i++)
580  if (script_file[i] == '\'')
581  abc_script += "'\\''";
582  else if (script_file[i] == ',')
583  abc_script += " ";
584  else
585  abc_script += script_file[i];
586  } else
587  abc_script += stringf("source %s", script_file.c_str());
588  } else if (lut_mode)
589  abc_script += fast_mode ? ABC_FAST_COMMAND_LUT : ABC_COMMAND_LUT;
590  else if (!liberty_file.empty())
591  abc_script += constr_file.empty() ? (fast_mode ? ABC_FAST_COMMAND_LIB : ABC_COMMAND_LIB) : (fast_mode ? ABC_FAST_COMMAND_CTR : ABC_COMMAND_CTR);
592  else
593  abc_script += fast_mode ? ABC_FAST_COMMAND_DFL : ABC_COMMAND_DFL;
594 
595  for (size_t pos = abc_script.find("{D}"); pos != std::string::npos; pos = abc_script.find("{D}", pos))
596  abc_script = abc_script.substr(0, pos) + delay_target + abc_script.substr(pos+3);
597 
598  abc_script += stringf("; write_blif %s/output.blif", tempdir_name.c_str());
599  abc_script = add_echos_to_abc_cmd(abc_script);
600 
601  for (size_t i = 0; i+1 < abc_script.size(); i++)
602  if (abc_script[i] == ';' && abc_script[i+1] == ' ')
603  abc_script[i+1] = '\n';
604 
605  FILE *f = fopen(stringf("%s/abc.script", tempdir_name.c_str()).c_str(), "wt");
606  fprintf(f, "%s\n", abc_script.c_str());
607  fclose(f);
608 
609  if (clk_str.empty()) {
610  if (clk_str[0] == '!') {
611  clk_polarity = false;
612  clk_str = clk_str.substr(1);
613  }
614  if (module->wires_.count(RTLIL::escape_id(clk_str)) != 0)
615  clk_sig = assign_map(RTLIL::SigSpec(module->wires_.at(RTLIL::escape_id(clk_str)), 0));
616  }
617 
618  if (dff_mode && clk_sig.size() == 0)
619  {
620  int best_dff_counter = 0;
621  std::map<std::pair<bool, RTLIL::SigSpec>, int> dff_counters;
622 
623  for (auto &it : module->cells_)
624  {
625  RTLIL::Cell *cell = it.second;
626  if (cell->type != "$_DFF_N_" && cell->type != "$_DFF_P_")
627  continue;
628 
629  std::pair<bool, RTLIL::SigSpec> key(cell->type == "$_DFF_P_", assign_map(cell->getPort("\\C")));
630  if (++dff_counters[key] > best_dff_counter) {
631  best_dff_counter = dff_counters[key];
632  clk_polarity = key.first;
633  clk_sig = key.second;
634  }
635  }
636  }
637 
638  if (dff_mode || !clk_str.empty()) {
639  if (clk_sig.size() == 0)
640  log("No (matching) clock domain found. Not extracting any FF cells.\n");
641  else
642  log("Found (matching) %s clock domain: %s\n", clk_polarity ? "posedge" : "negedge", log_signal(clk_sig));
643  }
644 
645  if (clk_sig.size() != 0)
646  mark_port(clk_sig);
647 
648  std::vector<RTLIL::Cell*> cells;
649  cells.reserve(module->cells_.size());
650  for (auto &it : module->cells_)
651  if (design->selected(current_module, it.second))
652  cells.push_back(it.second);
653  for (auto c : cells)
654  extract_cell(c, keepff);
655 
656  for (auto &wire_it : module->wires_) {
657  if (wire_it.second->port_id > 0 || wire_it.second->get_bool_attribute("\\keep"))
658  mark_port(RTLIL::SigSpec(wire_it.second));
659  }
660 
661  for (auto &cell_it : module->cells_)
662  for (auto &port_it : cell_it.second->connections())
663  mark_port(port_it.second);
664 
665  handle_loops();
666 
667  std::string buffer = stringf("%s/input.blif", tempdir_name.c_str());
668  f = fopen(buffer.c_str(), "wt");
669  if (f == NULL)
670  log_error("Opening %s for writing failed: %s\n", buffer.c_str(), strerror(errno));
671 
672  fprintf(f, ".model netlist\n");
673 
674  int count_input = 0;
675  fprintf(f, ".inputs");
676  for (auto &si : signal_list) {
677  if (!si.is_port || si.type != G(NONE))
678  continue;
679  fprintf(f, " n%d", si.id);
680  count_input++;
681  }
682  if (count_input == 0)
683  fprintf(f, " dummy_input\n");
684  fprintf(f, "\n");
685 
686  int count_output = 0;
687  fprintf(f, ".outputs");
688  for (auto &si : signal_list) {
689  if (!si.is_port || si.type == G(NONE))
690  continue;
691  fprintf(f, " n%d", si.id);
692  count_output++;
693  }
694  fprintf(f, "\n");
695 
696  for (auto &si : signal_list)
697  fprintf(f, "# n%-5d %s\n", si.id, log_signal(si.bit));
698 
699  for (auto &si : signal_list) {
700  if (si.bit.wire == NULL) {
701  fprintf(f, ".names n%d\n", si.id);
702  if (si.bit == RTLIL::State::S1)
703  fprintf(f, "1\n");
704  }
705  }
706 
707  int count_gates = 0;
708  for (auto &si : signal_list) {
709  if (si.type == G(BUF)) {
710  fprintf(f, ".names n%d n%d\n", si.in1, si.id);
711  fprintf(f, "1 1\n");
712  } else if (si.type == G(NOT)) {
713  fprintf(f, ".names n%d n%d\n", si.in1, si.id);
714  fprintf(f, "0 1\n");
715  } else if (si.type == G(AND)) {
716  fprintf(f, ".names n%d n%d n%d\n", si.in1, si.in2, si.id);
717  fprintf(f, "11 1\n");
718  } else if (si.type == G(NAND)) {
719  fprintf(f, ".names n%d n%d n%d\n", si.in1, si.in2, si.id);
720  fprintf(f, "0- 1\n");
721  fprintf(f, "-0 1\n");
722  } else if (si.type == G(OR)) {
723  fprintf(f, ".names n%d n%d n%d\n", si.in1, si.in2, si.id);
724  fprintf(f, "-1 1\n");
725  fprintf(f, "1- 1\n");
726  } else if (si.type == G(NOR)) {
727  fprintf(f, ".names n%d n%d n%d\n", si.in1, si.in2, si.id);
728  fprintf(f, "00 1\n");
729  } else if (si.type == G(XOR)) {
730  fprintf(f, ".names n%d n%d n%d\n", si.in1, si.in2, si.id);
731  fprintf(f, "01 1\n");
732  fprintf(f, "10 1\n");
733  } else if (si.type == G(XNOR)) {
734  fprintf(f, ".names n%d n%d n%d\n", si.in1, si.in2, si.id);
735  fprintf(f, "00 1\n");
736  fprintf(f, "11 1\n");
737  } else if (si.type == G(MUX)) {
738  fprintf(f, ".names n%d n%d n%d n%d\n", si.in1, si.in2, si.in3, si.id);
739  fprintf(f, "1-0 1\n");
740  fprintf(f, "-11 1\n");
741  } else if (si.type == G(AOI3)) {
742  fprintf(f, ".names n%d n%d n%d n%d\n", si.in1, si.in2, si.in3, si.id);
743  fprintf(f, "-00 1\n");
744  fprintf(f, "0-0 1\n");
745  } else if (si.type == G(OAI3)) {
746  fprintf(f, ".names n%d n%d n%d n%d\n", si.in1, si.in2, si.in3, si.id);
747  fprintf(f, "00- 1\n");
748  fprintf(f, "--0 1\n");
749  } else if (si.type == G(AOI4)) {
750  fprintf(f, ".names n%d n%d n%d n%d n%d\n", si.in1, si.in2, si.in3, si.in4, si.id);
751  fprintf(f, "-0-0 1\n");
752  fprintf(f, "-00- 1\n");
753  fprintf(f, "0--0 1\n");
754  fprintf(f, "0-0- 1\n");
755  } else if (si.type == G(OAI4)) {
756  fprintf(f, ".names n%d n%d n%d n%d n%d\n", si.in1, si.in2, si.in3, si.in4, si.id);
757  fprintf(f, "00-- 1\n");
758  fprintf(f, "--00 1\n");
759  } else if (si.type == G(FF)) {
760  fprintf(f, ".latch n%d n%d\n", si.in1, si.id);
761  } else if (si.type != G(NONE))
762  log_abort();
763  if (si.type != G(NONE))
764  count_gates++;
765  }
766 
767  fprintf(f, ".end\n");
768  fclose(f);
769 
770  log("Extracted %d gates and %d wires to a netlist network with %d inputs and %d outputs.\n",
771  count_gates, GetSize(signal_list), count_input, count_output);
772  log_push();
773 
774  if (count_output > 0)
775  {
776  log_header("Executing ABC.\n");
777 
778  buffer = stringf("%s/stdcells.genlib", tempdir_name.c_str());
779  f = fopen(buffer.c_str(), "wt");
780  if (f == NULL)
781  log_error("Opening %s for writing failed: %s\n", buffer.c_str(), strerror(errno));
782  fprintf(f, "GATE ZERO 1 Y=CONST0;\n");
783  fprintf(f, "GATE ONE 1 Y=CONST1;\n");
784  fprintf(f, "GATE BUF %d Y=A; PIN * NONINV 1 999 1 0 1 0\n", get_cell_cost("$_BUF_"));
785  fprintf(f, "GATE NOT %d Y=!A; PIN * INV 1 999 1 0 1 0\n", get_cell_cost("$_NOT_"));
786  fprintf(f, "GATE AND %d Y=A*B; PIN * NONINV 1 999 1 0 1 0\n", get_cell_cost("$_AND_"));
787  fprintf(f, "GATE NAND %d Y=!(A*B); PIN * INV 1 999 1 0 1 0\n", get_cell_cost("$_NAND_"));
788  fprintf(f, "GATE OR %d Y=A+B; PIN * NONINV 1 999 1 0 1 0\n", get_cell_cost("$_OR_"));
789  fprintf(f, "GATE NOR %d Y=!(A+B); PIN * INV 1 999 1 0 1 0\n", get_cell_cost("$_NOR_"));
790  fprintf(f, "GATE XOR %d Y=(A*!B)+(!A*B); PIN * UNKNOWN 1 999 1 0 1 0\n", get_cell_cost("$_XOR_"));
791  fprintf(f, "GATE XNOR %d Y=(A*B)+(!A*!B); PIN * UNKNOWN 1 999 1 0 1 0\n", get_cell_cost("$_XNOR_"));
792  fprintf(f, "GATE AOI3 %d Y=!((A*B)+C); PIN * INV 1 999 1 0 1 0\n", get_cell_cost("$_AOI3_"));
793  fprintf(f, "GATE OAI3 %d Y=!((A+B)*C); PIN * INV 1 999 1 0 1 0\n", get_cell_cost("$_OAI3_"));
794  fprintf(f, "GATE AOI4 %d Y=!((A*B)+(C*D)); PIN * INV 1 999 1 0 1 0\n", get_cell_cost("$_AOI4_"));
795  fprintf(f, "GATE OAI4 %d Y=!((A+B)*(C+D)); PIN * INV 1 999 1 0 1 0\n", get_cell_cost("$_OAI4_"));
796  fprintf(f, "GATE MUX %d Y=(A*B)+(S*B)+(!S*A); PIN * UNKNOWN 1 999 1 0 1 0\n", get_cell_cost("$_MUX_"));
797  fclose(f);
798 
799  if (lut_mode) {
800  buffer = stringf("%s/lutdefs.txt", tempdir_name.c_str());
801  f = fopen(buffer.c_str(), "wt");
802  if (f == NULL)
803  log_error("Opening %s for writing failed: %s\n", buffer.c_str(), strerror(errno));
804  for (int i = 0; i < lut_mode; i++)
805  fprintf(f, "%d 1.00 1.00\n", i+1);
806  fclose(f);
807  }
808 
809  buffer = stringf("%s -s -f %s/abc.script 2>&1", exe_file.c_str(), tempdir_name.c_str());
810  log("Running ABC command: %s\n", buffer.c_str());
811 
812  abc_output_filter filt;
813  int ret = run_command(buffer, std::bind(&abc_output_filter::next_line, filt, std::placeholders::_1));
814  if (ret != 0)
815  log_error("ABC: execution of command \"%s\" failed: return code %d.\n", buffer.c_str(), ret);
816 
817  buffer = stringf("%s/%s", tempdir_name.c_str(), "output.blif");
818  f = fopen(buffer.c_str(), "rt");
819  if (f == NULL)
820  log_error("Can't open ABC output file `%s'.\n", buffer.c_str());
821 
822  bool builtin_lib = liberty_file.empty() && script_file.empty() && !lut_mode;
823  RTLIL::Design *mapped_design = abc_parse_blif(f, builtin_lib ? "\\DFF" : "\\_dff_");
824 
825  fclose(f);
826 
827  log_header("Re-integrating ABC results.\n");
828  RTLIL::Module *mapped_mod = mapped_design->modules_["\\netlist"];
829  if (mapped_mod == NULL)
830  log_error("ABC output file does not contain a module `netlist'.\n");
831  for (auto &it : mapped_mod->wires_) {
832  RTLIL::Wire *w = it.second;
833  RTLIL::Wire *wire = module->addWire(remap_name(w->name));
834  design->select(module, wire);
835  }
836 
837  std::map<std::string, int> cell_stats;
838  if (builtin_lib)
839  {
840  for (auto &it : mapped_mod->cells_) {
841  RTLIL::Cell *c = it.second;
842  cell_stats[RTLIL::unescape_id(c->type)]++;
843  if (c->type == "\\ZERO" || c->type == "\\ONE") {
844  RTLIL::SigSig conn;
845  conn.first = RTLIL::SigSpec(module->wires_[remap_name(c->getPort("\\Y").as_wire()->name)]);
846  conn.second = RTLIL::SigSpec(c->type == "\\ZERO" ? 0 : 1, 1);
847  module->connect(conn);
848  continue;
849  }
850  if (c->type == "\\BUF") {
851  RTLIL::SigSig conn;
852  conn.first = RTLIL::SigSpec(module->wires_[remap_name(c->getPort("\\Y").as_wire()->name)]);
853  conn.second = RTLIL::SigSpec(module->wires_[remap_name(c->getPort("\\A").as_wire()->name)]);
854  module->connect(conn);
855  continue;
856  }
857  if (c->type == "\\NOT") {
858  RTLIL::Cell *cell = module->addCell(remap_name(c->name), "$_NOT_");
859  cell->setPort("\\A", RTLIL::SigSpec(module->wires_[remap_name(c->getPort("\\A").as_wire()->name)]));
860  cell->setPort("\\Y", RTLIL::SigSpec(module->wires_[remap_name(c->getPort("\\Y").as_wire()->name)]));
861  design->select(module, cell);
862  continue;
863  }
864  if (c->type == "\\AND" || c->type == "\\OR" || c->type == "\\XOR" || c->type == "\\NAND" || c->type == "\\NOR" || c->type == "\\XNOR") {
865  RTLIL::Cell *cell = module->addCell(remap_name(c->name), "$_" + c->type.substr(1) + "_");
866  cell->setPort("\\A", RTLIL::SigSpec(module->wires_[remap_name(c->getPort("\\A").as_wire()->name)]));
867  cell->setPort("\\B", RTLIL::SigSpec(module->wires_[remap_name(c->getPort("\\B").as_wire()->name)]));
868  cell->setPort("\\Y", RTLIL::SigSpec(module->wires_[remap_name(c->getPort("\\Y").as_wire()->name)]));
869  design->select(module, cell);
870  continue;
871  }
872  if (c->type == "\\MUX") {
873  RTLIL::Cell *cell = module->addCell(remap_name(c->name), "$_MUX_");
874  cell->setPort("\\A", RTLIL::SigSpec(module->wires_[remap_name(c->getPort("\\A").as_wire()->name)]));
875  cell->setPort("\\B", RTLIL::SigSpec(module->wires_[remap_name(c->getPort("\\B").as_wire()->name)]));
876  cell->setPort("\\S", RTLIL::SigSpec(module->wires_[remap_name(c->getPort("\\S").as_wire()->name)]));
877  cell->setPort("\\Y", RTLIL::SigSpec(module->wires_[remap_name(c->getPort("\\Y").as_wire()->name)]));
878  design->select(module, cell);
879  continue;
880  }
881  if (c->type == "\\AOI3" || c->type == "\\OAI3") {
882  RTLIL::Cell *cell = module->addCell(remap_name(c->name), "$_" + c->type.substr(1) + "_");
883  cell->setPort("\\A", RTLIL::SigSpec(module->wires_[remap_name(c->getPort("\\A").as_wire()->name)]));
884  cell->setPort("\\B", RTLIL::SigSpec(module->wires_[remap_name(c->getPort("\\B").as_wire()->name)]));
885  cell->setPort("\\C", RTLIL::SigSpec(module->wires_[remap_name(c->getPort("\\C").as_wire()->name)]));
886  cell->setPort("\\Y", RTLIL::SigSpec(module->wires_[remap_name(c->getPort("\\Y").as_wire()->name)]));
887  design->select(module, cell);
888  continue;
889  }
890  if (c->type == "\\AOI4" || c->type == "\\OAI4") {
891  RTLIL::Cell *cell = module->addCell(remap_name(c->name), "$_" + c->type.substr(1) + "_");
892  cell->setPort("\\A", RTLIL::SigSpec(module->wires_[remap_name(c->getPort("\\A").as_wire()->name)]));
893  cell->setPort("\\B", RTLIL::SigSpec(module->wires_[remap_name(c->getPort("\\B").as_wire()->name)]));
894  cell->setPort("\\C", RTLIL::SigSpec(module->wires_[remap_name(c->getPort("\\C").as_wire()->name)]));
895  cell->setPort("\\D", RTLIL::SigSpec(module->wires_[remap_name(c->getPort("\\D").as_wire()->name)]));
896  cell->setPort("\\Y", RTLIL::SigSpec(module->wires_[remap_name(c->getPort("\\Y").as_wire()->name)]));
897  design->select(module, cell);
898  continue;
899  }
900  if (c->type == "\\DFF") {
901  log_assert(clk_sig.size() == 1);
902  RTLIL::Cell *cell = module->addCell(remap_name(c->name), clk_polarity ? "$_DFF_P_" : "$_DFF_N_");
903  cell->setPort("\\D", RTLIL::SigSpec(module->wires_[remap_name(c->getPort("\\D").as_wire()->name)]));
904  cell->setPort("\\Q", RTLIL::SigSpec(module->wires_[remap_name(c->getPort("\\Q").as_wire()->name)]));
905  cell->setPort("\\C", clk_sig);
906  design->select(module, cell);
907  continue;
908  }
909  log_abort();
910  }
911  }
912  else
913  {
914  for (auto &it : mapped_mod->cells_)
915  {
916  RTLIL::Cell *c = it.second;
917  cell_stats[RTLIL::unescape_id(c->type)]++;
918  if (c->type == "\\_const0_" || c->type == "\\_const1_") {
919  RTLIL::SigSig conn;
920  conn.first = RTLIL::SigSpec(module->wires_[remap_name(c->connections().begin()->second.as_wire()->name)]);
921  conn.second = RTLIL::SigSpec(c->type == "\\_const0_" ? 0 : 1, 1);
922  module->connect(conn);
923  continue;
924  }
925  if (c->type == "\\_dff_") {
926  log_assert(clk_sig.size() == 1);
927  RTLIL::Cell *cell = module->addCell(remap_name(c->name), clk_polarity ? "$_DFF_P_" : "$_DFF_N_");
928  cell->setPort("\\D", RTLIL::SigSpec(module->wires_[remap_name(c->getPort("\\D").as_wire()->name)]));
929  cell->setPort("\\Q", RTLIL::SigSpec(module->wires_[remap_name(c->getPort("\\Q").as_wire()->name)]));
930  cell->setPort("\\C", clk_sig);
931  design->select(module, cell);
932  continue;
933  }
934  RTLIL::Cell *cell = module->addCell(remap_name(c->name), c->type);
935  cell->parameters = c->parameters;
936  for (auto &conn : c->connections()) {
937  RTLIL::SigSpec newsig;
938  for (auto &c : conn.second.chunks()) {
939  if (c.width == 0)
940  continue;
941  log_assert(c.width == 1);
942  newsig.append(module->wires_[remap_name(c.wire->name)]);
943  }
944  cell->setPort(conn.first, newsig);
945  }
946  design->select(module, cell);
947  }
948  }
949 
950  for (auto conn : mapped_mod->connections()) {
951  if (!conn.first.is_fully_const())
952  conn.first = RTLIL::SigSpec(module->wires_[remap_name(conn.first.as_wire()->name)]);
953  if (!conn.second.is_fully_const())
954  conn.second = RTLIL::SigSpec(module->wires_[remap_name(conn.second.as_wire()->name)]);
955  module->connect(conn);
956  }
957 
958  for (auto &it : cell_stats)
959  log("ABC RESULTS: %15s cells: %8d\n", it.first.c_str(), it.second);
960  int in_wires = 0, out_wires = 0;
961  for (auto &si : signal_list)
962  if (si.is_port) {
963  char buffer[100];
964  snprintf(buffer, 100, "\\n%d", si.id);
965  RTLIL::SigSig conn;
966  if (si.type != G(NONE)) {
967  conn.first = si.bit;
968  conn.second = RTLIL::SigSpec(module->wires_[remap_name(buffer)]);
969  out_wires++;
970  } else {
971  conn.first = RTLIL::SigSpec(module->wires_[remap_name(buffer)]);
972  conn.second = si.bit;
973  in_wires++;
974  }
975  module->connect(conn);
976  }
977  log("ABC RESULTS: internal signals: %8d\n", int(signal_list.size()) - in_wires - out_wires);
978  log("ABC RESULTS: input signals: %8d\n", in_wires);
979  log("ABC RESULTS: output signals: %8d\n", out_wires);
980 
981  delete mapped_design;
982  }
983  else
984  {
985  log("Don't call ABC as there is nothing to map.\n");
986  }
987 
988  if (cleanup)
989  {
990  log_header("Removing temp directory `%s':\n", tempdir_name.c_str());
991  remove_directory(tempdir_name);
992  }
993 
994  log_pop();
995 }
996 
997 struct AbcPass : public Pass {
998  AbcPass() : Pass("abc", "use ABC for technology mapping") { }
999  virtual void help()
1000  {
1001  // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
1002  log("\n");
1003  log(" abc [options] [selection]\n");
1004  log("\n");
1005  log("This pass uses the ABC tool [1] for technology mapping of yosys's internal gate\n");
1006  log("library to a target architecture.\n");
1007  log("\n");
1008  log(" -exe <command>\n");
1009  log(" use the specified command name instead of \"yosys-abc\" to execute ABC.\n");
1010  log(" This can e.g. be used to call a specific version of ABC or a wrapper.\n");
1011  log("\n");
1012  log(" -script <file>\n");
1013  log(" use the specified ABC script file instead of the default script.\n");
1014  log("\n");
1015  log(" if <file> starts with a plus sign (+), then the rest of the filename\n");
1016  log(" string is interprated as the command string to be passed to ABC. the\n");
1017  log(" leading plus sign is removed and all commas (,) in the string are\n");
1018  log(" replaced with blanks before the string is passed to ABC.\n");
1019  log("\n");
1020  log(" if no -script parameter is given, the following scripts are used:\n");
1021  log("\n");
1022  log(" for -liberty without -constr:\n");
1023  log("%s\n", fold_abc_cmd(ABC_COMMAND_LIB).c_str());
1024  log("\n");
1025  log(" for -liberty with -constr:\n");
1026  log("%s\n", fold_abc_cmd(ABC_COMMAND_CTR).c_str());
1027  log("\n");
1028  log(" for -lut:\n");
1029  log("%s\n", fold_abc_cmd(ABC_COMMAND_LUT).c_str());
1030  log("\n");
1031  log(" otherwise:\n");
1032  log("%s\n", fold_abc_cmd(ABC_COMMAND_DFL).c_str());
1033  log("\n");
1034  log(" -fast\n");
1035  log(" use different default scripts that are slightly faster (at the cost\n");
1036  log(" of output quality):\n");
1037  log("\n");
1038  log(" for -liberty without -constr:\n");
1039  log("%s\n", fold_abc_cmd(ABC_FAST_COMMAND_LIB).c_str());
1040  log("\n");
1041  log(" for -liberty with -constr:\n");
1042  log("%s\n", fold_abc_cmd(ABC_FAST_COMMAND_CTR).c_str());
1043  log("\n");
1044  log(" for -lut:\n");
1045  log("%s\n", fold_abc_cmd(ABC_FAST_COMMAND_LUT).c_str());
1046  log("\n");
1047  log(" otherwise:\n");
1048  log("%s\n", fold_abc_cmd(ABC_FAST_COMMAND_DFL).c_str());
1049  log("\n");
1050  log(" -liberty <file>\n");
1051  log(" generate netlists for the specified cell library (using the liberty\n");
1052  log(" file format).\n");
1053  log("\n");
1054  log(" -constr <file>\n");
1055  log(" pass this file with timing constraints to ABC. use with -liberty.\n");
1056  log("\n");
1057  log(" a constr file contains two lines:\n");
1058  log(" set_driving_cell <cell_name>\n");
1059  log(" set_load <floating_point_number>\n");
1060  log("\n");
1061  log(" the set_driving_cell statement defines which cell type is assumed to\n");
1062  log(" drive the primary inputs and the set_load statement sets the load in\n");
1063  log(" femtofarads for each primary output.\n");
1064  log("\n");
1065  log(" -D <picoseconds>\n");
1066  log(" set delay target. the string {D} in the default scripts above is\n");
1067  log(" replaced by this option when used, and an empty string otherwise.\n");
1068  log("\n");
1069  log(" -lut <width>\n");
1070  log(" generate netlist using luts of (max) the specified width.\n");
1071  log("\n");
1072  log(" -dff\n");
1073  log(" also pass $_DFF_?_ cells through ABC (only one clock domain, if many\n");
1074  log(" clock domains are present in a module, the one with the largest number\n");
1075  log(" of $_DFF_?_ cells in it is used)\n");
1076  log("\n");
1077  log(" -clk [!]<signal-name>\n");
1078  log(" use the specified clock domain. (when this option is used in combination\n");
1079  log(" with -dff, then it falls back to the automatic dection of clock domain\n");
1080  log(" if the specified clock is not found in a module.)\n");
1081  log("\n");
1082  log(" -keepff\n");
1083  log(" set the \"keep\" attribute on flip-flop output wires. (and thus preserve\n");
1084  log(" them, for example for equivialence checking.)\n");
1085  log("\n");
1086  log(" -nocleanup\n");
1087  log(" when this option is used, the temporary files created by this pass\n");
1088  log(" are not removed. this is useful for debugging.\n");
1089  log("\n");
1090  log("When neither -liberty nor -lut is used, the Yosys standard cell library is\n");
1091  log("loaded into ABC before the ABC script is executed.\n");
1092  log("\n");
1093  log("This pass does not operate on modules with unprocessed processes in it.\n");
1094  log("(I.e. the 'proc' pass should be used first to convert processes to netlists.)\n");
1095  log("\n");
1096  log("[1] http://www.eecs.berkeley.edu/~alanmi/abc/\n");
1097  log("\n");
1098  }
1099  virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
1100  {
1101  log_header("Executing ABC pass (technology mapping using ABC).\n");
1102  log_push();
1103 
1104  std::string exe_file = proc_self_dirname() + "yosys-abc";
1105  std::string script_file, liberty_file, constr_file, clk_str, delay_target;
1106  bool fast_mode = false, dff_mode = false, keepff = false, cleanup = true;
1107  int lut_mode = 0;
1108 
1109 #ifdef _WIN32
1110  if (!check_file_exists(exe_file + ".exe") && check_file_exists(proc_self_dirname() + "..\\yosys-abc.exe"))
1111  exe_file = proc_self_dirname() + "..\\yosys-abc";
1112 #endif
1113 
1114  size_t argidx;
1115  char pwd [PATH_MAX];
1116  if (!getcwd(pwd, sizeof(pwd))) {
1117  log_cmd_error("getcwd failed: %s\n", strerror(errno));
1118  log_abort();
1119  }
1120  for (argidx = 1; argidx < args.size(); argidx++) {
1121  std::string arg = args[argidx];
1122  if (arg == "-exe" && argidx+1 < args.size()) {
1123  exe_file = args[++argidx];
1124  continue;
1125  }
1126  if (arg == "-script" && argidx+1 < args.size()) {
1127  script_file = args[++argidx];
1128  if (!script_file.empty() && script_file[0] != '/' && script_file[0] != '+')
1129  script_file = std::string(pwd) + "/" + script_file;
1130  continue;
1131  }
1132  if (arg == "-liberty" && argidx+1 < args.size()) {
1133  liberty_file = args[++argidx];
1134  if (!liberty_file.empty() && liberty_file[0] != '/')
1135  liberty_file = std::string(pwd) + "/" + liberty_file;
1136  continue;
1137  }
1138  if (arg == "-constr" && argidx+1 < args.size()) {
1139  constr_file = args[++argidx];
1140  if (!constr_file.empty() && constr_file[0] != '/')
1141  constr_file = std::string(pwd) + "/" + constr_file;
1142  continue;
1143  }
1144  if (arg == "-D" && argidx+1 < args.size()) {
1145  delay_target = "-D " + args[++argidx];
1146  continue;
1147  }
1148  if (arg == "-lut" && argidx+1 < args.size()) {
1149  lut_mode = atoi(args[++argidx].c_str());
1150  continue;
1151  }
1152  if (arg == "-fast") {
1153  fast_mode = true;
1154  continue;
1155  }
1156  if (arg == "-dff") {
1157  dff_mode = true;
1158  continue;
1159  }
1160  if (arg == "-clk" && argidx+1 < args.size()) {
1161  clk_str = args[++argidx];
1162  continue;
1163  }
1164  if (arg == "-keepff") {
1165  keepff = true;
1166  continue;
1167  }
1168  if (arg == "-nocleanup") {
1169  cleanup = false;
1170  continue;
1171  }
1172  break;
1173  }
1174  extra_args(args, argidx, design);
1175 
1176  if (lut_mode != 0 && !liberty_file.empty())
1177  log_cmd_error("Got -lut and -liberty! This two options are exclusive.\n");
1178  if (!constr_file.empty() && liberty_file.empty())
1179  log_cmd_error("Got -constr but no -liberty!\n");
1180 
1181  for (auto &mod_it : design->modules_)
1182  if (design->selected(mod_it.second)) {
1183  if (mod_it.second->processes.size() > 0)
1184  log("Skipping module %s as it contains processes.\n", mod_it.second->name.c_str());
1185  else
1186  abc_module(design, mod_it.second, script_file, exe_file, liberty_file, constr_file, cleanup, lut_mode, dff_mode, clk_str, keepff, delay_target, fast_mode);
1187  }
1188 
1189  assign_map.clear();
1190  signal_list.clear();
1191  signal_map.clear();
1192 
1193  log_pop();
1194  }
1195 } AbcPass;
1196 
AbcPass AbcPass
const char * c_str() const
Definition: rtlil.h:178
bool selected(T1 *module) const
Definition: rtlil.h:551
RTLIL::Wire * wire
Definition: rtlil.h:907
std::string str() const
Definition: rtlil.h:182
int map_autoidx
Definition: abc.cc:92
#define ABC_FAST_COMMAND_DFL
Definition: abc.cc:40
std::string add_echos_to_abc_cmd(std::string str)
Definition: abc.cc:453
RTLIL::SigBit bit
Definition: abc.cc:89
std::string stringf(const char *fmt,...)
Definition: yosys.cc:58
void remove_directory(std::string dirname)
Definition: yosys.cc:308
void extract_cell(RTLIL::Cell *cell, bool keepff)
Definition: abc.cc:142
RTLIL::Cell * addCell(RTLIL::IdString name, RTLIL::IdString type)
Definition: rtlil.cc:1353
bool clk_polarity
Definition: abc.cc:98
void log_header(const char *format,...)
Definition: log.cc:188
const std::vector< RTLIL::SigSig > & connections() const
Definition: rtlil.cc:1307
gate_type_t
Definition: abc.cc:63
bool is_port
Definition: abc.cc:88
std::map< RTLIL::IdString, RTLIL::Wire * > wires_
Definition: rtlil.h:595
static std::string unescape_id(std::string str)
Definition: rtlil.h:257
void mark_port(RTLIL::SigSpec sig)
Definition: abc.cc:135
const char * log_signal(const RTLIL::SigSpec &sig, bool autoint)
Definition: log.cc:269
void clear()
Definition: sigtools.h:263
RTLIL::IdString name
Definition: rtlil.h:853
void setPort(RTLIL::IdString portname, RTLIL::SigSpec signal)
Definition: rtlil.cc:1789
void log_error(const char *format,...)
Definition: log.cc:204
RTLIL::Module * module
Definition: abc.cc:94
Definition: abc.cc:83
RTLIL::IdString type
Definition: rtlil.h:854
std::map< RTLIL::IdString, RTLIL::Const > parameters
Definition: rtlil.h:856
void log_pop()
Definition: log.cc:237
int in4
Definition: abc.cc:87
int escape_seq_state
Definition: abc.cc:499
void handle_loops()
Definition: abc.cc:320
int size() const
Definition: rtlil.h:1019
#define log_abort()
Definition: log.h:84
bool in(T first, Args...rest)
Definition: rtlil.h:241
void select(T1 *module, T2 *member)
Definition: rtlil.h:559
tuple n
Definition: fsm/generate.py:59
int run_command(const std::string &command, std::function< void(const std::string &)> process_line)
Definition: yosys.cc:195
int in2
Definition: abc.cc:87
void apply(RTLIL::SigBit &bit) const
Definition: sigtools.h:383
SigMap assign_map
Definition: abc.cc:93
static std::string escape_id(std::string str)
Definition: rtlil.h:251
RTLIL::Design * abc_parse_blif(FILE *f, std::string dff_name)
Definition: blifparse.cc:52
gate_type_t type
Definition: abc.cc:86
#define ABC_COMMAND_LIB
Definition: abc.cc:32
#define ABC_COMMAND_CTR
Definition: abc.cc:33
void set(RTLIL::Module *module)
Definition: sigtools.h:273
void connect(const RTLIL::SigSig &conn)
Definition: rtlil.cc:1278
std::string proc_self_dirname()
#define G(_name)
Definition: abc.cc:81
#define PRIVATE_NAMESPACE_BEGIN
Definition: yosys.h:97
const RTLIL::SigSpec & getPort(RTLIL::IdString portname) const
Definition: rtlil.cc:1809
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
void abc_module(RTLIL::Design *design, RTLIL::Module *current_module, std::string script_file, std::string exe_file, std::string liberty_file, std::string constr_file, bool cleanup, int lut_mode, bool dff_mode, std::string clk_str, bool keepff, std::string delay_target, bool fast_mode)
Definition: abc.cc:545
#define log_assert(_assert_expr_)
Definition: log.h:85
void dump_loop_graph(FILE *f, int &nr, std::map< int, std::set< int >> &edges, std::set< int > &workpool, std::vector< int > &in_counts)
Definition: abc.cc:291
RTLIL::Wire * addWire(RTLIL::IdString name, int width=1)
Definition: rtlil.cc:1331
int in3
Definition: abc.cc:87
RTLIL::IdString name
Definition: rtlil.h:599
int map_signal(RTLIL::SigBit bit, gate_type_t gate_type=G(NONE), int in1=-1, int in2=-1, int in3=-1, int in4=-1)
Definition: abc.cc:101
std::vector< gate_t > signal_list
Definition: abc.cc:95
YOSYS_NAMESPACE_BEGIN int get_cell_cost(RTLIL::Cell *cell, std::map< RTLIL::Module *, int > *mod_cost_cache=nullptr)
Definition: cost.h:77
std::string make_temp_dir(std::string template_str)
Definition: yosys.cc:273
void next_char(char ch)
Definition: abc.cc:508
RTLIL::SigSpec clk_sig
Definition: abc.cc:99
virtual void execute(std::vector< std::string > args, RTLIL::Design *design)
Definition: abc.cc:1099
#define PRIVATE_NAMESPACE_END
Definition: yosys.h:98
Definition: register.h:27
RTLIL::IdString name
Definition: rtlil.h:825
void log_cmd_error(const char *format,...)
Definition: log.cc:211
bool check_file_exists(std::string filename, bool is_exec)
Definition: yosys.cc:302
#define ABC_FAST_COMMAND_LUT
Definition: abc.cc:39
std::string substr(size_t pos=0, size_t len=std::string::npos) const
Definition: rtlil.h:208
#define USING_YOSYS_NAMESPACE
Definition: yosys.h:102
std::map< RTLIL::SigBit, int > signal_map
Definition: abc.cc:96
std::string fold_abc_cmd(std::string str)
Definition: abc.cc:477
std::map< RTLIL::IdString, RTLIL::Module * > modules_
Definition: rtlil.h:507
Definition: abc.cc:997
void next_line(const std::string &line)
Definition: abc.cc:538
abc_output_filter()
Definition: abc.cc:502
#define NULL
std::map< RTLIL::IdString, RTLIL::Cell * > cells_
Definition: rtlil.h:596
AbcPass()
Definition: abc.cc:998
void remove(const std::set< RTLIL::Wire * > &wires)
Definition: rtlil.cc:1158
virtual void help()
Definition: abc.cc:999
void log(const char *format,...)
Definition: log.cc:180
#define ABC_FAST_COMMAND_CTR
Definition: abc.cc:38
std::string remap_name(RTLIL::IdString abc_name)
Definition: abc.cc:284
void log_push()
Definition: log.cc:232
void append(const RTLIL::SigSpec &signal)
Definition: rtlil.cc:2523
#define ABC_FAST_COMMAND_LIB
Definition: abc.cc:37
AstModule * current_module
Definition: ast.cc:62
void extra_args(std::vector< std::string > args, size_t argidx, RTLIL::Design *design, bool select=true)
Definition: register.cc:128
std::string linebuf
Definition: abc.cc:500
RTLIL::Wire * as_wire() const
Definition: rtlil.cc:2868
std::pair< SigSpec, SigSpec > SigSig
Definition: rtlil.h:71
const std::map< RTLIL::IdString, RTLIL::SigSpec > & connections() const
Definition: rtlil.cc:1814
YOSYS_NAMESPACE_BEGIN int autoidx
Definition: yosys.cc:51
#define ABC_COMMAND_DFL
Definition: abc.cc:35
#define ABC_COMMAND_LUT
Definition: abc.cc:34
int id
Definition: abc.cc:85
int in1
Definition: abc.cc:87
const std::vector< RTLIL::SigChunk > & chunks() const
Definition: rtlil.h:1016