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