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