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"
39 using namespace AST_INTERNAL
;
41 // helper function for creating RTLIL code for unary operations
42 static RTLIL::SigSpec
uniop2rtlil(AstNode
*that
, std::string type
, int result_width
, const RTLIL::SigSpec
&arg
, bool gen_attributes
= true)
44 std::stringstream sstr
;
45 sstr
<< type
<< "$" << that
->filename
<< ":" << that
->linenum
<< "$" << (RTLIL::autoidx
++);
47 RTLIL::Cell
*cell
= new RTLIL::Cell
;
48 cell
->attributes
["\\src"] = stringf("%s:%d", that
->filename
.c_str(), that
->linenum
);
49 cell
->name
= sstr
.str();
51 current_module
->cells
[cell
->name
] = cell
;
53 RTLIL::Wire
*wire
= new RTLIL::Wire
;
54 wire
->attributes
["\\src"] = stringf("%s:%d", that
->filename
.c_str(), that
->linenum
);
55 wire
->name
= cell
->name
+ "_Y";
56 wire
->width
= result_width
;
57 current_module
->wires
[wire
->name
] = wire
;
59 RTLIL::SigChunk chunk
;
61 chunk
.width
= wire
->width
;
65 sig
.chunks
.push_back(chunk
);
66 sig
.width
= chunk
.width
;
69 for (auto &attr
: that
->attributes
) {
70 if (attr
.second
->type
!= AST_CONSTANT
)
71 log_error("Attribute `%s' with non-constant value at %s:%d!\n",
72 attr
.first
.c_str(), that
->filename
.c_str(), that
->linenum
);
73 cell
->attributes
[attr
.first
] = attr
.second
->asAttrConst();
76 cell
->parameters
["\\A_SIGNED"] = RTLIL::Const(that
->children
[0]->is_signed
);
77 cell
->parameters
["\\A_WIDTH"] = RTLIL::Const(arg
.width
);
78 cell
->connections
["\\A"] = arg
;
80 cell
->parameters
["\\Y_WIDTH"] = result_width
;
81 cell
->connections
["\\Y"] = sig
;
85 // helper function for extending bit width (preferred over SigSpec::extend() because of correct undef propagation in ConstEval)
86 static void widthExtend(AstNode
*that
, RTLIL::SigSpec
&sig
, int width
, bool is_signed
, std::string celltype
)
88 if (width
<= sig
.width
) {
89 sig
.extend(width
, is_signed
);
93 std::stringstream sstr
;
94 sstr
<< "$extend" << "$" << that
->filename
<< ":" << that
->linenum
<< "$" << (RTLIL::autoidx
++);
96 RTLIL::Cell
*cell
= new RTLIL::Cell
;
97 cell
->attributes
["\\src"] = stringf("%s:%d", that
->filename
.c_str(), that
->linenum
);
98 cell
->name
= sstr
.str();
99 cell
->type
= celltype
;
100 current_module
->cells
[cell
->name
] = cell
;
102 RTLIL::Wire
*wire
= new RTLIL::Wire
;
103 wire
->attributes
["\\src"] = stringf("%s:%d", that
->filename
.c_str(), that
->linenum
);
104 wire
->name
= cell
->name
+ "_Y";
106 current_module
->wires
[wire
->name
] = wire
;
108 RTLIL::SigChunk chunk
;
110 chunk
.width
= wire
->width
;
113 RTLIL::SigSpec new_sig
;
114 new_sig
.chunks
.push_back(chunk
);
115 new_sig
.width
= chunk
.width
;
118 for (auto &attr
: that
->attributes
) {
119 if (attr
.second
->type
!= AST_CONSTANT
)
120 log_error("Attribute `%s' with non-constant value at %s:%d!\n",
121 attr
.first
.c_str(), that
->filename
.c_str(), that
->linenum
);
122 cell
->attributes
[attr
.first
] = attr
.second
->asAttrConst();
125 cell
->parameters
["\\A_SIGNED"] = RTLIL::Const(is_signed
);
126 cell
->parameters
["\\A_WIDTH"] = RTLIL::Const(sig
.width
);
127 cell
->connections
["\\A"] = sig
;
129 cell
->parameters
["\\Y_WIDTH"] = width
;
130 cell
->connections
["\\Y"] = new_sig
;
134 // helper function for creating RTLIL code for binary operations
135 static RTLIL::SigSpec
binop2rtlil(AstNode
*that
, std::string type
, int result_width
, const RTLIL::SigSpec
&left
, const RTLIL::SigSpec
&right
)
137 std::stringstream sstr
;
138 sstr
<< type
<< "$" << that
->filename
<< ":" << that
->linenum
<< "$" << (RTLIL::autoidx
++);
140 RTLIL::Cell
*cell
= new RTLIL::Cell
;
141 cell
->attributes
["\\src"] = stringf("%s:%d", that
->filename
.c_str(), that
->linenum
);
142 cell
->name
= sstr
.str();
144 current_module
->cells
[cell
->name
] = cell
;
146 RTLIL::Wire
*wire
= new RTLIL::Wire
;
147 wire
->attributes
["\\src"] = stringf("%s:%d", that
->filename
.c_str(), that
->linenum
);
148 wire
->name
= cell
->name
+ "_Y";
149 wire
->width
= result_width
;
150 current_module
->wires
[wire
->name
] = wire
;
152 RTLIL::SigChunk chunk
;
154 chunk
.width
= wire
->width
;
158 sig
.chunks
.push_back(chunk
);
159 sig
.width
= chunk
.width
;
161 for (auto &attr
: that
->attributes
) {
162 if (attr
.second
->type
!= AST_CONSTANT
)
163 log_error("Attribute `%s' with non-constant value at %s:%d!\n",
164 attr
.first
.c_str(), that
->filename
.c_str(), that
->linenum
);
165 cell
->attributes
[attr
.first
] = attr
.second
->asAttrConst();
168 cell
->parameters
["\\A_SIGNED"] = RTLIL::Const(that
->children
[0]->is_signed
);
169 cell
->parameters
["\\B_SIGNED"] = RTLIL::Const(that
->children
[1]->is_signed
);
171 cell
->parameters
["\\A_WIDTH"] = RTLIL::Const(left
.width
);
172 cell
->parameters
["\\B_WIDTH"] = RTLIL::Const(right
.width
);
174 cell
->connections
["\\A"] = left
;
175 cell
->connections
["\\B"] = right
;
177 cell
->parameters
["\\Y_WIDTH"] = result_width
;
178 cell
->connections
["\\Y"] = sig
;
182 // helper function for creating RTLIL code for multiplexers
183 static RTLIL::SigSpec
mux2rtlil(AstNode
*that
, const RTLIL::SigSpec
&cond
, const RTLIL::SigSpec
&left
, const RTLIL::SigSpec
&right
)
185 assert(cond
.width
== 1);
187 std::stringstream sstr
;
188 sstr
<< "$ternary$" << that
->filename
<< ":" << that
->linenum
<< "$" << (RTLIL::autoidx
++);
190 RTLIL::Cell
*cell
= new RTLIL::Cell
;
191 cell
->attributes
["\\src"] = stringf("%s:%d", that
->filename
.c_str(), that
->linenum
);
192 cell
->name
= sstr
.str();
194 current_module
->cells
[cell
->name
] = cell
;
196 RTLIL::Wire
*wire
= new RTLIL::Wire
;
197 wire
->attributes
["\\src"] = stringf("%s:%d", that
->filename
.c_str(), that
->linenum
);
198 wire
->name
= cell
->name
+ "_Y";
199 wire
->width
= left
.width
;
200 current_module
->wires
[wire
->name
] = wire
;
202 RTLIL::SigChunk chunk
;
204 chunk
.width
= wire
->width
;
208 sig
.chunks
.push_back(chunk
);
209 sig
.width
= chunk
.width
;
211 for (auto &attr
: that
->attributes
) {
212 if (attr
.second
->type
!= AST_CONSTANT
)
213 log_error("Attribute `%s' with non-constant value at %s:%d!\n",
214 attr
.first
.c_str(), that
->filename
.c_str(), that
->linenum
);
215 cell
->attributes
[attr
.first
] = attr
.second
->asAttrConst();
218 cell
->parameters
["\\WIDTH"] = RTLIL::Const(left
.width
);
220 cell
->connections
["\\A"] = right
;
221 cell
->connections
["\\B"] = left
;
222 cell
->connections
["\\S"] = cond
;
223 cell
->connections
["\\Y"] = sig
;
228 // helper class for converting AST always nodes to RTLIL processes
229 struct AST_INTERNAL::ProcessGenerator
231 // input and output structures
233 RTLIL::SigSpec initSyncSignals
;
234 RTLIL::Process
*proc
;
235 const RTLIL::SigSpec
&outputSignals
;
237 // This always points to the RTLIL::CaseRule beeing filled at the moment
238 RTLIL::CaseRule
*current_case
;
240 // This two variables contain the replacement pattern to be used in the right hand side
241 // of an assignment. E.g. in the code "foo = bar; foo = func(foo);" the foo in the right
242 // hand side of the 2nd assignment needs to be replace with the temporary signal holding
243 // the value assigned in the first assignment. So when the first assignement is processed
244 // the according information is appended to subst_rvalue_from and subst_rvalue_to.
245 RTLIL::SigSpec subst_rvalue_from
, subst_rvalue_to
;
247 // This two variables contain the replacement pattern to be used in the left hand side
248 // of an assignment. E.g. in the code "always @(posedge clk) foo <= bar" the signal bar
249 // should not be connected to the signal foo. Instead it must be connected to the temporary
250 // signal that is used as input for the register that drives the signal foo.
251 RTLIL::SigSpec subst_lvalue_from
, subst_lvalue_to
;
253 // The code here generates a number of temprorary signal for each output register. This
254 // map helps generating nice numbered names for all this temporary signals.
255 std::map
<RTLIL::Wire
*, int> new_temp_count
;
257 // Buffer for generating the init action
258 RTLIL::SigSpec init_lvalue
, init_rvalue
;
260 ProcessGenerator(AstNode
*always
, RTLIL::SigSpec initSyncSignalsArg
= RTLIL::SigSpec()) : always(always
), initSyncSignals(initSyncSignalsArg
), outputSignals(subst_lvalue_from
)
262 // generate process and simple root case
263 proc
= new RTLIL::Process
;
264 proc
->attributes
["\\src"] = stringf("%s:%d", always
->filename
.c_str(), always
->linenum
);
265 proc
->name
= stringf("$proc$%s:%d$%d", always
->filename
.c_str(), always
->linenum
, RTLIL::autoidx
++);
266 for (auto &attr
: always
->attributes
) {
267 if (attr
.second
->type
!= AST_CONSTANT
)
268 log_error("Attribute `%s' with non-constant value at %s:%d!\n",
269 attr
.first
.c_str(), always
->filename
.c_str(), always
->linenum
);
270 proc
->attributes
[attr
.first
] = attr
.second
->asAttrConst();
272 current_module
->processes
[proc
->name
] = proc
;
273 current_case
= &proc
->root_case
;
275 // create initial temporary signal for all output registers
276 collect_lvalues(subst_lvalue_from
, always
, true, true);
277 subst_lvalue_to
= new_temp_signal(subst_lvalue_from
);
279 bool found_anyedge_syncs
= false;
280 for (auto child
: always
->children
)
281 if (child
->type
== AST_EDGE
)
282 found_anyedge_syncs
= true;
284 if (found_anyedge_syncs
) {
285 log("Note: Assuming pure combinatorial block at %s:%d in\n", always
->filename
.c_str(), always
->linenum
);
286 log("compliance with IEC 62142(E):2005 / IEEE Std. 1364.1(E):2002. Recommending\n");
287 log("use of @* instead of @(...) for better match of synthesis and simulation.\n");
290 // create syncs for the process
291 bool found_clocked_sync
= false;
292 for (auto child
: always
->children
)
293 if (child
->type
== AST_POSEDGE
|| child
->type
== AST_NEGEDGE
) {
294 found_clocked_sync
= true;
295 if (found_anyedge_syncs
)
296 log_error("Found non-synthesizable event list at %s:%d!\n", always
->filename
.c_str(), always
->linenum
);
297 RTLIL::SyncRule
*syncrule
= new RTLIL::SyncRule
;
298 syncrule
->type
= child
->type
== AST_POSEDGE
? RTLIL::STp
: RTLIL::STn
;
299 syncrule
->signal
= child
->children
[0]->genRTLIL();
300 addChunkActions(syncrule
->actions
, subst_lvalue_from
, subst_lvalue_to
, true);
301 proc
->syncs
.push_back(syncrule
);
303 if (proc
->syncs
.empty()) {
304 RTLIL::SyncRule
*syncrule
= new RTLIL::SyncRule
;
305 syncrule
->type
= RTLIL::STa
;
306 syncrule
->signal
= RTLIL::SigSpec();
307 addChunkActions(syncrule
->actions
, subst_lvalue_from
, subst_lvalue_to
, true);
308 proc
->syncs
.push_back(syncrule
);
311 // create initial assignments for the temporary signals
312 if ((flag_nolatches
|| always
->get_bool_attribute("\\nolatches") || current_module
->get_bool_attribute("\\nolatches")) && !found_clocked_sync
) {
313 subst_rvalue_from
= subst_lvalue_from
;
314 subst_rvalue_to
= RTLIL::SigSpec(RTLIL::State::Sx
, subst_rvalue_from
.width
);
316 addChunkActions(current_case
->actions
, subst_lvalue_to
, subst_lvalue_from
);
320 for (auto child
: always
->children
)
321 if (child
->type
== AST_BLOCK
)
324 if (initSyncSignals
.width
> 0)
326 RTLIL::SyncRule
*sync
= new RTLIL::SyncRule
;
327 sync
->type
= RTLIL::SyncType::STi
;
328 proc
->syncs
.push_back(sync
);
330 assert(init_lvalue
.width
== init_rvalue
.width
);
331 init_lvalue
.optimize();
332 init_rvalue
.optimize();
335 for (size_t i
= 0; i
< init_lvalue
.chunks
.size(); i
++) {
336 RTLIL::SigSpec lhs
= init_lvalue
.chunks
[i
];
337 RTLIL::SigSpec rhs
= init_rvalue
.extract(offset
, init_lvalue
.chunks
[i
].width
);
338 sync
->actions
.push_back(RTLIL::SigSig(lhs
, rhs
));
344 // create new temporary signals
345 RTLIL::SigSpec
new_temp_signal(RTLIL::SigSpec sig
)
348 for (size_t i
= 0; i
< sig
.chunks
.size(); i
++)
350 RTLIL::SigChunk
&chunk
= sig
.chunks
[i
];
351 if (chunk
.wire
== NULL
)
354 RTLIL::Wire
*wire
= new RTLIL::Wire
;
355 wire
->attributes
["\\src"] = stringf("%s:%d", always
->filename
.c_str(), always
->linenum
);
357 wire
->name
= stringf("$%d%s[%d:%d]", new_temp_count
[chunk
.wire
]++,
358 chunk
.wire
->name
.c_str(), chunk
.width
+chunk
.offset
-1, chunk
.offset
);;
359 if (chunk
.wire
->name
.find('$') != std::string::npos
)
360 wire
->name
+= stringf("$%d", RTLIL::autoidx
++);
361 } while (current_module
->wires
.count(wire
->name
) > 0);
362 wire
->width
= chunk
.width
;
363 current_module
->wires
[wire
->name
] = wire
;
371 // recursively traverse the AST an collect all assigned signals
372 void collect_lvalues(RTLIL::SigSpec
®
, AstNode
*ast
, bool type_eq
, bool type_le
, bool run_sort_and_unify
= true)
377 for (auto child
: ast
->children
)
378 if (child
!= ast
->children
[0]) {
379 assert(child
->type
== AST_COND
);
380 collect_lvalues(reg
, child
, type_eq
, type_le
, false);
387 for (auto child
: ast
->children
)
388 if (child
->type
== AST_BLOCK
)
389 collect_lvalues(reg
, child
, type_eq
, type_le
, false);
393 for (auto child
: ast
->children
) {
394 if (child
->type
== AST_ASSIGN_EQ
&& type_eq
)
395 reg
.append(child
->children
[0]->genRTLIL());
396 if (child
->type
== AST_ASSIGN_LE
&& type_le
)
397 reg
.append(child
->children
[0]->genRTLIL());
398 if (child
->type
== AST_CASE
|| child
->type
== AST_BLOCK
)
399 collect_lvalues(reg
, child
, type_eq
, type_le
, false);
407 if (run_sort_and_unify
)
408 reg
.sort_and_unify();
411 // remove all assignments to the given signal pattern in a case and all its children.
412 // e.g. when the last statement in the code "a = 23; if (b) a = 42; a = 0;" is processed this
413 // function is called to clean up the first two assignments as they are overwritten by
414 // the third assignment.
415 void removeSignalFromCaseTree(RTLIL::SigSpec pattern
, RTLIL::CaseRule
*cs
)
417 for (auto it
= cs
->actions
.begin(); it
!= cs
->actions
.end(); it
++)
418 it
->first
.remove2(pattern
, &it
->second
);
420 for (auto it
= cs
->switches
.begin(); it
!= cs
->switches
.end(); it
++)
421 for (auto it2
= (*it
)->cases
.begin(); it2
!= (*it
)->cases
.end(); it2
++)
422 removeSignalFromCaseTree(pattern
, *it2
);
425 // add an assignment (aka "action") but split it up in chunks. this way huge assignments
426 // are avoided and the generated $mux cells have a more "natural" size.
427 void addChunkActions(std::vector
<RTLIL::SigSig
> &actions
, RTLIL::SigSpec lvalue
, RTLIL::SigSpec rvalue
, bool inSyncRule
= false)
429 if (inSyncRule
&& initSyncSignals
.width
> 0) {
430 init_lvalue
.append(lvalue
.extract(initSyncSignals
));
431 init_rvalue
.append(lvalue
.extract(initSyncSignals
, &rvalue
));
432 lvalue
.remove2(initSyncSignals
, &rvalue
);
434 assert(lvalue
.width
== rvalue
.width
);
439 for (size_t i
= 0; i
< lvalue
.chunks
.size(); i
++) {
440 RTLIL::SigSpec lhs
= lvalue
.chunks
[i
];
441 RTLIL::SigSpec rhs
= rvalue
.extract(offset
, lvalue
.chunks
[i
].width
);
442 if (inSyncRule
&& lvalue
.chunks
[i
].wire
&& lvalue
.chunks
[i
].wire
->get_bool_attribute("\\nosync"))
443 rhs
= RTLIL::SigSpec(RTLIL::State::Sx
, rhs
.width
);
444 actions
.push_back(RTLIL::SigSig(lhs
, rhs
));
449 // recursively process the AST and fill the RTLIL::Process
450 void processAst(AstNode
*ast
)
455 for (auto child
: ast
->children
)
462 RTLIL::SigSpec unmapped_lvalue
= ast
->children
[0]->genRTLIL(), lvalue
= unmapped_lvalue
;
463 RTLIL::SigSpec rvalue
= ast
->children
[1]->genWidthRTLIL(lvalue
.width
, &subst_rvalue_from
, &subst_rvalue_to
);
464 lvalue
.replace(subst_lvalue_from
, subst_lvalue_to
);
466 if (ast
->type
== AST_ASSIGN_EQ
) {
467 subst_rvalue_from
.remove2(unmapped_lvalue
, &subst_rvalue_to
);
468 subst_rvalue_from
.append(unmapped_lvalue
);
469 subst_rvalue_from
.optimize();
470 subst_rvalue_to
.append(rvalue
);
471 subst_rvalue_to
.optimize();
474 removeSignalFromCaseTree(lvalue
, current_case
);
475 current_case
->actions
.push_back(RTLIL::SigSig(lvalue
, rvalue
));
481 RTLIL::SwitchRule
*sw
= new RTLIL::SwitchRule
;
482 sw
->signal
= ast
->children
[0]->genWidthRTLIL(-1, &subst_rvalue_from
, &subst_rvalue_to
);
483 current_case
->switches
.push_back(sw
);
485 for (auto &attr
: ast
->attributes
) {
486 if (attr
.second
->type
!= AST_CONSTANT
)
487 log_error("Attribute `%s' with non-constant value at %s:%d!\n",
488 attr
.first
.c_str(), ast
->filename
.c_str(), ast
->linenum
);
489 sw
->attributes
[attr
.first
] = attr
.second
->asAttrConst();
492 RTLIL::SigSpec this_case_eq_lvalue
;
493 collect_lvalues(this_case_eq_lvalue
, ast
, true, false);
495 RTLIL::SigSpec this_case_eq_ltemp
= new_temp_signal(this_case_eq_lvalue
);
497 RTLIL::SigSpec this_case_eq_rvalue
= this_case_eq_lvalue
;
498 this_case_eq_rvalue
.replace(subst_rvalue_from
, subst_rvalue_to
);
500 RTLIL::SigSpec backup_subst_lvalue_from
= subst_lvalue_from
;
501 RTLIL::SigSpec backup_subst_lvalue_to
= subst_lvalue_to
;
503 RTLIL::SigSpec backup_subst_rvalue_from
= subst_rvalue_from
;
504 RTLIL::SigSpec backup_subst_rvalue_to
= subst_rvalue_to
;
506 RTLIL::CaseRule
*default_case
= NULL
;
507 RTLIL::CaseRule
*last_generated_case
= NULL
;
508 for (auto child
: ast
->children
)
510 if (child
== ast
->children
[0])
512 assert(child
->type
== AST_COND
);
514 subst_lvalue_from
= backup_subst_lvalue_from
;
515 subst_lvalue_to
= backup_subst_lvalue_to
;
517 subst_rvalue_from
= backup_subst_rvalue_from
;
518 subst_rvalue_to
= backup_subst_rvalue_to
;
520 subst_lvalue_from
.remove2(this_case_eq_lvalue
, &subst_lvalue_to
);
521 subst_lvalue_from
.append(this_case_eq_lvalue
);
522 subst_lvalue_from
.optimize();
523 subst_lvalue_to
.append(this_case_eq_ltemp
);
524 subst_lvalue_to
.optimize();
526 RTLIL::CaseRule
*backup_case
= current_case
;
527 current_case
= new RTLIL::CaseRule
;
528 last_generated_case
= current_case
;
529 addChunkActions(current_case
->actions
, this_case_eq_ltemp
, this_case_eq_rvalue
);
530 for (auto node
: child
->children
) {
531 if (node
->type
== AST_DEFAULT
)
532 default_case
= current_case
;
533 else if (node
->type
== AST_BLOCK
)
536 current_case
->compare
.push_back(node
->genWidthRTLIL(sw
->signal
.width
, &subst_rvalue_from
, &subst_rvalue_to
));
538 if (default_case
!= current_case
)
539 sw
->cases
.push_back(current_case
);
541 log_assert(current_case
->compare
.size() == 0);
542 current_case
= backup_case
;
545 if (last_generated_case
!= NULL
&& ast
->get_bool_attribute("\\full_case") && default_case
== NULL
) {
546 last_generated_case
->compare
.clear();
548 if (default_case
== NULL
) {
549 default_case
= new RTLIL::CaseRule
;
550 addChunkActions(default_case
->actions
, this_case_eq_ltemp
, this_case_eq_rvalue
);
552 sw
->cases
.push_back(default_case
);
555 subst_lvalue_from
= backup_subst_lvalue_from
;
556 subst_lvalue_to
= backup_subst_lvalue_to
;
558 subst_rvalue_from
= backup_subst_rvalue_from
;
559 subst_rvalue_to
= backup_subst_rvalue_to
;
561 subst_rvalue_from
.remove2(this_case_eq_lvalue
, &subst_rvalue_to
);
562 subst_rvalue_from
.append(this_case_eq_lvalue
);
563 subst_rvalue_from
.optimize();
564 subst_rvalue_to
.append(this_case_eq_ltemp
);
565 subst_rvalue_to
.optimize();
567 this_case_eq_lvalue
.replace(subst_lvalue_from
, subst_lvalue_to
);
568 removeSignalFromCaseTree(this_case_eq_lvalue
, current_case
);
569 addChunkActions(current_case
->actions
, this_case_eq_lvalue
, this_case_eq_ltemp
);
574 log_error("Found wire declaration in block without label at at %s:%d!\n", ast
->filename
.c_str(), ast
->linenum
);
587 // detect sign and width of an expression
588 void AstNode::detectSignWidthWorker(int &width_hint
, bool &sign_hint
)
590 std::string type_name
;
591 bool sub_sign_hint
= true;
592 int sub_width_hint
= -1;
594 AstNode
*range
= NULL
;
595 AstNode
*id_ast
= NULL
;
600 width_hint
= std::max(width_hint
, int(bits
.size()));
607 if (id_ast
== NULL
&& current_scope
.count(str
))
608 id_ast
= current_scope
.at(str
);
610 log_error("Failed to resolve identifier %s for width detection at %s:%d!\n", str
.c_str(), filename
.c_str(), linenum
);
611 if (id_ast
->type
== AST_PARAMETER
|| id_ast
->type
== AST_LOCALPARAM
) {
612 if (id_ast
->children
.size() > 1 && id_ast
->children
[1]->range_valid
) {
613 this_width
= id_ast
->children
[1]->range_left
- id_ast
->children
[1]->range_right
+ 1;
615 if (id_ast
->children
[0]->type
== AST_CONSTANT
) {
616 this_width
= id_ast
->children
[0]->bits
.size();
618 log_error("Failed to detect width for parameter %s at %s:%d!\n", str
.c_str(), filename
.c_str(), linenum
);
619 if (children
.size() != 0)
621 } else if (id_ast
->type
== AST_WIRE
|| id_ast
->type
== AST_AUTOWIRE
) {
622 if (!id_ast
->range_valid
) {
623 if (id_ast
->type
== AST_AUTOWIRE
)
626 // current_ast_mod->dumpAst(stdout, "");
628 // dumpAst(stdout, "");
630 log_error("Failed to detect with of signal access `%s' at %s:%d!\n", str
.c_str(), filename
.c_str(), linenum
);
633 this_width
= id_ast
->range_left
- id_ast
->range_right
+ 1;
634 if (children
.size() != 0)
637 } else if (id_ast
->type
== AST_GENVAR
) {
639 } else if (id_ast
->type
== AST_MEMORY
) {
640 if (!id_ast
->children
[0]->range_valid
)
641 log_error("Failed to detect with of memory access `%s' at %s:%d!\n", str
.c_str(), filename
.c_str(), linenum
);
642 this_width
= id_ast
->children
[0]->range_left
- id_ast
->children
[0]->range_right
+ 1;
644 log_error("Failed to detect width for identifier %s at %s:%d!\n", str
.c_str(), filename
.c_str(), linenum
);
646 if (range
->children
.size() == 1)
648 else if (!range
->range_valid
) {
649 AstNode
*left_at_zero_ast
= children
[0]->children
[0]->clone();
650 AstNode
*right_at_zero_ast
= children
[0]->children
.size() >= 2 ? children
[0]->children
[1]->clone() : left_at_zero_ast
->clone();
651 while (left_at_zero_ast
->simplify(true, true, false, 1, -1, false, false)) { }
652 while (right_at_zero_ast
->simplify(true, true, false, 1, -1, false, false)) { }
653 if (left_at_zero_ast
->type
!= AST_CONSTANT
|| right_at_zero_ast
->type
!= AST_CONSTANT
)
654 log_error("Unsupported expression on dynamic range select on signal `%s' at %s:%d!\n",
655 str
.c_str(), filename
.c_str(), linenum
);
656 this_width
= left_at_zero_ast
->integer
- right_at_zero_ast
->integer
+ 1;
657 delete left_at_zero_ast
;
658 delete right_at_zero_ast
;
660 this_width
= range
->range_left
- range
->range_right
+ 1;
662 width_hint
= std::max(width_hint
, this_width
);
663 if (!id_ast
->is_signed
)
668 while (children
[0]->simplify(true, false, false, 1, -1, false, false) == true) { }
669 if (children
[0]->type
!= AST_CONSTANT
)
670 log_error("Left operand of tobits expression is not constant at %s:%d!\n", filename
.c_str(), linenum
);
671 children
[1]->detectSignWidthWorker(sub_width_hint
, sign_hint
);
672 width_hint
= std::max(width_hint
, children
[0]->bitsAsConst().as_int());
676 children
.at(0)->detectSignWidthWorker(width_hint
, sub_sign_hint
);
679 case AST_TO_UNSIGNED
:
680 children
.at(0)->detectSignWidthWorker(width_hint
, sub_sign_hint
);
685 for (auto child
: children
) {
687 sub_sign_hint
= true;
688 child
->detectSignWidthWorker(sub_width_hint
, sub_sign_hint
);
689 this_width
+= sub_width_hint
;
691 width_hint
= std::max(width_hint
, this_width
);
696 while (children
[0]->simplify(true, false, false, 1, -1, false, false) == true) { }
697 if (children
[0]->type
!= AST_CONSTANT
)
698 log_error("Left operand of replicate expression is not constant at %s:%d!\n", filename
.c_str(), linenum
);
699 children
[1]->detectSignWidthWorker(sub_width_hint
, sub_sign_hint
);
700 width_hint
= std::max(width_hint
, children
[0]->bitsAsConst().as_int() * sub_width_hint
);
707 children
[0]->detectSignWidthWorker(width_hint
, sign_hint
);
714 for (auto child
: children
)
715 child
->detectSignWidthWorker(width_hint
, sign_hint
);
721 case AST_REDUCE_XNOR
:
722 case AST_REDUCE_BOOL
:
723 width_hint
= std::max(width_hint
, 1);
728 case AST_SHIFT_RIGHT
:
729 case AST_SHIFT_SLEFT
:
730 case AST_SHIFT_SRIGHT
:
732 children
[0]->detectSignWidthWorker(width_hint
, sign_hint
);
743 width_hint
= std::max(width_hint
, 1);
752 for (auto child
: children
)
753 child
->detectSignWidthWorker(width_hint
, sign_hint
);
759 width_hint
= std::max(width_hint
, 1);
764 children
.at(1)->detectSignWidthWorker(width_hint
, sign_hint
);
765 children
.at(2)->detectSignWidthWorker(width_hint
, sign_hint
);
769 if (!id2ast
->is_signed
)
771 if (!id2ast
->children
[0]->range_valid
)
772 log_error("Failed to detect with of memory access `%s' at %s:%d!\n", str
.c_str(), filename
.c_str(), linenum
);
773 this_width
= id2ast
->children
[0]->range_left
- id2ast
->children
[0]->range_right
+ 1;
774 width_hint
= std::max(width_hint
, this_width
);
777 // everything should have been handled above -> print error if not.
779 for (auto f
: log_files
)
780 current_ast
->dumpAst(f
, "verilog-ast> ");
781 log_error("Don't know how to detect sign and width for %s node at %s:%d!\n",
782 type2str(type
).c_str(), filename
.c_str(), linenum
);
786 // detect sign and width of an expression
787 void AstNode::detectSignWidth(int &width_hint
, bool &sign_hint
)
789 width_hint
= -1, sign_hint
= true;
790 detectSignWidthWorker(width_hint
, sign_hint
);
793 // create RTLIL from an AST node
794 // all generated cells, wires and processes are added to the module pointed to by 'current_module'
795 // when the AST node is an expression (AST_ADD, AST_BIT_XOR, etc.), the result signal is returned.
797 // note that this function is influenced by a number of global variables that might be set when
798 // called from genWidthRTLIL(). also note that this function recursively calls itself to transform
799 // larger expressions into a netlist of cells.
800 RTLIL::SigSpec
AstNode::genRTLIL(int width_hint
, bool sign_hint
)
802 // in the following big switch() statement there are some uses of
803 // Clifford's Device (http://www.clifford.at/cfun/cliffdev/). In this
804 // cases this variable is used to hold the type of the cell that should
805 // be instanciated for this type of AST node.
806 std::string type_name
;
808 current_filename
= filename
;
809 set_line_num(linenum
);
813 // simply ignore this nodes.
814 // they are eighter leftovers from simplify() or are referenced by other nodes
815 // and are only accessed here thru this references
828 // remember the parameter, needed for example in techmap
830 current_module
->avail_parameters
.insert(str
);
833 // create an RTLIL::Wire for an AST_WIRE node
835 if (current_module
->wires
.count(str
) != 0)
836 log_error("Re-definition of signal `%s' at %s:%d!\n",
837 str
.c_str(), filename
.c_str(), linenum
);
839 log_error("Signal `%s' with non-constant width at %s:%d!\n",
840 str
.c_str(), filename
.c_str(), linenum
);
842 if (range_left
< range_right
&& (range_left
!= -1 || range_right
!= 0)) {
843 int tmp
= range_left
;
844 range_left
= range_right
;
848 RTLIL::Wire
*wire
= new RTLIL::Wire
;
849 wire
->attributes
["\\src"] = stringf("%s:%d", filename
.c_str(), linenum
);
851 wire
->width
= range_left
- range_right
+ 1;
852 wire
->start_offset
= range_right
;
853 wire
->port_id
= port_id
;
854 wire
->port_input
= is_input
;
855 wire
->port_output
= is_output
;
856 current_module
->wires
[wire
->name
] = wire
;
858 for (auto &attr
: attributes
) {
859 if (attr
.second
->type
!= AST_CONSTANT
)
860 log_error("Attribute `%s' with non-constant value at %s:%d!\n",
861 attr
.first
.c_str(), filename
.c_str(), linenum
);
862 wire
->attributes
[attr
.first
] = attr
.second
->asAttrConst();
867 // create an RTLIL::Memory for an AST_MEMORY node
869 if (current_module
->memories
.count(str
) != 0)
870 log_error("Re-definition of memory `%s' at %s:%d!\n",
871 str
.c_str(), filename
.c_str(), linenum
);
873 assert(children
.size() >= 2);
874 assert(children
[0]->type
== AST_RANGE
);
875 assert(children
[1]->type
== AST_RANGE
);
877 if (!children
[0]->range_valid
|| !children
[1]->range_valid
)
878 log_error("Memory `%s' with non-constant width or size at %s:%d!\n",
879 str
.c_str(), filename
.c_str(), linenum
);
881 RTLIL::Memory
*memory
= new RTLIL::Memory
;
882 memory
->attributes
["\\src"] = stringf("%s:%d", filename
.c_str(), linenum
);
884 memory
->width
= children
[0]->range_left
- children
[0]->range_right
+ 1;
885 memory
->start_offset
= children
[0]->range_right
;
886 memory
->size
= children
[1]->range_left
- children
[1]->range_right
;
887 current_module
->memories
[memory
->name
] = memory
;
889 if (memory
->size
< 0)
891 memory
->size
+= std::min(children
[1]->range_left
, children
[1]->range_right
) + 1;
893 for (auto &attr
: attributes
) {
894 if (attr
.second
->type
!= AST_CONSTANT
)
895 log_error("Attribute `%s' with non-constant value at %s:%d!\n",
896 attr
.first
.c_str(), filename
.c_str(), linenum
);
897 memory
->attributes
[attr
.first
] = attr
.second
->asAttrConst();
902 // simply return the corresponding RTLIL::SigSpec for an AST_CONSTANT node
906 detectSignWidth(width_hint
, sign_hint
);
908 is_signed
= sign_hint
;
909 return RTLIL::SigSpec(bitsAsConst());
912 // simply return the corresponding RTLIL::SigSpec for an AST_IDENTIFIER node
913 // for identifiers with dynamic bit ranges (e.g. "foo[bar]" or "foo[bar+3:bar]") a
914 // shifter cell is created and the output signal of this cell is returned
917 RTLIL::Wire
*wire
= NULL
;
918 RTLIL::SigChunk chunk
;
920 if (id2ast
&& id2ast
->type
== AST_AUTOWIRE
&& current_module
->wires
.count(str
) == 0) {
921 RTLIL::Wire
*wire
= new RTLIL::Wire
;
922 wire
->attributes
["\\src"] = stringf("%s:%d", filename
.c_str(), linenum
);
925 log("Warning: Identifier `%s' is implicitly declared at %s:%d.\n", str
.c_str(), filename
.c_str(), linenum
);
927 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
);
928 current_module
->wires
[str
] = wire
;
930 else if (id2ast
->type
== AST_PARAMETER
|| id2ast
->type
== AST_LOCALPARAM
) {
931 if (id2ast
->children
[0]->type
!= AST_CONSTANT
)
932 log_error("Parameter %s does not evaluate to constant value at %s:%d!\n",
933 str
.c_str(), filename
.c_str(), linenum
);
934 chunk
= RTLIL::Const(id2ast
->children
[0]->bits
);
935 goto use_const_chunk
;
937 else if (!id2ast
|| (id2ast
->type
!= AST_WIRE
&& id2ast
->type
!= AST_AUTOWIRE
&&
938 id2ast
->type
!= AST_MEMORY
) || current_module
->wires
.count(str
) == 0)
939 log_error("Identifier `%s' doesn't map to any signal at %s:%d!\n",
940 str
.c_str(), filename
.c_str(), linenum
);
942 if (id2ast
->type
== AST_MEMORY
)
943 log_error("Identifier `%s' does map to an unexpanded memory at %s:%d!\n",
944 str
.c_str(), filename
.c_str(), linenum
);
946 wire
= current_module
->wires
[str
];
948 chunk
.width
= wire
->width
;
952 if (children
.size() != 0) {
953 assert(children
[0]->type
== AST_RANGE
);
954 if (!children
[0]->range_valid
) {
955 AstNode
*left_at_zero_ast
= children
[0]->children
[0]->clone();
956 AstNode
*right_at_zero_ast
= children
[0]->children
.size() >= 2 ? children
[0]->children
[1]->clone() : left_at_zero_ast
->clone();
957 while (left_at_zero_ast
->simplify(true, true, false, 1, -1, false, false)) { }
958 while (right_at_zero_ast
->simplify(true, true, false, 1, -1, false, false)) { }
959 if (left_at_zero_ast
->type
!= AST_CONSTANT
|| right_at_zero_ast
->type
!= AST_CONSTANT
)
960 log_error("Unsupported expression on dynamic range select on signal `%s' at %s:%d!\n",
961 str
.c_str(), filename
.c_str(), linenum
);
962 int width
= left_at_zero_ast
->integer
- right_at_zero_ast
->integer
+ 1;
963 AstNode
*fake_ast
= new AstNode(AST_NONE
, clone(), children
[0]->children
.size() >= 2 ?
964 children
[0]->children
[1]->clone() : children
[0]->children
[0]->clone());
965 fake_ast
->children
[0]->delete_children();
966 RTLIL::SigSpec sig
= binop2rtlil(fake_ast
, "$shr", width
,
967 fake_ast
->children
[0]->genRTLIL(), fake_ast
->children
[1]->genRTLIL());
968 delete left_at_zero_ast
;
969 delete right_at_zero_ast
;
973 chunk
.offset
= children
[0]->range_right
- id2ast
->range_right
;
974 chunk
.width
= children
[0]->range_left
- children
[0]->range_right
+ 1;
975 if (children
[0]->range_left
> id2ast
->range_left
|| id2ast
->range_right
> children
[0]->range_right
)
976 log_error("Range select out of bounds on signal `%s' at %s:%d!\n",
977 str
.c_str(), filename
.c_str(), linenum
);
982 sig
.chunks
.push_back(chunk
);
983 sig
.width
= chunk
.width
;
985 if (genRTLIL_subst_from
&& genRTLIL_subst_to
)
986 sig
.replace(*genRTLIL_subst_from
, *genRTLIL_subst_to
);
988 is_signed
= children
.size() > 0 ? false : id2ast
->is_signed
&& sign_hint
;
992 // just pass thru the signal. the parent will evaluate the is_signed property and interpret the SigSpec accordingly
994 case AST_TO_UNSIGNED
: {
995 RTLIL::SigSpec sig
= children
[0]->genRTLIL();
996 if (sig
.width
< width_hint
)
997 sig
.extend_u0(width_hint
, sign_hint
);
998 is_signed
= sign_hint
;
1002 // concatenation of signals can be done directly using RTLIL::SigSpec
1006 for (auto it
= children
.begin(); it
!= children
.end(); it
++) {
1007 RTLIL::SigSpec s
= (*it
)->genRTLIL();
1008 for (size_t i
= 0; i
< s
.chunks
.size(); i
++) {
1009 sig
.chunks
.push_back(s
.chunks
[i
]);
1010 sig
.width
+= s
.chunks
[i
].width
;
1013 if (sig
.width
< width_hint
)
1014 sig
.extend_u0(width_hint
, false);
1018 // replication of signals can be done directly using RTLIL::SigSpec
1019 case AST_REPLICATE
: {
1020 RTLIL::SigSpec left
= children
[0]->genRTLIL();
1021 RTLIL::SigSpec right
= children
[1]->genRTLIL();
1022 if (!left
.is_fully_const())
1023 log_error("Left operand of replicate expression is not constant at %s:%d!\n", filename
.c_str(), linenum
);
1024 int count
= left
.as_int();
1026 for (int i
= 0; i
< count
; i
++)
1028 if (sig
.width
< width_hint
)
1029 sig
.extend_u0(width_hint
, false);
1034 // generate cells for unary operations: $not, $pos, $neg
1035 if (0) { case AST_BIT_NOT
: type_name
= "$not"; }
1036 if (0) { case AST_POS
: type_name
= "$pos"; }
1037 if (0) { case AST_NEG
: type_name
= "$neg"; }
1039 RTLIL::SigSpec arg
= children
[0]->genRTLIL(width_hint
, sign_hint
);
1040 is_signed
= children
[0]->is_signed
;
1041 int width
= arg
.width
;
1042 if (width_hint
> 0) {
1044 widthExtend(this, arg
, width
, is_signed
, "$pos");
1046 return uniop2rtlil(this, type_name
, width
, arg
);
1049 // generate cells for binary operations: $and, $or, $xor, $xnor
1050 if (0) { case AST_BIT_AND
: type_name
= "$and"; }
1051 if (0) { case AST_BIT_OR
: type_name
= "$or"; }
1052 if (0) { case AST_BIT_XOR
: type_name
= "$xor"; }
1053 if (0) { case AST_BIT_XNOR
: type_name
= "$xnor"; }
1056 detectSignWidth(width_hint
, sign_hint
);
1057 RTLIL::SigSpec left
= children
[0]->genRTLIL(width_hint
, sign_hint
);
1058 RTLIL::SigSpec right
= children
[1]->genRTLIL(width_hint
, sign_hint
);
1059 int width
= std::max(left
.width
, right
.width
);
1062 is_signed
= children
[0]->is_signed
&& children
[1]->is_signed
;
1063 return binop2rtlil(this, type_name
, width
, left
, right
);
1066 // generate cells for unary operations: $reduce_and, $reduce_or, $reduce_xor, $reduce_xnor
1067 if (0) { case AST_REDUCE_AND
: type_name
= "$reduce_and"; }
1068 if (0) { case AST_REDUCE_OR
: type_name
= "$reduce_or"; }
1069 if (0) { case AST_REDUCE_XOR
: type_name
= "$reduce_xor"; }
1070 if (0) { case AST_REDUCE_XNOR
: type_name
= "$reduce_xnor"; }
1072 RTLIL::SigSpec arg
= children
[0]->genRTLIL();
1073 RTLIL::SigSpec sig
= uniop2rtlil(this, type_name
, std::max(width_hint
, 1), arg
);
1077 // generate cells for unary operations: $reduce_bool
1078 // (this is actually just an $reduce_or, but for clearity a different cell type is used)
1079 if (0) { case AST_REDUCE_BOOL
: type_name
= "$reduce_bool"; }
1081 RTLIL::SigSpec arg
= children
[0]->genRTLIL();
1082 RTLIL::SigSpec sig
= arg
.width
> 1 ? uniop2rtlil(this, type_name
, std::max(width_hint
, 1), arg
) : arg
;
1086 // generate cells for binary operations: $shl, $shr, $sshl, $sshr
1087 if (0) { case AST_SHIFT_LEFT
: type_name
= "$shl"; }
1088 if (0) { case AST_SHIFT_RIGHT
: type_name
= "$shr"; }
1089 if (0) { case AST_SHIFT_SLEFT
: type_name
= "$sshl"; }
1090 if (0) { case AST_SHIFT_SRIGHT
: type_name
= "$sshr"; }
1093 detectSignWidth(width_hint
, sign_hint
);
1094 RTLIL::SigSpec left
= children
[0]->genRTLIL(width_hint
, sign_hint
);
1095 RTLIL::SigSpec right
= children
[1]->genRTLIL();
1096 int width
= width_hint
> 0 ? width_hint
: left
.width
;
1097 is_signed
= children
[0]->is_signed
;
1098 return binop2rtlil(this, type_name
, width
, left
, right
);
1101 // generate cells for binary operations: $pow
1106 children
[1]->detectSignWidth(right_width
, right_signed
);
1108 detectSignWidth(width_hint
, sign_hint
);
1109 RTLIL::SigSpec left
= children
[0]->genRTLIL(width_hint
, sign_hint
);
1110 RTLIL::SigSpec right
= children
[1]->genRTLIL(right_width
, right_signed
);
1111 int width
= width_hint
> 0 ? width_hint
: left
.width
;
1112 is_signed
= children
[0]->is_signed
;
1113 if (!flag_noopt
&& left
.is_fully_const() && left
.as_int() == 2 && !right_signed
)
1114 return binop2rtlil(this, "$shl", width
, RTLIL::SigSpec(1, left
.width
), right
);
1115 return binop2rtlil(this, "$pow", width
, left
, right
);
1118 // generate cells for binary operations: $lt, $le, $eq, $ne, $ge, $gt
1119 if (0) { case AST_LT
: type_name
= "$lt"; }
1120 if (0) { case AST_LE
: type_name
= "$le"; }
1121 if (0) { case AST_EQ
: type_name
= "$eq"; }
1122 if (0) { case AST_NE
: type_name
= "$ne"; }
1123 if (0) { case AST_EQX
: type_name
= "$eqx"; }
1124 if (0) { case AST_NEX
: type_name
= "$nex"; }
1125 if (0) { case AST_GE
: type_name
= "$ge"; }
1126 if (0) { case AST_GT
: type_name
= "$gt"; }
1128 int width
= std::max(width_hint
, 1);
1129 width_hint
= -1, sign_hint
= true;
1130 children
[0]->detectSignWidthWorker(width_hint
, sign_hint
);
1131 children
[1]->detectSignWidthWorker(width_hint
, sign_hint
);
1132 RTLIL::SigSpec left
= children
[0]->genRTLIL(width_hint
, sign_hint
);
1133 RTLIL::SigSpec right
= children
[1]->genRTLIL(width_hint
, sign_hint
);
1134 RTLIL::SigSpec sig
= binop2rtlil(this, type_name
, width
, left
, right
);
1138 // generate cells for binary operations: $add, $sub, $mul, $div, $mod
1139 if (0) { case AST_ADD
: type_name
= "$add"; }
1140 if (0) { case AST_SUB
: type_name
= "$sub"; }
1141 if (0) { case AST_MUL
: type_name
= "$mul"; }
1142 if (0) { case AST_DIV
: type_name
= "$div"; }
1143 if (0) { case AST_MOD
: type_name
= "$mod"; }
1146 detectSignWidth(width_hint
, sign_hint
);
1147 RTLIL::SigSpec left
= children
[0]->genRTLIL(width_hint
, sign_hint
);
1148 RTLIL::SigSpec right
= children
[1]->genRTLIL(width_hint
, sign_hint
);
1150 int width
= std::max(left
.width
, right
.width
);
1151 if (width
> width_hint
&& width_hint
> 0)
1153 if (width
< width_hint
) {
1154 if (type
== AST_ADD
|| type
== AST_SUB
|| type
== AST_DIV
)
1156 if (type
== AST_SUB
&& (!children
[0]->is_signed
|| !children
[1]->is_signed
))
1158 if (type
== AST_MUL
)
1159 width
= std::min(left
.width
+ right
.width
, width_hint
);
1162 int width
= std::max(std::max(left
.width
, right
.width
), width_hint
);
1164 is_signed
= children
[0]->is_signed
&& children
[1]->is_signed
;
1165 return binop2rtlil(this, type_name
, width
, left
, right
);
1168 // generate cells for binary operations: $logic_and, $logic_or
1169 if (0) { case AST_LOGIC_AND
: type_name
= "$logic_and"; }
1170 if (0) { case AST_LOGIC_OR
: type_name
= "$logic_or"; }
1172 RTLIL::SigSpec left
= children
[0]->genRTLIL();
1173 RTLIL::SigSpec right
= children
[1]->genRTLIL();
1174 return binop2rtlil(this, type_name
, std::max(width_hint
, 1), left
, right
);
1177 // generate cells for unary operations: $logic_not
1180 RTLIL::SigSpec arg
= children
[0]->genRTLIL();
1181 return uniop2rtlil(this, "$logic_not", std::max(width_hint
, 1), arg
);
1184 // generate multiplexer for ternary operator (aka ?:-operator)
1188 detectSignWidth(width_hint
, sign_hint
);
1190 RTLIL::SigSpec cond
= children
[0]->genRTLIL();
1191 RTLIL::SigSpec val1
= children
[1]->genRTLIL(width_hint
, sign_hint
);
1192 RTLIL::SigSpec val2
= children
[2]->genRTLIL(width_hint
, sign_hint
);
1195 cond
= uniop2rtlil(this, "$reduce_bool", 1, cond
, false);
1197 int width
= std::max(val1
.width
, val2
.width
);
1198 is_signed
= children
[1]->is_signed
&& children
[2]->is_signed
;
1199 widthExtend(this, val1
, width
, is_signed
, "$bu0");
1200 widthExtend(this, val2
, width
, is_signed
, "$bu0");
1202 RTLIL::SigSpec sig
= mux2rtlil(this, cond
, val1
, val2
);
1204 if (sig
.width
< width_hint
)
1205 sig
.extend_u0(width_hint
, sign_hint
);
1209 // generate $memrd cells for memory read ports
1212 std::stringstream sstr
;
1213 sstr
<< "$memrd$" << str
<< "$" << filename
<< ":" << linenum
<< "$" << (RTLIL::autoidx
++);
1215 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1216 cell
->attributes
["\\src"] = stringf("%s:%d", filename
.c_str(), linenum
);
1217 cell
->name
= sstr
.str();
1218 cell
->type
= "$memrd";
1219 current_module
->cells
[cell
->name
] = cell
;
1221 RTLIL::Wire
*wire
= new RTLIL::Wire
;
1222 wire
->attributes
["\\src"] = stringf("%s:%d", filename
.c_str(), linenum
);
1223 wire
->name
= cell
->name
+ "_DATA";
1224 wire
->width
= current_module
->memories
[str
]->width
;
1225 current_module
->wires
[wire
->name
] = wire
;
1228 while ((1 << addr_bits
) < current_module
->memories
[str
]->size
)
1231 cell
->connections
["\\CLK"] = RTLIL::SigSpec(RTLIL::State::Sx
, 1);
1232 cell
->connections
["\\ADDR"] = children
[0]->genWidthRTLIL(addr_bits
);
1233 cell
->connections
["\\DATA"] = RTLIL::SigSpec(wire
);
1235 cell
->parameters
["\\MEMID"] = RTLIL::Const(str
);
1236 cell
->parameters
["\\ABITS"] = RTLIL::Const(addr_bits
);
1237 cell
->parameters
["\\WIDTH"] = RTLIL::Const(wire
->width
);
1239 cell
->parameters
["\\CLK_ENABLE"] = RTLIL::Const(0);
1240 cell
->parameters
["\\CLK_POLARITY"] = RTLIL::Const(0);
1241 cell
->parameters
["\\TRANSPARENT"] = RTLIL::Const(0);
1243 return RTLIL::SigSpec(wire
);
1246 // generate $memwr cells for memory write ports
1249 std::stringstream sstr
;
1250 sstr
<< "$memwr$" << str
<< "$" << filename
<< ":" << linenum
<< "$" << (RTLIL::autoidx
++);
1252 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1253 cell
->attributes
["\\src"] = stringf("%s:%d", filename
.c_str(), linenum
);
1254 cell
->name
= sstr
.str();
1255 cell
->type
= "$memwr";
1256 current_module
->cells
[cell
->name
] = cell
;
1259 while ((1 << addr_bits
) < current_module
->memories
[str
]->size
)
1262 cell
->connections
["\\CLK"] = RTLIL::SigSpec(RTLIL::State::Sx
, 1);
1263 cell
->connections
["\\ADDR"] = children
[0]->genWidthRTLIL(addr_bits
);
1264 cell
->connections
["\\DATA"] = children
[1]->genWidthRTLIL(current_module
->memories
[str
]->width
);
1265 cell
->connections
["\\EN"] = children
[2]->genRTLIL();
1267 if (cell
->connections
["\\EN"].width
> 1)
1268 cell
->connections
["\\EN"] = uniop2rtlil(this, "$reduce_bool", 1, cell
->connections
["\\EN"], false);
1270 cell
->parameters
["\\MEMID"] = RTLIL::Const(str
);
1271 cell
->parameters
["\\ABITS"] = RTLIL::Const(addr_bits
);
1272 cell
->parameters
["\\WIDTH"] = RTLIL::Const(current_module
->memories
[str
]->width
);
1274 cell
->parameters
["\\CLK_ENABLE"] = RTLIL::Const(0);
1275 cell
->parameters
["\\CLK_POLARITY"] = RTLIL::Const(0);
1277 cell
->parameters
["\\PRIORITY"] = RTLIL::Const(RTLIL::autoidx
-1);
1281 // generate $assert cells
1284 log_assert(children
.size() == 2);
1286 RTLIL::SigSpec check
= children
[0]->genRTLIL();
1287 log_assert(check
.width
== 1);
1289 RTLIL::SigSpec en
= children
[1]->genRTLIL();
1290 log_assert(en
.width
== 1);
1292 std::stringstream sstr
;
1293 sstr
<< "$assert$" << filename
<< ":" << linenum
<< "$" << (RTLIL::autoidx
++);
1295 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1296 cell
->attributes
["\\src"] = stringf("%s:%d", filename
.c_str(), linenum
);
1297 cell
->name
= sstr
.str();
1298 cell
->type
= "$assert";
1299 current_module
->cells
[cell
->name
] = cell
;
1301 for (auto &attr
: attributes
) {
1302 if (attr
.second
->type
!= AST_CONSTANT
)
1303 log_error("Attribute `%s' with non-constant value at %s:%d!\n",
1304 attr
.first
.c_str(), filename
.c_str(), linenum
);
1305 cell
->attributes
[attr
.first
] = attr
.second
->asAttrConst();
1308 cell
->connections
["\\A"] = check
;
1309 cell
->connections
["\\EN"] = en
;
1313 // add entries to current_module->connections for assignments (outside of always blocks)
1316 if (children
[0]->type
== AST_IDENTIFIER
&& children
[0]->id2ast
&& children
[0]->id2ast
->type
== AST_AUTOWIRE
) {
1317 RTLIL::SigSpec right
= children
[1]->genRTLIL();
1318 RTLIL::SigSpec left
= children
[0]->genWidthRTLIL(right
.width
);
1319 current_module
->connections
.push_back(RTLIL::SigSig(left
, right
));
1321 RTLIL::SigSpec left
= children
[0]->genRTLIL();
1322 RTLIL::SigSpec right
= children
[1]->genWidthRTLIL(left
.width
);
1323 current_module
->connections
.push_back(RTLIL::SigSig(left
, right
));
1328 // create an RTLIL::Cell for an AST_CELL
1331 int port_counter
= 0, para_counter
= 0;
1332 RTLIL::Cell
*cell
= new RTLIL::Cell
;
1333 cell
->attributes
["\\src"] = stringf("%s:%d", filename
.c_str(), linenum
);
1335 for (auto it
= children
.begin(); it
!= children
.end(); it
++) {
1336 AstNode
*child
= *it
;
1337 if (child
->type
== AST_CELLTYPE
) {
1338 cell
->type
= child
->str
;
1339 if (flag_icells
&& cell
->type
.substr(0, 2) == "\\$")
1340 cell
->type
= cell
->type
.substr(1);
1343 if (child
->type
== AST_PARASET
) {
1344 if (child
->children
[0]->type
!= AST_CONSTANT
)
1345 log_error("Parameter `%s' with non-constant value at %s:%d!\n",
1346 child
->str
.c_str(), filename
.c_str(), linenum
);
1347 if (child
->str
.size() == 0) {
1349 snprintf(buf
, 100, "$%d", ++para_counter
);
1350 cell
->parameters
[buf
] = child
->children
[0]->asParaConst();
1352 cell
->parameters
[child
->str
] = child
->children
[0]->asParaConst();
1356 if (child
->type
== AST_ARGUMENT
) {
1358 if (child
->children
.size() > 0)
1359 sig
= child
->children
[0]->genRTLIL();
1360 if (child
->str
.size() == 0) {
1362 snprintf(buf
, 100, "$%d", ++port_counter
);
1363 cell
->connections
[buf
] = sig
;
1365 cell
->connections
[child
->str
] = sig
;
1371 for (auto &attr
: attributes
) {
1372 if (attr
.second
->type
!= AST_CONSTANT
)
1373 log_error("Attribute `%s' with non-constant value at %s:%d!\n",
1374 attr
.first
.c_str(), filename
.c_str(), linenum
);
1375 cell
->attributes
[attr
.first
] = attr
.second
->asAttrConst();
1377 if (current_module
->cells
.count(cell
->name
) != 0)
1378 log_error("Re-definition of cell `%s' at %s:%d!\n",
1379 str
.c_str(), filename
.c_str(), linenum
);
1380 current_module
->cells
[str
] = cell
;
1384 // use ProcessGenerator for always blocks
1386 AstNode
*always
= this->clone();
1387 ProcessGenerator
generator(always
);
1388 ignoreThisSignalsInInitial
.append(generator
.outputSignals
);
1393 AstNode
*always
= this->clone();
1394 ProcessGenerator
generator(always
, ignoreThisSignalsInInitial
);
1398 // everything should have been handled above -> print error if not.
1400 for (auto f
: log_files
)
1401 current_ast
->dumpAst(f
, "verilog-ast> ");
1402 type_name
= type2str(type
);
1403 log_error("Don't know how to generate RTLIL code for %s node at %s:%d!\n",
1404 type_name
.c_str(), filename
.c_str(), linenum
);
1407 return RTLIL::SigSpec();
1410 // this is a wrapper for AstNode::genRTLIL() when a specific signal width is requested and/or
1411 // signals must be substituted before beeing used as input values (used by ProcessGenerator)
1412 // note that this is using some global variables to communicate this special settings to AstNode::genRTLIL().
1413 RTLIL::SigSpec
AstNode::genWidthRTLIL(int width
, RTLIL::SigSpec
*subst_from
, RTLIL::SigSpec
*subst_to
)
1415 RTLIL::SigSpec
*backup_subst_from
= genRTLIL_subst_from
;
1416 RTLIL::SigSpec
*backup_subst_to
= genRTLIL_subst_to
;
1419 genRTLIL_subst_from
= subst_from
;
1421 genRTLIL_subst_to
= subst_to
;
1423 bool sign_hint
= true;
1424 int width_hint
= width
;
1425 detectSignWidthWorker(width_hint
, sign_hint
);
1426 RTLIL::SigSpec sig
= genRTLIL(width_hint
, sign_hint
);
1428 genRTLIL_subst_from
= backup_subst_from
;
1429 genRTLIL_subst_to
= backup_subst_to
;
1432 sig
.extend_u0(width
, is_signed
);