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"
38 using namespace AST_INTERNAL
;
40 // instanciate global variables (public API)
42 std::string current_filename
;
43 void (*set_line_num
)(int) = NULL
;
44 int (*get_line_num
)() = NULL
;
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
;
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
;
59 // convert node types to string
60 std::string
AST::type2str(AstNodeType type
)
64 #define X(_item) case _item: return #_item;
148 // check if attribute exists and has non-zero value
149 bool AstNode::get_bool_attribute(RTLIL::IdString id
)
151 if (attributes
.count(id
) == 0)
154 AstNode
*attr
= attributes
.at(id
);
155 if (attr
->type
!= AST_CONSTANT
)
156 log_error("Attribute `%s' with non-constant value at %s:%d!\n",
157 id
.c_str(), attr
->filename
.c_str(), attr
->linenum
);
159 return attr
->integer
!= 0;
162 // create new node (AstNode constructor)
163 // (the optional child arguments make it easier to create AST trees)
164 AstNode::AstNode(AstNodeType type
, AstNode
*child1
, AstNode
*child2
)
167 filename
= current_filename
;
168 linenum
= get_line_num();
181 children
.push_back(child1
);
183 children
.push_back(child2
);
186 // create a (deep recursive) copy of a node
187 AstNode
*AstNode::clone()
189 AstNode
*that
= new AstNode
;
191 for (auto &it
: that
->children
)
193 for (auto &it
: that
->attributes
)
194 it
.second
= it
.second
->clone();
198 // create a (deep recursive) copy of a node use 'other' as target root node
199 void AstNode::cloneInto(AstNode
*other
)
201 AstNode
*tmp
= clone();
202 other
->delete_children();
204 tmp
->children
.clear();
205 tmp
->attributes
.clear();
209 // delete all children in this node
210 void AstNode::delete_children()
212 for (auto &it
: children
)
216 for (auto &it
: attributes
)
221 // AstNode destructor
227 // create a nice text representation of the node
228 // (traverse tree by recursion, use 'other' pointer for diffing two AST trees)
229 void AstNode::dumpAst(FILE *f
, std::string indent
)
232 for (auto f
: log_files
)
237 std::string type_name
= type2str(type
);
238 fprintf(f
, "%s%s <%s:%d>", indent
.c_str(), type_name
.c_str(), filename
.c_str(), linenum
);
240 fprintf(f
, " str='%s'", str
.c_str());
242 fprintf(f
, " bits='");
243 for (size_t i
= bits
.size(); i
> 0; i
--)
244 fprintf(f
, "%c", bits
[i
-1] == RTLIL::S0
? '0' :
245 bits
[i
-1] == RTLIL::S1
? '1' :
246 bits
[i
-1] == RTLIL::Sx
? 'x' :
247 bits
[i
-1] == RTLIL::Sz
? 'z' : '?');
248 fprintf(f
, "'(%zd)", bits
.size());
251 fprintf(f
, " input");
253 fprintf(f
, " output");
257 fprintf(f
, " signed");
259 fprintf(f
, " port=%d", port_id
);
260 if (range_valid
|| range_left
!= -1 || range_right
!= 0)
261 fprintf(f
, " range=[%d:%d]%s", range_left
, range_right
, range_valid
? "" : "!");
263 fprintf(f
, " int=%u", (int)integer
);
266 for (size_t i
= 0; i
< children
.size(); i
++)
267 children
[i
]->dumpAst(f
, indent
+ " ");
270 // helper function for AstNode::dumpVlog()
271 static std::string
id2vl(std::string txt
)
273 if (txt
.size() > 1 && txt
[0] == '\\')
275 for (size_t i
= 0; i
< txt
.size(); i
++) {
276 if ('A' <= txt
[i
] && txt
[i
] <= 'Z') continue;
277 if ('a' <= txt
[i
] && txt
[i
] <= 'z') continue;
278 if ('0' <= txt
[i
] && txt
[i
] <= '9') continue;
279 if (txt
[i
] == '_') continue;
280 txt
= "\\" + txt
+ " ";
286 // dump AST node as verilog pseudo-code
287 void AstNode::dumpVlog(FILE *f
, std::string indent
)
291 std::vector
<AstNode
*> rem_children1
, rem_children2
;
294 for (auto f
: log_files
)
302 fprintf(f
, "%s" "module %s(", indent
.c_str(), id2vl(str
).c_str());
303 for (auto child
: children
)
304 if (child
->type
== AST_WIRE
&& (child
->is_input
|| child
->is_output
)) {
305 fprintf(f
, "%s%s", first
? "" : ", ", id2vl(child
->str
).c_str());
310 for (auto child
: children
)
311 if (child
->type
== AST_PARAMETER
|| child
->type
== AST_LOCALPARAM
|| child
->type
== AST_DEFPARAM
)
312 child
->dumpVlog(f
, indent
+ " ");
314 rem_children1
.push_back(child
);
316 for (auto child
: rem_children1
)
317 if (child
->type
== AST_WIRE
|| child
->type
== AST_AUTOWIRE
|| child
->type
== AST_MEMORY
)
318 child
->dumpVlog(f
, indent
+ " ");
320 rem_children2
.push_back(child
);
321 rem_children1
.clear();
323 for (auto child
: rem_children2
)
324 if (child
->type
== AST_TASK
|| child
->type
== AST_FUNCTION
)
325 child
->dumpVlog(f
, indent
+ " ");
327 rem_children1
.push_back(child
);
328 rem_children2
.clear();
330 for (auto child
: rem_children1
)
331 child
->dumpVlog(f
, indent
+ " ");
332 rem_children1
.clear();
334 fprintf(f
, "%s" "endmodule\n", indent
.c_str());
338 if (is_input
&& is_output
)
339 fprintf(f
, "%s" "inout", indent
.c_str());
341 fprintf(f
, "%s" "input", indent
.c_str());
343 fprintf(f
, "%s" "output", indent
.c_str());
345 fprintf(f
, "%s" "wire", indent
.c_str());
347 fprintf(f
, "%s" "reg", (is_input
|| is_output
) ? " " : indent
.c_str());
349 fprintf(f
, " signed");
350 for (auto child
: children
) {
352 child
->dumpVlog(f
, "");
354 fprintf(f
, " %s", id2vl(str
).c_str());
359 fprintf(f
, "%s" "memory", indent
.c_str());
361 fprintf(f
, " signed");
362 for (auto child
: children
) {
364 child
->dumpVlog(f
, "");
366 fprintf(f
, " %s", id2vl(str
).c_str());
374 fprintf(f
, "[%d:%d]", range_left
, range_right
);
376 for (auto child
: children
) {
377 fprintf(f
, "%c", first
? '[' : ':');
378 child
->dumpVlog(f
, "");
386 fprintf(f
, "%s" "always @(", indent
.c_str());
387 for (auto child
: children
) {
388 if (child
->type
!= AST_POSEDGE
&& child
->type
!= AST_NEGEDGE
&& child
->type
!= AST_EDGE
)
392 child
->dumpVlog(f
, "");
396 for (auto child
: children
) {
397 if (child
->type
!= AST_POSEDGE
&& child
->type
!= AST_NEGEDGE
&& child
->type
!= AST_EDGE
)
398 child
->dumpVlog(f
, indent
+ " ");
403 fprintf(f
, "%s" "initial\n", indent
.c_str());
404 for (auto child
: children
) {
405 if (child
->type
!= AST_POSEDGE
&& child
->type
!= AST_NEGEDGE
&& child
->type
!= AST_EDGE
)
406 child
->dumpVlog(f
, indent
+ " ");
413 if (type
== AST_POSEDGE
)
414 fprintf(f
, "posedge ");
415 if (type
== AST_NEGEDGE
)
416 fprintf(f
, "negedge ");
417 for (auto child
: children
)
418 child
->dumpVlog(f
, "");
422 fprintf(f
, "%s", id2vl(str
).c_str());
423 for (auto child
: children
)
424 child
->dumpVlog(f
, "");
429 fprintf(f
, "\"%s\"", str
.c_str());
430 else if (bits
.size() == 32)
431 fprintf(f
, "%d", RTLIL::Const(bits
).as_int());
433 fprintf(f
, "%zd'b %s", bits
.size(), RTLIL::Const(bits
).as_string().c_str());
437 if (children
.size() == 1) {
438 children
[0]->dumpVlog(f
, indent
);
440 fprintf(f
, "%s" "begin\n", indent
.c_str());
441 for (auto child
: children
)
442 child
->dumpVlog(f
, indent
+ " ");
443 fprintf(f
, "%s" "end\n", indent
.c_str());
448 fprintf(f
, "%s" "case (", indent
.c_str());
449 children
[0]->dumpVlog(f
, "");
451 for (size_t i
= 1; i
< children
.size(); i
++) {
452 AstNode
*child
= children
[i
];
453 child
->dumpVlog(f
, indent
+ " ");
455 fprintf(f
, "%s" "endcase\n", indent
.c_str());
459 for (auto child
: children
) {
460 if (child
->type
== AST_BLOCK
) {
462 child
->dumpVlog(f
, indent
+ " ");
465 fprintf(f
, "%s", first
? indent
.c_str() : ", ");
466 if (child
->type
== AST_DEFAULT
)
467 fprintf(f
, "default");
469 child
->dumpVlog(f
, "");
477 fprintf(f
, "%s", indent
.c_str());
478 children
[0]->dumpVlog(f
, "");
479 fprintf(f
, " %s ", type
== AST_ASSIGN_EQ
? "=" : "<=");
480 children
[1]->dumpVlog(f
, "");
486 for (auto child
: children
) {
489 child
->dumpVlog(f
, "");
497 children
[0]->dumpVlog(f
, "");
499 children
[1]->dumpVlog(f
, "");
503 if (0) { case AST_BIT_NOT
: txt
= "~"; }
504 if (0) { case AST_REDUCE_AND
: txt
= "&"; }
505 if (0) { case AST_REDUCE_OR
: txt
= "|"; }
506 if (0) { case AST_REDUCE_XOR
: txt
= "^"; }
507 if (0) { case AST_REDUCE_XNOR
: txt
= "~^"; }
508 if (0) { case AST_REDUCE_BOOL
: txt
= "|"; }
509 if (0) { case AST_POS
: txt
= "+"; }
510 if (0) { case AST_NEG
: txt
= "-"; }
511 if (0) { case AST_LOGIC_NOT
: txt
= "!"; }
512 fprintf(f
, "%s(", txt
.c_str());
513 children
[0]->dumpVlog(f
, "");
517 if (0) { case AST_BIT_AND
: txt
= "&"; }
518 if (0) { case AST_BIT_OR
: txt
= "|"; }
519 if (0) { case AST_BIT_XOR
: txt
= "^"; }
520 if (0) { case AST_BIT_XNOR
: txt
= "~^"; }
521 if (0) { case AST_SHIFT_LEFT
: txt
= "<<"; }
522 if (0) { case AST_SHIFT_RIGHT
: txt
= ">>"; }
523 if (0) { case AST_SHIFT_SLEFT
: txt
= "<<<"; }
524 if (0) { case AST_SHIFT_SRIGHT
: txt
= ">>>"; }
525 if (0) { case AST_LT
: txt
= "<"; }
526 if (0) { case AST_LE
: txt
= "<="; }
527 if (0) { case AST_EQ
: txt
= "=="; }
528 if (0) { case AST_NE
: txt
= "!="; }
529 if (0) { case AST_GE
: txt
= ">="; }
530 if (0) { case AST_GT
: txt
= ">"; }
531 if (0) { case AST_ADD
: txt
= "+"; }
532 if (0) { case AST_SUB
: txt
= "-"; }
533 if (0) { case AST_MUL
: txt
= "*"; }
534 if (0) { case AST_DIV
: txt
= "/"; }
535 if (0) { case AST_MOD
: txt
= "%"; }
536 if (0) { case AST_POW
: txt
= "**"; }
537 if (0) { case AST_LOGIC_AND
: txt
= "&&"; }
538 if (0) { case AST_LOGIC_OR
: txt
= "||"; }
540 children
[0]->dumpVlog(f
, "");
541 fprintf(f
, ")%s(", txt
.c_str());
542 children
[1]->dumpVlog(f
, "");
548 children
[0]->dumpVlog(f
, "");
550 children
[1]->dumpVlog(f
, "");
552 children
[2]->dumpVlog(f
, "");
557 std::string type_name
= type2str(type
);
558 fprintf(f
, "%s" "/** %s **/%s", indent
.c_str(), type_name
.c_str(), indent
.empty() ? "" : "\n");
559 // dumpAst(f, indent, NULL);
563 // check if two AST nodes are identical
564 bool AstNode::operator==(const AstNode
&other
) const
566 if (type
!= other
.type
)
568 if (children
.size() != other
.children
.size())
570 if (str
!= other
.str
)
572 if (bits
!= other
.bits
)
574 if (is_input
!= other
.is_input
)
576 if (is_output
!= other
.is_output
)
578 if (is_reg
!= other
.is_reg
)
580 if (is_signed
!= other
.is_signed
)
582 if (range_valid
!= other
.range_valid
)
584 if (port_id
!= other
.port_id
)
586 if (range_left
!= other
.range_left
)
588 if (range_right
!= other
.range_right
)
590 if (integer
!= other
.integer
)
592 for (size_t i
= 0; i
< children
.size(); i
++)
593 if (*children
[i
] != *other
.children
[i
])
598 // check if two AST nodes are not identical
599 bool AstNode::operator!=(const AstNode
&other
) const
601 return !(*this == other
);
604 // check if this AST contains the given node
605 bool AstNode::contains(const AstNode
*other
) const
609 for (auto child
: children
)
610 if (child
->contains(other
))
615 // create an AST node for a constant (using a 32 bit int as value)
616 AstNode
*AstNode::mkconst_int(uint32_t v
, bool is_signed
, int width
)
618 AstNode
*node
= new AstNode(AST_CONSTANT
);
620 node
->is_signed
= is_signed
;
621 for (int i
= 0; i
< width
; i
++) {
622 node
->bits
.push_back((v
& 1) ? RTLIL::S1
: RTLIL::S0
);
625 node
->range_valid
= true;
626 node
->range_left
= width
-1;
627 node
->range_right
= 0;
631 // create an AST node for a constant (using a bit vector as value)
632 AstNode
*AstNode::mkconst_bits(const std::vector
<RTLIL::State
> &v
, bool is_signed
)
634 AstNode
*node
= new AstNode(AST_CONSTANT
);
635 node
->is_signed
= is_signed
;
637 for (size_t i
= 0; i
< 32; i
++) {
638 if (i
< node
->bits
.size())
639 node
->integer
|= (node
->bits
[i
] == RTLIL::S1
) << i
;
641 node
->integer
|= (node
->bits
.back() == RTLIL::S1
) << i
;
643 node
->range_valid
= true;
644 node
->range_left
= node
->bits
.size()-1;
645 node
->range_right
= 0;
649 RTLIL::Const
AstNode::bitsAsConst(int width
, bool is_signed
)
651 std::vector
<RTLIL::State
> bits
= this->bits
;
652 if (width
>= 0 && width
< int(bits
.size()))
654 if (width
>= 0 && width
> int(bits
.size())) {
655 RTLIL::State extbit
= RTLIL::State::S0
;
656 if (is_signed
&& !bits
.empty())
657 extbit
= bits
.back();
658 while (width
> int(bits
.size()))
659 bits
.push_back(extbit
);
661 return RTLIL::Const(bits
);
664 RTLIL::Const
AstNode::bitsAsConst(int width
)
666 return bitsAsConst(width
, is_signed
);
669 // create a new AstModule from an AST_MODULE AST node
670 static AstModule
* process_module(AstNode
*ast
)
672 assert(ast
->type
== AST_MODULE
);
673 log("Generating RTLIL representation for module `%s'.\n", ast
->str
.c_str());
675 current_module
= new AstModule
;
676 current_module
->ast
= NULL
;
677 current_module
->name
= ast
->str
;
678 current_module
->attributes
["\\src"] = stringf("%s:%d", ast
->filename
.c_str(), ast
->linenum
);
680 current_ast_mod
= ast
;
681 AstNode
*ast_before_simplify
= ast
->clone();
683 if (flag_dump_ast1
) {
684 log("Dumping verilog AST before simplification:\n");
685 ast
->dumpAst(NULL
, " ");
686 log("--- END OF AST DUMP ---\n");
689 while (ast
->simplify(!flag_noopt
, false, false, 0, -1, false)) { }
691 if (flag_dump_ast2
) {
692 log("Dumping verilog AST after simplification:\n");
693 ast
->dumpAst(NULL
, " ");
694 log("--- END OF AST DUMP ---\n");
697 if (flag_dump_vlog
) {
698 log("Dumping verilog AST (as requested by dump_vlog option):\n");
699 ast
->dumpVlog(NULL
, " ");
700 log("--- END OF AST DUMP ---\n");
704 std::vector
<AstNode
*> new_children
;
705 for (auto child
: ast
->children
) {
706 if (child
->type
== AST_WIRE
&& (child
->is_input
|| child
->is_output
))
707 new_children
.push_back(child
);
711 ast
->children
.swap(new_children
);
712 ast
->attributes
["\\placeholder"] = AstNode::mkconst_int(1, false);
715 ignoreThisSignalsInInitial
= RTLIL::SigSpec();
717 for (auto &attr
: ast
->attributes
) {
718 if (attr
.second
->type
!= AST_CONSTANT
)
719 log_error("Attribute `%s' with non-constant value at %s:%d!\n",
720 attr
.first
.c_str(), ast
->filename
.c_str(), ast
->linenum
);
721 current_module
->attributes
[attr
.first
].str
= attr
.second
->str
;
722 current_module
->attributes
[attr
.first
].bits
= attr
.second
->bits
;
724 for (size_t i
= 0; i
< ast
->children
.size(); i
++) {
725 AstNode
*node
= ast
->children
[i
];
726 if (node
->type
== AST_WIRE
|| node
->type
== AST_MEMORY
)
729 for (size_t i
= 0; i
< ast
->children
.size(); i
++) {
730 AstNode
*node
= ast
->children
[i
];
731 if (node
->type
!= AST_WIRE
&& node
->type
!= AST_MEMORY
&& node
->type
!= AST_INITIAL
)
735 ignoreThisSignalsInInitial
.sort_and_unify();
737 for (size_t i
= 0; i
< ast
->children
.size(); i
++) {
738 AstNode
*node
= ast
->children
[i
];
739 if (node
->type
== AST_INITIAL
)
743 ignoreThisSignalsInInitial
= RTLIL::SigSpec();
745 current_module
->ast
= ast_before_simplify
;
746 current_module
->nolatches
= flag_nolatches
;
747 current_module
->nomem2reg
= flag_nomem2reg
;
748 current_module
->mem2reg
= flag_mem2reg
;
749 current_module
->lib
= flag_lib
;
750 current_module
->noopt
= flag_noopt
;
751 return current_module
;
754 // create AstModule instances for all modules in the AST tree and add them to 'design'
755 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
)
758 flag_dump_ast1
= dump_ast1
;
759 flag_dump_ast2
= dump_ast2
;
760 flag_dump_vlog
= dump_vlog
;
761 flag_nolatches
= nolatches
;
762 flag_nomem2reg
= nomem2reg
;
763 flag_mem2reg
= mem2reg
;
767 assert(current_ast
->type
== AST_DESIGN
);
768 for (auto it
= current_ast
->children
.begin(); it
!= current_ast
->children
.end(); it
++) {
769 if (design
->modules
.count((*it
)->str
) != 0)
770 log_error("Re-definition of module `%s' at %s:%d!\n",
771 (*it
)->str
.c_str(), (*it
)->filename
.c_str(), (*it
)->linenum
);
772 design
->modules
[(*it
)->str
] = process_module(*it
);
776 // AstModule destructor
777 AstModule::~AstModule()
783 // create a new parametric module (when needed) and return the name of the generated module
784 RTLIL::IdString
AstModule::derive(RTLIL::Design
*design
, std::map
<RTLIL::IdString
, RTLIL::Const
> parameters
)
786 log_header("Executing AST frontend in derive mode using pre-parsed AST for module `%s'.\n", name
.c_str());
789 flag_dump_ast1
= false;
790 flag_dump_ast2
= false;
791 flag_dump_vlog
= false;
792 flag_nolatches
= nolatches
;
793 flag_nomem2reg
= nomem2reg
;
794 flag_mem2reg
= mem2reg
;
797 use_internal_line_num();
799 std::string para_info
;
800 std::vector
<unsigned char> hash_data
;
801 hash_data
.insert(hash_data
.end(), name
.begin(), name
.end());
802 hash_data
.push_back(0);
804 AstNode
*new_ast
= ast
->clone();
806 int para_counter
= 0;
807 for (auto it
= new_ast
->children
.begin(); it
!= new_ast
->children
.end(); it
++) {
808 AstNode
*child
= *it
;
809 if (child
->type
!= AST_PARAMETER
)
812 std::string para_id
= child
->str
;
813 if (parameters
.count(para_id
) > 0) {
814 log("Parameter %s = %s\n", child
->str
.c_str(), log_signal(RTLIL::SigSpec(parameters
[child
->str
])));
816 para_info
+= stringf("%s=%s", child
->str
.c_str(), log_signal(RTLIL::SigSpec(parameters
[para_id
])));
817 delete child
->children
.at(0);
818 child
->children
[0] = AstNode::mkconst_bits(parameters
[para_id
].bits
, child
->is_signed
);
819 hash_data
.insert(hash_data
.end(), child
->str
.begin(), child
->str
.end());
820 hash_data
.push_back(0);
821 hash_data
.insert(hash_data
.end(), parameters
[para_id
].bits
.begin(), parameters
[para_id
].bits
.end());
822 hash_data
.push_back(0xff);
823 parameters
.erase(para_id
);
826 para_id
= stringf("$%d", para_counter
);
827 if (parameters
.count(para_id
) > 0) {
828 log("Parameter %d (%s) = %s\n", para_counter
, child
->str
.c_str(), log_signal(RTLIL::SigSpec(parameters
[para_id
])));
829 goto rewrite_parameter
;
832 if (parameters
.size() > 0)
833 log_error("Requested parameter `%s' does not exist in module `%s'!\n", parameters
.begin()->first
.c_str(), name
.c_str());
837 if (para_info
.size() > 60)
839 unsigned char hash
[20];
840 unsigned char *hash_data2
= new unsigned char[hash_data
.size()];
841 for (size_t i
= 0; i
< hash_data
.size(); i
++)
842 hash_data2
[i
] = hash_data
[i
];
843 sha1::calc(hash_data2
, hash_data
.size(), hash
);
847 sha1::toHexString(hash
, hexstring
);
849 modname
= "$paramod$" + std::string(hexstring
) + name
;
853 modname
= "$paramod" + name
+ para_info
;
856 if (design
->modules
.count(modname
) == 0) {
857 new_ast
->str
= modname
;
858 design
->modules
[modname
] = process_module(new_ast
);
860 log("Found cached RTLIL representation for module `%s'.\n", modname
.c_str());
867 // recompile a module from AST with updated widths for auto-wires
868 // (auto-wires are wires that are used but not declared an thus have an automatically determined width)
869 void AstModule::update_auto_wires(std::map
<RTLIL::IdString
, int> auto_sizes
)
871 log_header("Executing AST frontend in update_auto_wires mode using pre-parsed AST for module `%s'.\n", name
.c_str());
874 flag_dump_ast1
= false;
875 flag_dump_ast2
= false;
876 flag_dump_vlog
= false;
877 flag_nolatches
= nolatches
;
878 flag_nomem2reg
= nomem2reg
;
879 flag_mem2reg
= mem2reg
;
882 use_internal_line_num();
884 for (auto it
= auto_sizes
.begin(); it
!= auto_sizes
.end(); it
++) {
885 log("Adding extra wire declaration to AST: wire [%d:0] %s\n", it
->second
- 1, it
->first
.c_str());
886 AstNode
*wire
= new AstNode(AST_WIRE
, new AstNode(AST_RANGE
, AstNode::mkconst_int(it
->second
- 1, true), AstNode::mkconst_int(0, true)));
887 wire
->str
= it
->first
;
888 ast
->children
.insert(ast
->children
.begin(), wire
);
891 AstModule
*newmod
= process_module(ast
);
897 wires
.swap(newmod
->wires
);
898 cells
.swap(newmod
->cells
);
899 processes
.swap(newmod
->processes
);
900 connections
.swap(newmod
->connections
);
901 attributes
.swap(newmod
->attributes
);
905 RTLIL::Module
*AstModule::clone() const
907 AstModule
*new_mod
= new AstModule
;
910 new_mod
->ast
= ast
->clone();
911 new_mod
->nolatches
= nolatches
;
912 new_mod
->nomem2reg
= nomem2reg
;
913 new_mod
->mem2reg
= mem2reg
;
915 new_mod
->noopt
= noopt
;
920 // internal dummy line number callbacks
922 int internal_line_num
;
923 void internal_set_line_num(int n
) {
924 internal_line_num
= n
;
926 int internal_get_line_num() {
927 return internal_line_num
;
931 // use internal dummy line number callbacks
932 void AST::use_internal_line_num()
934 set_line_num
= &internal_set_line_num
;
935 get_line_num
= &internal_get_line_num
;