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/log.h"
30 #include "libs/sha1/sha1.h"
40 using namespace AST_INTERNAL
;
42 // instanciate global variables (public API)
44 std::string current_filename
;
45 void (*set_line_num
)(int) = NULL
;
46 int (*get_line_num
)() = NULL
;
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
;
60 // convert node types to string
61 std::string
AST::type2str(AstNodeType type
)
65 #define X(_item) case _item: return #_item;
160 // check if attribute exists and has non-zero value
161 bool AstNode::get_bool_attribute(RTLIL::IdString id
)
163 if (attributes
.count(id
) == 0)
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
);
171 return attr
->integer
!= 0;
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
)
179 filename
= current_filename
;
180 linenum
= get_line_num();
187 range_swapped
= false;
197 children
.push_back(child1
);
199 children
.push_back(child2
);
202 // create a (deep recursive) copy of a node
203 AstNode
*AstNode::clone()
205 AstNode
*that
= new AstNode
;
207 for (auto &it
: that
->children
)
209 for (auto &it
: that
->attributes
)
210 it
.second
= it
.second
->clone();
214 // create a (deep recursive) copy of a node use 'other' as target root node
215 void AstNode::cloneInto(AstNode
*other
)
217 AstNode
*tmp
= clone();
218 other
->delete_children();
220 tmp
->children
.clear();
221 tmp
->attributes
.clear();
225 // delete all children in this node
226 void AstNode::delete_children()
228 for (auto &it
: children
)
232 for (auto &it
: attributes
)
237 // AstNode destructor
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
)
248 for (auto f
: log_files
)
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
);
257 fprintf(f
, " [%p -> %p]", this, id2ast
);
259 fprintf(f
, " [%p]", this);
262 fprintf(f
, " str='%s'", str
.c_str());
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
, "'(%d)", GetSize(bits
));
273 fprintf(f
, " input");
275 fprintf(f
, " output");
279 fprintf(f
, " signed");
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
? "" : "!");
285 fprintf(f
, " int=%u", (int)integer
);
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
);
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
+ " ");
301 for (size_t i
= 0; i
< children
.size(); i
++)
302 children
[i
]->dumpAst(f
, indent
+ " ");
305 // helper function for AstNode::dumpVlog()
306 static std::string
id2vl(std::string txt
)
308 if (txt
.size() > 1 && txt
[0] == '\\')
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
+ " ";
321 // dump AST node as verilog pseudo-code
322 void AstNode::dumpVlog(FILE *f
, std::string indent
)
326 std::vector
<AstNode
*> rem_children1
, rem_children2
;
329 for (auto f
: log_files
)
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");
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());
351 for (auto child
: children
)
352 if (child
->type
== AST_PARAMETER
|| child
->type
== AST_LOCALPARAM
|| child
->type
== AST_DEFPARAM
)
353 child
->dumpVlog(f
, indent
+ " ");
355 rem_children1
.push_back(child
);
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
+ " ");
361 rem_children2
.push_back(child
);
362 rem_children1
.clear();
364 for (auto child
: rem_children2
)
365 if (child
->type
== AST_TASK
|| child
->type
== AST_FUNCTION
)
366 child
->dumpVlog(f
, indent
+ " ");
368 rem_children1
.push_back(child
);
369 rem_children2
.clear();
371 for (auto child
: rem_children1
)
372 child
->dumpVlog(f
, indent
+ " ");
373 rem_children1
.clear();
375 fprintf(f
, "%s" "endmodule\n", indent
.c_str());
379 if (is_input
&& is_output
)
380 fprintf(f
, "%s" "inout", indent
.c_str());
382 fprintf(f
, "%s" "input", indent
.c_str());
384 fprintf(f
, "%s" "output", indent
.c_str());
386 fprintf(f
, "%s" "wire", indent
.c_str());
388 fprintf(f
, "%s" "reg", (is_input
|| is_output
) ? " " : indent
.c_str());
390 fprintf(f
, " signed");
391 for (auto child
: children
) {
393 child
->dumpVlog(f
, "");
395 fprintf(f
, " %s", id2vl(str
).c_str());
400 fprintf(f
, "%s" "memory", indent
.c_str());
402 fprintf(f
, " signed");
403 for (auto child
: children
) {
405 child
->dumpVlog(f
, "");
407 fprintf(f
, " %s", id2vl(str
).c_str());
415 fprintf(f
, "[%d:%d]", range_left
, range_right
);
417 for (auto child
: children
) {
418 fprintf(f
, "%c", first
? '[' : ':');
419 child
->dumpVlog(f
, "");
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
)
433 child
->dumpVlog(f
, "");
437 for (auto child
: children
) {
438 if (child
->type
!= AST_POSEDGE
&& child
->type
!= AST_NEGEDGE
&& child
->type
!= AST_EDGE
)
439 child
->dumpVlog(f
, indent
+ " ");
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
+ " ");
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
, "");
463 fprintf(f
, "%s", id2vl(str
).c_str());
464 for (auto child
: children
)
465 child
->dumpVlog(f
, "");
470 fprintf(f
, "\"%s\"", str
.c_str());
471 else if (bits
.size() == 32)
472 fprintf(f
, "%d", RTLIL::Const(bits
).as_int());
474 fprintf(f
, "%d'b %s", GetSize(bits
), RTLIL::Const(bits
).as_string().c_str());
478 fprintf(f
, "%e", realvalue
);
482 if (children
.size() == 1) {
483 children
[0]->dumpVlog(f
, indent
);
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());
493 fprintf(f
, "%s" "case (", indent
.c_str());
494 children
[0]->dumpVlog(f
, "");
496 for (size_t i
= 1; i
< children
.size(); i
++) {
497 AstNode
*child
= children
[i
];
498 child
->dumpVlog(f
, indent
+ " ");
500 fprintf(f
, "%s" "endcase\n", indent
.c_str());
504 for (auto child
: children
) {
505 if (child
->type
== AST_BLOCK
) {
507 child
->dumpVlog(f
, indent
+ " ");
510 fprintf(f
, "%s", first
? indent
.c_str() : ", ");
511 if (child
->type
== AST_DEFAULT
)
512 fprintf(f
, "default");
514 child
->dumpVlog(f
, "");
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
, "");
531 for (auto child
: children
) {
534 child
->dumpVlog(f
, "");
542 children
[0]->dumpVlog(f
, "");
544 children
[1]->dumpVlog(f
, "");
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
, "");
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
= "||"; }
587 children
[0]->dumpVlog(f
, "");
588 fprintf(f
, ")%s(", txt
.c_str());
589 children
[1]->dumpVlog(f
, "");
595 children
[0]->dumpVlog(f
, "");
597 children
[1]->dumpVlog(f
, "");
599 children
[2]->dumpVlog(f
, "");
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);
610 // check if two AST nodes are identical
611 bool AstNode::operator==(const AstNode
&other
) const
613 if (type
!= other
.type
)
615 if (children
.size() != other
.children
.size())
617 if (str
!= other
.str
)
619 if (bits
!= other
.bits
)
621 if (is_input
!= other
.is_input
)
623 if (is_output
!= other
.is_output
)
625 if (is_reg
!= other
.is_reg
)
627 if (is_signed
!= other
.is_signed
)
629 if (is_string
!= other
.is_string
)
631 if (range_valid
!= other
.range_valid
)
633 if (range_swapped
!= other
.range_swapped
)
635 if (port_id
!= other
.port_id
)
637 if (range_left
!= other
.range_left
)
639 if (range_right
!= other
.range_right
)
641 if (integer
!= other
.integer
)
643 for (size_t i
= 0; i
< children
.size(); i
++)
644 if (*children
[i
] != *other
.children
[i
])
649 // check if two AST nodes are not identical
650 bool AstNode::operator!=(const AstNode
&other
) const
652 return !(*this == other
);
655 // check if this AST contains the given node
656 bool AstNode::contains(const AstNode
*other
) const
660 for (auto child
: children
)
661 if (child
->contains(other
))
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
)
669 AstNode
*node
= new AstNode(AST_CONSTANT
);
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
);
676 node
->range_valid
= true;
677 node
->range_left
= width
-1;
678 node
->range_right
= 0;
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
)
685 AstNode
*node
= new AstNode(AST_CONSTANT
);
686 node
->is_signed
= is_signed
;
688 for (size_t i
= 0; i
< 32; i
++) {
689 if (i
< node
->bits
.size())
690 node
->integer
|= (node
->bits
[i
] == RTLIL::S1
) << i
;
692 node
->integer
|= (node
->bits
.back() == RTLIL::S1
) << i
;
694 node
->range_valid
= true;
695 node
->range_left
= node
->bits
.size()-1;
696 node
->range_right
= 0;
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
)
703 AstNode
*node
= mkconst_str(RTLIL::Const(v
).decode_string());
704 log_assert(node
->bits
== v
);
708 // create an AST node for a constant (using a string as value)
709 AstNode
*AstNode::mkconst_str(const std::string
&str
)
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
);
720 AstNode
*node
= AstNode::mkconst_bits(data
, false);
721 node
->is_string
= true;
726 bool AstNode::bits_only_01()
728 for (auto bit
: bits
)
729 if (bit
!= RTLIL::S0
&& bit
!= RTLIL::S1
)
734 RTLIL::Const
AstNode::bitsAsConst(int width
, bool is_signed
)
736 std::vector
<RTLIL::State
> bits
= this->bits
;
737 if (width
>= 0 && width
< int(bits
.size()))
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
);
746 return RTLIL::Const(bits
);
749 RTLIL::Const
AstNode::bitsAsConst(int width
)
751 return bitsAsConst(width
, is_signed
);
754 RTLIL::Const
AstNode::asAttrConst()
756 log_assert(type
== AST_CONSTANT
);
762 val
.flags
|= RTLIL::CONST_FLAG_STRING
;
763 log_assert(val
.decode_string() == str
);
769 RTLIL::Const
AstNode::asParaConst()
771 RTLIL::Const val
= asAttrConst();
773 val
.flags
|= RTLIL::CONST_FLAG_SIGNED
;
777 bool AstNode::asBool()
779 log_assert(type
== AST_CONSTANT
);
780 for (auto &bit
: bits
)
781 if (bit
== RTLIL::State::S1
)
786 int AstNode::isConst()
788 if (type
== AST_CONSTANT
)
790 if (type
== AST_REALVALUE
)
795 uint64_t AstNode::asInt(bool is_signed
)
797 if (type
== AST_CONSTANT
)
799 RTLIL::Const v
= bitsAsConst(64, is_signed
);
802 for (int i
= 0; i
< 64; i
++)
803 if (v
.bits
.at(i
) == RTLIL::State::S1
)
804 ret
|= uint64_t(1) << i
;
809 if (type
== AST_REALVALUE
)
815 double AstNode::asReal(bool is_signed
)
817 if (type
== AST_CONSTANT
)
819 RTLIL::Const
val(bits
);
821 bool is_negative
= is_signed
&& val
.bits
.back() == RTLIL::State::S1
;
823 val
= const_neg(val
, val
, false, false, val
.bits
.size());
826 for (size_t i
= 0; i
< val
.bits
.size(); i
++)
827 // IEEE Std 1800-2012 Par 6.12.2: Individual bits that are x or z in
828 // the net or the variable shall be treated as zero upon conversion.
829 if (val
.bits
.at(i
) == RTLIL::State::S1
)
837 if (type
== AST_REALVALUE
)
843 RTLIL::Const
AstNode::realAsConst(int width
)
845 double v
= round(realvalue
);
850 if (!std::isfinite(v
)) {
852 result
.bits
= std::vector
<RTLIL::State
>(width
, RTLIL::State::Sx
);
854 bool is_negative
= v
< 0;
857 for (int i
= 0; i
< width
; i
++, v
/= 2)
858 result
.bits
.push_back((fmod(floor(v
), 2) != 0) ? RTLIL::State::S1
: RTLIL::State::S0
);
860 result
= const_neg(result
, result
, false, false, result
.bits
.size());
865 // create a new AstModule from an AST_MODULE AST node
866 static AstModule
* process_module(AstNode
*ast
, bool defer
)
868 log_assert(ast
->type
== AST_MODULE
);
871 log("Storing AST representation for module `%s'.\n", ast
->str
.c_str());
873 log("Generating RTLIL representation for module `%s'.\n", ast
->str
.c_str());
875 current_module
= new AstModule
;
876 current_module
->ast
= NULL
;
877 current_module
->name
= ast
->str
;
878 current_module
->attributes
["\\src"] = stringf("%s:%d", ast
->filename
.c_str(), ast
->linenum
);
880 current_ast_mod
= ast
;
881 AstNode
*ast_before_simplify
= ast
->clone();
883 if (flag_dump_ast1
) {
884 log("Dumping verilog AST before simplification:\n");
885 ast
->dumpAst(NULL
, " ");
886 log("--- END OF AST DUMP ---\n");
891 while (ast
->simplify(!flag_noopt
, false, false, 0, -1, false, false)) { }
893 if (flag_dump_ast2
) {
894 log("Dumping verilog AST after simplification:\n");
895 ast
->dumpAst(NULL
, " ");
896 log("--- END OF AST DUMP ---\n");
899 if (flag_dump_vlog
) {
900 log("Dumping verilog AST (as requested by dump_vlog option):\n");
901 ast
->dumpVlog(NULL
, " ");
902 log("--- END OF AST DUMP ---\n");
906 std::vector
<AstNode
*> new_children
;
907 for (auto child
: ast
->children
) {
908 if (child
->type
== AST_WIRE
&& (child
->is_input
|| child
->is_output
))
909 new_children
.push_back(child
);
913 ast
->children
.swap(new_children
);
914 ast
->attributes
["\\blackbox"] = AstNode::mkconst_int(1, false);
917 ignoreThisSignalsInInitial
= RTLIL::SigSpec();
919 for (auto &attr
: ast
->attributes
) {
920 if (attr
.second
->type
!= AST_CONSTANT
)
921 log_error("Attribute `%s' with non-constant value at %s:%d!\n",
922 attr
.first
.c_str(), ast
->filename
.c_str(), ast
->linenum
);
923 current_module
->attributes
[attr
.first
] = attr
.second
->asAttrConst();
925 for (size_t i
= 0; i
< ast
->children
.size(); i
++) {
926 AstNode
*node
= ast
->children
[i
];
927 if (node
->type
== AST_WIRE
|| node
->type
== AST_MEMORY
)
930 for (size_t i
= 0; i
< ast
->children
.size(); i
++) {
931 AstNode
*node
= ast
->children
[i
];
932 if (node
->type
!= AST_WIRE
&& node
->type
!= AST_MEMORY
&& node
->type
!= AST_INITIAL
)
936 ignoreThisSignalsInInitial
.sort_and_unify();
938 for (size_t i
= 0; i
< ast
->children
.size(); i
++) {
939 AstNode
*node
= ast
->children
[i
];
940 if (node
->type
== AST_INITIAL
)
944 ignoreThisSignalsInInitial
= RTLIL::SigSpec();
947 current_module
->ast
= ast_before_simplify
;
948 current_module
->nolatches
= flag_nolatches
;
949 current_module
->nomem2reg
= flag_nomem2reg
;
950 current_module
->mem2reg
= flag_mem2reg
;
951 current_module
->lib
= flag_lib
;
952 current_module
->noopt
= flag_noopt
;
953 current_module
->icells
= flag_icells
;
954 current_module
->autowire
= flag_autowire
;
955 current_module
->fixup_ports();
956 return current_module
;
959 // create AstModule instances for all modules in the AST tree and add them to 'design'
960 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
)
963 flag_dump_ast1
= dump_ast1
;
964 flag_dump_ast2
= dump_ast2
;
965 flag_dump_vlog
= dump_vlog
;
966 flag_nolatches
= nolatches
;
967 flag_nomem2reg
= nomem2reg
;
968 flag_mem2reg
= mem2reg
;
971 flag_icells
= icells
;
972 flag_autowire
= autowire
;
974 std::vector
<AstNode
*> global_decls
;
976 log_assert(current_ast
->type
== AST_DESIGN
);
977 for (auto it
= current_ast
->children
.begin(); it
!= current_ast
->children
.end(); it
++)
979 if ((*it
)->type
== AST_MODULE
)
981 for (auto n
: global_decls
)
982 (*it
)->children
.push_back(n
->clone());
984 if (flag_icells
&& (*it
)->str
.substr(0, 2) == "\\$")
985 (*it
)->str
= (*it
)->str
.substr(1);
988 (*it
)->str
= "$abstract" + (*it
)->str
;
990 if (design
->has((*it
)->str
)) {
992 log_error("Re-definition of module `%s' at %s:%d!\n",
993 (*it
)->str
.c_str(), (*it
)->filename
.c_str(), (*it
)->linenum
);
994 log("Ignoring re-definition of module `%s' at %s:%d!\n",
995 (*it
)->str
.c_str(), (*it
)->filename
.c_str(), (*it
)->linenum
);
999 design
->add(process_module(*it
, defer
));
1002 global_decls
.push_back(*it
);
1006 // AstModule destructor
1007 AstModule::~AstModule()
1013 // create a new parametric module (when needed) and return the name of the generated module
1014 RTLIL::IdString
AstModule::derive(RTLIL::Design
*design
, std::map
<RTLIL::IdString
, RTLIL::Const
> parameters
)
1016 std::string stripped_name
= name
.str();
1018 if (stripped_name
.substr(0, 9) == "$abstract")
1019 stripped_name
= stripped_name
.substr(9);
1021 log_header("Executing AST frontend in derive mode using pre-parsed AST for module `%s'.\n", stripped_name
.c_str());
1024 flag_dump_ast1
= false;
1025 flag_dump_ast2
= false;
1026 flag_dump_vlog
= false;
1027 flag_nolatches
= nolatches
;
1028 flag_nomem2reg
= nomem2reg
;
1029 flag_mem2reg
= mem2reg
;
1032 flag_icells
= icells
;
1033 flag_autowire
= autowire
;
1034 use_internal_line_num();
1036 std::string para_info
;
1037 AstNode
*new_ast
= ast
->clone();
1039 int para_counter
= 0;
1040 int orig_parameters_n
= parameters
.size();
1041 for (auto it
= new_ast
->children
.begin(); it
!= new_ast
->children
.end(); it
++) {
1042 AstNode
*child
= *it
;
1043 if (child
->type
!= AST_PARAMETER
)
1046 std::string para_id
= child
->str
;
1047 if (parameters
.count(para_id
) > 0) {
1048 log("Parameter %s = %s\n", child
->str
.c_str(), log_signal(RTLIL::SigSpec(parameters
[child
->str
])));
1050 para_info
+= stringf("%s=%s", child
->str
.c_str(), log_signal(RTLIL::SigSpec(parameters
[para_id
])));
1051 delete child
->children
.at(0);
1052 child
->children
[0] = AstNode::mkconst_bits(parameters
[para_id
].bits
, (parameters
[para_id
].flags
& RTLIL::CONST_FLAG_SIGNED
) != 0);
1053 parameters
.erase(para_id
);
1056 para_id
= stringf("$%d", para_counter
);
1057 if (parameters
.count(para_id
) > 0) {
1058 log("Parameter %d (%s) = %s\n", para_counter
, child
->str
.c_str(), log_signal(RTLIL::SigSpec(parameters
[para_id
])));
1059 goto rewrite_parameter
;
1062 if (parameters
.size() > 0)
1063 log_error("Requested parameter `%s' does not exist in module `%s'!\n", parameters
.begin()->first
.c_str(), stripped_name
.c_str());
1065 std::string modname
;
1067 if (orig_parameters_n
== 0)
1068 modname
= stripped_name
;
1069 else if (para_info
.size() > 60)
1070 modname
= "$paramod$" + sha1(para_info
) + stripped_name
;
1072 modname
= "$paramod" + stripped_name
+ para_info
;
1074 if (!design
->has(modname
)) {
1075 new_ast
->str
= modname
;
1076 design
->add(process_module(new_ast
, false));
1077 design
->module(modname
)->check();
1079 log("Found cached RTLIL representation for module `%s'.\n", modname
.c_str());
1086 RTLIL::Module
*AstModule::clone() const
1088 AstModule
*new_mod
= new AstModule
;
1089 new_mod
->name
= name
;
1092 new_mod
->ast
= ast
->clone();
1093 new_mod
->nolatches
= nolatches
;
1094 new_mod
->nomem2reg
= nomem2reg
;
1095 new_mod
->mem2reg
= mem2reg
;
1097 new_mod
->noopt
= noopt
;
1098 new_mod
->icells
= icells
;
1099 new_mod
->autowire
= autowire
;
1104 // internal dummy line number callbacks
1106 int internal_line_num
;
1107 void internal_set_line_num(int n
) {
1108 internal_line_num
= n
;
1110 int internal_get_line_num() {
1111 return internal_line_num
;
1115 // use internal dummy line number callbacks
1116 void AST::use_internal_line_num()
1118 set_line_num
= &internal_set_line_num
;
1119 get_line_num
= &internal_get_line_num
;