24
24
25
25
#include " rtlil_backend.h"
26
26
#include " kernel/yosys.h"
27
+ #include " kernel/utils.h"
27
28
#include < errno.h>
29
+ #include < iterator>
28
30
29
31
USING_YOSYS_NAMESPACE
30
32
using namespace RTLIL_BACKEND ;
31
33
YOSYS_NAMESPACE_BEGIN
32
34
35
+ void RTLIL_BACKEND::dump_attributes (std::ostream &f, std::string indent, const RTLIL::AttrObject *obj)
36
+ {
37
+ for (const auto & [name, value] : reversed (obj->attributes )) {
38
+ f << stringf (" %s" " attribute %s " , indent, name);
39
+ dump_const (f, value);
40
+ f << stringf (" \n " );
41
+ }
42
+ }
43
+
33
44
void RTLIL_BACKEND::dump_const (std::ostream &f, const RTLIL::Const &data, int width, int offset, bool autoint)
34
45
{
35
46
if (width < 0 )
@@ -110,8 +121,8 @@ void RTLIL_BACKEND::dump_sigspec(std::ostream &f, const RTLIL::SigSpec &sig, boo
110
121
dump_sigchunk (f, sig.as_chunk (), autoint);
111
122
} else {
112
123
f << stringf (" { " );
113
- for (auto it = sig. chunks (). rbegin (); it != sig.chunks (). rend (); ++it ) {
114
- dump_sigchunk (f, *it , false );
124
+ for (const auto & chunk : reversed ( sig.chunks ()) ) {
125
+ dump_sigchunk (f, chunk , false );
115
126
f << stringf (" " );
116
127
}
117
128
f << stringf (" }" );
@@ -120,14 +131,10 @@ void RTLIL_BACKEND::dump_sigspec(std::ostream &f, const RTLIL::SigSpec &sig, boo
120
131
121
132
void RTLIL_BACKEND::dump_wire (std::ostream &f, std::string indent, const RTLIL::Wire *wire)
122
133
{
123
- for (auto &it : wire->attributes ) {
124
- f << stringf (" %s" " attribute %s " , indent, it.first );
125
- dump_const (f, it.second );
126
- f << stringf (" \n " );
127
- }
134
+ dump_attributes (f, indent, wire);
128
135
if (wire->driverCell_ ) {
129
136
f << stringf (" %s" " # driver %s %s\n " , indent,
130
- wire->driverCell ()->name . c_str () , wire->driverPort (). c_str ());
137
+ wire->driverCell ()->name , wire->driverPort ());
131
138
}
132
139
f << stringf (" %s" " wire " , indent);
133
140
if (wire->width != 1 )
@@ -149,11 +156,7 @@ void RTLIL_BACKEND::dump_wire(std::ostream &f, std::string indent, const RTLIL::
149
156
150
157
void RTLIL_BACKEND::dump_memory (std::ostream &f, std::string indent, const RTLIL::Memory *memory)
151
158
{
152
- for (auto &it : memory->attributes ) {
153
- f << stringf (" %s" " attribute %s " , indent, it.first );
154
- dump_const (f, it.second );
155
- f << stringf (" \n " );
156
- }
159
+ dump_attributes (f, indent, memory);
157
160
f << stringf (" %s" " memory " , indent);
158
161
if (memory->width != 1 )
159
162
f << stringf (" width %d " , memory->width );
@@ -166,71 +169,58 @@ void RTLIL_BACKEND::dump_memory(std::ostream &f, std::string indent, const RTLIL
166
169
167
170
void RTLIL_BACKEND::dump_cell (std::ostream &f, std::string indent, const RTLIL::Cell *cell)
168
171
{
169
- for (auto &it : cell->attributes ) {
170
- f << stringf (" %s" " attribute %s " , indent, it.first );
171
- dump_const (f, it.second );
172
- f << stringf (" \n " );
173
- }
172
+ dump_attributes (f, indent, cell);
174
173
f << stringf (" %s" " cell %s %s\n " , indent, cell->type , cell->name );
175
- for (auto &it : cell->parameters ) {
174
+ for (const auto & [name, param] : reversed ( cell->parameters ) ) {
176
175
f << stringf (" %s parameter%s%s %s " , indent,
177
- (it. second .flags & RTLIL::CONST_FLAG_SIGNED) != 0 ? " signed" : " " ,
178
- (it. second .flags & RTLIL::CONST_FLAG_REAL) != 0 ? " real" : " " ,
179
- it. first . c_str () );
180
- dump_const (f, it. second );
176
+ (param .flags & RTLIL::CONST_FLAG_SIGNED) != 0 ? " signed" : " " ,
177
+ (param .flags & RTLIL::CONST_FLAG_REAL) != 0 ? " real" : " " ,
178
+ name );
179
+ dump_const (f, param );
181
180
f << stringf (" \n " );
182
181
}
183
- for (auto &it : cell->connections ( )) {
184
- f << stringf (" %s connect %s " , indent, it. first );
185
- dump_sigspec (f, it. second );
182
+ for (const auto & [port, sig] : reversed ( cell->connections_ )) {
183
+ f << stringf (" %s connect %s " , indent, port );
184
+ dump_sigspec (f, sig );
186
185
f << stringf (" \n " );
187
186
}
188
187
f << stringf (" %s" " end\n " , indent);
189
188
}
190
189
191
190
void RTLIL_BACKEND::dump_proc_case_body (std::ostream &f, std::string indent, const RTLIL::CaseRule *cs)
192
191
{
193
- for (auto it = cs->actions .begin (); it != cs->actions .end (); ++it)
194
- {
192
+ for (const auto & [lhs, rhs] : cs->actions ) {
195
193
f << stringf (" %s" " assign " , indent);
196
- dump_sigspec (f, it-> first );
194
+ dump_sigspec (f, lhs );
197
195
f << stringf (" " );
198
- dump_sigspec (f, it-> second );
196
+ dump_sigspec (f, rhs );
199
197
f << stringf (" \n " );
200
198
}
201
199
202
- for (auto it = cs->switches . begin (); it != cs-> switches . end (); ++it )
203
- dump_proc_switch (f, indent, *it );
200
+ for (const auto & sw : cs->switches )
201
+ dump_proc_switch (f, indent, sw );
204
202
}
205
203
206
204
void RTLIL_BACKEND::dump_proc_switch (std::ostream &f, std::string indent, const RTLIL::SwitchRule *sw)
207
205
{
208
- for (auto it = sw->attributes .begin (); it != sw->attributes .end (); ++it) {
209
- f << stringf (" %s" " attribute %s " , indent, it->first );
210
- dump_const (f, it->second );
211
- f << stringf (" \n " );
212
- }
206
+ dump_attributes (f, indent, sw);
213
207
214
208
f << stringf (" %s" " switch " , indent);
215
209
dump_sigspec (f, sw->signal );
216
210
f << stringf (" \n " );
217
211
218
- for (auto it = sw->cases . begin (); it != sw-> cases . end (); ++it )
212
+ for (const auto case_ : sw->cases )
219
213
{
220
- for (auto ait = (*it)->attributes .begin (); ait != (*it)->attributes .end (); ++ait) {
221
- f << stringf (" %s attribute %s " , indent, ait->first );
222
- dump_const (f, ait->second );
223
- f << stringf (" \n " );
224
- }
214
+ dump_attributes (f, indent, case_);
225
215
f << stringf (" %s case " , indent);
226
- for (size_t i = 0 ; i < (*it) ->compare .size (); i++) {
216
+ for (size_t i = 0 ; i < case_ ->compare .size (); i++) {
227
217
if (i > 0 )
228
218
f << stringf (" , " );
229
- dump_sigspec (f, (*it) ->compare [i]);
219
+ dump_sigspec (f, case_ ->compare [i]);
230
220
}
231
221
f << stringf (" \n " );
232
222
233
- dump_proc_case_body (f, indent + " " , *it );
223
+ dump_proc_case_body (f, indent + " " , case_ );
234
224
}
235
225
236
226
f << stringf (" %s" " end\n " , indent);
@@ -253,20 +243,16 @@ void RTLIL_BACKEND::dump_proc_sync(std::ostream &f, std::string indent, const RT
253
243
case RTLIL::STi: f << stringf (" init\n " ); break ;
254
244
}
255
245
256
- for (auto &it : sy->actions ) {
246
+ for (const auto & [lhs, rhs] : sy->actions ) {
257
247
f << stringf (" %s update " , indent);
258
- dump_sigspec (f, it. first );
248
+ dump_sigspec (f, lhs );
259
249
f << stringf (" " );
260
- dump_sigspec (f, it. second );
250
+ dump_sigspec (f, rhs );
261
251
f << stringf (" \n " );
262
252
}
263
253
264
254
for (auto &it: sy->mem_write_actions ) {
265
- for (auto it2 = it.attributes .begin (); it2 != it.attributes .end (); ++it2) {
266
- f << stringf (" %s attribute %s " , indent, it2->first );
267
- dump_const (f, it2->second );
268
- f << stringf (" \n " );
269
- }
255
+ dump_attributes (f, indent, &it);
270
256
f << stringf (" %s memwr %s " , indent, it.memid );
271
257
dump_sigspec (f, it.address );
272
258
f << stringf (" " );
@@ -281,15 +267,11 @@ void RTLIL_BACKEND::dump_proc_sync(std::ostream &f, std::string indent, const RT
281
267
282
268
void RTLIL_BACKEND::dump_proc (std::ostream &f, std::string indent, const RTLIL::Process *proc)
283
269
{
284
- for (auto it = proc->attributes .begin (); it != proc->attributes .end (); ++it) {
285
- f << stringf (" %s" " attribute %s " , indent, it->first );
286
- dump_const (f, it->second );
287
- f << stringf (" \n " );
288
- }
270
+ dump_attributes (f, indent, proc);
289
271
f << stringf (" %s" " process %s\n " , indent, proc->name );
290
272
dump_proc_case_body (f, indent + " " , &proc->root_case );
291
- for (auto it = proc->syncs . begin (); it != proc-> syncs . end (); ++it )
292
- dump_proc_sync (f, indent + " " , *it );
273
+ for (auto * sync : proc->syncs )
274
+ dump_proc_sync (f, indent + " " , sync );
293
275
f << stringf (" %s" " end\n " , indent);
294
276
}
295
277
@@ -309,11 +291,7 @@ void RTLIL_BACKEND::dump_module(std::ostream &f, std::string indent, RTLIL::Modu
309
291
310
292
if (print_header)
311
293
{
312
- for (auto it = module ->attributes .begin (); it != module ->attributes .end (); ++it) {
313
- f << stringf (" %s" " attribute %s " , indent, it->first );
314
- dump_const (f, it->second );
315
- f << stringf (" \n " );
316
- }
294
+ dump_attributes (f, indent, module );
317
295
318
296
f << stringf (" %s" " module %s\n " , indent, module ->name );
319
297
@@ -335,40 +313,40 @@ void RTLIL_BACKEND::dump_module(std::ostream &f, std::string indent, RTLIL::Modu
335
313
336
314
if (print_body)
337
315
{
338
- for (auto it : module ->wires ( ))
339
- if (!only_selected || design->selected (module , it )) {
316
+ for (const auto & [_, wire] : reversed ( module ->wires_ ))
317
+ if (!only_selected || design->selected (module , wire )) {
340
318
if (only_selected)
341
319
f << stringf (" \n " );
342
- dump_wire (f, indent + " " , it );
320
+ dump_wire (f, indent + " " , wire );
343
321
}
344
322
345
- for (auto it : module ->memories )
346
- if (!only_selected || design->selected (module , it. second )) {
323
+ for (const auto & [_, mem] : reversed ( module ->memories ) )
324
+ if (!only_selected || design->selected (module , mem )) {
347
325
if (only_selected)
348
326
f << stringf (" \n " );
349
- dump_memory (f, indent + " " , it. second );
327
+ dump_memory (f, indent + " " , mem );
350
328
}
351
329
352
- for (auto it : module ->cells ( ))
353
- if (!only_selected || design->selected (module , it )) {
330
+ for (const auto & [_, cell] : reversed ( module ->cells_ ))
331
+ if (!only_selected || design->selected (module , cell )) {
354
332
if (only_selected)
355
333
f << stringf (" \n " );
356
- dump_cell (f, indent + " " , it );
334
+ dump_cell (f, indent + " " , cell );
357
335
}
358
336
359
- for (auto it : module ->processes )
360
- if (!only_selected || design->selected (module , it. second )) {
337
+ for (const auto & [_, process] : reversed ( module ->processes ) )
338
+ if (!only_selected || design->selected (module , process )) {
361
339
if (only_selected)
362
340
f << stringf (" \n " );
363
- dump_proc (f, indent + " " , it. second );
341
+ dump_proc (f, indent + " " , process );
364
342
}
365
343
366
344
bool first_conn_line = true ;
367
- for (auto it = module -> connections (). begin (); it != module ->connections (). end (); ++it ) {
345
+ for (const auto & [lhs, rhs] : module ->connections ()) {
368
346
bool show_conn = !only_selected || design->selected_whole_module (module ->name );
369
347
if (!show_conn) {
370
- RTLIL::SigSpec sigs = it-> first ;
371
- sigs.append (it-> second );
348
+ RTLIL::SigSpec sigs = lhs ;
349
+ sigs.append (rhs );
372
350
for (auto &c : sigs.chunks ()) {
373
351
if (c.wire == NULL || !design->selected (module , c.wire ))
374
352
continue ;
@@ -378,7 +356,7 @@ void RTLIL_BACKEND::dump_module(std::ostream &f, std::string indent, RTLIL::Modu
378
356
if (show_conn) {
379
357
if (only_selected && first_conn_line)
380
358
f << stringf (" \n " );
381
- dump_conn (f, indent + " " , it-> first , it-> second );
359
+ dump_conn (f, indent + " " , lhs, rhs );
382
360
first_conn_line = false ;
383
361
}
384
362
}
@@ -394,7 +372,7 @@ void RTLIL_BACKEND::dump_design(std::ostream &f, RTLIL::Design *design, bool onl
394
372
395
373
if (!flag_m) {
396
374
int count_selected_mods = 0 ;
397
- for (auto module : design->modules ()) {
375
+ for (auto * module : design->modules ()) {
398
376
if (design->selected_whole_module (module ->name ))
399
377
flag_m = true ;
400
378
if (design->selected (module ))
@@ -410,7 +388,7 @@ void RTLIL_BACKEND::dump_design(std::ostream &f, RTLIL::Design *design, bool onl
410
388
f << stringf (" autoidx %d\n " , autoidx);
411
389
}
412
390
413
- for (auto module : design->modules ( )) {
391
+ for (const auto & [_, module ] : reversed ( design->modules_ )) {
414
392
if (!only_selected || design->selected (module )) {
415
393
if (only_selected)
416
394
f << stringf (" \n " );
@@ -438,10 +416,14 @@ struct RTLILBackend : public Backend {
438
416
log (" -selected\n " );
439
417
log (" only write selected parts of the design.\n " );
440
418
log (" \n " );
419
+ log (" -sort\n " );
420
+ log (" sort design in-place (used to be default).\n " );
421
+ log (" \n " );
441
422
}
442
423
void execute (std::ostream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design) override
443
424
{
444
425
bool selected = false ;
426
+ bool do_sort = false ;
445
427
446
428
log_header (design, " Executing RTLIL backend.\n " );
447
429
@@ -452,14 +434,19 @@ struct RTLILBackend : public Backend {
452
434
selected = true ;
453
435
continue ;
454
436
}
437
+ if (arg == " -sort" ) {
438
+ do_sort = true ;
439
+ continue ;
440
+ }
455
441
break ;
456
442
}
457
443
extra_args (f, filename, args, argidx);
458
444
459
- design->sort ();
460
-
461
445
log (" Output filename: %s\n " , filename);
462
446
447
+ if (do_sort)
448
+ design->sort ();
449
+
463
450
*f << stringf (" # Generated by %s\n " , yosys_maybe_version ());
464
451
RTLIL_BACKEND::dump_design (*f, design, selected, true , false );
465
452
}
@@ -528,7 +515,7 @@ struct DumpPass : public Pass {
528
515
if (!empty) {
529
516
rewrite_filename (filename);
530
517
std::ofstream *ff = new std::ofstream;
531
- ff->open (filename. c_str () , append ? std::ofstream::app : std::ofstream::trunc);
518
+ ff->open (filename, append ? std::ofstream::app : std::ofstream::trunc);
532
519
if (ff->fail ()) {
533
520
delete ff;
534
521
log_error (" Can't open file `%s' for writing: %s\n " , filename, strerror (errno));
0 commit comments