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