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 // helper function for creating RTLIL code for unary operations
43 static RTLIL::SigSpec
uniop2rtlil(AstNode
*that
, std::string type
, int result_width
, const RTLIL::SigSpec
&arg
, bool gen_attributes
= true)
45 std::stringstream sstr
;
46 sstr
<< type
<< "$" << that
->filename
<< ":" << that
->linenum
<< "$" << (autoidx
++);
48 RTLIL::Cell
*cell
= current_module
->addCell(sstr
.str(), type
);
49 cell
->attributes
["\\src"] = stringf("%s:%d", that
->filename
.c_str(), that
->linenum
);
51 RTLIL::Wire
*wire
= current_module
->addWire(cell
->name
.str() + "_Y", result_width
);
52 wire
->attributes
["\\src"] = stringf("%s:%d", that
->filename
.c_str(), that
->linenum
);
55 for (auto &attr
: that
->attributes
) {
56 if (attr
.second
->type
!= AST_CONSTANT
)
57 log_error("Attribute `%s' with non-constant value at %s:%d!\n",
58 attr
.first
.c_str(), that
->filename
.c_str(), that
->linenum
);
59 cell
->attributes
[attr
.first
] = attr
.second
->asAttrConst();
62 cell
->parameters
["\\A_SIGNED"] = RTLIL::Const(that
->children
[0]->is_signed
);
63 cell
->parameters
["\\A_WIDTH"] = RTLIL::Const(arg
.size());
64 cell
->setPort("\\A", arg
);
66 cell
->parameters
["\\Y_WIDTH"] = result_width
;
67 cell
->setPort("\\Y", wire
);
71 // helper function for extending bit width (preferred over SigSpec::extend() because of correct undef propagation in ConstEval)
72 static void widthExtend(AstNode
*that
, RTLIL::SigSpec
&sig
, int width
, bool is_signed
, std::string celltype
)
74 if (width
<= sig
.size()) {
75 sig
.extend(width
, is_signed
);
79 std::stringstream sstr
;
80 sstr
<< "$extend" << "$" << that
->filename
<< ":" << that
->linenum
<< "$" << (autoidx
++);
82 RTLIL::Cell
*cell
= current_module
->addCell(sstr
.str(), celltype
);
83 cell
->attributes
["\\src"] = stringf("%s:%d", that
->filename
.c_str(), that
->linenum
);
85 RTLIL::Wire
*wire
= current_module
->addWire(cell
->name
.str() + "_Y", width
);
86 wire
->attributes
["\\src"] = stringf("%s:%d", that
->filename
.c_str(), that
->linenum
);
89 for (auto &attr
: that
->attributes
) {
90 if (attr
.second
->type
!= AST_CONSTANT
)
91 log_error("Attribute `%s' with non-constant value at %s:%d!\n",
92 attr
.first
.c_str(), that
->filename
.c_str(), that
->linenum
);
93 cell
->attributes
[attr
.first
] = attr
.second
->asAttrConst();
96 cell
->parameters
["\\A_SIGNED"] = RTLIL::Const(is_signed
);
97 cell
->parameters
["\\A_WIDTH"] = RTLIL::Const(sig
.size());
98 cell
->setPort("\\A", sig
);
100 cell
->parameters
["\\Y_WIDTH"] = width
;
101 cell
->setPort("\\Y", wire
);
105 // helper function for creating RTLIL code for binary operations
106 static RTLIL::SigSpec
binop2rtlil(AstNode
*that
, std::string type
, int result_width
, const RTLIL::SigSpec
&left
, const RTLIL::SigSpec
&right
)
108 std::stringstream sstr
;
109 sstr
<< type
<< "$" << that
->filename
<< ":" << that
->linenum
<< "$" << (autoidx
++);
111 RTLIL::Cell
*cell
= current_module
->addCell(sstr
.str(), type
);
112 cell
->attributes
["\\src"] = stringf("%s:%d", that
->filename
.c_str(), that
->linenum
);
114 RTLIL::Wire
*wire
= current_module
->addWire(cell
->name
.str() + "_Y", result_width
);
115 wire
->attributes
["\\src"] = stringf("%s:%d", that
->filename
.c_str(), that
->linenum
);
117 for (auto &attr
: that
->attributes
) {
118 if (attr
.second
->type
!= AST_CONSTANT
)
119 log_error("Attribute `%s' with non-constant value at %s:%d!\n",
120 attr
.first
.c_str(), that
->filename
.c_str(), that
->linenum
);
121 cell
->attributes
[attr
.first
] = attr
.second
->asAttrConst();
124 cell
->parameters
["\\A_SIGNED"] = RTLIL::Const(that
->children
[0]->is_signed
);
125 cell
->parameters
["\\B_SIGNED"] = RTLIL::Const(that
->children
[1]->is_signed
);
127 cell
->parameters
["\\A_WIDTH"] = RTLIL::Const(left
.size());
128 cell
->parameters
["\\B_WIDTH"] = RTLIL::Const(right
.size());
130 cell
->setPort("\\A", left
);
131 cell
->setPort("\\B", right
);
133 cell
->parameters
["\\Y_WIDTH"] = result_width
;
134 cell
->setPort("\\Y", wire
);
138 // helper function for creating RTLIL code for multiplexers
139 static RTLIL::SigSpec
mux2rtlil(AstNode
*that
, const RTLIL::SigSpec
&cond
, const RTLIL::SigSpec
&left
, const RTLIL::SigSpec
&right
)
141 log_assert(cond
.size() == 1);
143 std::stringstream sstr
;
144 sstr
<< "$ternary$" << that
->filename
<< ":" << that
->linenum
<< "$" << (autoidx
++);
146 RTLIL::Cell
*cell
= current_module
->addCell(sstr
.str(), "$mux");
147 cell
->attributes
["\\src"] = stringf("%s:%d", that
->filename
.c_str(), that
->linenum
);
149 RTLIL::Wire
*wire
= current_module
->addWire(cell
->name
.str() + "_Y", left
.size());
150 wire
->attributes
["\\src"] = stringf("%s:%d", that
->filename
.c_str(), that
->linenum
);
152 for (auto &attr
: that
->attributes
) {
153 if (attr
.second
->type
!= AST_CONSTANT
)
154 log_error("Attribute `%s' with non-constant value at %s:%d!\n",
155 attr
.first
.c_str(), that
->filename
.c_str(), that
->linenum
);
156 cell
->attributes
[attr
.first
] = attr
.second
->asAttrConst();
159 cell
->parameters
["\\WIDTH"] = RTLIL::Const(left
.size());
161 cell
->setPort("\\A", right
);
162 cell
->setPort("\\B", left
);
163 cell
->setPort("\\S", cond
);
164 cell
->setPort("\\Y", wire
);
169 // helper class for converting AST always nodes to RTLIL processes
170 struct AST_INTERNAL::ProcessGenerator
172 // input and output structures
174 RTLIL::SigSpec initSyncSignals
;
175 RTLIL::Process
*proc
;
176 RTLIL::SigSpec outputSignals
;
178 // This always points to the RTLIL::CaseRule beeing filled at the moment
179 RTLIL::CaseRule
*current_case
;
181 // This two variables contain the replacement pattern to be used in the right hand side
182 // of an assignment. E.g. in the code "foo = bar; foo = func(foo);" the foo in the right
183 // hand side of the 2nd assignment needs to be replace with the temporary signal holding
184 // the value assigned in the first assignment. So when the first assignement is processed
185 // the according information is appended to subst_rvalue_from and subst_rvalue_to.
186 RTLIL::SigSpec subst_rvalue_from
, subst_rvalue_to
;
188 // This two variables contain the replacement pattern to be used in the left hand side
189 // of an assignment. E.g. in the code "always @(posedge clk) foo <= bar" the signal bar
190 // should not be connected to the signal foo. Instead it must be connected to the temporary
191 // signal that is used as input for the register that drives the signal foo.
192 RTLIL::SigSpec subst_lvalue_from
, subst_lvalue_to
;
194 // The code here generates a number of temprorary signal for each output register. This
195 // map helps generating nice numbered names for all this temporary signals.
196 std::map
<RTLIL::Wire
*, int> new_temp_count
;
198 // Buffer for generating the init action
199 RTLIL::SigSpec init_lvalue
, init_rvalue
;
201 ProcessGenerator(AstNode
*always
, RTLIL::SigSpec initSyncSignalsArg
= RTLIL::SigSpec()) : always(always
), initSyncSignals(initSyncSignalsArg
)
203 // generate process and simple root case
204 proc
= new RTLIL::Process
;
205 proc
->attributes
["\\src"] = stringf("%s:%d", always
->filename
.c_str(), always
->linenum
);
206 proc
->name
= stringf("$proc$%s:%d$%d", always
->filename
.c_str(), always
->linenum
, autoidx
++);
207 for (auto &attr
: always
->attributes
) {
208 if (attr
.second
->type
!= AST_CONSTANT
)
209 log_error("Attribute `%s' with non-constant value at %s:%d!\n",
210 attr
.first
.c_str(), always
->filename
.c_str(), always
->linenum
);
211 proc
->attributes
[attr
.first
] = attr
.second
->asAttrConst();
213 current_module
->processes
[proc
->name
] = proc
;
214 current_case
= &proc
->root_case
;
216 // create initial temporary signal for all output registers
217 collect_lvalues(subst_lvalue_from
, always
, true, true);
218 subst_lvalue_to
= new_temp_signal(subst_lvalue_from
);
220 bool found_anyedge_syncs
= false;
221 for (auto child
: always
->children
)
222 if (child
->type
== AST_EDGE
)
223 found_anyedge_syncs
= true;
225 if (found_anyedge_syncs
) {
226 log("Note: Assuming pure combinatorial block at %s:%d in\n", always
->filename
.c_str(), always
->linenum
);
227 log("compliance with IEC 62142(E):2005 / IEEE Std. 1364.1(E):2002. Recommending\n");
228 log("use of @* instead of @(...) for better match of synthesis and simulation.\n");
231 // create syncs for the process
232 bool found_clocked_sync
= false;
233 for (auto child
: always
->children
)
234 if (child
->type
== AST_POSEDGE
|| child
->type
== AST_NEGEDGE
) {
235 found_clocked_sync
= true;
236 if (found_anyedge_syncs
)
237 log_error("Found non-synthesizable event list at %s:%d!\n", always
->filename
.c_str(), always
->linenum
);
238 RTLIL::SyncRule
*syncrule
= new RTLIL::SyncRule
;
239 syncrule
->type
= child
->type
== AST_POSEDGE
? RTLIL::STp
: RTLIL::STn
;
240 syncrule
->signal
= child
->children
[0]->genRTLIL();
241 addChunkActions(syncrule
->actions
, subst_lvalue_from
, subst_lvalue_to
, true);
242 proc
->syncs
.push_back(syncrule
);
244 if (proc
->syncs
.empty()) {
245 RTLIL::SyncRule
*syncrule
= new RTLIL::SyncRule
;
246 syncrule
->type
= RTLIL::STa
;
247 syncrule
->signal
= RTLIL::SigSpec();
248 addChunkActions(syncrule
->actions
, subst_lvalue_from
, subst_lvalue_to
, true);
249 proc
->syncs
.push_back(syncrule
);
252 // create initial assignments for the temporary signals
253 if ((flag_nolatches
|| always
->get_bool_attribute("\\nolatches") || current_module
->get_bool_attribute("\\nolatches")) && !found_clocked_sync
) {
254 subst_rvalue_from
= subst_lvalue_from
;
255 subst_rvalue_to
= RTLIL::SigSpec(RTLIL::State::Sx
, subst_rvalue_from
.size());
257 addChunkActions(current_case
->actions
, subst_lvalue_to
, subst_lvalue_from
);
261 for (auto child
: always
->children
)
262 if (child
->type
== AST_BLOCK
)
265 if (initSyncSignals
.size() > 0)
267 RTLIL::SyncRule
*sync
= new RTLIL::SyncRule
;
268 sync
->type
= RTLIL::SyncType::STi
;
269 proc
->syncs
.push_back(sync
);
271 log_assert(init_lvalue
.size() == init_rvalue
.size());
274 for (auto &init_lvalue_c
: init_lvalue
.chunks()) {
275 RTLIL::SigSpec lhs
= init_lvalue_c
;
276 RTLIL::SigSpec rhs
= init_rvalue
.extract(offset
, init_lvalue_c
.width
);
277 sync
->actions
.push_back(RTLIL::SigSig(lhs
, rhs
));
278 offset
+= lhs
.size();
282 outputSignals
= RTLIL::SigSpec(subst_lvalue_from
);
285 // create new temporary signals
286 RTLIL::SigSpec
new_temp_signal(RTLIL::SigSpec sig
)
288 std::vector
<RTLIL::SigChunk
> chunks
= sig
.chunks();
290 for (int i
= 0; i
< SIZE(chunks
); i
++)
292 RTLIL::SigChunk
&chunk
= chunks
[i
];
293 if (chunk
.wire
== NULL
)
296 std::string wire_name
;
298 wire_name
= stringf("$%d%s[%d:%d]", new_temp_count
[chunk
.wire
]++,
299 chunk
.wire
->name
.c_str(), chunk
.width
+chunk
.offset
-1, chunk
.offset
);;
300 if (chunk
.wire
->name
.str().find('$') != std::string::npos
)
301 wire_name
+= stringf("$%d", autoidx
++);
302 } while (current_module
->wires_
.count(wire_name
) > 0);
304 RTLIL::Wire
*wire
= current_module
->addWire(wire_name
, chunk
.width
);
305 wire
->attributes
["\\src"] = stringf("%s:%d", always
->filename
.c_str(), always
->linenum
);
314 // recursively traverse the AST an collect all assigned signals
315 void collect_lvalues(RTLIL::SigSpec
®
, AstNode
*ast
, bool type_eq
, bool type_le
, bool run_sort_and_unify
= true)
320 for (auto child
: ast
->children
)
321 if (child
!= ast
->children
[0]) {
322 log_assert(child
->type
== AST_COND
);
323 collect_lvalues(reg
, child
, type_eq
, type_le
, false);
330 for (auto child
: ast
->children
)
331 if (child
->type
== AST_BLOCK
)
332 collect_lvalues(reg
, child
, type_eq
, type_le
, false);
336 for (auto child
: ast
->children
) {
337 if (child
->type
== AST_ASSIGN_EQ
&& type_eq
)
338 reg
.append(child
->children
[0]->genRTLIL());
339 if (child
->type
== AST_ASSIGN_LE
&& type_le
)
340 reg
.append(child
->children
[0]->genRTLIL());
341 if (child
->type
== AST_CASE
|| child
->type
== AST_BLOCK
)
342 collect_lvalues(reg
, child
, type_eq
, type_le
, false);
350 if (run_sort_and_unify
)
351 reg
.sort_and_unify();
354 // remove all assignments to the given signal pattern in a case and all its children.
355 // e.g. when the last statement in the code "a = 23; if (b) a = 42; a = 0;" is processed this
356 // function is called to clean up the first two assignments as they are overwritten by
357 // the third assignment.
358 void removeSignalFromCaseTree(RTLIL::SigSpec pattern
, RTLIL::CaseRule
*cs
)
360 for (auto it
= cs
->actions
.begin(); it
!= cs
->actions
.end(); it
++)
361 it
->first
.remove2(pattern
, &it
->second
);
363 for (auto it
= cs
->switches
.begin(); it
!= cs
->switches
.end(); it
++)
364 for (auto it2
= (*it
)->cases
.begin(); it2
!= (*it
)->cases
.end(); it2
++)
365 removeSignalFromCaseTree(pattern
, *it2
);
368 // add an assignment (aka "action") but split it up in chunks. this way huge assignments
369 // are avoided and the generated $mux cells have a more "natural" size.
370 void addChunkActions(std::vector
<RTLIL::SigSig
> &actions
, RTLIL::SigSpec lvalue
, RTLIL::SigSpec rvalue
, bool inSyncRule
= false)
372 if (inSyncRule
&& initSyncSignals
.size() > 0) {
373 init_lvalue
.append(lvalue
.extract(initSyncSignals
));
374 init_rvalue
.append(lvalue
.extract(initSyncSignals
, &rvalue
));
375 lvalue
.remove2(initSyncSignals
, &rvalue
);
377 log_assert(lvalue
.size() == rvalue
.size());
380 for (auto &lvalue_c
: lvalue
.chunks()) {
381 RTLIL::SigSpec lhs
= lvalue_c
;
382 RTLIL::SigSpec rhs
= rvalue
.extract(offset
, lvalue_c
.width
);
383 if (inSyncRule
&& lvalue_c
.wire
&& lvalue_c
.wire
->get_bool_attribute("\\nosync"))
384 rhs
= RTLIL::SigSpec(RTLIL::State::Sx
, rhs
.size());
385 actions
.push_back(RTLIL::SigSig(lhs
, rhs
));
386 offset
+= lhs
.size();
390 // recursively process the AST and fill the RTLIL::Process
391 void processAst(AstNode
*ast
)
396 for (auto child
: ast
->children
)
403 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> new_subst_rvalue_map
;
404 for (int i
= 0; i
< SIZE(subst_rvalue_to
); i
++)
405 new_subst_rvalue_map
[subst_rvalue_from
[i
]] = subst_rvalue_to
[i
];
407 RTLIL::SigSpec unmapped_lvalue
= ast
->children
[0]->genRTLIL(), lvalue
= unmapped_lvalue
;
408 RTLIL::SigSpec rvalue
= ast
->children
[1]->genWidthRTLIL(lvalue
.size(), &new_subst_rvalue_map
);
409 lvalue
.replace(subst_lvalue_from
, subst_lvalue_to
);
411 if (ast
->type
== AST_ASSIGN_EQ
) {
412 subst_rvalue_from
.remove2(unmapped_lvalue
, &subst_rvalue_to
);
413 subst_rvalue_from
.append(unmapped_lvalue
);
414 subst_rvalue_to
.append(rvalue
);
417 removeSignalFromCaseTree(lvalue
, current_case
);
418 current_case
->actions
.push_back(RTLIL::SigSig(lvalue
, rvalue
));
424 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> new_subst_rvalue_map
;
425 for (int i
= 0; i
< SIZE(subst_rvalue_to
); i
++)
426 new_subst_rvalue_map
[subst_rvalue_from
[i
]] = subst_rvalue_to
[i
];
428 RTLIL::SwitchRule
*sw
= new RTLIL::SwitchRule
;
429 sw
->signal
= ast
->children
[0]->genWidthRTLIL(-1, &new_subst_rvalue_map
);
430 current_case
->switches
.push_back(sw
);
432 for (auto &attr
: ast
->attributes
) {
433 if (attr
.second
->type
!= AST_CONSTANT
)
434 log_error("Attribute `%s' with non-constant value at %s:%d!\n",
435 attr
.first
.c_str(), ast
->filename
.c_str(), ast
->linenum
);
436 sw
->attributes
[attr
.first
] = attr
.second
->asAttrConst();
439 RTLIL::SigSpec this_case_eq_lvalue
;
440 collect_lvalues(this_case_eq_lvalue
, ast
, true, false);
442 RTLIL::SigSpec this_case_eq_ltemp
= new_temp_signal(this_case_eq_lvalue
);
444 RTLIL::SigSpec this_case_eq_rvalue
= this_case_eq_lvalue
;
445 this_case_eq_rvalue
.replace(subst_rvalue_from
, subst_rvalue_to
);
447 RTLIL::SigSpec backup_subst_lvalue_from
= subst_lvalue_from
;
448 RTLIL::SigSpec backup_subst_lvalue_to
= subst_lvalue_to
;
450 RTLIL::SigSpec backup_subst_rvalue_from
= subst_rvalue_from
;
451 RTLIL::SigSpec backup_subst_rvalue_to
= subst_rvalue_to
;
453 RTLIL::CaseRule
*default_case
= NULL
;
454 RTLIL::CaseRule
*last_generated_case
= NULL
;
455 for (auto child
: ast
->children
)
457 if (child
== ast
->children
[0])
459 log_assert(child
->type
== AST_COND
);
461 subst_lvalue_from
= backup_subst_lvalue_from
;
462 subst_lvalue_to
= backup_subst_lvalue_to
;
464 subst_rvalue_from
= backup_subst_rvalue_from
;
465 subst_rvalue_to
= backup_subst_rvalue_to
;
467 subst_lvalue_from
.remove2(this_case_eq_lvalue
, &subst_lvalue_to
);
468 subst_lvalue_from
.append(this_case_eq_lvalue
);
469 subst_lvalue_to
.append(this_case_eq_ltemp
);
471 RTLIL::CaseRule
*backup_case
= current_case
;
472 current_case
= new RTLIL::CaseRule
;
473 last_generated_case
= current_case
;
474 addChunkActions(current_case
->actions
, this_case_eq_ltemp
, this_case_eq_rvalue
);
475 for (auto node
: child
->children
) {
476 if (node
->type
== AST_DEFAULT
)
477 default_case
= current_case
;
478 else if (node
->type
== AST_BLOCK
)
481 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> new_subst_rvalue_map
;
482 for (int i
= 0; i
< SIZE(subst_rvalue_to
); i
++)
483 new_subst_rvalue_map
[subst_rvalue_from
[i
]] = subst_rvalue_to
[i
];
484 current_case
->compare
.push_back(node
->genWidthRTLIL(sw
->signal
.size(), &new_subst_rvalue_map
));
487 if (default_case
!= current_case
)
488 sw
->cases
.push_back(current_case
);
490 log_assert(current_case
->compare
.size() == 0);
491 current_case
= backup_case
;
494 if (last_generated_case
!= NULL
&& ast
->get_bool_attribute("\\full_case") && default_case
== NULL
) {
495 last_generated_case
->compare
.clear();
497 if (default_case
== NULL
) {
498 default_case
= new RTLIL::CaseRule
;
499 addChunkActions(default_case
->actions
, this_case_eq_ltemp
, this_case_eq_rvalue
);
501 sw
->cases
.push_back(default_case
);
504 subst_lvalue_from
= backup_subst_lvalue_from
;
505 subst_lvalue_to
= backup_subst_lvalue_to
;
507 subst_rvalue_from
= backup_subst_rvalue_from
;
508 subst_rvalue_to
= backup_subst_rvalue_to
;
510 subst_rvalue_from
.remove2(this_case_eq_lvalue
, &subst_rvalue_to
);
511 subst_rvalue_from
.append(this_case_eq_lvalue
);
512 subst_rvalue_to
.append(this_case_eq_ltemp
);
514 this_case_eq_lvalue
.replace(subst_lvalue_from
, subst_lvalue_to
);
515 removeSignalFromCaseTree(this_case_eq_lvalue
, current_case
);
516 addChunkActions(current_case
->actions
, this_case_eq_lvalue
, this_case_eq_ltemp
);
521 log_error("Found wire declaration in block without label at at %s:%d!\n", ast
->filename
.c_str(), ast
->linenum
);
534 // detect sign and width of an expression
535 void AstNode::detectSignWidthWorker(int &width_hint
, bool &sign_hint
, bool *found_real
)
537 std::string type_name
;
538 bool sub_sign_hint
= true;
539 int sub_width_hint
= -1;
541 AstNode
*range
= NULL
;
542 AstNode
*id_ast
= NULL
;
544 bool local_found_real
= false;
545 if (found_real
== NULL
)
546 found_real
= &local_found_real
;
551 width_hint
= std::max(width_hint
, int(bits
.size()));
558 width_hint
= std::max(width_hint
, 32);
563 if (id_ast
== NULL
&& current_scope
.count(str
))
564 id_ast
= current_scope
.at(str
);
566 log_error("Failed to resolve identifier %s for width detection at %s:%d!\n", str
.c_str(), filename
.c_str(), linenum
);
567 if (id_ast
->type
== AST_PARAMETER
|| id_ast
->type
== AST_LOCALPARAM
) {
568 if (id_ast
->children
.size() > 1 && id_ast
->children
[1]->range_valid
) {
569 this_width
= id_ast
->children
[1]->range_left
- id_ast
->children
[1]->range_right
+ 1;
571 if (id_ast
->children
[0]->type
== AST_CONSTANT
) {
572 this_width
= id_ast
->children
[0]->bits
.size();
574 log_error("Failed to detect width for parameter %s at %s:%d!\n", str
.c_str(), filename
.c_str(), linenum
);
575 if (children
.size() != 0)
577 } else if (id_ast
->type
== AST_WIRE
|| id_ast
->type
== AST_AUTOWIRE
) {
578 if (!id_ast
->range_valid
) {
579 if (id_ast
->type
== AST_AUTOWIRE
)
582 // current_ast_mod->dumpAst(NULL, "mod> ");
584 // id_ast->dumpAst(NULL, "decl> ");
585 // dumpAst(NULL, "ref> ");
586 log_error("Failed to detect with of signal access `%s' at %s:%d!\n", str
.c_str(), filename
.c_str(), linenum
);
589 this_width
= id_ast
->range_left
- id_ast
->range_right
+ 1;
590 if (children
.size() != 0)
593 } else if (id_ast
->type
== AST_GENVAR
) {
595 } else if (id_ast
->type
== AST_MEMORY
) {
596 if (!id_ast
->children
[0]->range_valid
)
597 log_error("Failed to detect with of memory access `%s' at %s:%d!\n", str
.c_str(), filename
.c_str(), linenum
);
598 this_width
= id_ast
->children
[0]->range_left
- id_ast
->children
[0]->range_right
+ 1;
600 log_error("Failed to detect width for identifier %s at %s:%d!\n", str
.c_str(), filename
.c_str(), linenum
);
602 if (range
->children
.size() == 1)
604 else if (!range
->range_valid
) {
605 AstNode
*left_at_zero_ast
= children
[0]->children
[0]->clone();
606 AstNode
*right_at_zero_ast
= children
[0]->children
.size() >= 2 ? children
[0]->children
[1]->clone() : left_at_zero_ast
->clone();
607 while (left_at_zero_ast
->simplify(true, true, false, 1, -1, false, false)) { }
608 while (right_at_zero_ast
->simplify(true, true, false, 1, -1, false, false)) { }
609 if (left_at_zero_ast
->type
!= AST_CONSTANT
|| right_at_zero_ast
->type
!= AST_CONSTANT
)
610 log_error("Unsupported expression on dynamic range select on signal `%s' at %s:%d!\n",
611 str
.c_str(), filename
.c_str(), linenum
);
612 this_width
= left_at_zero_ast
->integer
- right_at_zero_ast
->integer
+ 1;
613 delete left_at_zero_ast
;
614 delete right_at_zero_ast
;
616 this_width
= range
->range_left
- range
->range_right
+ 1;
619 width_hint
= std::max(width_hint
, this_width
);
620 if (!id_ast
->is_signed
)
625 while (children
[0]->simplify(true, false, false, 1, -1, false, false) == true) { }
626 if (children
[0]->type
!= AST_CONSTANT
)
627 log_error("Left operand of tobits expression is not constant at %s:%d!\n", filename
.c_str(), linenum
);
628 children
[1]->detectSignWidthWorker(sub_width_hint
, sign_hint
);
629 width_hint
= std::max(width_hint
, children
[0]->bitsAsConst().as_int());
633 children
.at(0)->detectSignWidthWorker(width_hint
, sub_sign_hint
);
636 case AST_TO_UNSIGNED
:
637 children
.at(0)->detectSignWidthWorker(width_hint
, sub_sign_hint
);
642 for (auto child
: children
) {
644 sub_sign_hint
= true;
645 child
->detectSignWidthWorker(sub_width_hint
, sub_sign_hint
);
646 this_width
+= sub_width_hint
;
648 width_hint
= std::max(width_hint
, this_width
);
653 while (children
[0]->simplify(true, false, false, 1, -1, false, true) == true) { }
654 if (children
[0]->type
!= AST_CONSTANT
)
655 log_error("Left operand of replicate expression is not constant at %s:%d!\n", filename
.c_str(), linenum
);
656 children
[1]->detectSignWidthWorker(sub_width_hint
, sub_sign_hint
);
657 width_hint
= std::max(width_hint
, children
[0]->bitsAsConst().as_int() * sub_width_hint
);
664 children
[0]->detectSignWidthWorker(width_hint
, sign_hint
, found_real
);
671 for (auto child
: children
)
672 child
->detectSignWidthWorker(width_hint
, sign_hint
, found_real
);
678 case AST_REDUCE_XNOR
:
679 case AST_REDUCE_BOOL
:
680 width_hint
= std::max(width_hint
, 1);
685 case AST_SHIFT_RIGHT
:
686 case AST_SHIFT_SLEFT
:
687 case AST_SHIFT_SRIGHT
:
689 children
[0]->detectSignWidthWorker(width_hint
, sign_hint
, found_real
);
700 width_hint
= std::max(width_hint
, 1);
709 for (auto child
: children
)
710 child
->detectSignWidthWorker(width_hint
, sign_hint
, found_real
);
716 width_hint
= std::max(width_hint
, 1);
721 children
.at(1)->detectSignWidthWorker(width_hint
, sign_hint
, found_real
);
722 children
.at(2)->detectSignWidthWorker(width_hint
, sign_hint
, found_real
);
726 if (!id2ast
->is_signed
)
728 if (!id2ast
->children
[0]->range_valid
)
729 log_error("Failed to detect with of memory access `%s' at %s:%d!\n", str
.c_str(), filename
.c_str(), linenum
);
730 this_width
= id2ast
->children
[0]->range_left
- id2ast
->children
[0]->range_right
+ 1;
731 width_hint
= std::max(width_hint
, this_width
);
734 // everything should have been handled above -> print error if not.
736 for (auto f
: log_files
)
737 current_ast
->dumpAst(f
, "verilog-ast> ");
738 log_error("Don't know how to detect sign and width for %s node at %s:%d!\n",
739 type2str(type
).c_str(), filename
.c_str(), linenum
);
746 // detect sign and width of an expression
747 void AstNode::detectSignWidth(int &width_hint
, bool &sign_hint
, bool *found_real
)
753 detectSignWidthWorker(width_hint
, sign_hint
, found_real
);
756 // create RTLIL from an AST node
757 // all generated cells, wires and processes are added to the module pointed to by 'current_module'
758 // when the AST node is an expression (AST_ADD, AST_BIT_XOR, etc.), the result signal is returned.
760 // note that this function is influenced by a number of global variables that might be set when
761 // called from genWidthRTLIL(). also note that this function recursively calls itself to transform
762 // larger expressions into a netlist of cells.
763 RTLIL::SigSpec
AstNode::genRTLIL(int width_hint
, bool sign_hint
)
765 // in the following big switch() statement there are some uses of
766 // Clifford's Device (http://www.clifford.at/cfun/cliffdev/). In this
767 // cases this variable is used to hold the type of the cell that should
768 // be instanciated for this type of AST node.
769 std::string type_name
;
771 current_filename
= filename
;
772 set_line_num(linenum
);
776 // simply ignore this nodes.
777 // they are eighter leftovers from simplify() or are referenced by other nodes
778 // and are only accessed here thru this references
791 // remember the parameter, needed for example in techmap
793 current_module
->avail_parameters
.insert(str
);
796 // create an RTLIL::Wire for an AST_WIRE node
798 if (current_module
->wires_
.count(str
) != 0)
799 log_error("Re-definition of signal `%s' at %s:%d!\n",
800 str
.c_str(), filename
.c_str(), linenum
);
802 log_error("Signal `%s' with non-constant width at %s:%d!\n",
803 str
.c_str(), filename
.c_str(), linenum
);
805 log_assert(range_left
>= range_right
|| (range_left
== -1 && range_right
== 0));
807 RTLIL::Wire
*wire
= current_module
->addWire(str
, range_left
- range_right
+ 1);
808 wire
->attributes
["\\src"] = stringf("%s:%d", filename
.c_str(), linenum
);
809 wire
->start_offset
= range_right
;
810 wire
->port_id
= port_id
;
811 wire
->port_input
= is_input
;
812 wire
->port_output
= is_output
;
813 wire
->upto
= range_swapped
;
815 for (auto &attr
: attributes
) {
816 if (attr
.second
->type
!= AST_CONSTANT
)
817 log_error("Attribute `%s' with non-constant value at %s:%d!\n",
818 attr
.first
.c_str(), filename
.c_str(), linenum
);
819 wire
->attributes
[attr
.first
] = attr
.second
->asAttrConst();
824 // create an RTLIL::Memory for an AST_MEMORY node
826 if (current_module
->memories
.count(str
) != 0)
827 log_error("Re-definition of memory `%s' at %s:%d!\n",
828 str
.c_str(), filename
.c_str(), linenum
);
830 log_assert(children
.size() >= 2);
831 log_assert(children
[0]->type
== AST_RANGE
);
832 log_assert(children
[1]->type
== AST_RANGE
);
834 if (!children
[0]->range_valid
|| !children
[1]->range_valid
)
835 log_error("Memory `%s' with non-constant width or size at %s:%d!\n",
836 str
.c_str(), filename
.c_str(), linenum
);
838 RTLIL::Memory
*memory
= new RTLIL::Memory
;
839 memory
->attributes
["\\src"] = stringf("%s:%d", filename
.c_str(), linenum
);
841 memory
->width
= children
[0]->range_left
- children
[0]->range_right
+ 1;
842 memory
->start_offset
= children
[0]->range_right
;
843 memory
->size
= children
[1]->range_left
- children
[1]->range_right
;
844 current_module
->memories
[memory
->name
] = memory
;
846 if (memory
->size
< 0)
848 memory
->size
+= std::min(children
[1]->range_left
, children
[1]->range_right
) + 1;
850 for (auto &attr
: attributes
) {
851 if (attr
.second
->type
!= AST_CONSTANT
)
852 log_error("Attribute `%s' with non-constant value at %s:%d!\n",
853 attr
.first
.c_str(), filename
.c_str(), linenum
);
854 memory
->attributes
[attr
.first
] = attr
.second
->asAttrConst();
859 // simply return the corresponding RTLIL::SigSpec for an AST_CONSTANT node
863 detectSignWidth(width_hint
, sign_hint
);
865 is_signed
= sign_hint
;
866 return RTLIL::SigSpec(bitsAsConst());
871 RTLIL::SigSpec sig
= realAsConst(width_hint
);
872 log("Warning: converting real value %e to binary %s at %s:%d.\n",
873 realvalue
, log_signal(sig
), filename
.c_str(), linenum
);
877 // simply return the corresponding RTLIL::SigSpec for an AST_IDENTIFIER node
878 // for identifiers with dynamic bit ranges (e.g. "foo[bar]" or "foo[bar+3:bar]") a
879 // shifter cell is created and the output signal of this cell is returned
882 RTLIL::Wire
*wire
= NULL
;
883 RTLIL::SigChunk chunk
;
885 int add_undef_bits_msb
= 0;
886 int add_undef_bits_lsb
= 0;
888 if (id2ast
&& id2ast
->type
== AST_AUTOWIRE
&& current_module
->wires_
.count(str
) == 0) {
889 RTLIL::Wire
*wire
= current_module
->addWire(str
);
890 wire
->attributes
["\\src"] = stringf("%s:%d", filename
.c_str(), linenum
);
893 log("Warning: Identifier `%s' is implicitly declared at %s:%d.\n", str
.c_str(), filename
.c_str(), linenum
);
895 log_error("Identifier `%s' is implicitly declared at %s:%d and `default_nettype is set to none.\n", str
.c_str(), filename
.c_str(), linenum
);
897 else if (id2ast
->type
== AST_PARAMETER
|| id2ast
->type
== AST_LOCALPARAM
) {
898 if (id2ast
->children
[0]->type
!= AST_CONSTANT
)
899 log_error("Parameter %s does not evaluate to constant value at %s:%d!\n",
900 str
.c_str(), filename
.c_str(), linenum
);
901 chunk
= RTLIL::Const(id2ast
->children
[0]->bits
);
902 goto use_const_chunk
;
904 else if (!id2ast
|| (id2ast
->type
!= AST_WIRE
&& id2ast
->type
!= AST_AUTOWIRE
&&
905 id2ast
->type
!= AST_MEMORY
) || current_module
->wires_
.count(str
) == 0)
906 log_error("Identifier `%s' doesn't map to any signal at %s:%d!\n",
907 str
.c_str(), filename
.c_str(), linenum
);
909 if (id2ast
->type
== AST_MEMORY
)
910 log_error("Identifier `%s' does map to an unexpanded memory at %s:%d!\n",
911 str
.c_str(), filename
.c_str(), linenum
);
913 wire
= current_module
->wires_
[str
];
915 chunk
.width
= wire
->width
;
919 if (children
.size() != 0) {
920 log_assert(children
[0]->type
== AST_RANGE
);
921 int source_width
= id2ast
->range_left
- id2ast
->range_right
+ 1;
922 int source_offset
= id2ast
->range_right
;
923 if (!children
[0]->range_valid
) {
924 AstNode
*left_at_zero_ast
= children
[0]->children
[0]->clone();
925 AstNode
*right_at_zero_ast
= children
[0]->children
.size() >= 2 ? children
[0]->children
[1]->clone() : left_at_zero_ast
->clone();
926 while (left_at_zero_ast
->simplify(true, true, false, 1, -1, false, false)) { }
927 while (right_at_zero_ast
->simplify(true, true, false, 1, -1, false, false)) { }
928 if (left_at_zero_ast
->type
!= AST_CONSTANT
|| right_at_zero_ast
->type
!= AST_CONSTANT
)
929 log_error("Unsupported expression on dynamic range select on signal `%s' at %s:%d!\n",
930 str
.c_str(), filename
.c_str(), linenum
);
931 int width
= left_at_zero_ast
->integer
- right_at_zero_ast
->integer
+ 1;
932 AstNode
*fake_ast
= new AstNode(AST_NONE
, clone(), children
[0]->children
.size() >= 2 ?
933 children
[0]->children
[1]->clone() : children
[0]->children
[0]->clone());
934 fake_ast
->children
[0]->delete_children();
935 RTLIL::SigSpec shift_val
= fake_ast
->children
[1]->genRTLIL();
936 if (id2ast
->range_right
!= 0) {
937 shift_val
= current_module
->Sub(NEW_ID
, shift_val
, id2ast
->range_right
, fake_ast
->children
[1]->is_signed
);
938 fake_ast
->children
[1]->is_signed
= true;
940 if (id2ast
->range_swapped
) {
941 shift_val
= current_module
->Sub(NEW_ID
, RTLIL::SigSpec(source_width
- width
), shift_val
, fake_ast
->children
[1]->is_signed
);
942 fake_ast
->children
[1]->is_signed
= true;
944 if (SIZE(shift_val
) >= 32)
945 fake_ast
->children
[1]->is_signed
= true;
946 RTLIL::SigSpec sig
= binop2rtlil(fake_ast
, "$shiftx", width
, fake_ast
->children
[0]->genRTLIL(), shift_val
);
947 delete left_at_zero_ast
;
948 delete right_at_zero_ast
;
952 chunk
.width
= children
[0]->range_left
- children
[0]->range_right
+ 1;
953 chunk
.offset
= children
[0]->range_right
- source_offset
;
954 if (id2ast
->range_swapped
)
955 chunk
.offset
= (id2ast
->range_left
- id2ast
->range_right
+ 1) - (chunk
.offset
+ chunk
.width
);
956 if (chunk
.offset
>= source_width
|| chunk
.offset
+ chunk
.width
< 0) {
957 if (chunk
.width
== 1)
958 log("Warning: Range select out of bounds on signal `%s' at %s:%d: Setting result bit to undef.\n",
959 str
.c_str(), filename
.c_str(), linenum
);
961 log("Warning: Range select out of bounds on signal `%s' at %s:%d: Setting all %d result bits to undef.\n",
962 str
.c_str(), filename
.c_str(), linenum
, chunk
.width
);
963 chunk
= RTLIL::SigChunk(RTLIL::State::Sx
, chunk
.width
);
965 if (chunk
.width
+ chunk
.offset
> source_width
) {
966 add_undef_bits_msb
= (chunk
.width
+ chunk
.offset
) - source_width
;
967 chunk
.width
-= add_undef_bits_msb
;
969 if (chunk
.offset
< 0) {
970 add_undef_bits_lsb
= -chunk
.offset
;
971 chunk
.width
-= add_undef_bits_lsb
;
972 chunk
.offset
+= add_undef_bits_lsb
;
974 if (add_undef_bits_lsb
)
975 log("Warning: Range select out of bounds on signal `%s' at %s:%d: Setting %d LSB bits to undef.\n",
976 str
.c_str(), filename
.c_str(), linenum
, add_undef_bits_lsb
);
977 if (add_undef_bits_msb
)
978 log("Warning: Range select out of bounds on signal `%s' at %s:%d: Setting %d MSB bits to undef.\n",
979 str
.c_str(), filename
.c_str(), linenum
, add_undef_bits_msb
);
984 RTLIL::SigSpec sig
= { RTLIL::SigSpec(RTLIL::State::Sx
, add_undef_bits_msb
), chunk
, RTLIL::SigSpec(RTLIL::State::Sx
, add_undef_bits_lsb
) };
986 if (genRTLIL_subst_ptr
)
987 sig
.replace(*genRTLIL_subst_ptr
);
989 is_signed
= children
.size() > 0 ? false : id2ast
->is_signed
&& sign_hint
;
993 // just pass thru the signal. the parent will evaluate the is_signed property and interpret the SigSpec accordingly
995 case AST_TO_UNSIGNED
: {
996 RTLIL::SigSpec sig
= children
[0]->genRTLIL();
997 if (sig
.size() < width_hint
)
998 sig
.extend_u0(width_hint
, sign_hint
);
999 is_signed
= sign_hint
;
1003 // concatenation of signals can be done directly using RTLIL::SigSpec
1006 for (auto it
= children
.begin(); it
!= children
.end(); it
++)
1007 sig
.append((*it
)->genRTLIL());
1008 if (sig
.size() < width_hint
)
1009 sig
.extend_u0(width_hint
, false);
1013 // replication of signals can be done directly using RTLIL::SigSpec
1014 case AST_REPLICATE
: {
1015 RTLIL::SigSpec left
= children
[0]->genRTLIL();
1016 RTLIL::SigSpec right
= children
[1]->genRTLIL();
1017 if (!left
.is_fully_const())
1018 log_error("Left operand of replicate expression is not constant at %s:%d!\n", filename
.c_str(), linenum
);
1019 int count
= left
.as_int();
1021 for (int i
= 0; i
< count
; i
++)
1023 if (sig
.size() < width_hint
)
1024 sig
.extend_u0(width_hint
, false);
1029 // generate cells for unary operations: $not, $pos, $neg
1030 if (0) { case AST_BIT_NOT
: type_name
= "$not"; }
1031 if (0) { case AST_POS
: type_name
= "$pos"; }
1032 if (0) { case AST_NEG
: type_name
= "$neg"; }
1034 RTLIL::SigSpec arg
= children
[0]->genRTLIL(width_hint
, sign_hint
);
1035 is_signed
= children
[0]->is_signed
;
1036 int width
= arg
.size();
1037 if (width_hint
> 0) {
1039 widthExtend(this, arg
, width
, is_signed
, "$pos");
1041 return uniop2rtlil(this, type_name
, width
, arg
);
1044 // generate cells for binary operations: $and, $or, $xor, $xnor
1045 if (0) { case AST_BIT_AND
: type_name
= "$and"; }
1046 if (0) { case AST_BIT_OR
: type_name
= "$or"; }
1047 if (0) { case AST_BIT_XOR
: type_name
= "$xor"; }
1048 if (0) { case AST_BIT_XNOR
: type_name
= "$xnor"; }
1051 detectSignWidth(width_hint
, sign_hint
);
1052 RTLIL::SigSpec left
= children
[0]->genRTLIL(width_hint
, sign_hint
);
1053 RTLIL::SigSpec right
= children
[1]->genRTLIL(width_hint
, sign_hint
);
1054 int width
= std::max(left
.size(), right
.size());
1057 is_signed
= children
[0]->is_signed
&& children
[1]->is_signed
;
1058 return binop2rtlil(this, type_name
, width
, left
, right
);
1061 // generate cells for unary operations: $reduce_and, $reduce_or, $reduce_xor, $reduce_xnor
1062 if (0) { case AST_REDUCE_AND
: type_name
= "$reduce_and"; }
1063 if (0) { case AST_REDUCE_OR
: type_name
= "$reduce_or"; }
1064 if (0) { case AST_REDUCE_XOR
: type_name
= "$reduce_xor"; }
1065 if (0) { case AST_REDUCE_XNOR
: type_name
= "$reduce_xnor"; }
1067 RTLIL::SigSpec arg
= children
[0]->genRTLIL();
1068 RTLIL::SigSpec sig
= uniop2rtlil(this, type_name
, std::max(width_hint
, 1), arg
);
1072 // generate cells for unary operations: $reduce_bool
1073 // (this is actually just an $reduce_or, but for clearity a different cell type is used)
1074 if (0) { case AST_REDUCE_BOOL
: type_name
= "$reduce_bool"; }
1076 RTLIL::SigSpec arg
= children
[0]->genRTLIL();
1077 RTLIL::SigSpec sig
= arg
.size() > 1 ? uniop2rtlil(this, type_name
, std::max(width_hint
, 1), arg
) : arg
;
1081 // generate cells for binary operations: $shl, $shr, $sshl, $sshr
1082 if (0) { case AST_SHIFT_LEFT
: type_name
= "$shl"; }
1083 if (0) { case AST_SHIFT_RIGHT
: type_name
= "$shr"; }
1084 if (0) { case AST_SHIFT_SLEFT
: type_name
= "$sshl"; }
1085 if (0) { case AST_SHIFT_SRIGHT
: type_name
= "$sshr"; }
1088 detectSignWidth(width_hint
, sign_hint
);
1089 RTLIL::SigSpec left
= children
[0]->genRTLIL(width_hint
, sign_hint
);
1090 RTLIL::SigSpec right
= children
[1]->genRTLIL();
1091 int width
= width_hint
> 0 ? width_hint
: left
.size();
1092 is_signed
= children
[0]->is_signed
;
1093 return binop2rtlil(this, type_name
, width
, left
, right
);
1096 // generate cells for binary operations: $pow
1101 children
[1]->detectSignWidth(right_width
, right_signed
);
1103 detectSignWidth(width_hint
, sign_hint
);
1104 RTLIL::SigSpec left
= children
[0]->genRTLIL(width_hint
, sign_hint
);
1105 RTLIL::SigSpec right
= children
[1]->genRTLIL(right_width
, right_signed
);
1106 int width
= width_hint
> 0 ? width_hint
: left
.size();
1107 is_signed
= children
[0]->is_signed
;
1108 if (!flag_noopt
&& left
.is_fully_const() && left
.as_int() == 2 && !right_signed
)
1109 return binop2rtlil(this, "$shl", width
, RTLIL::SigSpec(1, left
.size()), right
);
1110 return binop2rtlil(this, "$pow", width
, left
, right
);
1113 // generate cells for binary operations: $lt, $le, $eq, $ne, $ge, $gt
1114 if (0) { case AST_LT
: type_name
= "$lt"; }
1115 if (0) { case AST_LE
: type_name
= "$le"; }
1116 if (0) { case AST_EQ
: type_name
= "$eq"; }
1117 if (0) { case AST_NE
: type_name
= "$ne"; }
1118 if (0) { case AST_EQX
: type_name
= "$eqx"; }
1119 if (0) { case AST_NEX
: type_name
= "$nex"; }
1120 if (0) { case AST_GE
: type_name
= "$ge"; }
1121 if (0) { case AST_GT
: type_name
= "$gt"; }
1123 int width
= std::max(width_hint
, 1);
1124 width_hint
= -1, sign_hint
= true;
1125 children
[0]->detectSignWidthWorker(width_hint
, sign_hint
);
1126 children
[1]->detectSignWidthWorker(width_hint
, sign_hint
);
1127 RTLIL::SigSpec left
= children
[0]->genRTLIL(width_hint
, sign_hint
);
1128 RTLIL::SigSpec right
= children
[1]->genRTLIL(width_hint
, sign_hint
);
1129 RTLIL::SigSpec sig
= binop2rtlil(this, type_name
, width
, left
, right
);
1133 // generate cells for binary operations: $add, $sub, $mul, $div, $mod
1134 if (0) { case AST_ADD
: type_name
= "$add"; }
1135 if (0) { case AST_SUB
: type_name
= "$sub"; }
1136 if (0) { case AST_MUL
: type_name
= "$mul"; }
1137 if (0) { case AST_DIV
: type_name
= "$div"; }
1138 if (0) { case AST_MOD
: type_name
= "$mod"; }
1141 detectSignWidth(width_hint
, sign_hint
);
1142 RTLIL::SigSpec left
= children
[0]->genRTLIL(width_hint
, sign_hint
);
1143 RTLIL::SigSpec right
= children
[1]->genRTLIL(width_hint
, sign_hint
);
1145 int width
= std::max(left
.size(), right
.size());
1146 if (width
> width_hint
&& width_hint
> 0)
1148 if (width
< width_hint
) {
1149 if (type
== AST_ADD
|| type
== AST_SUB
|| type
== AST_DIV
)
1151 if (type
== AST_SUB
&& (!children
[0]->is_signed
|| !children
[1]->is_signed
))
1153 if (type
== AST_MUL
)
1154 width
= std::min(left
.size() + right
.size(), width_hint
);
1157 int width
= std::max(std::max(left
.size(), right
.size()), width_hint
);
1159 is_signed
= children
[0]->is_signed
&& children
[1]->is_signed
;
1160 return binop2rtlil(this, type_name
, width
, left
, right
);
1163 // generate cells for binary operations: $logic_and, $logic_or
1164 if (0) { case AST_LOGIC_AND
: type_name
= "$logic_and"; }
1165 if (0) { case AST_LOGIC_OR
: type_name
= "$logic_or"; }
1167 RTLIL::SigSpec left
= children
[0]->genRTLIL();
1168 RTLIL::SigSpec right
= children
[1]->genRTLIL();
1169 return binop2rtlil(this, type_name
, std::max(width_hint
, 1), left
, right
);
1172 // generate cells for unary operations: $logic_not
1175 RTLIL::SigSpec arg
= children
[0]->genRTLIL();
1176 return uniop2rtlil(this, "$logic_not", std::max(width_hint
, 1), arg
);
1179 // generate multiplexer for ternary operator (aka ?:-operator)
1183 detectSignWidth(width_hint
, sign_hint
);
1185 RTLIL::SigSpec cond
= children
[0]->genRTLIL();
1186 RTLIL::SigSpec val1
= children
[1]->genRTLIL(width_hint
, sign_hint
);
1187 RTLIL::SigSpec val2
= children
[2]->genRTLIL(width_hint
, sign_hint
);
1189 if (cond
.size() > 1)
1190 cond
= uniop2rtlil(this, "$reduce_bool", 1, cond
, false);
1192 int width
= std::max(val1
.size(), val2
.size());
1193 is_signed
= children
[1]->is_signed
&& children
[2]->is_signed
;
1194 widthExtend(this, val1
, width
, is_signed
, "$bu0");
1195 widthExtend(this, val2
, width
, is_signed
, "$bu0");
1197 RTLIL::SigSpec sig
= mux2rtlil(this, cond
, val1
, val2
);
1199 if (sig
.size() < width_hint
)
1200 sig
.extend_u0(width_hint
, sign_hint
);
1204 // generate $memrd cells for memory read ports
1207 std::stringstream sstr
;
1208 sstr
<< "$memrd$" << str
<< "$" << filename
<< ":" << linenum
<< "$" << (autoidx
++);
1210 RTLIL::Cell
*cell
= current_module
->addCell(sstr
.str(), "$memrd");
1211 cell
->attributes
["\\src"] = stringf("%s:%d", filename
.c_str(), linenum
);
1213 RTLIL::Wire
*wire
= current_module
->addWire(cell
->name
.str() + "_DATA", current_module
->memories
[str
]->width
);
1214 wire
->attributes
["\\src"] = stringf("%s:%d", filename
.c_str(), linenum
);
1217 while ((1 << addr_bits
) < current_module
->memories
[str
]->size
)
1220 cell
->setPort("\\CLK", RTLIL::SigSpec(RTLIL::State::Sx
, 1));
1221 cell
->setPort("\\ADDR", children
[0]->genWidthRTLIL(addr_bits
));
1222 cell
->setPort("\\DATA", RTLIL::SigSpec(wire
));
1224 cell
->parameters
["\\MEMID"] = RTLIL::Const(str
);
1225 cell
->parameters
["\\ABITS"] = RTLIL::Const(addr_bits
);
1226 cell
->parameters
["\\WIDTH"] = RTLIL::Const(wire
->width
);
1228 cell
->parameters
["\\CLK_ENABLE"] = RTLIL::Const(0);
1229 cell
->parameters
["\\CLK_POLARITY"] = RTLIL::Const(0);
1230 cell
->parameters
["\\TRANSPARENT"] = RTLIL::Const(0);
1232 return RTLIL::SigSpec(wire
);
1235 // generate $memwr cells for memory write ports
1238 std::stringstream sstr
;
1239 sstr
<< "$memwr$" << str
<< "$" << filename
<< ":" << linenum
<< "$" << (autoidx
++);
1241 RTLIL::Cell
*cell
= current_module
->addCell(sstr
.str(), "$memwr");
1242 cell
->attributes
["\\src"] = stringf("%s:%d", filename
.c_str(), linenum
);
1245 while ((1 << addr_bits
) < current_module
->memories
[str
]->size
)
1248 cell
->setPort("\\CLK", RTLIL::SigSpec(RTLIL::State::Sx
, 1));
1249 cell
->setPort("\\ADDR", children
[0]->genWidthRTLIL(addr_bits
));
1250 cell
->setPort("\\DATA", children
[1]->genWidthRTLIL(current_module
->memories
[str
]->width
));
1251 cell
->setPort("\\EN", children
[2]->genRTLIL());
1253 cell
->parameters
["\\MEMID"] = RTLIL::Const(str
);
1254 cell
->parameters
["\\ABITS"] = RTLIL::Const(addr_bits
);
1255 cell
->parameters
["\\WIDTH"] = RTLIL::Const(current_module
->memories
[str
]->width
);
1257 cell
->parameters
["\\CLK_ENABLE"] = RTLIL::Const(0);
1258 cell
->parameters
["\\CLK_POLARITY"] = RTLIL::Const(0);
1260 cell
->parameters
["\\PRIORITY"] = RTLIL::Const(autoidx
-1);
1264 // generate $assert cells
1267 log_assert(children
.size() == 2);
1269 RTLIL::SigSpec check
= children
[0]->genRTLIL();
1270 log_assert(check
.size() == 1);
1272 RTLIL::SigSpec en
= children
[1]->genRTLIL();
1273 log_assert(en
.size() == 1);
1275 std::stringstream sstr
;
1276 sstr
<< "$assert$" << filename
<< ":" << linenum
<< "$" << (autoidx
++);
1278 RTLIL::Cell
*cell
= current_module
->addCell(sstr
.str(), "$assert");
1279 cell
->attributes
["\\src"] = stringf("%s:%d", filename
.c_str(), linenum
);
1281 for (auto &attr
: attributes
) {
1282 if (attr
.second
->type
!= AST_CONSTANT
)
1283 log_error("Attribute `%s' with non-constant value at %s:%d!\n",
1284 attr
.first
.c_str(), filename
.c_str(), linenum
);
1285 cell
->attributes
[attr
.first
] = attr
.second
->asAttrConst();
1288 cell
->setPort("\\A", check
);
1289 cell
->setPort("\\EN", en
);
1293 // add entries to current_module->connections for assignments (outside of always blocks)
1296 if (children
[0]->type
== AST_IDENTIFIER
&& children
[0]->id2ast
&& children
[0]->id2ast
->type
== AST_AUTOWIRE
) {
1297 RTLIL::SigSpec right
= children
[1]->genRTLIL();
1298 RTLIL::SigSpec left
= children
[0]->genWidthRTLIL(right
.size());
1299 current_module
->connect(RTLIL::SigSig(left
, right
));
1301 RTLIL::SigSpec left
= children
[0]->genRTLIL();
1302 RTLIL::SigSpec right
= children
[1]->genWidthRTLIL(left
.size());
1303 current_module
->connect(RTLIL::SigSig(left
, right
));
1308 // create an RTLIL::Cell for an AST_CELL
1311 int port_counter
= 0, para_counter
= 0;
1313 if (current_module
->count_id(str
) != 0)
1314 log_error("Re-definition of cell `%s' at %s:%d!\n",
1315 str
.c_str(), filename
.c_str(), linenum
);
1317 RTLIL::Cell
*cell
= current_module
->addCell(str
, "");
1318 cell
->attributes
["\\src"] = stringf("%s:%d", filename
.c_str(), linenum
);
1320 for (auto it
= children
.begin(); it
!= children
.end(); it
++) {
1321 AstNode
*child
= *it
;
1322 if (child
->type
== AST_CELLTYPE
) {
1323 cell
->type
= child
->str
;
1324 if (flag_icells
&& cell
->type
.substr(0, 2) == "\\$")
1325 cell
->type
= cell
->type
.substr(1);
1328 if (child
->type
== AST_PARASET
) {
1329 if (child
->children
[0]->type
!= AST_CONSTANT
)
1330 log_error("Parameter `%s' with non-constant value at %s:%d!\n",
1331 child
->str
.c_str(), filename
.c_str(), linenum
);
1332 if (child
->str
.size() == 0) {
1334 snprintf(buf
, 100, "$%d", ++para_counter
);
1335 cell
->parameters
[buf
] = child
->children
[0]->asParaConst();
1337 cell
->parameters
[child
->str
] = child
->children
[0]->asParaConst();
1341 if (child
->type
== AST_ARGUMENT
) {
1343 if (child
->children
.size() > 0)
1344 sig
= child
->children
[0]->genRTLIL();
1345 if (child
->str
.size() == 0) {
1347 snprintf(buf
, 100, "$%d", ++port_counter
);
1348 cell
->setPort(buf
, sig
);
1350 cell
->setPort(child
->str
, sig
);
1356 for (auto &attr
: attributes
) {
1357 if (attr
.second
->type
!= AST_CONSTANT
)
1358 log_error("Attribute `%s' with non-constant value at %s:%d!\n",
1359 attr
.first
.c_str(), filename
.c_str(), linenum
);
1360 cell
->attributes
[attr
.first
] = attr
.second
->asAttrConst();
1365 // use ProcessGenerator for always blocks
1367 AstNode
*always
= this->clone();
1368 ProcessGenerator
generator(always
);
1369 ignoreThisSignalsInInitial
.append(generator
.outputSignals
);
1374 AstNode
*always
= this->clone();
1375 ProcessGenerator
generator(always
, ignoreThisSignalsInInitial
);
1379 // everything should have been handled above -> print error if not.
1381 for (auto f
: log_files
)
1382 current_ast
->dumpAst(f
, "verilog-ast> ");
1383 type_name
= type2str(type
);
1384 log_error("Don't know how to generate RTLIL code for %s node at %s:%d!\n",
1385 type_name
.c_str(), filename
.c_str(), linenum
);
1388 return RTLIL::SigSpec();
1391 // this is a wrapper for AstNode::genRTLIL() when a specific signal width is requested and/or
1392 // signals must be substituted before beeing used as input values (used by ProcessGenerator)
1393 // note that this is using some global variables to communicate this special settings to AstNode::genRTLIL().
1394 RTLIL::SigSpec
AstNode::genWidthRTLIL(int width
, std::map
<RTLIL::SigBit
, RTLIL::SigBit
> *new_subst_ptr
)
1396 std::map
<RTLIL::SigBit
, RTLIL::SigBit
> *backup_subst_ptr
= genRTLIL_subst_ptr
;
1399 genRTLIL_subst_ptr
= new_subst_ptr
;
1401 bool sign_hint
= true;
1402 int width_hint
= width
;
1403 detectSignWidthWorker(width_hint
, sign_hint
);
1404 RTLIL::SigSpec sig
= genRTLIL(width_hint
, sign_hint
);
1406 genRTLIL_subst_ptr
= backup_subst_ptr
;
1409 sig
.extend_u0(width
, is_signed
);