-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathuhdm_ast.cc
5698 lines (5407 loc) · 242 KB
/
uhdm_ast.cc
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
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits>
#include <regex>
#include <string>
#include <utility>
#include <vector>
#include "frontends/ast/ast.h"
#include "libs/sha1/sha1.h"
#include "uhdm_ast.h"
#include "utils/memory.h"
// UHDM
#include <uhdm/ExprEval.h>
#include <uhdm/uhdm.h>
#include <uhdm/vpi_user.h>
#include "synlig_const2ast.h"
#include "synlig_simplify.h"
YOSYS_NAMESPACE_BEGIN
namespace VERILOG_FRONTEND
{
extern bool sv_mode;
}
YOSYS_NAMESPACE_END
namespace systemverilog_plugin
{
using namespace ::Yosys;
namespace AST
{
using namespace ::Yosys::AST;
namespace Extended
{
enum AstNodeTypeExtended {
AST_DOT = ::Yosys::AST::AST_BIND + 1, // here we always want to point to the last element of yosys' AstNodeType
AST_BREAK,
AST_CONTINUE
};
}
} // namespace AST
namespace attr_id
{
static bool already_initialized = false;
static IdString partial;
static IdString packed_ranges;
static IdString unpacked_ranges;
static IdString force_convert;
static IdString is_imported;
static IdString is_simplified_wire;
static IdString low_high_bound;
static IdString is_type_parameter;
static IdString is_elaborated_module;
}; // namespace attr_id
// TODO(mglb): use attr_id::* directly everywhere and remove those methods.
/*static*/ const IdString &UhdmAst::partial() { return attr_id::partial; }
/*static*/ const IdString &UhdmAst::packed_ranges() { return attr_id::packed_ranges; }
/*static*/ const IdString &UhdmAst::unpacked_ranges() { return attr_id::unpacked_ranges; }
/*static*/ const IdString &UhdmAst::force_convert() { return attr_id::force_convert; }
/*static*/ const IdString &UhdmAst::is_imported() { return attr_id::is_imported; }
/*static*/ const IdString &UhdmAst::is_simplified_wire() { return attr_id::is_simplified_wire; }
/*static*/ const IdString &UhdmAst::low_high_bound() { return attr_id::low_high_bound; }
/*static*/ const IdString &UhdmAst::is_elaborated_module() { return attr_id::is_elaborated_module; }
#define MAKE_INTERNAL_ID(X) IdString("$systemverilog_plugin$" #X)
void attr_id_init()
{
// Initialize only once
if (attr_id::already_initialized)
return;
attr_id::already_initialized = true;
// Actual initialization
// Register IdStrings. Can't be done statically, as the IdString class uses resources created during Yosys initialization which happens after
// static initialization of the plugin when everything is statically linked.
attr_id::partial = MAKE_INTERNAL_ID(partial);
attr_id::packed_ranges = MAKE_INTERNAL_ID(packed_ranges);
attr_id::unpacked_ranges = MAKE_INTERNAL_ID(unpacked_ranges);
attr_id::force_convert = MAKE_INTERNAL_ID(force_convert);
attr_id::is_imported = MAKE_INTERNAL_ID(is_imported);
attr_id::is_simplified_wire = MAKE_INTERNAL_ID(is_simplified_wire);
attr_id::low_high_bound = MAKE_INTERNAL_ID(low_high_bound);
attr_id::is_type_parameter = MAKE_INTERNAL_ID(is_type_parameter);
attr_id::is_elaborated_module = MAKE_INTERNAL_ID(is_elaborated_module);
}
void attr_id_cleanup()
{
// Release static copies of private IdStrings.
attr_id::low_high_bound = IdString();
attr_id::is_simplified_wire = IdString();
attr_id::is_imported = IdString();
attr_id::force_convert = IdString();
attr_id::unpacked_ranges = IdString();
attr_id::packed_ranges = IdString();
attr_id::partial = IdString();
attr_id::is_type_parameter = IdString();
attr_id::is_elaborated_module = IdString();
attr_id::already_initialized = false;
}
static AST::AstNode *get_attribute(AST::AstNode *node, const IdString &attribute)
{
log_assert(node);
if (!node->attributes.count(attribute))
return nullptr;
return node->attributes[attribute];
}
// Consumes attr_node.
static void set_attribute(AST::AstNode *node, const IdString &attribute, AST::AstNode *attr_node)
{
log_assert(node);
log_assert(attr_node);
delete node->attributes[attribute];
node->attributes[attribute] = attr_node;
}
// Delete the selected attribute if it exists.
// Does nothing if the node doesn't exist, or the attribute doesn't exists.
static void delete_attribute(AST::AstNode *node, const IdString &attribute)
{
if (!node)
return;
if (node->attributes.count(attribute)) {
delete node->attributes[attribute];
node->attributes.erase(attribute);
}
}
// Delete all attributes that belong to the SV plugin.
// The attributes beloning to Yosys are *not* deleted here.
static void delete_internal_attributes(AST::AstNode *node)
{
if (!node)
return;
for (auto &attr : {UhdmAst::partial(), UhdmAst::packed_ranges(), UhdmAst::unpacked_ranges(), UhdmAst::force_convert(), UhdmAst::is_imported(),
UhdmAst::is_simplified_wire(), UhdmAst::low_high_bound(), attr_id::is_type_parameter, attr_id::is_elaborated_module}) {
delete_attribute(node, attr);
}
}
template <typename T> class ScopedValueChanger
{
T &ref;
const T prev_val;
public:
ScopedValueChanger() = delete;
explicit ScopedValueChanger(T &r) : ref(r), prev_val(ref) {}
ScopedValueChanger(T &r, const T &val) : ref(r), prev_val(ref) { ref = val; }
ScopedValueChanger(ScopedValueChanger &&) = delete;
ScopedValueChanger &operator=(ScopedValueChanger &&) = delete;
ScopedValueChanger(const ScopedValueChanger &) = delete;
ScopedValueChanger &operator=(const ScopedValueChanger &) = delete;
~ScopedValueChanger() { ref = prev_val; }
};
template <typename T> ScopedValueChanger(T &) -> ScopedValueChanger<T>;
template <typename T> ScopedValueChanger(T &, const T &) -> ScopedValueChanger<T>;
// Delete all children nodes.
// Does *not* delete attributes.
// This function exists as Yosys's function node->delete_children() does remove all children and attributes.
static void delete_children(AST::AstNode *node)
{
if (!node)
return;
for (auto *child : node->children) {
delete child;
}
node->children.clear();
}
static std::vector<AST::AstNode *> get_ranges(AST::AstNode *id)
{
log_assert(!id->children.empty());
std::vector<AST::AstNode *> ranges;
for (auto c : id->children) {
if (c->type == AST::AST_RANGE) {
ranges.push_back(c);
}
}
return ranges;
}
static std::vector<AST::AstNode *> get_all_ranges(AST::AstNode *id)
{
log_assert(!id->children.empty());
std::vector<AST::AstNode *> ranges;
for (auto c : id->children) {
if (c->type == AST::AST_RANGE) {
ranges.push_back(c);
}
for (auto pair : c->attributes) {
if (pair.second && !pair.second->children.empty()) {
for (auto r : get_all_ranges(pair.second)) {
ranges.push_back(r);
}
}
}
}
return ranges;
}
static void simplify_sv(AST::AstNode *current_node, AST::AstNode *parent_node);
static void sanitize_symbol_name(std::string &name)
{
if (!name.empty()) {
auto pos = name.find_last_of('@');
name = name.substr(pos + 1);
// symbol names must begin with '\'
name.insert(0, "\\");
}
}
static std::string get_object_name(vpiHandle obj_h, const std::vector<int> &name_fields = {vpiName})
{
std::string objectName;
for (auto name : name_fields) {
if (auto s = vpi_get_str(name, obj_h)) {
objectName = s;
sanitize_symbol_name(objectName);
break;
}
}
return objectName;
}
static std::string get_name(vpiHandle obj_h) { return get_object_name(obj_h, {vpiName, vpiDefName}); }
static std::string strip_package_name(std::string name)
{
auto sep_index = name.find("::");
if (sep_index != string::npos) {
name = name.substr(sep_index + 1);
name[0] = '\\';
}
return name;
}
static AST::AstNode *mkconst_real(double d)
{
AST::AstNode *node = new AST::AstNode(AST::AST_REALVALUE);
node->realvalue = d;
return node;
}
static AST::AstNode *make_range(int left, int right, bool is_signed = false)
{
// generate a pre-validated range node for a fixed signal range.
auto range = new AST::AstNode(AST::AST_RANGE);
range->range_left = left;
range->range_right = right;
range->range_valid = true;
range->children.push_back(AST::AstNode::mkconst_int(left, true));
range->children.push_back(AST::AstNode::mkconst_int(right, true));
range->is_signed = is_signed;
return range;
}
static void copy_packed_unpacked_attribute(AST::AstNode *from, AST::AstNode *to)
{
if (!to->attributes.count(UhdmAst::packed_ranges()))
to->attributes[UhdmAst::packed_ranges()] = AST::AstNode::mkconst_int(1, false, 1);
if (!to->attributes.count(UhdmAst::unpacked_ranges()))
to->attributes[UhdmAst::unpacked_ranges()] = AST::AstNode::mkconst_int(1, false, 1);
if (from->attributes.count(UhdmAst::packed_ranges())) {
for (auto r : from->attributes[UhdmAst::packed_ranges()]->children) {
to->attributes[UhdmAst::packed_ranges()]->children.push_back(r->clone());
}
}
if (from->attributes.count(UhdmAst::unpacked_ranges())) {
for (auto r : from->attributes[UhdmAst::unpacked_ranges()]->children) {
to->attributes[UhdmAst::unpacked_ranges()]->children.push_back(r->clone());
}
}
}
static int get_max_offset_struct(AST::AstNode *node)
{
// get the width from the MS member in the struct
// as members are laid out from left to right in the packed wire
log_assert(node->type == AST::AST_STRUCT || node->type == AST::AST_UNION);
while (node->range_left < 0) {
node = node->children[0];
}
return node->range_left;
}
static void visitEachDescendant(AST::AstNode *node, const std::function<void(AST::AstNode *)> &f)
{
for (auto child : node->children) {
f(child);
visitEachDescendant(child, f);
}
}
static void add_multirange_wire(AST::AstNode *node, std::vector<AST::AstNode *> packed_ranges, std::vector<AST::AstNode *> unpacked_ranges)
{
delete_attribute(node, UhdmAst::packed_ranges());
node->attributes[UhdmAst::packed_ranges()] = AST::AstNode::mkconst_int(1, false, 1);
if (!packed_ranges.empty()) {
node->attributes[UhdmAst::packed_ranges()]->children = std::move(packed_ranges);
}
delete_attribute(node, UhdmAst::unpacked_ranges());
node->attributes[UhdmAst::unpacked_ranges()] = AST::AstNode::mkconst_int(1, false, 1);
if (!unpacked_ranges.empty()) {
node->attributes[UhdmAst::unpacked_ranges()]->children = std::move(unpacked_ranges);
}
}
// Sets the `wire_node->multirange_dimensions` attribute and returns the total sizes of packed and unpacked ranges.
static std::pair<size_t, size_t> set_multirange_dimensions(AST::AstNode *wire_node, const std::vector<AST::AstNode *> packed_ranges,
const std::vector<AST::AstNode *> unpacked_ranges)
{
// node->multirange_dimensions stores dimensions' offsets and widths.
// It shall have even number of elements.
// For a range of [A:B] it should be appended with {min(A,B)} and {max(A,B)-min(A,B)+1}
// For a range of [A] it should be appended with {0} and {A}
auto calc_range_size = [wire_node](const std::vector<AST::AstNode *> &ranges) -> size_t {
size_t size = 1;
for (size_t i = 0; i < ranges.size(); i++) {
log_assert(AST_INTERNAL::current_ast_mod);
simplify_sv(ranges[i], wire_node);
// If it's a range of [A], make it [A:A].
if (ranges[i]->children.size() == 1) {
ranges[i]->children.push_back(ranges[i]->children[0]->clone());
}
while (simplify(ranges[i], true, false, false, 1, -1, false, false)) {
}
// this workaround case, where yosys doesn't follow id2ast and simplifies it to resolve constant
if (ranges[i]->children[0]->id2ast) {
simplify_sv(ranges[i]->children[0]->id2ast, ranges[i]->children[0]);
while (simplify(ranges[i]->children[0]->id2ast, true, false, false, 1, -1, false, false)) {
}
}
if (ranges[i]->children[1]->id2ast) {
simplify_sv(ranges[i]->children[1]->id2ast, ranges[i]->children[1]);
while (simplify(ranges[i]->children[1]->id2ast, true, false, false, 1, -1, false, false)) {
}
}
simplify_sv(ranges[i], wire_node);
while (simplify(ranges[i], true, false, false, 1, -1, false, false)) {
}
log_assert(ranges[i]->children[0]->type == AST::AST_CONSTANT);
log_assert(ranges[i]->children[1]->type == AST::AST_CONSTANT);
const auto low = min(ranges[i]->children[0]->integer, ranges[i]->children[1]->integer);
const auto high = max(ranges[i]->children[0]->integer, ranges[i]->children[1]->integer);
const auto elem_size = high - low + 1;
wire_node->multirange_dimensions.push_back(low);
wire_node->multirange_dimensions.push_back(elem_size);
wire_node->multirange_swapped.push_back(ranges[i]->range_swapped);
size *= elem_size;
}
return size;
};
size_t packed_size = calc_range_size(packed_ranges);
size_t unpacked_size = calc_range_size(unpacked_ranges);
log_assert(wire_node->multirange_dimensions.size() % 2 == 0);
return {packed_size, unpacked_size};
}
static AST::AstNode *convert_range(AST::AstNode *id, int packed_ranges_size, int unpacked_ranges_size, int index)
{
log_assert(AST_INTERNAL::current_ast_mod);
log_assert(AST_INTERNAL::current_scope.count(id->str));
AST::AstNode *wire_node = AST_INTERNAL::current_scope[id->str];
log_assert(!wire_node->multirange_dimensions.empty());
std::vector<AST::AstNode *> ranges = get_ranges(id);
// Order of ranges evaluation (IEEE 7.4.5):
// logic [A:B][C:D] wire [E:F][G:H];
// | | | |
// 3 4 1 2
// create vector containing size of single element for given range in evaluation order:
// [wire_size/(max(E, F) - min(E, F) + 1), (wire_size/(max(E, F) - min(E, F) + 1)) / (max(G, H) - min(G, H) - 1)...]
std::vector<int> single_elem_size;
const std::vector<AST::AstNode *> &unpacked_ranges = wire_node->attributes[UhdmAst::unpacked_ranges()]->children;
const std::vector<AST::AstNode *> &packed_ranges = wire_node->attributes[UhdmAst::packed_ranges()]->children;
// TODO(krak): maybe there is better way to get size of whole wire?
log_assert(!wire_node->children.empty());
int elem_size = wire_node->children[0]->range_left + 1;
// multirange_dimensions also contains ranges from wiretype, but
// they should be handled after packed ranges,
// this is primitive way of calculating how many packed ranges is from wiretype
// TODO(krak): this is good candidate for refactor
size_t skip = 0;
if (wire_node->attributes.count(ID::wiretype)) {
AST::AstNode *wiretype_node = wire_node->attributes[ID::wiretype]->id2ast;
if (wiretype_node->type == AST::AST_STRUCT) {
skip = 1;
} else if (wiretype_node->type == AST::AST_WIRE) {
// skip number of dimensions of the object - number of dimensions of the var select
skip = (wire_node->multirange_dimensions.size() / 2) - id->children.size();
} else {
wiretype_node->dumpAst(NULL, "wiretype >");
log_error("Unhandled case in multirange wiretype!");
}
}
for (auto range = unpacked_ranges.begin(); range != unpacked_ranges.end(); range++) {
auto range_node = *range;
// add_multirange_attribute should already make sure there are 2 children nodes in range and they are consts
const auto low = min(range_node->children[0]->integer, range_node->children[1]->integer);
const auto high = max(range_node->children[0]->integer, range_node->children[1]->integer);
elem_size /= high - low + 1;
single_elem_size.push_back(elem_size);
}
for (auto range = packed_ranges.begin() + skip; range != packed_ranges.end(); range++) {
auto range_node = *range;
// 'add_multirange_attribute' function should already make sure there are 2 children nodes in range and they are consts
const auto low = min(range_node->children[0]->integer, range_node->children[1]->integer);
const auto high = max(range_node->children[0]->integer, range_node->children[1]->integer);
elem_size /= high - low + 1;
single_elem_size.push_back(elem_size);
}
if (single_elem_size.empty()) {
// TODO(krak): this needs better support
// only wiretype ranges
return make_range(elem_size - 1, 0);
}
// Same as above but, for offset
// start from unpacked ranges
// TODO(krak): we could also calculate offset while calculating elem_size, good place for refactor
std::vector<int> range_offset;
for (auto i = packed_ranges.size(); i < packed_ranges.size() + unpacked_ranges.size(); i++) {
range_offset.push_back(wire_node->multirange_dimensions[i * 2]);
}
// then packed
for (auto i = skip; i < packed_ranges.size(); i++) {
range_offset.push_back(wire_node->multirange_dimensions[i * 2]);
}
const auto make_node = [](AST::AstNodeType type) {
auto node = std::make_unique<AST::AstNode>(type);
return AstNodeBuilder(std::move(node));
};
const auto make_const = [make_node](uint32_t value, uint8_t width) {
log_assert(width <= 32);
return make_node(AST::AST_CONSTANT).value(value, false, width);
};
AST::AstNode *result = nullptr;
AST::AstNode *range_left = nullptr;
AST::AstNode *range_right = nullptr;
for (size_t i = 0; i < ranges.size(); i++) {
if (i + 1 == ranges.size()) {
// last range can have 2 children
log_assert(ranges[i]->children.size() > 0 && ranges[i]->children.size() < 3);
if (ranges[i]->children.size() == 2) {
range_left = ranges[i]->children[0]->clone();
range_right = ranges[i]->children[1]->clone();
} else {
range_left = ranges[i]->children[0]->clone();
range_right = ranges[i]->children[0]->clone();
}
} else {
// other ranges needs to have single child
log_assert(ranges[i]->children.size() == 1);
range_left = ranges[i]->children[0]->clone();
range_right = ranges[i]->children[0]->clone();
}
if ((i < range_offset.size()) && (range_offset[i] != 0)) {
range_left = make_node(AST::AST_SUB)({range_left, make_const(range_offset[i], 32)});
range_right = make_node(AST::AST_SUB)({range_right, make_const(range_offset[i], 32)});
}
int single_elem_size_val = 1;
if (i < single_elem_size.size()) {
single_elem_size_val = single_elem_size[i];
} else if (!single_elem_size.empty()) {
single_elem_size_val = single_elem_size[0];
}
// range_right = range_right * single_elem_size_val
// range_left = (((range_left + 1) * single_elem_size_val) - range_right) - 1
range_right = make_node(AST::AST_MUL)({range_right, make_const(single_elem_size_val, 32)});
range_left = make_node(AST::AST_SUB)(
{make_node(AST::AST_SUB)(
{make_node(AST::AST_MUL)({make_node(AST::AST_ADD)({range_left, make_const(1, 32)}), make_const(single_elem_size_val, 32)}),
range_right->clone()}),
make_const(1, 32)});
if (result) {
range_right = make_node(AST::AST_ADD)({result->children[1]->clone(), range_right});
delete result;
result = nullptr;
}
range_left = make_node(AST::AST_ADD)({range_right->clone(), range_left});
result = make_node(AST::AST_RANGE)({range_left, range_right});
}
id->basic_prep = true;
return result;
}
static void resolve_wiretype(AST::AstNode *wire_node)
{
AST::AstNode *wiretype_node = nullptr;
if (!wire_node->children.empty()) {
if (wire_node->children[0]->type == AST::AST_WIRETYPE) {
wiretype_node = wire_node->children[0];
}
}
if (wire_node->children.size() > 1) {
if (wire_node->children[1]->type == AST::AST_WIRETYPE) {
wiretype_node = wire_node->children[1];
}
}
if (wiretype_node == nullptr)
return;
unique_resource<std::vector<AST::AstNode *>> packed_ranges = wire_node->attributes.count(attr_id::packed_ranges)
? std::move(wire_node->attributes[attr_id::packed_ranges]->children)
: std::vector<AST::AstNode *>{};
delete_attribute(wire_node, attr_id::packed_ranges);
unique_resource<std::vector<AST::AstNode *>> unpacked_ranges = wire_node->attributes.count(attr_id::unpacked_ranges)
? std::move(wire_node->attributes[attr_id::unpacked_ranges]->children)
: std::vector<AST::AstNode *>{};
delete_attribute(wire_node, attr_id::unpacked_ranges);
AST::AstNode *wiretype_ast = nullptr;
log_assert(AST_INTERNAL::current_scope.count(wiretype_node->str));
wiretype_ast = AST_INTERNAL::current_scope[wiretype_node->str];
// we need to setup current top ast as this simplify
// needs to have access to all already defined ids
simplify_sv(wiretype_ast, nullptr);
while (simplify(wire_node, true, false, false, 1, -1, false, false)) {
}
log_assert(!wiretype_ast->children.empty());
if ((wiretype_ast->children[0]->type == AST::AST_STRUCT || wiretype_ast->children[0]->type == AST::AST_UNION) &&
wire_node->type == AST::AST_WIRE) {
auto struct_width = get_max_offset_struct(wiretype_ast->children[0]);
wire_node->range_left = struct_width;
wire_node->children[0]->range_left = struct_width;
wire_node->children[0]->children[0]->integer = struct_width;
}
if (wiretype_ast) {
log_assert(wire_node->attributes.count(ID::wiretype));
log_assert(wiretype_ast->type == AST::AST_TYPEDEF);
wire_node->attributes[ID::wiretype]->id2ast = wiretype_ast->children[0];
}
if (((wire_node->children.size() > 0 && wire_node->children[0]->type == AST::AST_RANGE) ||
(wire_node->children.size() > 1 && wire_node->children[1]->type == AST::AST_RANGE)) &&
wire_node->multirange_dimensions.empty()) {
// We need to save order in which ranges appear in wiretype and add them before wire range
// We need to copy this ranges, so create new vector for them
std::vector<AST::AstNode *> packed_ranges_wiretype;
std::vector<AST::AstNode *> unpacked_ranges_wiretype;
if (wiretype_ast && !wiretype_ast->children.empty() && wiretype_ast->children[0]->attributes.count(UhdmAst::packed_ranges()) &&
wiretype_ast->children[0]->attributes.count(UhdmAst::unpacked_ranges())) {
for (auto r : wiretype_ast->children[0]->attributes[UhdmAst::packed_ranges()]->children) {
packed_ranges_wiretype.push_back(r->clone());
}
for (auto r : wiretype_ast->children[0]->attributes[UhdmAst::unpacked_ranges()]->children) {
unpacked_ranges_wiretype.push_back(r->clone());
}
} else {
if (wire_node->children[0]->type == AST::AST_RANGE)
packed_ranges_wiretype.push_back(wire_node->children[0]->clone());
else if (wire_node->children[1]->type == AST::AST_RANGE)
packed_ranges_wiretype.push_back(wire_node->children[1]->clone());
else
log_error("Unhandled case in resolve_wiretype!\n");
}
// add wiretype range before current wire ranges
std::reverse(packed_ranges_wiretype.begin(), packed_ranges_wiretype.end());
std::reverse(unpacked_ranges_wiretype.begin(), unpacked_ranges_wiretype.end());
std::reverse(packed_ranges->begin(), packed_ranges->end());
std::reverse(unpacked_ranges->begin(), unpacked_ranges->end());
packed_ranges->insert(packed_ranges->begin(), packed_ranges_wiretype.begin(), packed_ranges_wiretype.end());
unpacked_ranges->insert(unpacked_ranges->begin(), unpacked_ranges_wiretype.begin(), unpacked_ranges_wiretype.end());
AST::AstNode *value = nullptr;
if (wire_node->children[0]->type != AST::AST_RANGE) {
value = wire_node->children[0]->clone();
}
delete_children(wire_node);
if (value)
wire_node->children.push_back(value);
add_multirange_wire(wire_node, packed_ranges.release(), unpacked_ranges.release());
}
}
static void add_force_convert_attribute(AST::AstNode *wire_node, uint32_t val = 1)
{
AST::AstNode *&attr = wire_node->attributes[UhdmAst::force_convert()];
if (!attr) {
attr = AST::AstNode::mkconst_int(val, true);
} else if (attr->integer != val) {
attr->integer = val;
}
}
static void check_memories(AST::AstNode *node, std::string scope, std::map<std::string, AST::AstNode *> &memories)
{
for (auto *child : node->children) {
if (child->type == AST::AST_FUNCTION) {
std::map<std::string, AST::AstNode *> memories_in_scope;
check_memories(child, node->type == AST::AST_GENBLOCK ? scope + "." + node->str : scope, memories_in_scope);
} else {
check_memories(child, node->type == AST::AST_GENBLOCK ? scope + "." + node->str : scope, memories);
}
}
if (node->str == "\\$readmemh") {
if (node->children.size() != 2 || node->children[1]->str.empty() || node->children[1]->type != AST::AST_IDENTIFIER) {
log_error("%s:%d: Wrong usage of '\\$readmemh'\n", node->filename.c_str(), node->location.first_line);
}
// TODO: Look for the memory in all other scope levels, like we do in case of AST::AST_IDENTIFIER,
// as here the memory can also be defined before before the current scope.
std::string name = scope + "." + node->children[1]->str;
const auto iter = memories.find(name);
if (iter != memories.end()) {
add_force_convert_attribute(iter->second, 0);
}
}
if (node->type == AST::AST_WIRE) {
const std::size_t packed_ranges_count =
node->attributes.count(UhdmAst::packed_ranges()) ? node->attributes[UhdmAst::packed_ranges()]->children.size() : 0;
const std::size_t unpacked_ranges_count =
node->attributes.count(UhdmAst::unpacked_ranges()) ? node->attributes[UhdmAst::unpacked_ranges()]->children.size() : 0;
if (packed_ranges_count == 1 && unpacked_ranges_count == 1) {
std::string name = scope + "." + node->str;
auto [iter, did_insert] = memories.insert_or_assign(std::move(name), node);
log_assert(did_insert);
}
return;
}
if (node->type == AST::AST_IDENTIFIER) {
std::string full_id = scope;
std::size_t scope_end_pos = scope.size();
for (;;) {
full_id += "." + node->str;
const auto iter = memories.find(full_id);
if (iter != memories.end()) {
// Memory node found!
if (!iter->second->attributes.count(UhdmAst::force_convert())) {
const bool is_full_memory_access = (node->children.size() == 0);
const bool is_slice_memory_access = (node->children.size() == 1 && node->children[0]->children.size() != 1);
// convert memory to list of registers
// in case of access to whole memory
// or slice of memory
// e.g.
// logic [3:0] mem [8:0];
// always_ff @ (posedge clk) begin
// mem <= '{default:0};
// mem[7:1] <= mem[6:0];
// end
// don't convert in case of accessing
// memory using address, e.g.
// mem[0] <= '{default:0}
if (is_full_memory_access || is_slice_memory_access) {
add_force_convert_attribute(iter->second);
}
}
break;
} else {
if (scope_end_pos == 0) {
// We reached the top scope and the memory node wasn't found.
break;
} else {
// Memory node wasn't found.
// Erase node name and last segment of the scope to check the previous scope.
// FIXME: This doesn't work with escaped identifiers containing a dot.
scope_end_pos = full_id.find_last_of('.', scope_end_pos - 1);
if (scope_end_pos == std::string::npos) {
scope_end_pos = 0;
}
full_id.erase(scope_end_pos);
}
}
}
}
}
static void check_memories(AST::AstNode *node)
{
std::map<std::string, AST::AstNode *> memories;
check_memories(node, "", memories);
}
static void warn_start_range(const std::vector<AST::AstNode *> ranges)
{
for (size_t i = 0; i < ranges.size(); i++) {
auto start_elem = min(ranges[i]->children[0]->integer, ranges[i]->children[1]->integer);
if (start_elem != 0) {
log_file_warning(ranges[i]->filename, ranges[i]->location.first_line, "Limited support for multirange wires that don't start from 0\n");
}
}
}
// This function is workaround missing support for multirange (with n-ranges) packed/unpacked nodes
// It converts multirange node to single-range node and translates access to this node
// to correct range
static void convert_packed_unpacked_range(AST::AstNode *wire_node)
{
resolve_wiretype(wire_node);
const std::vector<AST::AstNode *> packed_ranges = wire_node->attributes.count(UhdmAst::packed_ranges())
? wire_node->attributes[UhdmAst::packed_ranges()]->children
: std::vector<AST::AstNode *>();
const std::vector<AST::AstNode *> unpacked_ranges = wire_node->attributes.count(UhdmAst::unpacked_ranges())
? wire_node->attributes[UhdmAst::unpacked_ranges()]->children
: std::vector<AST::AstNode *>();
if (packed_ranges.empty() && unpacked_ranges.empty()) {
delete_attribute(wire_node, UhdmAst::packed_ranges());
delete_attribute(wire_node, UhdmAst::unpacked_ranges());
wire_node->range_left = 0;
wire_node->range_right = 0;
wire_node->range_valid = true;
return;
}
std::vector<AST::AstNode *> ranges;
// Convert only when node is not a memory and at least 1 of the ranges has more than 1 range
const bool convert_node = [&]() {
if (wire_node->type == AST::AST_MEMORY)
return false;
if (packed_ranges.size() > 1)
return true;
if (unpacked_ranges.size() > 1)
return true;
if (wire_node->attributes.count(ID::wiretype))
return true;
if (wire_node->type == AST::AST_PARAMETER)
return true;
if (wire_node->type == AST::AST_LOCALPARAM)
return true;
if ((wire_node->is_input || wire_node->is_output) && (packed_ranges.size() > 0 || unpacked_ranges.size() > 0))
return true;
if (wire_node->attributes.count(UhdmAst::force_convert()) && wire_node->attributes[UhdmAst::force_convert()]->integer == 1)
return true;
return false;
}();
if (convert_node) {
// if not already converted
if (wire_node->multirange_dimensions.empty()) {
const auto [packed_size, unpacked_size] = set_multirange_dimensions(wire_node, packed_ranges, unpacked_ranges);
if (packed_ranges.size() == 1 && unpacked_ranges.empty()) {
ranges.push_back(packed_ranges[0]->clone());
} else if (unpacked_ranges.size() == 1 && packed_ranges.empty()) {
ranges.push_back(unpacked_ranges[0]->clone());
} else {
// currently we have limited support
// for multirange wires that doesn't start from 0
warn_start_range(packed_ranges);
warn_start_range(unpacked_ranges);
const size_t size = packed_size * unpacked_size;
log_assert(size >= 1);
ranges.push_back(make_range(size - 1, 0));
}
}
} else {
for (auto r : packed_ranges) {
ranges.push_back(r->clone());
}
for (auto r : unpacked_ranges) {
ranges.push_back(r->clone());
}
// if there is only one packed and one unpacked range,
// and wire is not port wire, change type to AST_MEMORY
if (wire_node->type == AST::AST_WIRE && packed_ranges.size() == 1 && unpacked_ranges.size() == 1 && !wire_node->is_input &&
!wire_node->is_output) {
wire_node->type = AST::AST_MEMORY;
wire_node->is_logic = true;
}
}
// Insert new range
wire_node->children.insert(wire_node->children.end(), ranges.begin(), ranges.end());
}
// Assert macro that prints location in C++ code and location of currently processed UHDM object.
// Use only inside UhdmAst methods.
#ifndef NDEBUG
#if __GNUC__
// gcc/clang's __builtin_trap() makes gdb stop on the line containing an assertion.
#define uhdmast_assert(expr) \
if ((expr)) { \
} else { \
this->uhdmast_assert_log(#expr, __PRETTY_FUNCTION__, __FILE__, __LINE__); \
__builtin_trap(); \
}
#else // #if __GNUC__
// Just abort when using compiler other than gcc/clang.
#define uhdmast_assert(expr) \
if ((expr)) { \
} else { \
this->uhdmast_assert_log(#expr, __func__, __FILE__, __LINE__); \
std::abort(); \
}
#endif // #if __GNUC__
#else // #ifndef NDEBUG
#define uhdmast_assert(expr) \
if ((expr)) { \
} else { \
}
#endif // #ifndef NDEBUG
void UhdmAst::uhdmast_assert_log(const char *expr_str, const char *func, const char *file, int line) const
{
std::cerr << file << ':' << line << ": error: Assertion failed: " << expr_str << std::endl;
std::cerr << file << ':' << line << ": note: In function: " << func << std::endl;
if (obj_h != 0) {
const char *const svfile = vpi_get_str(vpiFile, obj_h);
int svline = vpi_get(vpiLineNo, obj_h);
int svcolumn = vpi_get(vpiColumnNo, obj_h);
std::string obj_type_name = UHDM::VpiTypeName(obj_h);
const char *obj_name = vpi_get_str(vpiName, obj_h);
std::cerr << svfile << ':' << svline << ':' << svcolumn << ": note: When processing object of type '" << obj_type_name << '\'';
if (obj_name && obj_name[0] != '\0') {
std::cerr << " named '" << obj_name << '\'';
}
std::cerr << '.' << std::endl;
}
}
static AST::AstNode *expand_dot(const AST::AstNode *current_struct, const AST::AstNode *search_node)
{
AST::AstNode *current_struct_elem = nullptr;
auto search_str = search_node->str.find("\\") == 0 ? search_node->str.substr(1) : search_node->str;
auto struct_elem_it =
std::find_if(current_struct->children.begin(), current_struct->children.end(), [&](AST::AstNode *node) { return node->str == search_str; });
if (struct_elem_it == current_struct->children.end()) {
current_struct->dumpAst(NULL, "struct >");
log_error("Couldn't find search elem: %s in struct\n", search_str.c_str());
}
current_struct_elem = *struct_elem_it;
AST::AstNode *sub_dot = nullptr;
std::vector<AST::AstNode *> struct_ranges;
for (auto c : search_node->children) {
if (c->type == static_cast<int>(AST::Extended::AST_DOT)) {
// There should be only 1 AST_DOT node children
log_assert(!sub_dot);
sub_dot = expand_dot(current_struct_elem, c);
}
if (c->type == AST::AST_RANGE) {
struct_ranges.push_back(c);
}
}
AST::AstNode *left = nullptr, *right = nullptr;
switch (current_struct_elem->type) {
case AST::AST_STRUCT_ITEM:
left = AST::AstNode::mkconst_int(current_struct_elem->range_left, true);
right = AST::AstNode::mkconst_int(current_struct_elem->range_right, true);
break;
case AST::AST_STRUCT:
case AST::AST_UNION:
// TODO(krak): add proper support for accessing struct/union elements
// with multirange
// Currently support only special access to 2 dimensional packed element
// when selecting single range
log_assert(current_struct_elem->multirange_dimensions.size() % 2 == 0);
if (!struct_ranges.empty() && (current_struct_elem->multirange_dimensions.size() / 2) == 2) {
// get element size in number of bits
const int single_elem_size = current_struct_elem->children.front()->range_left + 1;
left = AST::AstNode::mkconst_int(single_elem_size * current_struct_elem->multirange_dimensions.back(), true);
right =
AST::AstNode::mkconst_int(current_struct_elem->children.back()->range_right * current_struct_elem->multirange_dimensions.back(), true);
} else {
left = AST::AstNode::mkconst_int(current_struct_elem->children.front()->range_left, true);
right = AST::AstNode::mkconst_int(current_struct_elem->children.back()->range_right, true);
}
break;
default:
// Structs currently can only have AST_STRUCT, AST_STRUCT_ITEM, or AST_UNION.
log_file_error(current_struct_elem->filename, current_struct_elem->location.first_line,
"Accessing struct member of type %s is unsupported.\n", type2str(current_struct_elem->type).c_str());
};
auto elem_size =
new AST::AstNode(AST::AST_ADD, new AST::AstNode(AST::AST_SUB, left->clone(), right->clone()), AST::AstNode::mkconst_int(1, true));
if (sub_dot) {
// First select correct element in first struct
std::swap(left, sub_dot->children[0]);
std::swap(right, sub_dot->children[1]);
delete sub_dot;
}
for (size_t i = 0; i < struct_ranges.size(); i++) {
const auto *struct_range = struct_ranges[i];
auto const idx = (struct_ranges.size() - i) * 2 - 1;
auto const range_width_idx = idx;
auto const range_offset_idx = idx - 1;
int range_width = 0;
if (current_struct_elem->multirange_dimensions.empty()) {
range_width = 1;
} else if (current_struct_elem->multirange_dimensions.size() > range_width_idx) {
range_width = current_struct_elem->multirange_dimensions[range_width_idx];
const auto range_offset = current_struct_elem->multirange_dimensions[range_offset_idx];
if (range_offset != 0) {
log_file_error(struct_range->filename, struct_range->location.first_line,
"Accessing ranges that do not start from 0 is not supported.");
}
} else {
struct_range->dumpAst(NULL, "range >");
log_file_error(struct_range->filename, struct_range->location.first_line, "Couldn't find range width.");
}
// now we have correct element set,
// but we still need to set correct struct
log_assert(!struct_range->children.empty());
if (current_struct_elem->type == AST::AST_STRUCT_ITEM) {
// if we selecting range of struct item, just add this range
// to our current select
if (current_struct_elem->multirange_dimensions.size() > 2 && struct_range->children.size() == 2) {
if (i < (struct_ranges.size() - 1))
log_error(
"Selecting a range of positions from a multirange is not supported in the dot notation, unless it is the last index.\n");
}
if (struct_range->children.size() == 2) {
auto range_size = new AST::AstNode(
AST::AST_ADD, new AST::AstNode(AST::AST_SUB, struct_range->children[0]->clone(), struct_range->children[1]->clone()),
AST::AstNode::mkconst_int(1, true));
right = new AST::AstNode(AST::AST_ADD, right, struct_range->children[1]->clone());
delete left;
left = new AST::AstNode(AST::AST_ADD, right->clone(), new AST::AstNode(AST::AST_SUB, range_size, AST::AstNode::mkconst_int(1, true)));
} else if (struct_range->children.size() == 1) {
// Selected a single position, as in `foo.bar[i]`.
if (range_width > 1 && range_width_idx > 1) {
// if it's not the last dimension.
right = new AST::AstNode(
AST::AST_ADD, right,
new AST::AstNode(AST::AST_MUL, struct_range->children[0]->clone(), AST::AstNode::mkconst_int(range_width, true)));
delete left;
left = new AST::AstNode(AST::AST_ADD, right->clone(), AST::AstNode::mkconst_int(range_width - 1, true));
} else {
right = new AST::AstNode(AST::AST_ADD, right, struct_range->children[0]->clone());
delete left;
left = right->clone();
}
} else {
struct_range->dumpAst(NULL, "range >");
log_error("Unhandled range select (AST_STRUCT_ITEM) in AST_DOT!\n");
}
} else if (current_struct_elem->type == AST::AST_STRUCT) {
if (struct_range->children.size() == 2) {
right = new AST::AstNode(AST::AST_ADD, right, struct_range->children[1]->clone());
auto range_size = new AST::AstNode(
AST::AST_ADD, new AST::AstNode(AST::AST_SUB, struct_range->children[0]->clone(), struct_range->children[1]->clone()),
AST::AstNode::mkconst_int(1, true));
left = new AST::AstNode(AST::AST_ADD, left, new AST::AstNode(AST::AST_SUB, range_size, elem_size->clone()));
} else if (struct_range->children.size() == 1) {
AST::AstNode *mul = new AST::AstNode(AST::AST_MUL, elem_size->clone(), struct_range->children[0]->clone());
left = new AST::AstNode(AST::AST_ADD, left, mul);
right = new AST::AstNode(AST::AST_ADD, right, mul->clone());
} else {
struct_range->dumpAst(NULL, "range >");
log_error("Unhandled range select (AST_STRUCT) in AST_DOT!\n");
}
} else {
log_file_error(current_struct_elem->filename, current_struct_elem->location.first_line,
"Accessing member of a slice of type %s is unsupported.\n", type2str(current_struct_elem->type).c_str());
}
}
delete elem_size;
// Return range from the begining of *current* struct
// When all AST_DOT are expanded it will return range
// from original wire
return new AST::AstNode(AST::AST_RANGE, left, right);
}
static AST::AstNode *convert_dot(AST::AstNode *wire_node, AST::AstNode *node, AST::AstNode *dot)
{
AST::AstNode *struct_node = nullptr;
if (wire_node->type == AST::AST_STRUCT || wire_node->type == AST::AST_UNION) {
struct_node = wire_node;
} else if (wire_node->attributes.count(ID::wiretype)) {
log_assert(wire_node->attributes[ID::wiretype]->id2ast);
struct_node = wire_node->attributes[ID::wiretype]->id2ast;