2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
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.
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.
20 * This is the AST frontend library.
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.
29 #include "kernel/yosys.h"
30 #include "libs/sha1/sha1.h"
36 #if defined(__APPLE__)
45 using namespace AST_INTERNAL
;
47 // instanciate global variables (public API)
49 std::string current_filename
;
50 void (*set_line_num
)(int) = NULL
;
51 int (*get_line_num
)() = NULL
;
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
;
65 // convert node types to string
66 std::string
AST::type2str(AstNodeType type
)
70 #define X(_item) case _item: return #_item;
169 // check if attribute exists and has non-zero value
170 bool AstNode::get_bool_attribute(RTLIL::IdString id
)
172 if (attributes
.count(id
) == 0)
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
);
180 return attr
->integer
!= 0;
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
)
187 static unsigned int hashidx_count
= 123456789;
188 hashidx_count
= mkhash_xorshift(hashidx_count
);
189 hashidx_
= hashidx_count
;
192 filename
= current_filename
;
193 linenum
= get_line_num();
200 range_swapped
= false;
210 children
.push_back(child1
);
212 children
.push_back(child2
);
215 // create a (deep recursive) copy of a node
216 AstNode
*AstNode::clone()
218 AstNode
*that
= new AstNode
;
220 for (auto &it
: that
->children
)
222 for (auto &it
: that
->attributes
)
223 it
.second
= it
.second
->clone();
227 // create a (deep recursive) copy of a node use 'other' as target root node
228 void AstNode::cloneInto(AstNode
*other
)
230 AstNode
*tmp
= clone();
231 other
->delete_children();
233 tmp
->children
.clear();
234 tmp
->attributes
.clear();
238 // delete all children in this node
239 void AstNode::delete_children()
241 for (auto &it
: children
)
245 for (auto &it
: attributes
)
250 // AstNode destructor
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
)
261 for (auto f
: log_files
)
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
);
270 fprintf(f
, " [%p -> %p]", this, id2ast
);
272 fprintf(f
, " [%p]", this);
275 fprintf(f
, " str='%s'", str
.c_str());
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
));
286 fprintf(f
, " input");
288 fprintf(f
, " output");
292 fprintf(f
, " signed");
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
? "" : "!");
298 fprintf(f
, " int=%u", (int)integer
);
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
);
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
+ " ");
314 for (size_t i
= 0; i
< children
.size(); i
++)
315 children
[i
]->dumpAst(f
, indent
+ " ");
318 // helper function for AstNode::dumpVlog()
319 static std::string
id2vl(std::string txt
)
321 if (txt
.size() > 1 && txt
[0] == '\\')
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
+ " ";
334 // dump AST node as Verilog pseudo-code
335 void AstNode::dumpVlog(FILE *f
, std::string indent
)
339 std::vector
<AstNode
*> rem_children1
, rem_children2
;
342 for (auto f
: log_files
)
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");
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());
364 for (auto child
: children
)
365 if (child
->type
== AST_PARAMETER
|| child
->type
== AST_LOCALPARAM
|| child
->type
== AST_DEFPARAM
)
366 child
->dumpVlog(f
, indent
+ " ");
368 rem_children1
.push_back(child
);
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
+ " ");
374 rem_children2
.push_back(child
);
375 rem_children1
.clear();
377 for (auto child
: rem_children2
)
378 if (child
->type
== AST_TASK
|| child
->type
== AST_FUNCTION
)
379 child
->dumpVlog(f
, indent
+ " ");
381 rem_children1
.push_back(child
);
382 rem_children2
.clear();
384 for (auto child
: rem_children1
)
385 child
->dumpVlog(f
, indent
+ " ");
386 rem_children1
.clear();
388 fprintf(f
, "%s" "endmodule\n", indent
.c_str());
392 if (is_input
&& is_output
)
393 fprintf(f
, "%s" "inout", indent
.c_str());
395 fprintf(f
, "%s" "input", indent
.c_str());
397 fprintf(f
, "%s" "output", indent
.c_str());
399 fprintf(f
, "%s" "wire", indent
.c_str());
401 fprintf(f
, "%s" "reg", (is_input
|| is_output
) ? " " : indent
.c_str());
403 fprintf(f
, " signed");
404 for (auto child
: children
) {
406 child
->dumpVlog(f
, "");
408 fprintf(f
, " %s", id2vl(str
).c_str());
413 fprintf(f
, "%s" "memory", indent
.c_str());
415 fprintf(f
, " signed");
416 for (auto child
: children
) {
418 child
->dumpVlog(f
, "");
420 fprintf(f
, " %s", id2vl(str
).c_str());
428 fprintf(f
, "[%d:%d]", range_left
, range_right
);
430 for (auto child
: children
) {
431 fprintf(f
, "%c", first
? '[' : ':');
432 child
->dumpVlog(f
, "");
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
)
446 child
->dumpVlog(f
, "");
450 for (auto child
: children
) {
451 if (child
->type
!= AST_POSEDGE
&& child
->type
!= AST_NEGEDGE
&& child
->type
!= AST_EDGE
)
452 child
->dumpVlog(f
, indent
+ " ");
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
+ " ");
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
, "");
476 fprintf(f
, "%s", id2vl(str
).c_str());
477 for (auto child
: children
)
478 child
->dumpVlog(f
, "");
483 fprintf(f
, "\"%s\"", str
.c_str());
484 else if (bits
.size() == 32)
485 fprintf(f
, "%d", RTLIL::Const(bits
).as_int());
487 fprintf(f
, "%d'b %s", GetSize(bits
), RTLIL::Const(bits
).as_string().c_str());
491 fprintf(f
, "%e", realvalue
);
495 if (children
.size() == 1) {
496 children
[0]->dumpVlog(f
, indent
);
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());
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());
511 fprintf(f
, "%s" "case (", indent
.c_str());
512 children
[0]->dumpVlog(f
, "");
514 for (size_t i
= 1; i
< children
.size(); i
++) {
515 AstNode
*child
= children
[i
];
516 child
->dumpVlog(f
, indent
+ " ");
518 fprintf(f
, "%s" "endcase\n", indent
.c_str());
524 for (auto child
: children
) {
525 if (child
->type
== AST_BLOCK
) {
527 child
->dumpVlog(f
, indent
+ " ");
530 fprintf(f
, "%s", first
? indent
.c_str() : ", ");
531 if (child
->type
== AST_DEFAULT
)
532 fprintf(f
, "default");
534 child
->dumpVlog(f
, "");
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
, "");
551 for (auto child
: children
) {
554 child
->dumpVlog(f
, "");
562 children
[0]->dumpVlog(f
, "");
564 children
[1]->dumpVlog(f
, "");
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
, "");
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
= "||"; }
607 children
[0]->dumpVlog(f
, "");
608 fprintf(f
, ")%s(", txt
.c_str());
609 children
[1]->dumpVlog(f
, "");
615 children
[0]->dumpVlog(f
, "");
617 children
[1]->dumpVlog(f
, "");
619 children
[2]->dumpVlog(f
, "");
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);
630 // check if two AST nodes are identical
631 bool AstNode::operator==(const AstNode
&other
) const
633 if (type
!= other
.type
)
635 if (children
.size() != other
.children
.size())
637 if (str
!= other
.str
)
639 if (bits
!= other
.bits
)
641 if (is_input
!= other
.is_input
)
643 if (is_output
!= other
.is_output
)
645 if (is_reg
!= other
.is_reg
)
647 if (is_signed
!= other
.is_signed
)
649 if (is_string
!= other
.is_string
)
651 if (range_valid
!= other
.range_valid
)
653 if (range_swapped
!= other
.range_swapped
)
655 if (port_id
!= other
.port_id
)
657 if (range_left
!= other
.range_left
)
659 if (range_right
!= other
.range_right
)
661 if (integer
!= other
.integer
)
663 for (size_t i
= 0; i
< children
.size(); i
++)
664 if (*children
[i
] != *other
.children
[i
])
669 // check if two AST nodes are not identical
670 bool AstNode::operator!=(const AstNode
&other
) const
672 return !(*this == other
);
675 // check if this AST contains the given node
676 bool AstNode::contains(const AstNode
*other
) const
680 for (auto child
: children
)
681 if (child
->contains(other
))
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
)
689 AstNode
*node
= new AstNode(AST_CONSTANT
);
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
);
696 node
->range_valid
= true;
697 node
->range_left
= width
-1;
698 node
->range_right
= 0;
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
)
705 AstNode
*node
= new AstNode(AST_CONSTANT
);
706 node
->is_signed
= is_signed
;
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
;
714 node
->range_valid
= true;
715 node
->range_left
= node
->bits
.size()-1;
716 node
->range_right
= 0;
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
)
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
);
730 // create an AST node for a constant (using a string as value)
731 AstNode
*AstNode::mkconst_str(const std::string
&str
)
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
);
742 AstNode
*node
= AstNode::mkconst_bits(data
, false);
743 node
->is_string
= true;
748 bool AstNode::bits_only_01()
750 for (auto bit
: bits
)
751 if (bit
!= RTLIL::S0
&& bit
!= RTLIL::S1
)
756 RTLIL::Const
AstNode::bitsAsConst(int width
, bool is_signed
)
758 std::vector
<RTLIL::State
> bits
= this->bits
;
759 if (width
>= 0 && width
< int(bits
.size()))
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
);
768 return RTLIL::Const(bits
);
771 RTLIL::Const
AstNode::bitsAsConst(int width
)
773 return bitsAsConst(width
, is_signed
);
776 RTLIL::Const
AstNode::asAttrConst()
778 log_assert(type
== AST_CONSTANT
);
784 val
.flags
|= RTLIL::CONST_FLAG_STRING
;
785 log_assert(val
.decode_string() == str
);
791 RTLIL::Const
AstNode::asParaConst()
793 RTLIL::Const val
= asAttrConst();
795 val
.flags
|= RTLIL::CONST_FLAG_SIGNED
;
799 bool AstNode::asBool()
801 log_assert(type
== AST_CONSTANT
);
802 for (auto &bit
: bits
)
803 if (bit
== RTLIL::State::S1
)
808 int AstNode::isConst()
810 if (type
== AST_CONSTANT
)
812 if (type
== AST_REALVALUE
)
817 uint64_t AstNode::asInt(bool is_signed
)
819 if (type
== AST_CONSTANT
)
821 RTLIL::Const v
= bitsAsConst(64, is_signed
);
824 for (int i
= 0; i
< 64; i
++)
825 if (v
.bits
.at(i
) == RTLIL::State::S1
)
826 ret
|= uint64_t(1) << i
;
831 if (type
== AST_REALVALUE
)
832 return uint64_t(realvalue
);
837 double AstNode::asReal(bool is_signed
)
839 if (type
== AST_CONSTANT
)
841 RTLIL::Const
val(bits
);
843 bool is_negative
= is_signed
&& !val
.bits
.empty() && val
.bits
.back() == RTLIL::State::S1
;
845 val
= const_neg(val
, val
, false, false, val
.bits
.size());
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
)
859 if (type
== AST_REALVALUE
)
865 RTLIL::Const
AstNode::realAsConst(int width
)
867 double v
= round(realvalue
);
872 if (!std::isfinite(v
)) {
874 result
.bits
= std::vector
<RTLIL::State
>(width
, RTLIL::State::Sx
);
876 bool is_negative
= v
< 0;
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
);
882 result
= const_neg(result
, result
, false, false, result
.bits
.size());
887 // create a new AstModule from an AST_MODULE AST node
888 static AstModule
* process_module(AstNode
*ast
, bool defer
)
890 log_assert(ast
->type
== AST_MODULE
);
893 log("Storing AST representation for module `%s'.\n", ast
->str
.c_str());
895 log("Generating RTLIL representation for module `%s'.\n", ast
->str
.c_str());
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
);
902 current_ast_mod
= ast
;
903 AstNode
*ast_before_simplify
= ast
->clone();
905 if (flag_dump_ast1
) {
906 log("Dumping Verilog AST before simplification:\n");
907 ast
->dumpAst(NULL
, " ");
908 log("--- END OF AST DUMP ---\n");
913 while (ast
->simplify(!flag_noopt
, false, false, 0, -1, false, false)) { }
915 if (flag_dump_ast2
) {
916 log("Dumping Verilog AST after simplification:\n");
917 ast
->dumpAst(NULL
, " ");
918 log("--- END OF AST DUMP ---\n");
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");
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
);
935 ast
->children
.swap(new_children
);
936 ast
->attributes
["\\blackbox"] = AstNode::mkconst_int(1, false);
939 ignoreThisSignalsInInitial
= RTLIL::SigSpec();
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();
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
)
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
)
958 ignoreThisSignalsInInitial
.sort_and_unify();
960 for (size_t i
= 0; i
< ast
->children
.size(); i
++) {
961 AstNode
*node
= ast
->children
[i
];
962 if (node
->type
== AST_INITIAL
)
966 ignoreThisSignalsInInitial
= RTLIL::SigSpec();
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
;
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
)
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
;
995 flag_icells
= icells
;
996 flag_autowire
= autowire
;
998 std::vector
<AstNode
*> global_decls
;
1000 log_assert(current_ast
->type
== AST_DESIGN
);
1001 for (auto it
= current_ast
->children
.begin(); it
!= current_ast
->children
.end(); it
++)
1003 if ((*it
)->type
== AST_MODULE
)
1005 for (auto n
: global_decls
)
1006 (*it
)->children
.push_back(n
->clone());
1008 if (flag_icells
&& (*it
)->str
.substr(0, 2) == "\\$")
1009 (*it
)->str
= (*it
)->str
.substr(1);
1012 (*it
)->str
= "$abstract" + (*it
)->str
;
1014 if (design
->has((*it
)->str
)) {
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
);
1023 design
->add(process_module(*it
, defer
));
1026 global_decls
.push_back(*it
);
1030 // AstModule destructor
1031 AstModule::~AstModule()
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
)
1040 std::string stripped_name
= name
.str();
1042 if (stripped_name
.substr(0, 9) == "$abstract")
1043 stripped_name
= stripped_name
.substr(9);
1045 log_header(design
, "Executing AST frontend in derive mode using pre-parsed AST for module `%s'.\n", stripped_name
.c_str());
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
;
1057 flag_icells
= icells
;
1058 flag_autowire
= autowire
;
1059 use_internal_line_num();
1061 std::string para_info
;
1062 AstNode
*new_ast
= ast
->clone();
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
)
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
])));
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
);
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
;
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());
1090 std::string modname
;
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
;
1097 modname
= "$paramod" + stripped_name
+ para_info
;
1099 if (!design
->has(modname
)) {
1100 new_ast
->str
= modname
;
1101 design
->add(process_module(new_ast
, false));
1102 design
->module(modname
)->check();
1104 log("Found cached RTLIL representation for module `%s'.\n", modname
.c_str());
1111 RTLIL::Module
*AstModule::clone() const
1113 AstModule
*new_mod
= new AstModule
;
1114 new_mod
->name
= name
;
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
;
1123 new_mod
->noopt
= noopt
;
1124 new_mod
->icells
= icells
;
1125 new_mod
->autowire
= autowire
;
1130 // internal dummy line number callbacks
1132 int internal_line_num
;
1133 void internal_set_line_num(int n
) {
1134 internal_line_num
= n
;
1136 int internal_get_line_num() {
1137 return internal_line_num
;
1141 // use internal dummy line number callbacks
1142 void AST::use_internal_line_num()
1144 set_line_num
= &internal_set_line_num
;
1145 get_line_num
= &internal_get_line_num
;