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