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