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;
149 // check if attribute exists and has non-zero value
150 bool AstNode::get_bool_attribute(RTLIL::IdString id
)
152 if (attributes
.count(id
) == 0)
155 AstNode
*attr
= attributes
.at(id
);
156 if (attr
->type
!= AST_CONSTANT
)
157 log_error("Attribute `%s' with non-constant value at %s:%d!\n",
158 id
.c_str(), attr
->filename
.c_str(), attr
->linenum
);
160 return attr
->integer
!= 0;
163 // create new node (AstNode constructor)
164 // (the optional child arguments make it easier to create AST trees)
165 AstNode::AstNode(AstNodeType type
, AstNode
*child1
, AstNode
*child2
)
168 filename
= current_filename
;
169 linenum
= get_line_num();
182 children
.push_back(child1
);
184 children
.push_back(child2
);
187 // create a (deep recursive) copy of a node
188 AstNode
*AstNode::clone()
190 AstNode
*that
= new AstNode
;
192 for (auto &it
: that
->children
)
194 for (auto &it
: that
->attributes
)
195 it
.second
= it
.second
->clone();
199 // create a (deep recursive) copy of a node use 'other' as target root node
200 void AstNode::cloneInto(AstNode
*other
)
202 AstNode
*tmp
= clone();
203 other
->delete_children();
205 tmp
->children
.clear();
206 tmp
->attributes
.clear();
210 // delete all children in this node
211 void AstNode::delete_children()
213 for (auto &it
: children
)
217 for (auto &it
: attributes
)
222 // AstNode destructor
228 // create a nice text representation of the node
229 // (traverse tree by recursion, use 'other' pointer for diffing two AST trees)
230 void AstNode::dumpAst(FILE *f
, std::string indent
)
233 for (auto f
: log_files
)
238 std::string type_name
= type2str(type
);
239 fprintf(f
, "%s%s <%s:%d>", indent
.c_str(), type_name
.c_str(), filename
.c_str(), linenum
);
241 fprintf(f
, " str='%s'", str
.c_str());
243 fprintf(f
, " bits='");
244 for (size_t i
= bits
.size(); i
> 0; i
--)
245 fprintf(f
, "%c", bits
[i
-1] == RTLIL::S0
? '0' :
246 bits
[i
-1] == RTLIL::S1
? '1' :
247 bits
[i
-1] == RTLIL::Sx
? 'x' :
248 bits
[i
-1] == RTLIL::Sz
? 'z' : '?');
249 fprintf(f
, "'(%zd)", bits
.size());
252 fprintf(f
, " input");
254 fprintf(f
, " output");
258 fprintf(f
, " signed");
260 fprintf(f
, " port=%d", port_id
);
261 if (range_valid
|| range_left
!= -1 || range_right
!= 0)
262 fprintf(f
, " range=[%d:%d]%s", range_left
, range_right
, range_valid
? "" : "!");
264 fprintf(f
, " int=%u", (int)integer
);
267 for (auto &it
: attributes
) {
268 fprintf(f
, "%s ATTR %s:\n", indent
.c_str(), it
.first
.c_str());
269 it
.second
->dumpAst(f
, indent
+ " ");
272 for (size_t i
= 0; i
< children
.size(); i
++)
273 children
[i
]->dumpAst(f
, indent
+ " ");
276 // helper function for AstNode::dumpVlog()
277 static std::string
id2vl(std::string txt
)
279 if (txt
.size() > 1 && txt
[0] == '\\')
281 for (size_t i
= 0; i
< txt
.size(); i
++) {
282 if ('A' <= txt
[i
] && txt
[i
] <= 'Z') continue;
283 if ('a' <= txt
[i
] && txt
[i
] <= 'z') continue;
284 if ('0' <= txt
[i
] && txt
[i
] <= '9') continue;
285 if (txt
[i
] == '_') continue;
286 txt
= "\\" + txt
+ " ";
292 // dump AST node as verilog pseudo-code
293 void AstNode::dumpVlog(FILE *f
, std::string indent
)
297 std::vector
<AstNode
*> rem_children1
, rem_children2
;
300 for (auto f
: log_files
)
305 for (auto &it
: attributes
) {
306 fprintf(f
, "%s" "(* %s = ", indent
.c_str(), id2vl(it
.first
).c_str());
307 it
.second
->dumpVlog(f
, "");
308 fprintf(f
, " *)%s", indent
.empty() ? "" : "\n");
314 fprintf(f
, "%s" "module %s(", indent
.c_str(), id2vl(str
).c_str());
315 for (auto child
: children
)
316 if (child
->type
== AST_WIRE
&& (child
->is_input
|| child
->is_output
)) {
317 fprintf(f
, "%s%s", first
? "" : ", ", id2vl(child
->str
).c_str());
322 for (auto child
: children
)
323 if (child
->type
== AST_PARAMETER
|| child
->type
== AST_LOCALPARAM
|| child
->type
== AST_DEFPARAM
)
324 child
->dumpVlog(f
, indent
+ " ");
326 rem_children1
.push_back(child
);
328 for (auto child
: rem_children1
)
329 if (child
->type
== AST_WIRE
|| child
->type
== AST_AUTOWIRE
|| child
->type
== AST_MEMORY
)
330 child
->dumpVlog(f
, indent
+ " ");
332 rem_children2
.push_back(child
);
333 rem_children1
.clear();
335 for (auto child
: rem_children2
)
336 if (child
->type
== AST_TASK
|| child
->type
== AST_FUNCTION
)
337 child
->dumpVlog(f
, indent
+ " ");
339 rem_children1
.push_back(child
);
340 rem_children2
.clear();
342 for (auto child
: rem_children1
)
343 child
->dumpVlog(f
, indent
+ " ");
344 rem_children1
.clear();
346 fprintf(f
, "%s" "endmodule\n", indent
.c_str());
350 if (is_input
&& is_output
)
351 fprintf(f
, "%s" "inout", indent
.c_str());
353 fprintf(f
, "%s" "input", indent
.c_str());
355 fprintf(f
, "%s" "output", indent
.c_str());
357 fprintf(f
, "%s" "wire", indent
.c_str());
359 fprintf(f
, "%s" "reg", (is_input
|| is_output
) ? " " : indent
.c_str());
361 fprintf(f
, " signed");
362 for (auto child
: children
) {
364 child
->dumpVlog(f
, "");
366 fprintf(f
, " %s", id2vl(str
).c_str());
371 fprintf(f
, "%s" "memory", indent
.c_str());
373 fprintf(f
, " signed");
374 for (auto child
: children
) {
376 child
->dumpVlog(f
, "");
378 fprintf(f
, " %s", id2vl(str
).c_str());
386 fprintf(f
, "[%d:%d]", range_left
, range_right
);
388 for (auto child
: children
) {
389 fprintf(f
, "%c", first
? '[' : ':');
390 child
->dumpVlog(f
, "");
398 fprintf(f
, "%s" "always @(", indent
.c_str());
399 for (auto child
: children
) {
400 if (child
->type
!= AST_POSEDGE
&& child
->type
!= AST_NEGEDGE
&& child
->type
!= AST_EDGE
)
404 child
->dumpVlog(f
, "");
408 for (auto child
: children
) {
409 if (child
->type
!= AST_POSEDGE
&& child
->type
!= AST_NEGEDGE
&& child
->type
!= AST_EDGE
)
410 child
->dumpVlog(f
, indent
+ " ");
415 fprintf(f
, "%s" "initial\n", indent
.c_str());
416 for (auto child
: children
) {
417 if (child
->type
!= AST_POSEDGE
&& child
->type
!= AST_NEGEDGE
&& child
->type
!= AST_EDGE
)
418 child
->dumpVlog(f
, indent
+ " ");
425 if (type
== AST_POSEDGE
)
426 fprintf(f
, "posedge ");
427 if (type
== AST_NEGEDGE
)
428 fprintf(f
, "negedge ");
429 for (auto child
: children
)
430 child
->dumpVlog(f
, "");
434 fprintf(f
, "%s", id2vl(str
).c_str());
435 for (auto child
: children
)
436 child
->dumpVlog(f
, "");
441 fprintf(f
, "\"%s\"", str
.c_str());
442 else if (bits
.size() == 32)
443 fprintf(f
, "%d", RTLIL::Const(bits
).as_int());
445 fprintf(f
, "%zd'b %s", bits
.size(), RTLIL::Const(bits
).as_string().c_str());
449 if (children
.size() == 1) {
450 children
[0]->dumpVlog(f
, indent
);
452 fprintf(f
, "%s" "begin\n", indent
.c_str());
453 for (auto child
: children
)
454 child
->dumpVlog(f
, indent
+ " ");
455 fprintf(f
, "%s" "end\n", indent
.c_str());
460 fprintf(f
, "%s" "case (", indent
.c_str());
461 children
[0]->dumpVlog(f
, "");
463 for (size_t i
= 1; i
< children
.size(); i
++) {
464 AstNode
*child
= children
[i
];
465 child
->dumpVlog(f
, indent
+ " ");
467 fprintf(f
, "%s" "endcase\n", indent
.c_str());
471 for (auto child
: children
) {
472 if (child
->type
== AST_BLOCK
) {
474 child
->dumpVlog(f
, indent
+ " ");
477 fprintf(f
, "%s", first
? indent
.c_str() : ", ");
478 if (child
->type
== AST_DEFAULT
)
479 fprintf(f
, "default");
481 child
->dumpVlog(f
, "");
489 fprintf(f
, "%s", indent
.c_str());
490 children
[0]->dumpVlog(f
, "");
491 fprintf(f
, " %s ", type
== AST_ASSIGN_EQ
? "=" : "<=");
492 children
[1]->dumpVlog(f
, "");
498 for (auto child
: children
) {
501 child
->dumpVlog(f
, "");
509 children
[0]->dumpVlog(f
, "");
511 children
[1]->dumpVlog(f
, "");
515 if (0) { case AST_BIT_NOT
: txt
= "~"; }
516 if (0) { case AST_REDUCE_AND
: txt
= "&"; }
517 if (0) { case AST_REDUCE_OR
: txt
= "|"; }
518 if (0) { case AST_REDUCE_XOR
: txt
= "^"; }
519 if (0) { case AST_REDUCE_XNOR
: txt
= "~^"; }
520 if (0) { case AST_REDUCE_BOOL
: txt
= "|"; }
521 if (0) { case AST_POS
: txt
= "+"; }
522 if (0) { case AST_NEG
: txt
= "-"; }
523 if (0) { case AST_LOGIC_NOT
: txt
= "!"; }
524 fprintf(f
, "%s(", txt
.c_str());
525 children
[0]->dumpVlog(f
, "");
529 if (0) { case AST_BIT_AND
: txt
= "&"; }
530 if (0) { case AST_BIT_OR
: txt
= "|"; }
531 if (0) { case AST_BIT_XOR
: txt
= "^"; }
532 if (0) { case AST_BIT_XNOR
: txt
= "~^"; }
533 if (0) { case AST_SHIFT_LEFT
: txt
= "<<"; }
534 if (0) { case AST_SHIFT_RIGHT
: txt
= ">>"; }
535 if (0) { case AST_SHIFT_SLEFT
: txt
= "<<<"; }
536 if (0) { case AST_SHIFT_SRIGHT
: txt
= ">>>"; }
537 if (0) { case AST_LT
: txt
= "<"; }
538 if (0) { case AST_LE
: txt
= "<="; }
539 if (0) { case AST_EQ
: txt
= "=="; }
540 if (0) { case AST_NE
: txt
= "!="; }
541 if (0) { case AST_GE
: txt
= ">="; }
542 if (0) { case AST_GT
: txt
= ">"; }
543 if (0) { case AST_ADD
: txt
= "+"; }
544 if (0) { case AST_SUB
: txt
= "-"; }
545 if (0) { case AST_MUL
: txt
= "*"; }
546 if (0) { case AST_DIV
: txt
= "/"; }
547 if (0) { case AST_MOD
: txt
= "%"; }
548 if (0) { case AST_POW
: txt
= "**"; }
549 if (0) { case AST_LOGIC_AND
: txt
= "&&"; }
550 if (0) { case AST_LOGIC_OR
: txt
= "||"; }
552 children
[0]->dumpVlog(f
, "");
553 fprintf(f
, ")%s(", txt
.c_str());
554 children
[1]->dumpVlog(f
, "");
560 children
[0]->dumpVlog(f
, "");
562 children
[1]->dumpVlog(f
, "");
564 children
[2]->dumpVlog(f
, "");
569 std::string type_name
= type2str(type
);
570 fprintf(f
, "%s" "/** %s **/%s", indent
.c_str(), type_name
.c_str(), indent
.empty() ? "" : "\n");
571 // dumpAst(f, indent, NULL);
575 // check if two AST nodes are identical
576 bool AstNode::operator==(const AstNode
&other
) const
578 if (type
!= other
.type
)
580 if (children
.size() != other
.children
.size())
582 if (str
!= other
.str
)
584 if (bits
!= other
.bits
)
586 if (is_input
!= other
.is_input
)
588 if (is_output
!= other
.is_output
)
590 if (is_reg
!= other
.is_reg
)
592 if (is_signed
!= other
.is_signed
)
594 if (range_valid
!= other
.range_valid
)
596 if (port_id
!= other
.port_id
)
598 if (range_left
!= other
.range_left
)
600 if (range_right
!= other
.range_right
)
602 if (integer
!= other
.integer
)
604 for (size_t i
= 0; i
< children
.size(); i
++)
605 if (*children
[i
] != *other
.children
[i
])
610 // check if two AST nodes are not identical
611 bool AstNode::operator!=(const AstNode
&other
) const
613 return !(*this == other
);
616 // check if this AST contains the given node
617 bool AstNode::contains(const AstNode
*other
) const
621 for (auto child
: children
)
622 if (child
->contains(other
))
627 // create an AST node for a constant (using a 32 bit int as value)
628 AstNode
*AstNode::mkconst_int(uint32_t v
, bool is_signed
, int width
)
630 AstNode
*node
= new AstNode(AST_CONSTANT
);
632 node
->is_signed
= is_signed
;
633 for (int i
= 0; i
< width
; i
++) {
634 node
->bits
.push_back((v
& 1) ? RTLIL::S1
: RTLIL::S0
);
637 node
->range_valid
= true;
638 node
->range_left
= width
-1;
639 node
->range_right
= 0;
643 // create an AST node for a constant (using a bit vector as value)
644 AstNode
*AstNode::mkconst_bits(const std::vector
<RTLIL::State
> &v
, bool is_signed
)
646 AstNode
*node
= new AstNode(AST_CONSTANT
);
647 node
->is_signed
= is_signed
;
649 for (size_t i
= 0; i
< 32; i
++) {
650 if (i
< node
->bits
.size())
651 node
->integer
|= (node
->bits
[i
] == RTLIL::S1
) << i
;
653 node
->integer
|= (node
->bits
.back() == RTLIL::S1
) << i
;
655 node
->range_valid
= true;
656 node
->range_left
= node
->bits
.size()-1;
657 node
->range_right
= 0;
661 RTLIL::Const
AstNode::bitsAsConst(int width
, bool is_signed
)
663 std::vector
<RTLIL::State
> bits
= this->bits
;
664 if (width
>= 0 && width
< int(bits
.size()))
666 if (width
>= 0 && width
> int(bits
.size())) {
667 RTLIL::State extbit
= RTLIL::State::S0
;
668 if (is_signed
&& !bits
.empty())
669 extbit
= bits
.back();
670 while (width
> int(bits
.size()))
671 bits
.push_back(extbit
);
673 return RTLIL::Const(bits
);
676 RTLIL::Const
AstNode::bitsAsConst(int width
)
678 return bitsAsConst(width
, is_signed
);
681 RTLIL::Const
AstNode::asAttrConst()
683 log_assert(type
== AST_CONSTANT
);
689 val
.flags
|= RTLIL::CONST_FLAG_STRING
;
690 log_assert(val
.decode_string() == str
);
696 RTLIL::Const
AstNode::asParaConst()
698 RTLIL::Const val
= asAttrConst();
700 val
.flags
|= RTLIL::CONST_FLAG_SIGNED
;
704 bool AstNode::asBool()
706 log_assert(type
== AST_CONSTANT
);
707 for (auto &bit
: bits
)
708 if (bit
== RTLIL::State::S1
)
713 // create a new AstModule from an AST_MODULE AST node
714 static AstModule
* process_module(AstNode
*ast
)
716 assert(ast
->type
== AST_MODULE
);
717 log("Generating RTLIL representation for module `%s'.\n", ast
->str
.c_str());
719 current_module
= new AstModule
;
720 current_module
->ast
= NULL
;
721 current_module
->name
= ast
->str
;
722 current_module
->attributes
["\\src"] = stringf("%s:%d", ast
->filename
.c_str(), ast
->linenum
);
724 current_ast_mod
= ast
;
725 AstNode
*ast_before_simplify
= ast
->clone();
727 if (flag_dump_ast1
) {
728 log("Dumping verilog AST before simplification:\n");
729 ast
->dumpAst(NULL
, " ");
730 log("--- END OF AST DUMP ---\n");
733 while (ast
->simplify(!flag_noopt
, false, false, 0, -1, false)) { }
735 if (flag_dump_ast2
) {
736 log("Dumping verilog AST after simplification:\n");
737 ast
->dumpAst(NULL
, " ");
738 log("--- END OF AST DUMP ---\n");
741 if (flag_dump_vlog
) {
742 log("Dumping verilog AST (as requested by dump_vlog option):\n");
743 ast
->dumpVlog(NULL
, " ");
744 log("--- END OF AST DUMP ---\n");
748 std::vector
<AstNode
*> new_children
;
749 for (auto child
: ast
->children
) {
750 if (child
->type
== AST_WIRE
&& (child
->is_input
|| child
->is_output
))
751 new_children
.push_back(child
);
755 ast
->children
.swap(new_children
);
756 ast
->attributes
["\\blackbox"] = AstNode::mkconst_int(1, false);
759 ignoreThisSignalsInInitial
= RTLIL::SigSpec();
761 for (auto &attr
: ast
->attributes
) {
762 if (attr
.second
->type
!= AST_CONSTANT
)
763 log_error("Attribute `%s' with non-constant value at %s:%d!\n",
764 attr
.first
.c_str(), ast
->filename
.c_str(), ast
->linenum
);
765 current_module
->attributes
[attr
.first
] = attr
.second
->asAttrConst();
767 for (size_t i
= 0; i
< ast
->children
.size(); i
++) {
768 AstNode
*node
= ast
->children
[i
];
769 if (node
->type
== AST_WIRE
|| node
->type
== AST_MEMORY
)
772 for (size_t i
= 0; i
< ast
->children
.size(); i
++) {
773 AstNode
*node
= ast
->children
[i
];
774 if (node
->type
!= AST_WIRE
&& node
->type
!= AST_MEMORY
&& node
->type
!= AST_INITIAL
)
778 ignoreThisSignalsInInitial
.sort_and_unify();
780 for (size_t i
= 0; i
< ast
->children
.size(); i
++) {
781 AstNode
*node
= ast
->children
[i
];
782 if (node
->type
== AST_INITIAL
)
786 ignoreThisSignalsInInitial
= RTLIL::SigSpec();
788 current_module
->ast
= ast_before_simplify
;
789 current_module
->nolatches
= flag_nolatches
;
790 current_module
->nomem2reg
= flag_nomem2reg
;
791 current_module
->mem2reg
= flag_mem2reg
;
792 current_module
->lib
= flag_lib
;
793 current_module
->noopt
= flag_noopt
;
794 return current_module
;
797 // create AstModule instances for all modules in the AST tree and add them to 'design'
798 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 ignore_redef
)
801 flag_dump_ast1
= dump_ast1
;
802 flag_dump_ast2
= dump_ast2
;
803 flag_dump_vlog
= dump_vlog
;
804 flag_nolatches
= nolatches
;
805 flag_nomem2reg
= nomem2reg
;
806 flag_mem2reg
= mem2reg
;
810 assert(current_ast
->type
== AST_DESIGN
);
811 for (auto it
= current_ast
->children
.begin(); it
!= current_ast
->children
.end(); it
++) {
812 if (design
->modules
.count((*it
)->str
) != 0) {
814 log_error("Re-definition of module `%s' at %s:%d!\n",
815 (*it
)->str
.c_str(), (*it
)->filename
.c_str(), (*it
)->linenum
);
816 log_error("Ignoring re-definition of module `%s' at %s:%d!\n",
817 (*it
)->str
.c_str(), (*it
)->filename
.c_str(), (*it
)->linenum
);
820 design
->modules
[(*it
)->str
] = process_module(*it
);
824 // AstModule destructor
825 AstModule::~AstModule()
831 // create a new parametric module (when needed) and return the name of the generated module
832 RTLIL::IdString
AstModule::derive(RTLIL::Design
*design
, std::map
<RTLIL::IdString
, RTLIL::Const
> parameters
)
834 log_header("Executing AST frontend in derive mode using pre-parsed AST for module `%s'.\n", name
.c_str());
837 flag_dump_ast1
= false;
838 flag_dump_ast2
= false;
839 flag_dump_vlog
= false;
840 flag_nolatches
= nolatches
;
841 flag_nomem2reg
= nomem2reg
;
842 flag_mem2reg
= mem2reg
;
845 use_internal_line_num();
847 std::string para_info
;
848 std::vector
<unsigned char> hash_data
;
849 hash_data
.insert(hash_data
.end(), name
.begin(), name
.end());
850 hash_data
.push_back(0);
852 AstNode
*new_ast
= ast
->clone();
854 int para_counter
= 0;
855 for (auto it
= new_ast
->children
.begin(); it
!= new_ast
->children
.end(); it
++) {
856 AstNode
*child
= *it
;
857 if (child
->type
!= AST_PARAMETER
)
860 std::string para_id
= child
->str
;
861 if (parameters
.count(para_id
) > 0) {
862 log("Parameter %s = %s\n", child
->str
.c_str(), log_signal(RTLIL::SigSpec(parameters
[child
->str
])));
864 para_info
+= stringf("%s=%s", child
->str
.c_str(), log_signal(RTLIL::SigSpec(parameters
[para_id
])));
865 delete child
->children
.at(0);
866 child
->children
[0] = AstNode::mkconst_bits(parameters
[para_id
].bits
, (parameters
[para_id
].flags
& RTLIL::CONST_FLAG_SIGNED
) != 0);
867 hash_data
.insert(hash_data
.end(), child
->str
.begin(), child
->str
.end());
868 hash_data
.push_back(0);
869 hash_data
.insert(hash_data
.end(), parameters
[para_id
].bits
.begin(), parameters
[para_id
].bits
.end());
870 hash_data
.push_back(0xff);
871 parameters
.erase(para_id
);
874 para_id
= stringf("$%d", para_counter
);
875 if (parameters
.count(para_id
) > 0) {
876 log("Parameter %d (%s) = %s\n", para_counter
, child
->str
.c_str(), log_signal(RTLIL::SigSpec(parameters
[para_id
])));
877 goto rewrite_parameter
;
880 if (parameters
.size() > 0)
881 log_error("Requested parameter `%s' does not exist in module `%s'!\n", parameters
.begin()->first
.c_str(), name
.c_str());
885 if (para_info
.size() > 60)
887 unsigned char hash
[20];
888 unsigned char *hash_data2
= new unsigned char[hash_data
.size()];
889 for (size_t i
= 0; i
< hash_data
.size(); i
++)
890 hash_data2
[i
] = hash_data
[i
];
891 sha1::calc(hash_data2
, hash_data
.size(), hash
);
895 sha1::toHexString(hash
, hexstring
);
897 modname
= "$paramod$" + std::string(hexstring
) + name
;
901 modname
= "$paramod" + name
+ para_info
;
904 if (design
->modules
.count(modname
) == 0) {
905 new_ast
->str
= modname
;
906 design
->modules
[modname
] = process_module(new_ast
);
907 design
->modules
[modname
]->check();
909 log("Found cached RTLIL representation for module `%s'.\n", modname
.c_str());
916 RTLIL::Module
*AstModule::clone() const
918 AstModule
*new_mod
= new AstModule
;
921 new_mod
->ast
= ast
->clone();
922 new_mod
->nolatches
= nolatches
;
923 new_mod
->nomem2reg
= nomem2reg
;
924 new_mod
->mem2reg
= mem2reg
;
926 new_mod
->noopt
= noopt
;
931 // internal dummy line number callbacks
933 int internal_line_num
;
934 void internal_set_line_num(int n
) {
935 internal_line_num
= n
;
937 int internal_get_line_num() {
938 return internal_line_num
;
942 // use internal dummy line number callbacks
943 void AST::use_internal_line_num()
945 set_line_num
= &internal_set_line_num
;
946 get_line_num
= &internal_get_line_num
;