yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ast.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  * This is the AST frontend library.
21  *
22  * The AST frontend library is not a frontend on it's own but provides a
23  * generic abstract syntax tree (AST) abstraction for HDL code and can be
24  * used by HDL frontends. See "ast.h" for an overview of the API and the
25  * Verilog frontend for an usage example.
26  *
27  */
28 
29 #include "kernel/log.h"
30 #include "libs/sha1/sha1.h"
31 #include "ast.h"
32 
33 #include <sstream>
34 #include <stdarg.h>
35 
36 #if defined(__APPLE__)
37 # include <cmath>
38 #else
39 # include <math.h>
40 #endif
41 
43 
44 using namespace AST;
45 using namespace AST_INTERNAL;
46 
47 // instanciate global variables (public API)
48 namespace AST {
49  std::string current_filename;
50  void (*set_line_num)(int) = NULL;
51  int (*get_line_num)() = NULL;
52 }
53 
54 // instanciate global variables (private API)
55 namespace AST_INTERNAL {
58  std::map<std::string, AstNode*> current_scope;
59  const std::map<RTLIL::SigBit, RTLIL::SigBit> *genRTLIL_subst_ptr = NULL;
63 }
64 
65 // convert node types to string
66 std::string AST::type2str(AstNodeType type)
67 {
68  switch (type)
69  {
70 #define X(_item) case _item: return #_item;
71  X(AST_NONE)
72  X(AST_DESIGN)
73  X(AST_MODULE)
74  X(AST_TASK)
77  X(AST_WIRE)
78  X(AST_MEMORY)
83  X(AST_PARASET)
85  X(AST_RANGE)
91  X(AST_PREFIX)
92  X(AST_ASSERT)
93  X(AST_FCALL)
94  X(AST_TO_BITS)
97  X(AST_CONCAT)
99  X(AST_BIT_NOT)
100  X(AST_BIT_AND)
101  X(AST_BIT_OR)
102  X(AST_BIT_XOR)
103  X(AST_BIT_XNOR)
113  X(AST_LT)
114  X(AST_LE)
115  X(AST_EQ)
116  X(AST_NE)
117  X(AST_EQX)
118  X(AST_NEX)
119  X(AST_GE)
120  X(AST_GT)
121  X(AST_ADD)
122  X(AST_SUB)
123  X(AST_MUL)
124  X(AST_DIV)
125  X(AST_MOD)
126  X(AST_POW)
127  X(AST_POS)
128  X(AST_NEG)
130  X(AST_LOGIC_OR)
132  X(AST_TERNARY)
133  X(AST_MEMRD)
134  X(AST_MEMWR)
135  X(AST_TCALL)
136  X(AST_ASSIGN)
137  X(AST_CELL)
140  X(AST_ALWAYS)
141  X(AST_INITIAL)
142  X(AST_BLOCK)
145  X(AST_CASE)
146  X(AST_COND)
147  X(AST_DEFAULT)
148  X(AST_FOR)
149  X(AST_WHILE)
150  X(AST_REPEAT)
151  X(AST_GENVAR)
152  X(AST_GENFOR)
153  X(AST_GENIF)
154  X(AST_GENCASE)
155  X(AST_GENBLOCK)
156  X(AST_POSEDGE)
157  X(AST_NEGEDGE)
158  X(AST_EDGE)
159 #undef X
160  default:
161  log_abort();
162  }
163 }
164 
165 // check if attribute exists and has non-zero value
167 {
168  if (attributes.count(id) == 0)
169  return false;
170 
171  AstNode *attr = attributes.at(id);
172  if (attr->type != AST_CONSTANT)
173  log_error("Attribute `%s' with non-constant value at %s:%d!\n",
174  id.c_str(), attr->filename.c_str(), attr->linenum);
175 
176  return attr->integer != 0;
177 }
178 
179 // create new node (AstNode constructor)
180 // (the optional child arguments make it easier to create AST trees)
182 {
183  this->type = type;
184  filename = current_filename;
185  linenum = get_line_num();
186  is_input = false;
187  is_output = false;
188  is_reg = false;
189  is_signed = false;
190  is_string = false;
191  range_valid = false;
192  range_swapped = false;
193  port_id = 0;
194  range_left = -1;
195  range_right = 0;
196  integer = 0;
197  realvalue = 0;
198  id2ast = NULL;
199  basic_prep = false;
200 
201  if (child1)
202  children.push_back(child1);
203  if (child2)
204  children.push_back(child2);
205 }
206 
207 // create a (deep recursive) copy of a node
209 {
210  AstNode *that = new AstNode;
211  *that = *this;
212  for (auto &it : that->children)
213  it = it->clone();
214  for (auto &it : that->attributes)
215  it.second = it.second->clone();
216  return that;
217 }
218 
219 // create a (deep recursive) copy of a node use 'other' as target root node
221 {
222  AstNode *tmp = clone();
223  other->delete_children();
224  *other = *tmp;
225  tmp->children.clear();
226  tmp->attributes.clear();
227  delete tmp;
228 }
229 
230 // delete all children in this node
232 {
233  for (auto &it : children)
234  delete it;
235  children.clear();
236 
237  for (auto &it : attributes)
238  delete it.second;
239  attributes.clear();
240 }
241 
242 // AstNode destructor
244 {
245  delete_children();
246 }
247 
248 // create a nice text representation of the node
249 // (traverse tree by recursion, use 'other' pointer for diffing two AST trees)
250 void AstNode::dumpAst(FILE *f, std::string indent)
251 {
252  if (f == NULL) {
253  for (auto f : log_files)
254  dumpAst(f, indent);
255  return;
256  }
257 
258  std::string type_name = type2str(type);
259  fprintf(f, "%s%s <%s:%d>", indent.c_str(), type_name.c_str(), filename.c_str(), linenum);
260 
261  if (id2ast)
262  fprintf(f, " [%p -> %p]", this, id2ast);
263  else
264  fprintf(f, " [%p]", this);
265 
266  if (!str.empty())
267  fprintf(f, " str='%s'", str.c_str());
268  if (!bits.empty()) {
269  fprintf(f, " bits='");
270  for (size_t i = bits.size(); i > 0; i--)
271  fprintf(f, "%c", bits[i-1] == RTLIL::S0 ? '0' :
272  bits[i-1] == RTLIL::S1 ? '1' :
273  bits[i-1] == RTLIL::Sx ? 'x' :
274  bits[i-1] == RTLIL::Sz ? 'z' : '?');
275  fprintf(f, "'(%d)", GetSize(bits));
276  }
277  if (is_input)
278  fprintf(f, " input");
279  if (is_output)
280  fprintf(f, " output");
281  if (is_reg)
282  fprintf(f, " reg");
283  if (is_signed)
284  fprintf(f, " signed");
285  if (port_id > 0)
286  fprintf(f, " port=%d", port_id);
287  if (range_valid || range_left != -1 || range_right != 0)
288  fprintf(f, " %srange=[%d:%d]%s", range_swapped ? "swapped_" : "", range_left, range_right, range_valid ? "" : "!");
289  if (integer != 0)
290  fprintf(f, " int=%u", (int)integer);
291  if (realvalue != 0)
292  fprintf(f, " real=%e", realvalue);
293  if (!multirange_dimensions.empty()) {
294  fprintf(f, " multirange=[");
295  for (int v : multirange_dimensions)
296  fprintf(f, " %d", v);
297  fprintf(f, " ]");
298  }
299  fprintf(f, "\n");
300 
301  for (auto &it : attributes) {
302  fprintf(f, "%s ATTR %s:\n", indent.c_str(), it.first.c_str());
303  it.second->dumpAst(f, indent + " ");
304  }
305 
306  for (size_t i = 0; i < children.size(); i++)
307  children[i]->dumpAst(f, indent + " ");
308 }
309 
310 // helper function for AstNode::dumpVlog()
311 static std::string id2vl(std::string txt)
312 {
313  if (txt.size() > 1 && txt[0] == '\\')
314  txt = txt.substr(1);
315  for (size_t i = 0; i < txt.size(); i++) {
316  if ('A' <= txt[i] && txt[i] <= 'Z') continue;
317  if ('a' <= txt[i] && txt[i] <= 'z') continue;
318  if ('0' <= txt[i] && txt[i] <= '9') continue;
319  if (txt[i] == '_') continue;
320  txt = "\\" + txt + " ";
321  break;
322  }
323  return txt;
324 }
325 
326 // dump AST node as verilog pseudo-code
327 void AstNode::dumpVlog(FILE *f, std::string indent)
328 {
329  bool first = true;
330  std::string txt;
331  std::vector<AstNode*> rem_children1, rem_children2;
332 
333  if (f == NULL) {
334  for (auto f : log_files)
335  dumpVlog(f, indent);
336  return;
337  }
338 
339  for (auto &it : attributes) {
340  fprintf(f, "%s" "(* %s = ", indent.c_str(), id2vl(it.first.str()).c_str());
341  it.second->dumpVlog(f, "");
342  fprintf(f, " *)%s", indent.empty() ? "" : "\n");
343  }
344 
345  switch (type)
346  {
347  case AST_MODULE:
348  fprintf(f, "%s" "module %s(", indent.c_str(), id2vl(str).c_str());
349  for (auto child : children)
350  if (child->type == AST_WIRE && (child->is_input || child->is_output)) {
351  fprintf(f, "%s%s", first ? "" : ", ", id2vl(child->str).c_str());
352  first = false;
353  }
354  fprintf(f, ");\n");
355 
356  for (auto child : children)
357  if (child->type == AST_PARAMETER || child->type == AST_LOCALPARAM || child->type == AST_DEFPARAM)
358  child->dumpVlog(f, indent + " ");
359  else
360  rem_children1.push_back(child);
361 
362  for (auto child : rem_children1)
363  if (child->type == AST_WIRE || child->type == AST_AUTOWIRE || child->type == AST_MEMORY)
364  child->dumpVlog(f, indent + " ");
365  else
366  rem_children2.push_back(child);
367  rem_children1.clear();
368 
369  for (auto child : rem_children2)
370  if (child->type == AST_TASK || child->type == AST_FUNCTION)
371  child->dumpVlog(f, indent + " ");
372  else
373  rem_children1.push_back(child);
374  rem_children2.clear();
375 
376  for (auto child : rem_children1)
377  child->dumpVlog(f, indent + " ");
378  rem_children1.clear();
379 
380  fprintf(f, "%s" "endmodule\n", indent.c_str());
381  break;
382 
383  case AST_WIRE:
384  if (is_input && is_output)
385  fprintf(f, "%s" "inout", indent.c_str());
386  else if (is_input)
387  fprintf(f, "%s" "input", indent.c_str());
388  else if (is_output)
389  fprintf(f, "%s" "output", indent.c_str());
390  else if (!is_reg)
391  fprintf(f, "%s" "wire", indent.c_str());
392  if (is_reg)
393  fprintf(f, "%s" "reg", (is_input || is_output) ? " " : indent.c_str());
394  if (is_signed)
395  fprintf(f, " signed");
396  for (auto child : children) {
397  fprintf(f, " ");
398  child->dumpVlog(f, "");
399  }
400  fprintf(f, " %s", id2vl(str).c_str());
401  fprintf(f, ";\n");
402  break;
403 
404  case AST_MEMORY:
405  fprintf(f, "%s" "memory", indent.c_str());
406  if (is_signed)
407  fprintf(f, " signed");
408  for (auto child : children) {
409  fprintf(f, " ");
410  child->dumpVlog(f, "");
411  if (first)
412  fprintf(f, " %s", id2vl(str).c_str());
413  first = false;
414  }
415  fprintf(f, ";\n");
416  break;
417 
418  case AST_RANGE:
419  if (range_valid)
420  fprintf(f, "[%d:%d]", range_left, range_right);
421  else {
422  for (auto child : children) {
423  fprintf(f, "%c", first ? '[' : ':');
424  child->dumpVlog(f, "");
425  first = false;
426  }
427  fprintf(f, "]");
428  }
429  break;
430 
431  case AST_ALWAYS:
432  fprintf(f, "%s" "always @(", indent.c_str());
433  for (auto child : children) {
434  if (child->type != AST_POSEDGE && child->type != AST_NEGEDGE && child->type != AST_EDGE)
435  continue;
436  if (!first)
437  fprintf(f, ", ");
438  child->dumpVlog(f, "");
439  first = false;
440  }
441  fprintf(f, ")\n");
442  for (auto child : children) {
443  if (child->type != AST_POSEDGE && child->type != AST_NEGEDGE && child->type != AST_EDGE)
444  child->dumpVlog(f, indent + " ");
445  }
446  break;
447 
448  case AST_INITIAL:
449  fprintf(f, "%s" "initial\n", indent.c_str());
450  for (auto child : children) {
451  if (child->type != AST_POSEDGE && child->type != AST_NEGEDGE && child->type != AST_EDGE)
452  child->dumpVlog(f, indent + " ");
453  }
454  break;
455 
456  case AST_POSEDGE:
457  case AST_NEGEDGE:
458  case AST_EDGE:
459  if (type == AST_POSEDGE)
460  fprintf(f, "posedge ");
461  if (type == AST_NEGEDGE)
462  fprintf(f, "negedge ");
463  for (auto child : children)
464  child->dumpVlog(f, "");
465  break;
466 
467  case AST_IDENTIFIER:
468  fprintf(f, "%s", id2vl(str).c_str());
469  for (auto child : children)
470  child->dumpVlog(f, "");
471  break;
472 
473  case AST_CONSTANT:
474  if (!str.empty())
475  fprintf(f, "\"%s\"", str.c_str());
476  else if (bits.size() == 32)
477  fprintf(f, "%d", RTLIL::Const(bits).as_int());
478  else
479  fprintf(f, "%d'b %s", GetSize(bits), RTLIL::Const(bits).as_string().c_str());
480  break;
481 
482  case AST_REALVALUE:
483  fprintf(f, "%e", realvalue);
484  break;
485 
486  case AST_BLOCK:
487  if (children.size() == 1) {
488  children[0]->dumpVlog(f, indent);
489  } else {
490  fprintf(f, "%s" "begin\n", indent.c_str());
491  for (auto child : children)
492  child->dumpVlog(f, indent + " ");
493  fprintf(f, "%s" "end\n", indent.c_str());
494  }
495  break;
496 
497  case AST_CASE:
498  fprintf(f, "%s" "case (", indent.c_str());
499  children[0]->dumpVlog(f, "");
500  fprintf(f, ")\n");
501  for (size_t i = 1; i < children.size(); i++) {
502  AstNode *child = children[i];
503  child->dumpVlog(f, indent + " ");
504  }
505  fprintf(f, "%s" "endcase\n", indent.c_str());
506  break;
507 
508  case AST_COND:
509  for (auto child : children) {
510  if (child->type == AST_BLOCK) {
511  fprintf(f, ":\n");
512  child->dumpVlog(f, indent + " ");
513  first = true;
514  } else {
515  fprintf(f, "%s", first ? indent.c_str() : ", ");
516  if (child->type == AST_DEFAULT)
517  fprintf(f, "default");
518  else
519  child->dumpVlog(f, "");
520  first = false;
521  }
522  }
523  break;
524 
525  case AST_ASSIGN_EQ:
526  case AST_ASSIGN_LE:
527  fprintf(f, "%s", indent.c_str());
528  children[0]->dumpVlog(f, "");
529  fprintf(f, " %s ", type == AST_ASSIGN_EQ ? "=" : "<=");
530  children[1]->dumpVlog(f, "");
531  fprintf(f, ";\n");
532  break;
533 
534  case AST_CONCAT:
535  fprintf(f, "{");
536  for (auto child : children) {
537  if (!first)
538  fprintf(f, ", ");
539  child->dumpVlog(f, "");
540  first = false;
541  }
542  fprintf(f, "}");
543  break;
544 
545  case AST_REPLICATE:
546  fprintf(f, "{");
547  children[0]->dumpVlog(f, "");
548  fprintf(f, "{");
549  children[1]->dumpVlog(f, "");
550  fprintf(f, "}}");
551  break;
552 
553  if (0) { case AST_BIT_NOT: txt = "~"; }
554  if (0) { case AST_REDUCE_AND: txt = "&"; }
555  if (0) { case AST_REDUCE_OR: txt = "|"; }
556  if (0) { case AST_REDUCE_XOR: txt = "^"; }
557  if (0) { case AST_REDUCE_XNOR: txt = "~^"; }
558  if (0) { case AST_REDUCE_BOOL: txt = "|"; }
559  if (0) { case AST_POS: txt = "+"; }
560  if (0) { case AST_NEG: txt = "-"; }
561  if (0) { case AST_LOGIC_NOT: txt = "!"; }
562  fprintf(f, "%s(", txt.c_str());
563  children[0]->dumpVlog(f, "");
564  fprintf(f, ")");
565  break;
566 
567  if (0) { case AST_BIT_AND: txt = "&"; }
568  if (0) { case AST_BIT_OR: txt = "|"; }
569  if (0) { case AST_BIT_XOR: txt = "^"; }
570  if (0) { case AST_BIT_XNOR: txt = "~^"; }
571  if (0) { case AST_SHIFT_LEFT: txt = "<<"; }
572  if (0) { case AST_SHIFT_RIGHT: txt = ">>"; }
573  if (0) { case AST_SHIFT_SLEFT: txt = "<<<"; }
574  if (0) { case AST_SHIFT_SRIGHT: txt = ">>>"; }
575  if (0) { case AST_LT: txt = "<"; }
576  if (0) { case AST_LE: txt = "<="; }
577  if (0) { case AST_EQ: txt = "=="; }
578  if (0) { case AST_NE: txt = "!="; }
579  if (0) { case AST_EQX: txt = "==="; }
580  if (0) { case AST_NEX: txt = "!=="; }
581  if (0) { case AST_GE: txt = ">="; }
582  if (0) { case AST_GT: txt = ">"; }
583  if (0) { case AST_ADD: txt = "+"; }
584  if (0) { case AST_SUB: txt = "-"; }
585  if (0) { case AST_MUL: txt = "*"; }
586  if (0) { case AST_DIV: txt = "/"; }
587  if (0) { case AST_MOD: txt = "%"; }
588  if (0) { case AST_POW: txt = "**"; }
589  if (0) { case AST_LOGIC_AND: txt = "&&"; }
590  if (0) { case AST_LOGIC_OR: txt = "||"; }
591  fprintf(f, "(");
592  children[0]->dumpVlog(f, "");
593  fprintf(f, ")%s(", txt.c_str());
594  children[1]->dumpVlog(f, "");
595  fprintf(f, ")");
596  break;
597 
598  case AST_TERNARY:
599  fprintf(f, "(");
600  children[0]->dumpVlog(f, "");
601  fprintf(f, ") ? (");
602  children[1]->dumpVlog(f, "");
603  fprintf(f, ") : (");
604  children[2]->dumpVlog(f, "");
605  fprintf(f, ")");
606  break;
607 
608  default:
609  std::string type_name = type2str(type);
610  fprintf(f, "%s" "/** %s **/%s", indent.c_str(), type_name.c_str(), indent.empty() ? "" : "\n");
611  // dumpAst(f, indent, NULL);
612  }
613 }
614 
615 // check if two AST nodes are identical
616 bool AstNode::operator==(const AstNode &other) const
617 {
618  if (type != other.type)
619  return false;
620  if (children.size() != other.children.size())
621  return false;
622  if (str != other.str)
623  return false;
624  if (bits != other.bits)
625  return false;
626  if (is_input != other.is_input)
627  return false;
628  if (is_output != other.is_output)
629  return false;
630  if (is_reg != other.is_reg)
631  return false;
632  if (is_signed != other.is_signed)
633  return false;
634  if (is_string != other.is_string)
635  return false;
636  if (range_valid != other.range_valid)
637  return false;
638  if (range_swapped != other.range_swapped)
639  return false;
640  if (port_id != other.port_id)
641  return false;
642  if (range_left != other.range_left)
643  return false;
644  if (range_right != other.range_right)
645  return false;
646  if (integer != other.integer)
647  return false;
648  for (size_t i = 0; i < children.size(); i++)
649  if (*children[i] != *other.children[i])
650  return false;
651  return true;
652 }
653 
654 // check if two AST nodes are not identical
655 bool AstNode::operator!=(const AstNode &other) const
656 {
657  return !(*this == other);
658 }
659 
660 // check if this AST contains the given node
661 bool AstNode::contains(const AstNode *other) const
662 {
663  if (this == other)
664  return true;
665  for (auto child : children)
666  if (child->contains(other))
667  return true;
668  return false;
669 }
670 
671 // create an AST node for a constant (using a 32 bit int as value)
672 AstNode *AstNode::mkconst_int(uint32_t v, bool is_signed, int width)
673 {
674  AstNode *node = new AstNode(AST_CONSTANT);
675  node->integer = v;
676  node->is_signed = is_signed;
677  for (int i = 0; i < width; i++) {
678  node->bits.push_back((v & 1) ? RTLIL::S1 : RTLIL::S0);
679  v = v >> 1;
680  }
681  node->range_valid = true;
682  node->range_left = width-1;
683  node->range_right = 0;
684  return node;
685 }
686 
687 // create an AST node for a constant (using a bit vector as value)
688 AstNode *AstNode::mkconst_bits(const std::vector<RTLIL::State> &v, bool is_signed)
689 {
690  AstNode *node = new AstNode(AST_CONSTANT);
691  node->is_signed = is_signed;
692  node->bits = v;
693  for (size_t i = 0; i < 32; i++) {
694  if (i < node->bits.size())
695  node->integer |= (node->bits[i] == RTLIL::S1) << i;
696  else if (is_signed)
697  node->integer |= (node->bits.back() == RTLIL::S1) << i;
698  }
699  node->range_valid = true;
700  node->range_left = node->bits.size()-1;
701  node->range_right = 0;
702  return node;
703 }
704 
705 // create an AST node for a constant (using a string in bit vector form as value)
706 AstNode *AstNode::mkconst_str(const std::vector<RTLIL::State> &v)
707 {
708  AstNode *node = mkconst_str(RTLIL::Const(v).decode_string());
709  while (GetSize(node->bits) < GetSize(v))
710  node->bits.push_back(RTLIL::State::S0);
711  log_assert(node->bits == v);
712  return node;
713 }
714 
715 // create an AST node for a constant (using a string as value)
716 AstNode *AstNode::mkconst_str(const std::string &str)
717 {
718  std::vector<RTLIL::State> data;
719  data.reserve(str.size() * 8);
720  for (size_t i = 0; i < str.size(); i++) {
721  unsigned char ch = str[str.size() - i - 1];
722  for (int j = 0; j < 8; j++) {
723  data.push_back((ch & 1) ? RTLIL::S1 : RTLIL::S0);
724  ch = ch >> 1;
725  }
726  }
727  AstNode *node = AstNode::mkconst_bits(data, false);
728  node->is_string = true;
729  node->str = str;
730  return node;
731 }
732 
734 {
735  for (auto bit : bits)
736  if (bit != RTLIL::S0 && bit != RTLIL::S1)
737  return false;
738  return true;
739 }
740 
741 RTLIL::Const AstNode::bitsAsConst(int width, bool is_signed)
742 {
743  std::vector<RTLIL::State> bits = this->bits;
744  if (width >= 0 && width < int(bits.size()))
745  bits.resize(width);
746  if (width >= 0 && width > int(bits.size())) {
748  if (is_signed && !bits.empty())
749  extbit = bits.back();
750  while (width > int(bits.size()))
751  bits.push_back(extbit);
752  }
753  return RTLIL::Const(bits);
754 }
755 
757 {
758  return bitsAsConst(width, is_signed);
759 }
760 
762 {
763  log_assert(type == AST_CONSTANT);
764 
765  RTLIL::Const val;
766  val.bits = bits;
767 
768  if (is_string) {
770  log_assert(val.decode_string() == str);
771  }
772 
773  return val;
774 }
775 
777 {
778  RTLIL::Const val = asAttrConst();
779  if (is_signed)
781  return val;
782 }
783 
785 {
786  log_assert(type == AST_CONSTANT);
787  for (auto &bit : bits)
788  if (bit == RTLIL::State::S1)
789  return true;
790  return false;
791 }
792 
794 {
795  if (type == AST_CONSTANT)
796  return 1;
797  if (type == AST_REALVALUE)
798  return 2;
799  return 0;
800 }
801 
802 uint64_t AstNode::asInt(bool is_signed)
803 {
804  if (type == AST_CONSTANT)
805  {
806  RTLIL::Const v = bitsAsConst(64, is_signed);
807  uint64_t ret = 0;
808 
809  for (int i = 0; i < 64; i++)
810  if (v.bits.at(i) == RTLIL::State::S1)
811  ret |= uint64_t(1) << i;
812 
813  return ret;
814  }
815 
816  if (type == AST_REALVALUE)
817  return realvalue;
818 
819  log_abort();
820 }
821 
822 double AstNode::asReal(bool is_signed)
823 {
824  if (type == AST_CONSTANT)
825  {
826  RTLIL::Const val(bits);
827 
828  bool is_negative = is_signed && val.bits.back() == RTLIL::State::S1;
829  if (is_negative)
830  val = const_neg(val, val, false, false, val.bits.size());
831 
832  double v = 0;
833  for (size_t i = 0; i < val.bits.size(); i++)
834  // IEEE Std 1800-2012 Par 6.12.2: Individual bits that are x or z in
835  // the net or the variable shall be treated as zero upon conversion.
836  if (val.bits.at(i) == RTLIL::State::S1)
837  v += exp2(i);
838  if (is_negative)
839  v *= -1;
840 
841  return v;
842  }
843 
844  if (type == AST_REALVALUE)
845  return realvalue;
846 
847  log_abort();
848 }
849 
851 {
852  double v = round(realvalue);
853  RTLIL::Const result;
854 #ifdef EMSCRIPTEN
855  if (!isfinite(v)) {
856 #else
857  if (!std::isfinite(v)) {
858 #endif
859  result.bits = std::vector<RTLIL::State>(width, RTLIL::State::Sx);
860  } else {
861  bool is_negative = v < 0;
862  if (is_negative)
863  v *= -1;
864  for (int i = 0; i < width; i++, v /= 2)
865  result.bits.push_back((fmod(floor(v), 2) != 0) ? RTLIL::State::S1 : RTLIL::State::S0);
866  if (is_negative)
867  result = const_neg(result, result, false, false, result.bits.size());
868  }
869  return result;
870 }
871 
872 // create a new AstModule from an AST_MODULE AST node
873 static AstModule* process_module(AstNode *ast, bool defer)
874 {
875  log_assert(ast->type == AST_MODULE);
876 
877  if (defer)
878  log("Storing AST representation for module `%s'.\n", ast->str.c_str());
879  else
880  log("Generating RTLIL representation for module `%s'.\n", ast->str.c_str());
881 
883  current_module->ast = NULL;
884  current_module->name = ast->str;
885  current_module->attributes["\\src"] = stringf("%s:%d", ast->filename.c_str(), ast->linenum);
886 
887  current_ast_mod = ast;
888  AstNode *ast_before_simplify = ast->clone();
889 
890  if (flag_dump_ast1) {
891  log("Dumping verilog AST before simplification:\n");
892  ast->dumpAst(NULL, " ");
893  log("--- END OF AST DUMP ---\n");
894  }
895 
896  if (!defer)
897  {
898  while (ast->simplify(!flag_noopt, false, false, 0, -1, false, false)) { }
899 
900  if (flag_dump_ast2) {
901  log("Dumping verilog AST after simplification:\n");
902  ast->dumpAst(NULL, " ");
903  log("--- END OF AST DUMP ---\n");
904  }
905 
906  if (flag_dump_vlog) {
907  log("Dumping verilog AST (as requested by dump_vlog option):\n");
908  ast->dumpVlog(NULL, " ");
909  log("--- END OF AST DUMP ---\n");
910  }
911 
912  if (flag_lib) {
913  std::vector<AstNode*> new_children;
914  for (auto child : ast->children) {
915  if (child->type == AST_WIRE && (child->is_input || child->is_output))
916  new_children.push_back(child);
917  else
918  delete child;
919  }
920  ast->children.swap(new_children);
921  ast->attributes["\\blackbox"] = AstNode::mkconst_int(1, false);
922  }
923 
925 
926  for (auto &attr : ast->attributes) {
927  if (attr.second->type != AST_CONSTANT)
928  log_error("Attribute `%s' with non-constant value at %s:%d!\n",
929  attr.first.c_str(), ast->filename.c_str(), ast->linenum);
930  current_module->attributes[attr.first] = attr.second->asAttrConst();
931  }
932  for (size_t i = 0; i < ast->children.size(); i++) {
933  AstNode *node = ast->children[i];
934  if (node->type == AST_WIRE || node->type == AST_MEMORY)
935  node->genRTLIL();
936  }
937  for (size_t i = 0; i < ast->children.size(); i++) {
938  AstNode *node = ast->children[i];
939  if (node->type != AST_WIRE && node->type != AST_MEMORY && node->type != AST_INITIAL)
940  node->genRTLIL();
941  }
942 
944 
945  for (size_t i = 0; i < ast->children.size(); i++) {
946  AstNode *node = ast->children[i];
947  if (node->type == AST_INITIAL)
948  node->genRTLIL();
949  }
950 
952  }
953 
954  current_module->ast = ast_before_simplify;
955  current_module->nolatches = flag_nolatches;
956  current_module->nomem2reg = flag_nomem2reg;
957  current_module->mem2reg = flag_mem2reg;
958  current_module->lib = flag_lib;
959  current_module->noopt = flag_noopt;
960  current_module->icells = flag_icells;
961  current_module->autowire = flag_autowire;
962  current_module->fixup_ports();
963  return current_module;
964 }
965 
966 // create AstModule instances for all modules in the AST tree and add them to 'design'
967 void AST::process(RTLIL::Design *design, AstNode *ast, bool dump_ast1, bool dump_ast2, bool dump_vlog, bool nolatches, bool nomem2reg, bool mem2reg, bool lib, bool noopt, bool icells, bool ignore_redef, bool defer, bool autowire)
968 {
969  current_ast = ast;
970  flag_dump_ast1 = dump_ast1;
971  flag_dump_ast2 = dump_ast2;
972  flag_dump_vlog = dump_vlog;
973  flag_nolatches = nolatches;
974  flag_nomem2reg = nomem2reg;
975  flag_mem2reg = mem2reg;
976  flag_lib = lib;
977  flag_noopt = noopt;
978  flag_icells = icells;
979  flag_autowire = autowire;
980 
981  std::vector<AstNode*> global_decls;
982 
984  for (auto it = current_ast->children.begin(); it != current_ast->children.end(); it++)
985  {
986  if ((*it)->type == AST_MODULE)
987  {
988  for (auto n : global_decls)
989  (*it)->children.push_back(n->clone());
990 
991  if (flag_icells && (*it)->str.substr(0, 2) == "\\$")
992  (*it)->str = (*it)->str.substr(1);
993 
994  if (defer)
995  (*it)->str = "$abstract" + (*it)->str;
996 
997  if (design->has((*it)->str)) {
998  if (!ignore_redef)
999  log_error("Re-definition of module `%s' at %s:%d!\n",
1000  (*it)->str.c_str(), (*it)->filename.c_str(), (*it)->linenum);
1001  log("Ignoring re-definition of module `%s' at %s:%d!\n",
1002  (*it)->str.c_str(), (*it)->filename.c_str(), (*it)->linenum);
1003  continue;
1004  }
1005 
1006  design->add(process_module(*it, defer));
1007  }
1008  else
1009  global_decls.push_back(*it);
1010  }
1011 }
1012 
1013 // AstModule destructor
1015 {
1016  if (ast != NULL)
1017  delete ast;
1018 }
1019 
1020 // create a new parametric module (when needed) and return the name of the generated module
1021 RTLIL::IdString AstModule::derive(RTLIL::Design *design, std::map<RTLIL::IdString, RTLIL::Const> parameters)
1022 {
1023  std::string stripped_name = name.str();
1024 
1025  if (stripped_name.substr(0, 9) == "$abstract")
1026  stripped_name = stripped_name.substr(9);
1027 
1028  log_header("Executing AST frontend in derive mode using pre-parsed AST for module `%s'.\n", stripped_name.c_str());
1029 
1030  current_ast = NULL;
1031  flag_dump_ast1 = false;
1032  flag_dump_ast2 = false;
1033  flag_dump_vlog = false;
1034  flag_nolatches = nolatches;
1035  flag_nomem2reg = nomem2reg;
1036  flag_mem2reg = mem2reg;
1037  flag_lib = lib;
1038  flag_noopt = noopt;
1039  flag_icells = icells;
1040  flag_autowire = autowire;
1042 
1043  std::string para_info;
1044  AstNode *new_ast = ast->clone();
1045 
1046  int para_counter = 0;
1047  int orig_parameters_n = parameters.size();
1048  for (auto it = new_ast->children.begin(); it != new_ast->children.end(); it++) {
1049  AstNode *child = *it;
1050  if (child->type != AST_PARAMETER)
1051  continue;
1052  para_counter++;
1053  std::string para_id = child->str;
1054  if (parameters.count(para_id) > 0) {
1055  log("Parameter %s = %s\n", child->str.c_str(), log_signal(RTLIL::SigSpec(parameters[child->str])));
1056  rewrite_parameter:
1057  para_info += stringf("%s=%s", child->str.c_str(), log_signal(RTLIL::SigSpec(parameters[para_id])));
1058  delete child->children.at(0);
1059  child->children[0] = AstNode::mkconst_bits(parameters[para_id].bits, (parameters[para_id].flags & RTLIL::CONST_FLAG_SIGNED) != 0);
1060  parameters.erase(para_id);
1061  continue;
1062  }
1063  para_id = stringf("$%d", para_counter);
1064  if (parameters.count(para_id) > 0) {
1065  log("Parameter %d (%s) = %s\n", para_counter, child->str.c_str(), log_signal(RTLIL::SigSpec(parameters[para_id])));
1066  goto rewrite_parameter;
1067  }
1068  }
1069  if (parameters.size() > 0)
1070  log_error("Requested parameter `%s' does not exist in module `%s'!\n", parameters.begin()->first.c_str(), stripped_name.c_str());
1071 
1072  std::string modname;
1073 
1074  if (orig_parameters_n == 0)
1075  modname = stripped_name;
1076  else if (para_info.size() > 60)
1077  modname = "$paramod$" + sha1(para_info) + stripped_name;
1078  else
1079  modname = "$paramod" + stripped_name + para_info;
1080 
1081  if (!design->has(modname)) {
1082  new_ast->str = modname;
1083  design->add(process_module(new_ast, false));
1084  design->module(modname)->check();
1085  } else {
1086  log("Found cached RTLIL representation for module `%s'.\n", modname.c_str());
1087  }
1088 
1089  delete new_ast;
1090  return modname;
1091 }
1092 
1094 {
1095  AstModule *new_mod = new AstModule;
1096  new_mod->name = name;
1097  cloneInto(new_mod);
1098 
1099  new_mod->ast = ast->clone();
1100  new_mod->nolatches = nolatches;
1101  new_mod->nomem2reg = nomem2reg;
1102  new_mod->mem2reg = mem2reg;
1103  new_mod->lib = lib;
1104  new_mod->noopt = noopt;
1105  new_mod->icells = icells;
1106  new_mod->autowire = autowire;
1107 
1108  return new_mod;
1109 }
1110 
1111 // internal dummy line number callbacks
1112 namespace {
1113  int internal_line_num;
1114  void internal_set_line_num(int n) {
1115  internal_line_num = n;
1116  }
1117  int internal_get_line_num() {
1118  return internal_line_num;
1119  }
1120 }
1121 
1122 // use internal dummy line number callbacks
1124 {
1125  set_line_num = &internal_set_line_num;
1126  get_line_num = &internal_get_line_num;
1127 }
1128 
1130 
std::map< std::string, AstNode * > current_scope
Definition: ast.cc:58
RTLIL::Const bitsAsConst(int width, bool is_signed)
Definition: ast.cc:741
bool flag_lib
Definition: ast.cc:56
bool range_valid
Definition: ast.h:158
std::string str() const
Definition: rtlil.h:182
static AstNode * mkconst_int(uint32_t v, bool is_signed, int width=32)
Definition: ast.cc:672
std::string stringf(const char *fmt,...)
Definition: yosys.cc:58
static AstModule * process_module(AstNode *ast, bool defer)
Definition: ast.cc:873
AstNode * current_top_block
Definition: ast.cc:61
void add(RTLIL::Module *module)
Definition: rtlil.cc:259
virtual RTLIL::IdString derive(RTLIL::Design *design, std::map< RTLIL::IdString, RTLIL::Const > parameters)
Definition: ast.cc:1021
int flags
Definition: rtlil.h:437
static AstNode * mkconst_bits(const std::vector< RTLIL::State > &v, bool is_signed)
Definition: ast.cc:688
void log_header(const char *format,...)
Definition: log.cc:188
AstNode * current_block
Definition: ast.cc:61
static AstNode * mkconst_str(const std::vector< RTLIL::State > &v)
Definition: ast.cc:706
RTLIL::Const asParaConst()
Definition: ast.cc:776
#define YOSYS_NAMESPACE_END
Definition: yosys.h:100
void dumpAst(FILE *f, std::string indent)
Definition: ast.cc:250
const char * log_signal(const RTLIL::SigSpec &sig, bool autoint)
Definition: log.cc:269
#define X(_item)
virtual RTLIL::Module * clone() const
Definition: ast.cc:1093
RTLIL::SigSpec genRTLIL(int width_hint=-1, bool sign_hint=false)
Definition: genrtlil.cc:762
void(* set_line_num)(int)
Definition: ast.cc:50
std::map< RTLIL::IdString, AstNode * > attributes
Definition: ast.h:152
void dumpVlog(FILE *f, std::string indent)
Definition: ast.cc:327
bool flag_dump_ast1
Definition: ast.cc:56
void log_error(const char *format,...)
Definition: log.cc:204
bool bits_only_01()
Definition: ast.cc:733
#define log_abort()
Definition: log.h:84
AstNode * current_ast
Definition: ast.cc:57
virtual void check()
Definition: rtlil.cc:948
tuple n
Definition: fsm/generate.py:59
bool get_bool_attribute(RTLIL::IdString id)
Definition: ast.cc:166
bool range_swapped
Definition: ast.h:158
std::string type2str(AstNodeType type)
Definition: ast.cc:66
bool operator==(const AstNode &other) const
Definition: ast.cc:616
bool nolatches
Definition: ast.h:269
void cloneInto(AstNode *other)
Definition: ast.cc:220
RTLIL::SigSpec ignoreThisSignalsInInitial
Definition: ast.cc:60
bool mem2reg
Definition: ast.h:269
YOSYS_NAMESPACE_BEGIN std::vector< FILE * > log_files
Definition: log.cc:37
double asReal(bool is_signed)
Definition: ast.cc:822
int(* get_line_num)()
Definition: ast.cc:51
int range_right
Definition: ast.h:159
bool flag_autowire
Definition: ast.cc:56
AstNodeType
Definition: ast.h:42
bool is_reg
Definition: ast.h:158
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
AstNode(AstNodeType type=AST_NONE, AstNode *child1=NULL, AstNode *child2=NULL)
Definition: ast.cc:181
std::string decode_string() const
Definition: rtlil.cc:131
#define log_assert(_assert_expr_)
Definition: log.h:85
RTLIL::IdString name
Definition: rtlil.h:599
bool flag_dump_ast2
Definition: ast.cc:56
AstNode * current_block_child
Definition: ast.cc:61
virtual ~AstModule()
Definition: ast.cc:1014
bool flag_noopt
Definition: ast.cc:56
static std::string id2vl(std::string txt)
Definition: ast.cc:311
AstNodeType type
Definition: ast.h:146
bool autowire
Definition: ast.h:269
uint32_t integer
Definition: ast.h:160
int as_int(bool is_signed=false) const
Definition: rtlil.cc:104
int range_left
Definition: ast.h:159
RTLIL::Module * module(RTLIL::IdString name)
Definition: rtlil.cc:254
bool has(RTLIL::IdString id) const
Definition: rtlil.h:519
AstNode * ast
Definition: ast.h:268
bool contains(const AstNode *other) const
Definition: ast.cc:661
void sort_and_unify()
Definition: rtlil.cc:2291
std::string current_filename
Definition: ast.cc:49
AstNode * clone()
Definition: ast.cc:208
#define NULL
#define YOSYS_NAMESPACE_BEGIN
Definition: yosys.h:99
bool simplify(bool const_fold, bool at_zero, bool in_lvalue, int stage, int width_hint, bool sign_hint, bool in_param)
Definition: simplify.cc:50
bool nomem2reg
Definition: ast.h:269
std::string filename
Definition: ast.h:175
bool icells
Definition: ast.h:269
void use_internal_line_num()
Definition: ast.cc:1123
AstNode * current_ast_mod
Definition: ast.cc:57
void log(const char *format,...)
Definition: log.cc:180
std::string sha1(const std::string &string)
Definition: sha1.cpp:270
std::vector< RTLIL::State > bits
Definition: rtlil.h:438
bool is_signed
Definition: ast.h:158
RTLIL::Const const_neg(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len)
Definition: calc.cc:570
State
Definition: rtlil.h:29
std::string str
Definition: ast.h:156
std::vector< AstNode * > children
Definition: ast.h:149
bool flag_nolatches
Definition: ast.cc:56
bool flag_mem2reg
Definition: ast.cc:56
std::vector< RTLIL::State > bits
Definition: ast.h:157
bool noopt
Definition: ast.h:269
void process(RTLIL::Design *design, AstNode *ast, bool dump_ast1, bool dump_ast2, bool dump_vlog, bool nolatches, bool nomem2reg, bool mem2reg, bool lib, bool noopt, bool icells, bool ignore_redef, bool defer, bool autowire)
Definition: ast.cc:967
bool is_string
Definition: ast.h:158
int linenum
Definition: ast.h:176
const std::map< RTLIL::SigBit, RTLIL::SigBit > * genRTLIL_subst_ptr
Definition: ast.cc:59
AstModule * current_module
Definition: ast.cc:62
~AstNode()
Definition: ast.cc:243
uint64_t asInt(bool is_signed)
Definition: ast.cc:802
bool flag_icells
Definition: ast.cc:56
int isConst()
Definition: ast.cc:793
bool flag_nomem2reg
Definition: ast.cc:56
void delete_children()
Definition: ast.cc:231
RTLIL::Const realAsConst(int width)
Definition: ast.cc:850
bool is_output
Definition: ast.h:158
bool lib
Definition: ast.h:269
int port_id
Definition: ast.h:159
bool asBool()
Definition: ast.cc:784
bool flag_dump_vlog
Definition: ast.cc:56
bool operator!=(const AstNode &other) const
Definition: ast.cc:655
bool is_input
Definition: ast.h:158
RTLIL::Const asAttrConst()
Definition: ast.cc:761