-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathverilog_synthesis.cpp
3781 lines (2914 loc) · 94.9 KB
/
verilog_synthesis.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************\
Module: Verilog Synthesis
Author: Daniel Kroening, [email protected]
\*******************************************************************/
#include "verilog_synthesis.h"
#include "verilog_synthesis_class.h"
#include <util/bitvector_expr.h>
#include <util/bitvector_types.h>
#include <util/c_types.h>
#include <util/ebmc_util.h>
#include <util/expr_util.h>
#include <util/identifier.h>
#include <util/mathematical_types.h>
#include <util/simplify_expr.h>
#include <util/std_expr.h>
#include "aval_bval_encoding.h"
#include "expr2verilog.h"
#include "sva_expr.h"
#include "verilog_expr.h"
#include "verilog_lowering.h"
#include "verilog_typecheck_expr.h"
#include <cassert>
#include <map>
#include <set>
/*******************************************************************\
Function: verilog_synthesist::synth_expr
Inputs:
Outputs:
Purpose:
\*******************************************************************/
exprt verilog_synthesist::synth_expr(exprt expr, symbol_statet symbol_state)
{
// First lower any Verilog-specific expressions
auto lowered = verilog_lowering(expr);
// Now replace symbols by known values, and expand
// function definitions.
return synth_expr_rec(lowered, symbol_state);
}
/*******************************************************************\
Function: verilog_synthesist::synth_expr_rec
Inputs:
Outputs:
Purpose:
\*******************************************************************/
exprt verilog_synthesist::synth_expr_rec(exprt expr, symbol_statet symbol_state)
{
if(expr.id() == ID_function_call)
{
return expand_function_call(to_function_call_expr(expr), symbol_state);
}
else if(expr.id() == ID_hierarchical_identifier)
{
expand_hierarchical_identifier(
to_hierarchical_identifier_expr(expr), symbol_state);
return expr;
}
// Do the operands recursively
for(auto &op : expr.operands())
op = synth_expr_rec(op, symbol_state);
if(expr.id()==ID_symbol)
{
const symbolt &symbol=ns.lookup(to_symbol_expr(expr));
if(symbol.is_macro)
{
// substitute
assert(symbol.value.is_not_nil());
// recursive call
return synth_expr_rec(symbol.value, symbol_state);
}
else
{
switch(symbol_state)
{
case symbol_statet::SYMBOL:
return expr; // leave as is
case symbol_statet::CURRENT:
return current_value(symbol);
case symbol_statet::FINAL:
return final_value(symbol);
case symbol_statet::NONE:
throw errort().with_location(expr.source_location())
<< "symbols not allowed here";
}
UNREACHABLE;
}
}
else if(expr.id() == ID_typecast)
{
// We do some simplification
if(to_typecast_expr(expr).op().type().id() == ID_integer)
expr = simplify_expr(expr, ns);
return expr;
}
else
return expr; // leave as is
UNREACHABLE;
}
/*******************************************************************\
Function: verilog_synthesist::synthesis_constant
Inputs:
Outputs:
Purpose:
\*******************************************************************/
std::optional<mp_integer>
verilog_synthesist::synthesis_constant(const exprt &expr)
{
exprt synthesised = synth_expr(expr, symbol_statet::CURRENT);
exprt simplified = simplify_expr(synthesised, ns);
if(!simplified.is_constant())
return {};
if(simplified.type().id() == ID_bool)
return simplified.is_true() ? 1 : 0;
auto number = numeric_cast<mp_integer>(to_constant_expr(simplified));
if(!number.has_value())
DATA_INVARIANT_WITH_DIAGNOSTICS(
false,
"synthesis_constant expects a numerical type",
simplified.pretty());
return number.value();
}
/*******************************************************************\
Function: verilog_synthesist::synth_lhs_expr
Inputs:
Outputs:
Purpose:
\*******************************************************************/
exprt verilog_synthesist::synth_lhs_expr(exprt expr)
{
// case-split on possible expressions on the LHS of an assignment
if(expr.id() == ID_symbol)
{
return expr; // leave as is
}
else if(expr.id() == ID_concatenation)
{
for(auto &op : expr.operands())
op = synth_lhs_expr(op);
return expr;
}
else if(expr.id() == ID_verilog_non_indexed_part_select)
{
auto &part_select = to_verilog_non_indexed_part_select_expr(expr);
part_select.src() = synth_lhs_expr(part_select.src());
// The indices are expected to be constants.
return expr;
}
else if(
expr.id() == ID_verilog_indexed_part_select_plus ||
expr.id() == ID_verilog_indexed_part_select_minus)
{
auto &part_select = to_verilog_indexed_part_select_plus_or_minus_expr(expr);
part_select.src() = synth_lhs_expr(part_select.src());
// The index need not be a constant, and is _not_ an lhs.
part_select.index() =
synth_expr(part_select.index(), symbol_statet::CURRENT);
return expr;
}
else if(expr.id() == ID_index)
{
auto &index_expr = to_index_expr(expr);
// The array is an 'lhs' but the index is not.
index_expr.array() = synth_lhs_expr(index_expr.array());
index_expr.index() = synth_expr(index_expr.index(), symbol_statet::CURRENT);
return expr;
}
else if(expr.id() == ID_extractbit)
{
auto &extractbit_expr = to_extractbit_expr(expr);
// The vector is an 'lhs' but the bit index is not.
extractbit_expr.src() = synth_lhs_expr(extractbit_expr.src());
extractbit_expr.index() =
synth_expr(extractbit_expr.index(), symbol_statet::CURRENT);
return expr;
}
else if(expr.id() == ID_member)
{
auto &member_expr = to_member_expr(expr);
member_expr.struct_op() = synth_lhs_expr(member_expr.struct_op());
return expr;
}
else
{
DATA_INVARIANT_WITH_DIAGNOSTICS(
false, "unexpected lhs during synthesis", expr.pretty());
}
}
/*******************************************************************\
Function: verilog_synthesist::value_mapt::guarded_expr
Inputs:
Outputs:
Purpose:
\*******************************************************************/
exprt verilog_synthesist::value_mapt::guarded_expr(exprt expr) const
{
if(guard.empty())
return expr;
else
return implies_exprt{conjunction(guard), std::move(expr)};
}
/*******************************************************************\
Function: verilog_synthesist::function_locality
Inputs:
Outputs:
Purpose:
\*******************************************************************/
void verilog_synthesist::function_locality(const symbolt &function_symbol)
{
// Find all local symbols of the function, mark their
// assignments as local.
auto prefix = id2string(function_symbol.name) + '.';
for(auto &symbol : symbol_table.symbols)
{
if(symbol.first.starts_with(prefix))
{
assignments[symbol.first].is_cycle_local = true;
}
}
}
/*******************************************************************\
Function: verilog_synthesist::expand_function_call
Inputs:
Outputs:
Purpose:
\*******************************************************************/
exprt verilog_synthesist::expand_function_call(
const function_call_exprt &call,
symbol_statet symbol_state)
{
// Is it a 'system function call'?
if(call.is_system_function_call())
{
auto identifier = to_symbol_expr(call.function()).get_identifier();
if(identifier == "$ND")
{
std::string identifier =
id2string(module) + "::nondet::" + std::to_string(nondet_count++);
auto arguments = call.arguments();
exprt select_one(
ID_constraint_select_one, call.type(), std::move(arguments));
select_one.set(ID_identifier, identifier);
return select_one.with_source_location(call);
}
else if(identifier == "$past")
{
auto what = call.arguments()[0];
auto ticks = call.arguments().size() < 2
? from_integer(1, integer_typet())
: call.arguments()[1];
return verilog_past_exprt{what, ticks}.with_source_location(call);
}
else if(
identifier == "$stable" || identifier == "$rose" ||
identifier == "$fell" || identifier == "$changed")
{
DATA_INVARIANT(call.arguments().size() >= 1, "must have argument");
auto what = call.arguments()[0];
auto past = verilog_past_exprt{what, from_integer(1, integer_typet())}
.with_source_location(call);
auto lsb = [](exprt expr) {
return extractbit_exprt{
std::move(expr), from_integer(0, integer_typet{})};
};
if(identifier == "$stable")
return equal_exprt{what, past};
else if(identifier == "$changed")
return notequal_exprt{what, past};
else if(identifier == "$rose")
return and_exprt{not_exprt{lsb(past)}, lsb(what)};
else if(identifier == "$fell")
return and_exprt{lsb(past), not_exprt{lsb(what)}};
else
DATA_INVARIANT(false, "all cases covered");
}
else if(identifier == "$countones")
{
// lower to popcount
DATA_INVARIANT(
call.arguments().size() == 1, "$countones must have one argument");
auto what = synth_expr(call.arguments()[0], symbol_state); // rec. call
return popcount_exprt{what, call.type()};
}
else
{
// Attempt to constant fold.
verilog_typecheck_exprt verilog_typecheck_expr(
standard, false, ns, get_message_handler());
auto result =
verilog_typecheck_expr.elaborate_constant_system_function_call(call);
if(!result.is_constant())
{
throw errort().with_location(call.source_location())
<< "cannot synthesise system function " << to_string(call.function());
}
return result;
}
}
// check some restrictions
if(construct == constructt::OTHER)
{
throw errort().with_location(call.source_location())
<< "function call not allowed here";
}
// this is essentially inlined
const symbol_exprt &function=to_symbol_expr(call.function());
const symbolt &symbol=ns.lookup(function);
if(symbol.type.id()!=ID_code)
{
throw errort().with_location(call.source_location())
<< "function call expects function argument";
}
const code_typet &code_type=to_code_type(symbol.type);
if(code_type.return_type()==empty_typet())
{
throw errort().with_location(call.source_location())
<< "function call cannot call task";
}
const code_typet::parameterst ¶meters=
code_type.parameters();
const exprt::operandst &actuals=
call.op1().operands();
if(parameters.size()!=actuals.size())
{
throw errort().with_location(call.source_location())
<< "wrong number of arguments";
}
// preserve locality of function-local variables
function_locality(symbol);
// do assignments to function parameters
for(unsigned i=0; i<parameters.size(); i++)
{
const symbolt &a_symbol=ns.lookup(parameters[i].get_identifier());
verilog_blocking_assignt assignment{
a_symbol.symbol_expr().with_source_location(call), actuals[i]};
assignment.add_source_location()=call.source_location();
synth_statement(assignment);
}
synth_statement(to_verilog_statement(symbol.value));
// replace function call by return value symbol
const symbolt &return_symbol=
ns.lookup(id2string(symbol.name)+"."+
id2string(symbol.base_name));
// get the current value
auto result = synth_expr(return_symbol.symbol_expr(), symbol_statet::CURRENT);
return result;
}
/*******************************************************************\
Function: verilog_synthesist::expand_hierarchical_identifier
Inputs:
Outputs:
Purpose:
\*******************************************************************/
void verilog_synthesist::expand_hierarchical_identifier(
hierarchical_identifier_exprt &expr,
symbol_statet symbol_state)
{
expr.lhs() = synth_expr(expr.lhs(), symbol_state);
if(expr.lhs().id() != ID_symbol)
{
throw errort().with_location(expr.source_location())
<< "synthesis expected symbol on lhs of `.'";
}
if(expr.lhs().type().id() != ID_module_instance)
{
throw errort().with_location(expr.source_location())
<< "synthesis expected module instance on lhs of `.', but got `"
<< to_string(expr.lhs().type()) << '\'';
}
const irep_idt &lhs_identifier = expr.lhs().get(ID_identifier);
// rhs
const irep_idt &rhs_identifier = expr.rhs().get_identifier();
// just patch together
irep_idt full_identifier =
id2string(lhs_identifier) + '.' + id2string(rhs_identifier);
// Note: the instance copy may not yet be in symbol table,
// as the inst module item may be later.
// The type checker already checked that it's fine.
symbol_exprt new_symbol{full_identifier, expr.type()};
new_symbol.add_source_location()=expr.source_location();
expr.swap(new_symbol);
}
/*******************************************************************\
Function: verilog_synthesist::assignment_rec
Inputs:
Outputs:
Purpose:
\*******************************************************************/
void verilog_synthesist::assignment_rec(
const exprt &lhs,
const exprt &rhs,
bool blocking)
{
if(lhs.id()==ID_concatenation) // split it up
{
// TODO: split it up more intelligently;
// bit-wise is wasteful.
mp_integer offset = 0;
// do it from right to left
for(exprt::operandst::const_reverse_iterator
it=lhs.operands().rbegin();
it!=lhs.operands().rend();
it++)
{
auto offset_constant = from_integer(offset, natural_typet{});
if(it->type().id()==ID_bool)
{
exprt bit_extract(ID_extractbit, it->type());
bit_extract.add_to_operands(rhs);
bit_extract.add_to_operands(offset_constant);
++offset;
assignment_rec(*it, bit_extract, blocking);
}
else if(it->type().id()==ID_signedbv ||
it->type().id()==ID_unsignedbv)
{
extractbits_exprt bit_extract(rhs, offset_constant, it->type());
assignment_rec(*it, bit_extract, blocking);
auto width = get_width(it->type());
offset+=width;
}
else
{
throw errort().with_location(it->source_location())
<< "assignment to type `" << to_string(it->type())
<< "' not supported";
}
}
return;
}
// get identifier
const symbolt &symbol=assignment_symbol(lhs);
if(symbol.type.id()==ID_verilog_realtime)
{
// we silently ignore these
return;
}
if(!symbol.is_state_var)
{
throw errort().with_location(lhs.source_location())
<< "assignment to non-register";
}
if(construct==constructt::ALWAYS &&
event_guard==event_guardt::NONE)
{
throw errort().with_location(lhs.source_location())
<< "assignment in 'always' context without event guard";
}
if(construct == constructt::ALWAYS_FF && event_guard == event_guardt::NONE)
{
throw errort().with_location(lhs.source_location())
<< "assignment in 'always_ff' context without event guard";
}
{
event_guardt new_type;
if(construct == constructt::INITIAL)
new_type=event_guardt::CLOCK;
else if(construct == constructt::ALWAYS_COMB)
new_type = event_guardt::COMBINATIONAL;
else
new_type = event_guard;
assignmentt &assignment=assignments[symbol.name];
if(assignment.is_cycle_local)
{
}
else
{
if(assignment.type == event_guardt::NONE)
assignment.type = new_type;
else if(assignment.type != new_type)
{
throw errort().with_location(lhs.source_location())
<< "conflicting assignment types for `" << symbol.base_name
<< "' (new: " << as_string(new_type)
<< ", old: " << as_string(assignment.type) << ")";
}
membert member;
assignment_member_rec(
lhs,
member,
(construct == constructt::INITIAL) ? assignment.init : assignment.next);
}
}
{
assert(value_map!=NULL);
exprt new_rhs(rhs), new_value;
assignment_rec(lhs, new_rhs, new_value); // start of recursion
// These can explode if the symbol is assigned more than once
// and also used more than once, e.g., in a multi-dimensional array.
// We add a fresh symbol for anything that is not trivial
// to prevent that.
if(new_value.id()!=ID_symbol &&
new_value.id()!=ID_constant)
{
replace_by_wire(new_value, symbol);
}
if(blocking)
value_map->current.assign(symbol.name, new_value);
value_map->final.assign(symbol.name, new_value);
}
}
/*******************************************************************\
Function: verilog_synthesist::assignment_rec
Inputs:
Outputs:
Purpose:
\*******************************************************************/
void verilog_synthesist::assignment_rec(
const exprt &lhs,
exprt &rhs,
exprt &new_value)
{
if(lhs.id()==ID_symbol)
{
new_value.swap(rhs);
rhs.clear();
}
else if(lhs.id()==ID_index ||
lhs.id()==ID_extractbit)
{
if(lhs.operands().size()!=2)
{
throw errort() << "index takes two operands";
}
const exprt &lhs_array = to_binary_expr(lhs).op0();
const exprt &lhs_index = to_binary_expr(lhs).op1();
// turn
// a[i]=e
// into
// a'==a WITH [i:=e]
with_exprt new_rhs(lhs_array, lhs_index, rhs);
// do the array
new_rhs.old() = synth_expr(new_rhs.old(), symbol_statet::FINAL);
// do the index
new_rhs.where() = synth_expr(new_rhs.where(), symbol_statet::CURRENT);
// do the value
assignment_rec(lhs_array, new_rhs, new_value); // recursive call
}
else if(lhs.id() == ID_verilog_non_indexed_part_select)
{
// we flatten n-bit part select into n times extractbit
auto &part_select = to_verilog_non_indexed_part_select_expr(lhs);
const exprt &lhs_src = part_select.src();
const exprt &lhs_msb = part_select.msb();
const exprt &lhs_lsb = part_select.lsb();
mp_integer from, to;
if(to_integer_non_constant(lhs_msb, to))
{
throw errort().with_location(lhs_msb.source_location())
<< "failed to convert range";
}
if(to_integer_non_constant(lhs_lsb, from))
{
throw errort().with_location(lhs_lsb.source_location())
<< "failed to convert range";
}
if(from > to)
std::swap(from, to);
// redundant?
if(from == 0 && to == get_width(lhs_src.type()) - 1)
{
assignment_rec(lhs_src, rhs, new_value); // recursive call
return;
}
// turn
// a[i]=e
// into
// a'==a WITH [i:=e]
exprt synth_lhs_src(lhs_src);
// do the array, but just once
synth_lhs_src = synth_expr(synth_lhs_src, symbol_statet::FINAL);
exprt last_value;
last_value.make_nil();
const auto rhs_width = get_width(lhs_src.type());
// We drop bits that are out of bounds
auto from_in_range = std::max(mp_integer{0}, from);
auto to_in_range = std::min(rhs_width - 1, to);
// now add the indexes in the range
for(mp_integer i = from_in_range; i <= to_in_range; ++i)
{
exprt offset = from_integer(i - from, integer_typet());
exprt rhs_extractbit(ID_extractbit, bool_typet());
rhs_extractbit.reserve_operands(2);
rhs_extractbit.add_to_operands(rhs);
rhs_extractbit.add_to_operands(std::move(offset));
exprt count = from_integer(i, integer_typet());
exprt new_rhs(ID_with, lhs_src.type());
new_rhs.reserve_operands(3);
new_rhs.add_to_operands(synth_lhs_src);
new_rhs.add_to_operands(std::move(count));
new_rhs.add_to_operands(std::move(rhs_extractbit));
// do the value
assignment_rec(lhs_src, new_rhs, new_value); // recursive call
if(last_value.is_nil())
last_value.swap(new_value);
else
{
// merge the withs
assert(new_value.id() == ID_with);
assert(new_value.operands().size() == 3);
assert(last_value.id() == ID_with);
assert(last_value.operands().size() >= 3);
last_value.add_to_operands(std::move(to_with_expr(new_value).where()));
last_value.add_to_operands(
std::move(to_with_expr(new_value).new_value()));
}
}
new_value.swap(last_value);
}
else if(
lhs.id() == ID_verilog_indexed_part_select_plus ||
lhs.id() == ID_verilog_indexed_part_select_minus)
{
// we flatten n-bit part select into n times extractbit
auto &part_select = to_verilog_indexed_part_select_plus_or_minus_expr(lhs);
const exprt &lhs_src = part_select.src();
const exprt &lhs_index = part_select.index();
const exprt &lhs_width = part_select.width();
auto index_opt = synthesis_constant(lhs_index);
if(!index_opt.has_value())
{
throw errort().with_location(lhs_index.source_location())
<< "failed to convert part select index";
}
auto width_opt = synthesis_constant(lhs_width);
if(!width_opt.has_value())
{
throw errort().with_location(lhs_width.source_location())
<< "failed to convert part select width";
}
mp_integer index = *index_opt, width = *width_opt;
// turn
// a[i]=e
// into
// a'==a WITH [i:=e]
exprt synth_lhs_src(lhs_src);
// do the array, but just once
synth_lhs_src = synth_expr(synth_lhs_src, symbol_statet::FINAL);
exprt last_value;
last_value.make_nil();
const auto rhs_width = get_width(lhs_src.type());
// We drop bits that are out of bounds
auto from_in_range = std::max(mp_integer{0}, index);
auto to_in_range = std::min(rhs_width - 1, index + width - 1);
// now add the indexes in the range
for(mp_integer i = from_in_range; i <= to_in_range; ++i)
{
exprt offset = from_integer(i - index, integer_typet());
exprt rhs_extractbit(ID_extractbit, bool_typet());
rhs_extractbit.reserve_operands(2);
rhs_extractbit.add_to_operands(rhs);
rhs_extractbit.add_to_operands(std::move(offset));
exprt count = from_integer(i, integer_typet());
exprt new_rhs(ID_with, lhs_src.type());
new_rhs.reserve_operands(3);
new_rhs.add_to_operands(synth_lhs_src);
new_rhs.add_to_operands(std::move(count));
new_rhs.add_to_operands(std::move(rhs_extractbit));
// do the value
assignment_rec(lhs_src, new_rhs, new_value); // recursive call
if(last_value.is_nil())
last_value.swap(new_value);
else
{
// merge the withs
assert(new_value.id() == ID_with);
assert(new_value.operands().size() == 3);
assert(last_value.id() == ID_with);
assert(last_value.operands().size() >= 3);
last_value.add_to_operands(std::move(to_with_expr(new_value).where()));
last_value.add_to_operands(
std::move(to_with_expr(new_value).new_value()));
}
}
new_value.swap(last_value);
}
else if(lhs.id() == ID_member)
{
const auto &member_expr = to_member_expr(lhs);
const exprt &lhs_compound = member_expr.struct_op();
auto component_name = member_expr.get_component_name();
if(
lhs_compound.type().id() == ID_struct ||
lhs_compound.type().id() == ID_union)
{
// turn
// s.m=e
// into
// s'==s WITH [m:=e]
auto synth_compound = synth_expr(lhs_compound, symbol_statet::FINAL);
with_exprt new_rhs{
synth_compound, member_designatort{component_name}, rhs};
// recursive call
assignment_rec(lhs_compound, new_rhs, new_value); // recursive call
}
else
{
throw errort() << "unexpected member lhs: " << lhs_compound.type().id();
}
}
else
{
throw errort() << "unexpected lhs: " << lhs.id();
}
#if 0
// do "with" merging
if(new_value.id()==ID_with &&
new_value.op0().id()==ID_with)
{
exprt tmp;
tmp.swap(new_value.op0());
tmp.reserve_operands(tmp.operands().size()+2);
tmp.add_to_operands(std::move(new_value.op1()));
tmp.add_to_operands(std::move(new_value.op2()));
new_value.swap(tmp);
}
#endif
}
/*******************************************************************\
Function: verilog_synthesist::replace_by_wire
Inputs:
Outputs:
Purpose:
\*******************************************************************/
void verilog_synthesist::replace_by_wire(
exprt &what,
const symbolt &base)
{
symbolt new_symbol;
do
{
unsigned c=temporary_counter++;
new_symbol.name=id2string(base.name)+"_aux"+std::to_string(c);
new_symbol.base_name=id2string(base.base_name)+"_aux"+std::to_string(c);
}
while(symbol_table.symbols.find(new_symbol.name)!=symbol_table.symbols.end());
new_symbol.type=what.type();
new_symbol.module=base.module;
new_symbol.mode=base.mode;
new_symbol.location=base.location;
new_symbol.value=nil_exprt();
new_symbol.is_auxiliary=true;
symbol_exprt symbol_expr(new_symbol.name, new_symbol.type);
assignmentt &assignment=assignments[new_symbol.name];
assignment.next.value=what;
new_wires.insert(new_symbol.name);
if(symbol_table.add(new_symbol))
{
throw errort() << "failed to add replace_by_wire symbol";
}
what=symbol_expr;
}
/*******************************************************************\
Function: verilog_synthesist::assignment_member_rec
Inputs:
Outputs:
Purpose:
\*******************************************************************/
void verilog_synthesist::assignment_member_rec(
const exprt &lhs,
membert &member,
assignmentt::datat &data)
{
if(lhs.id()==ID_symbol)
{
// done
add_assignment_member(lhs, member, data);
}
else if(lhs.id()==ID_index ||
lhs.id()==ID_extractbit)
{
if(lhs.operands().size()!=2)
{
throw errort() << "index takes two operands";
}
exprt tmp_lhs_op1 = simplify_expr(to_binary_expr(lhs).op1(), ns);
// constant index?
mp_integer index;
if(to_integer_non_constant(tmp_lhs_op1, index))
{
// done
add_assignment_member(lhs, member, data);
}
else
{