forked from verilator/verilator
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUhdmAst.cpp
3508 lines (3280 loc) · 142 KB
/
UhdmAst.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
#include <vector>
#include <stack>
#include <functional>
#include <algorithm>
#include <regex>
#include <uhdm/uhdm.h>
#include "V3Ast.h"
#include "V3ParseSym.h"
#include "V3Global.h"
#include "UhdmAst.h"
namespace UhdmAst {
// Walks through one-to-many relationships from given parent
// node through the VPI interface, visiting child nodes belonging to
// ChildrenNodeTypes that are present in the given object.
void visit_one_to_many(const std::vector<int> childrenNodeTypes, vpiHandle parentHandle,
UhdmShared& shared, const std::function<void(AstNode*)>& f) {
for (auto child : childrenNodeTypes) {
vpiHandle itr = vpi_iterate(child, parentHandle);
while (vpiHandle vpi_child_obj = vpi_scan(itr)) {
auto* childNode = visit_object(vpi_child_obj, shared);
f(childNode);
vpi_release_handle(vpi_child_obj);
}
vpi_release_handle(itr);
}
}
// Walks through one-to-one relationships from given parent
// node through the VPI interface, visiting child nodes belonging to
// ChildrenNodeTypes that are present in the given object.
void visit_one_to_one(const std::vector<int> childrenNodeTypes, vpiHandle parentHandle,
UhdmShared& shared, const std::function<void(AstNode*)>& f) {
for (auto child : childrenNodeTypes) {
vpiHandle itr = vpi_handle(child, parentHandle);
if (itr) {
auto* childNode = visit_object(itr, shared);
f(childNode);
}
vpi_release_handle(itr);
}
}
std::string encodeObjectName(std::string name) {
// Verilator uses encodeName function on identifiers
// System functions names are not handled as identifiers
if (name[0] == '$') return name;
std::string encodedName = "";
std::regex r = std::regex("\\.|::");
std::smatch matchResult;
while (std::regex_search(name, matchResult, r)) {
std::string nameComponent = matchResult.prefix().str();
encodedName += AstNode::encodeName(nameComponent);
encodedName += matchResult.str();
name = matchResult.suffix().str();
}
encodedName += AstNode::encodeName(name);
return encodedName;
}
void sanitize_str(std::string& s) {
if (!s.empty()) {
auto pos = s.rfind("@");
s = s.substr(pos + 1);
s = encodeObjectName(s);
// Replace [ and ], seen in GenScope names
s = std::regex_replace(s, std::regex("\\["), "__BRA__");
s = std::regex_replace(s, std::regex("\\]"), "__KET__");
}
}
std::string remove_last_sanitized_index(const std::string& s) {
if (s.size() > 7 && s.substr(s.size() - 7) == "__KET__") {
auto pos = s.rfind("__BRA__");
return s.substr(0, pos);
}
return s;
}
FileLine* make_fileline(vpiHandle obj_h) {
std::string filename = "uhdm";
if (auto* s = vpi_get_str(vpiFile, obj_h)) { filename = s; }
const int lineNo = vpi_get(vpiLineNo, obj_h);
const int endLineNo = vpi_get(vpiEndLineNo, obj_h);
const int columnNo = vpi_get(vpiColumnNo, obj_h);
const int endColumnNo = vpi_get(vpiEndColumnNo, obj_h);
auto* fl = new FileLine(filename);
fl->firstLineno(lineNo);
fl->lastLineno(endLineNo);
fl->firstColumn(columnNo);
fl->lastColumn(endColumnNo);
return fl;
}
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_str(objectName);
break;
}
}
return objectName;
}
bool is_imported(vpiHandle obj_h) {
if (auto s = vpi_get_str(vpiImported, obj_h)) {
return true;
} else {
return false;
}
}
void remove_scope(std::string& s) {
auto pos = s.rfind("::");
if (pos != std::string::npos) s = s.substr(pos + 2);
}
AstParseRef* getVarRefIfAlreadyDeclared(vpiHandle obj_h, UhdmShared& shared) {
std::string objectName = get_object_name(obj_h);
const uhdm_handle* const handle = (const uhdm_handle*)obj_h;
const UHDM::BaseClass* const object = (const UHDM::BaseClass*)handle->object;
if (shared.visited_objects.find(object) != shared.visited_objects.end()) {
// Already seen this, create reference
return new AstParseRef(make_fileline(obj_h), VParseRefExp::en::PX_TEXT, objectName,
nullptr, nullptr);
}
shared.visited_objects.insert(object);
return nullptr;
}
AstPackage* get_package(UhdmShared& shared, const std::string& objectName) {
std::size_t delimiter_pos = objectName.rfind("::");
std::size_t prefix_pos = objectName.find("::");
AstPackage* classpackagep = nullptr;
if (delimiter_pos != std::string::npos) {
std::string classpackageName = "";
if (prefix_pos < delimiter_pos) {
// "Nested" packages - package importing package
// Last one is where definition is located
classpackageName = objectName.substr(prefix_pos + 2, delimiter_pos - prefix_pos - 2);
} else {
// Simple package reference
classpackageName = objectName.substr(0, delimiter_pos);
}
// Nested or not, func is named after last package
auto object_name = objectName.substr(delimiter_pos + 2, objectName.length());
UINFO(7, "Found package prefix: " << classpackageName << std::endl);
remove_scope(classpackageName);
auto it = shared.package_map.find(classpackageName);
if (it != shared.package_map.end()) { classpackagep = it->second; }
}
return classpackagep;
}
bool is_expr_context(vpiHandle obj_h) {
auto parent_h = vpi_handle(vpiParent, obj_h);
if (parent_h) {
auto parent_type = vpi_get(vpiType, parent_h);
vpi_release_handle(parent_h);
switch (parent_type) {
case vpiOperation:
case vpiIf:
case vpiIfElse:
case vpiAssignStmt:
case vpiAssignment:
case vpiContAssign:
case vpiFuncCall:
case vpiTaskCall:
case vpiSysFuncCall:
case vpiReturn: {
return true;
}
case vpiBegin:
case vpiCaseItem: {
return false;
}
default: {
make_fileline(obj_h)->v3error("Encountered unhandled parent type: "
<< UHDM::VpiTypeName(parent_h)
<< " in " << __FUNCTION__ << std::endl);
}
}
} else {
UINFO(3, "Missing parent handle in " << __FUNCTION__ << std::endl);
// TODO: it seems that this happens only in expr context?
return true;
}
}
string deQuote(FileLine* fileline, string text) {
// Fix up the quoted strings the user put in, for example "\"" becomes "
// Reverse is V3OutFormatter::quoteNameControls(...)
bool quoted = false;
string newtext;
unsigned char octal_val = 0;
int octal_digits = 0;
for (string::const_iterator cp = text.begin(); cp != text.end(); ++cp) {
if (quoted) {
if (isdigit(*cp)) {
octal_val = octal_val * 8 + (*cp - '0');
if (++octal_digits == 3) {
octal_digits = 0;
quoted = false;
newtext += octal_val;
}
} else {
if (octal_digits) {
// Spec allows 1-3 digits
octal_digits = 0;
quoted = false;
newtext += octal_val;
--cp; // Backup to reprocess terminating character as non-escaped
continue;
}
quoted = false;
if (*cp == 'n')
newtext += '\n';
else if (*cp == 'a')
newtext += '\a'; // SystemVerilog 3.1
else if (*cp == 'f')
newtext += '\f'; // SystemVerilog 3.1
else if (*cp == 'r')
newtext += '\r';
else if (*cp == 't')
newtext += '\t';
else if (*cp == 'v')
newtext += '\v'; // SystemVerilog 3.1
else if (*cp == 'x' && isxdigit(cp[1]) && isxdigit(cp[2])) { // SystemVerilog 3.1
#define vl_decodexdigit(c) ((isdigit(c) ? ((c) - '0') : (tolower((c)) - 'a' + 10)))
newtext += (char)(16 * vl_decodexdigit(cp[1]) + vl_decodexdigit(cp[2]));
cp += 2;
} else if (isalnum(*cp)) {
fileline->v3error("Unknown escape sequence: \\" << *cp);
break;
} else
newtext += *cp;
}
} else if (*cp == '\\') {
quoted = true;
octal_digits = 0;
} else if (*cp != '"') {
newtext += *cp;
}
}
return newtext;
}
AstNodeDType* applyPackedRanges(FileLine* fl, vpiHandle obj_h, AstNodeDType* dtypep,
UhdmShared& shared) {
std::stack<AstRange*> range_stack;
visit_one_to_many({vpiRange}, obj_h, shared, [&](AstNode* nodep) {
AstRange* rangeNodep = reinterpret_cast<AstRange*>(nodep);
range_stack.push(rangeNodep);
});
AstBasicDType* basicp = VN_CAST(dtypep, BasicDType);
if (basicp && !basicp->rangep() && !range_stack.empty()) {
basicp->rangep(range_stack.top());
range_stack.pop();
dtypep = basicp;
}
while (!range_stack.empty()) {
dtypep = new AstPackArrayDType(fl, VFlagChildDType(), dtypep, range_stack.top());
range_stack.pop();
}
return dtypep;
}
AstNodeDType* applyUnpackedRanges(FileLine* fl, vpiHandle obj_h, AstNodeDType* dtypep,
UhdmShared& shared) {
std::stack<AstRange*> range_stack;
visit_one_to_many({vpiRange}, obj_h, shared, [&](AstNode* nodep) {
AstRange* rangeNodep = reinterpret_cast<AstRange*>(nodep);
range_stack.push(rangeNodep);
});
while (!range_stack.empty()) {
dtypep = new AstUnpackArrayDType(fl, VFlagChildDType(), dtypep, range_stack.top());
range_stack.pop();
}
return dtypep;
}
AstSelBit* applyBitSelect(vpiHandle obj_h, AstNode* fromp, UhdmShared& shared) {
AstNode* bitp = nullptr;
visit_one_to_one({vpiIndex}, obj_h, shared, [&](AstNode* itemp) {
if (itemp) { bitp = itemp; }
});
return new AstSelBit(make_fileline(obj_h), fromp, bitp);
}
AstSelExtract* applyPartSelect(vpiHandle obj_h, AstNode* fromp, UhdmShared& shared) {
AstNode* leftp = nullptr;
visit_one_to_one({vpiLeftRange}, obj_h, shared, [&](AstNode* itemp) { leftp = itemp; });
AstNode* rightp = nullptr;
visit_one_to_one({vpiRightRange}, obj_h, shared, [&](AstNode* itemp) { rightp = itemp; });
return new AstSelExtract(make_fileline(obj_h), fromp, leftp, rightp);
}
AstNode* applyIndexedPartSelect(vpiHandle obj_h, AstNode* fromp, UhdmShared& shared) {
AstNode* basep = nullptr;
visit_one_to_one({vpiBaseExpr}, obj_h, shared, [&](AstNode* itemp) { basep = itemp; });
AstNode* widthp = nullptr;
visit_one_to_one({vpiWidthExpr}, obj_h, shared, [&](AstNode* itemp) { widthp = itemp; });
FileLine* fl = make_fileline(obj_h);
auto type = vpi_get(vpiIndexedPartSelectType, obj_h);
if (type == vpiPosIndexed) {
return new AstSelPlus(fl, fromp, basep, widthp);
} else if (type == vpiNegIndexed) {
return new AstSelMinus(fl, fromp, basep, widthp);
} else {
fl->v3error("Unknown value of vpiIndexedPartSelectType: " << type << std::endl);
return nullptr;
}
}
AstNode* get_class_package_ref_node(FileLine* fl, std::string objectName, UhdmShared& shared) {
AstNode* refp = nullptr;
size_t colon_pos = objectName.find("::");
while (colon_pos != std::string::npos) {
std::string classPkgName = objectName.substr(0, colon_pos);
objectName = objectName.substr(colon_pos + 2, objectName.length());
UINFO(7, "Creating ClassOrPackageRef" << std::endl);
AstPackage* classpackagep = nullptr;
auto it = shared.package_map.find(classPkgName);
if (it != shared.package_map.end()) { classpackagep = it->second; }
AstNode* classpackageref
= new AstClassOrPackageRef(fl, classPkgName, classpackagep, nullptr);
shared.m_symp->nextId(classpackagep);
if (refp == nullptr)
refp = classpackageref;
else
refp = new AstDot(fl, true, refp, classpackageref);
colon_pos = objectName.find("::");
}
return refp;
}
AstNode* get_referenceNode(FileLine* fl, string name, UhdmShared& shared) {
AstNode* colon_refp = get_class_package_ref_node(fl, name, shared);
size_t colon_pos = name.rfind("::");
if (colon_pos != std::string::npos) name = name.substr(colon_pos + 2);
AstNode* dot_refp = nullptr;
size_t dot_pos = name.find('.');
if (dot_pos != std::string::npos) {
std::string lhs = name.substr(0, dot_pos);
std::string rhs = name.substr(dot_pos + 1, name.length());
AstParseRef* lhsNode
= new AstParseRef(fl, VParseRefExp::en::PX_TEXT, lhs, nullptr, nullptr);
AstNode* rhsNode = get_referenceNode(fl, rhs, shared);
dot_refp = new AstDot(fl, false, lhsNode, rhsNode);
} else {
dot_refp = new AstParseRef(fl, VParseRefExp::en::PX_TEXT, name, nullptr, nullptr);
}
return AstDot::newIfPkg(fl, colon_refp, dot_refp);
}
AstNode* get_value_by_format(vpiHandle obj_h) {
std::string valStr;
int size = -1; // Treated as "invalid/unavailable" in UHDM
size = vpi_get(vpiSize, obj_h);
s_vpi_value val;
vpi_get_value(obj_h, &val);
switch (val.format) {
case vpiIntVal:
case vpiScalarVal:
case vpiUIntVal: {
if (val.format == vpiIntVal)
valStr = std::to_string(val.value.integer);
else if (val.format == vpiScalarVal)
valStr = std::to_string(val.value.scalar);
else if (val.format == vpiUIntVal)
valStr = std::to_string(val.value.uint);
if (valStr[0] == '-') {
valStr = valStr.substr(1);
auto* inner = new AstConst(make_fileline(obj_h), AstConst::StringToParse(), valStr.c_str());
return new AstNegate(make_fileline(obj_h), inner);
}
if (size != -1 && size != 0) {
valStr = std::to_string(size) + "'d" + valStr;
} else {
valStr = "'d" + valStr;
}
auto* constp = new AstConst(make_fileline(obj_h), AstConst::StringToParse(), valStr.c_str());
auto& num = constp->num();
if (num.width() >= 32 && num.widthMin() <= 32) {
UINFO(8, "Creating wide constant" << std::endl);
num.width(32, false);
}
if (val.format == vpiUIntVal)
num.isSigned(false);
else
num.isSigned(true);
constp = new AstConst(make_fileline(obj_h), num);
return constp;
}
case vpiRealVal: {
valStr = std::to_string(val.value.real);
bool parseSuccess;
double value = VString::parseDouble(valStr, &parseSuccess);
UASSERT(parseSuccess, "Unable to parse real value: " + valStr);
return new AstConst(make_fileline(obj_h), AstConst::RealDouble(), value);
}
case vpiBinStrVal:
case vpiOctStrVal:
case vpiDecStrVal:
case vpiHexStrVal: {
if (val.format == vpiBinStrVal)
valStr = "'b" + std::string(val.value.str);
else if (val.format == vpiOctStrVal)
valStr = "'o" + std::string(val.value.str);
else if (val.format == vpiDecStrVal)
valStr = "'d" + std::string(val.value.str);
else if (val.format == vpiHexStrVal)
valStr = "'h" + std::string(val.value.str);
if (size != -1 && size != 0)
valStr = std::to_string(size) + valStr;
UINFO(7, "Obtained string is " << valStr << " for type " << val.format << std::endl);
return new AstConst(make_fileline(obj_h), AstConst::StringToParse(), valStr.c_str());
}
case vpiStringVal: {
if (auto* s = val.value.str) valStr = std::to_string(*s);
valStr.assign(val.value.str);
return new AstConst(make_fileline(obj_h), AstConst::VerilogStringLiteral(),
deQuote(make_fileline(obj_h), valStr));
}
case 0: {
UINFO(7, "No value; value format is 0" << std::endl);
break;
}
default: {
v3error("Encountered unknown value format " << val.format << std::endl);
break;
}
}
return nullptr;
}
AstNode* get_decompiled_value(vpiHandle obj_h) {
std::string valStr;
int size = vpi_get(vpiSize, obj_h);
if (auto s = vpi_get_str(vpiDecompile, obj_h)) {
valStr = s;
auto type = vpi_get(vpiConstType, obj_h);
if (type == vpiStringConst) {
UINFO(8, "Decompiled vpiStringConst: " << valStr << std::endl);
return new AstConst(make_fileline(obj_h), AstConst::VerilogStringLiteral(),
deQuote(make_fileline(obj_h), valStr));
} else if (type == vpiRealConst) {
bool parseSuccess;
double value = VString::parseDouble(valStr, &parseSuccess);
UASSERT(parseSuccess, "Unable to parse real value: " + valStr);
UINFO(8, "Decompiled vpiRealConst: " << valStr << std::endl);
return new AstConst(make_fileline(obj_h), AstConst::RealDouble(), value);
} else {
valStr = s;
if (valStr.find('\'') == std::string::npos) {
if (size == -1) {
if (valStr == "0")
valStr = "'0";
else if (valStr == "1" || valStr == "18446744073709551615")
// Surelog's default constant size is 64
// 18446744073709551615 is 2^64 - 1
valStr = "'1";
else if (valStr == "x" || valStr == "X")
valStr = "'X";
else if (valStr == "z" || valStr == "Z")
valStr = "'Z";
else {
UINFO(7, "Unexpected value with vpiSize -1: " << valStr << std::endl);
return nullptr;
}
UINFO(8, "Decompiled special value: " << valStr << std::endl);
return new AstConst(make_fileline(obj_h), AstConst::StringToParse(), valStr.c_str());
} else {
if (type == vpiBinaryConst) valStr = "'b" + valStr;
else if (type == vpiOctConst) valStr = "'o" + valStr;
else if (type == vpiHexConst) valStr = "'h" + valStr;
else valStr = "'d" + valStr;
valStr = std::to_string(size) + valStr;
UINFO(8, "Decompiled value with recovered size: " << valStr << std::endl);
auto* constp = new AstConst(make_fileline(obj_h), AstConst::StringToParse(), valStr.c_str());
auto& num = constp->num();
if (num.width() >= 32 && num.widthMin() <= 32) {
UINFO(8, "Creating wide constant" << std::endl);
num.width(32, false);
num.isSigned(true);
return new AstConst(make_fileline(obj_h), num);
} else {
return constp;
}
}
} else {
UINFO(8, "Decompiled value: " << valStr << std::endl);
return new AstConst(make_fileline(obj_h), AstConst::StringToParse(), valStr.c_str());
}
}
} else {
UINFO(8, "Failed to get vpiDecompile" << std::endl);
}
return nullptr;
}
AstNode* get_value_as_node(vpiHandle obj_h, bool need_decompile = false) {
// Most nodes will have raw value in vpiDecompile, leave deducing the type to Verilator
if (need_decompile) {
if (AstNode* valuep = get_decompiled_value(obj_h))
return valuep;
else {
UINFO(7, "Requested vpiDecompile value not found in UHDM" << std::endl);
// Fallback - try to recover from UHDM node value
}
}
return get_value_by_format(obj_h);
}
AstRefDType* get_type_reference(FileLine* fl, std::string objectName, std::string fullTypeName,
UhdmShared& shared) {
AstRefDType* refp = nullptr;
size_t delimiter_pos = fullTypeName.rfind("::");
size_t prefix_pos = fullTypeName.find("::");
if (delimiter_pos == string::npos) {
UINFO(7, "No package prefix found, creating ref" << std::endl);
refp = new AstRefDType(fl, fullTypeName);
} else {
std::string classpackageName = "";
if (prefix_pos < delimiter_pos) {
// "Nested" packages - package importing package
// Last one is where definition is located
classpackageName = fullTypeName.substr(prefix_pos + 2, delimiter_pos - prefix_pos - 2);
} else {
// Simple package reference
classpackageName = fullTypeName.substr(0, delimiter_pos);
}
UINFO(7, "Found package prefix: " << classpackageName << std::endl);
// If we are in the same package - do not create reference,
// as it will confuse Verilator
if (classpackageName
== shared.package_prefix.substr(0, shared.package_prefix.length() - 2)) {
UINFO(7, "In the same package, creating simple ref" << std::endl);
refp = new AstRefDType(fl, objectName);
} else {
UINFO(7, "Creating ClassOrPackageRef" << std::endl);
AstPackage* classpackagep = nullptr;
auto it = shared.package_map.find(classpackageName);
if (it != shared.package_map.end()) { classpackagep = it->second; }
AstNode* classpackageref
= new AstClassOrPackageRef(fl, classpackageName, classpackagep, nullptr);
shared.m_symp->nextId(classpackagep);
refp = new AstRefDType(fl, objectName, classpackageref, nullptr);
}
}
return refp;
}
AstBasicDTypeKwd get_kwd_for_type(int vpi_var_type) {
switch (vpi_var_type) {
case vpiLogicTypespec:
case vpiLogicNet:
case vpiLogicVar: {
return AstBasicDTypeKwd::LOGIC;
}
case vpiIntTypespec:
case vpiIntVar: {
return AstBasicDTypeKwd::INT;
}
case vpiLongIntTypespec:
case vpiLongIntVar: {
return AstBasicDTypeKwd::LONGINT;
}
case vpiIntegerTypespec:
case vpiIntegerNet:
case vpiIntegerVar: {
return AstBasicDTypeKwd::INTEGER;
}
case vpiShortIntTypespec:
case vpiShortIntVar: {
return AstBasicDTypeKwd::SHORTINT;
}
case vpiBitTypespec:
case vpiBitVar: {
return AstBasicDTypeKwd::BIT;
}
case vpiByteTypespec:
case vpiByteVar: {
return AstBasicDTypeKwd::BYTE;
}
case vpiShortRealTypespec:
case vpiShortRealVar: {
// Warning thrown by original verilator
auto *fl = new FileLine("uhdm");
fl->v3warn(SHORTREAL,
"Unsupported: shortreal being promoted to real (suggest use real instead)");
return AstBasicDTypeKwd::DOUBLE;
}
case vpiRealTypespec:
case vpiRealVar: {
return AstBasicDTypeKwd::DOUBLE;
}
case vpiStringTypespec:
case vpiStringVar: {
return AstBasicDTypeKwd::STRING;
}
case vpiTimeTypespec:
case vpiTimeNet:
case vpiTimeVar: {
return AstBasicDTypeKwd::TIME;
}
case vpiChandleTypespec:
case vpiChandleVar: {
return AstBasicDTypeKwd::CHANDLE;
}
case vpiEnumTypespec:
case vpiEnumVar:
case vpiEnumNet:
case vpiStructTypespec:
case vpiStructNet:
case vpiStructVar:
case vpiArrayTypespec:
case vpiPackedArrayTypespec:
case vpiUnionTypespec: {
// Not a basic dtype, needs further handling
return AstBasicDTypeKwd::UNKNOWN;
}
default: v3error("Unknown object type " << vpi_var_type);
}
return AstBasicDTypeKwd::UNKNOWN;
}
AstNodeDType* getDType(FileLine* fl, vpiHandle obj_h, UhdmShared& shared) {
AstNodeDType* dtypep = nullptr;
auto type = vpi_get(vpiType, obj_h);
if (type == vpiPort) {
auto ref_h = vpi_handle(vpiLowConn, obj_h);
if (!ref_h) {
v3error("Could not get lowconn handle for port, aborting");
return nullptr;
}
auto actual_h = vpi_handle(vpiActual, ref_h);
if (!actual_h) {
v3error("Could not get actual handle for port, aborting");
return nullptr;
}
auto ref_type = vpi_get(vpiType, actual_h);
if (ref_type == vpiLogicNet || ref_type == vpiIntegerNet || ref_type == vpiTimeNet
|| ref_type == vpiArrayNet || ref_type == vpiPackedArrayNet) {
type = ref_type;
obj_h = actual_h;
} else if (ref_type == vpiEnumNet || ref_type == vpiStructNet || ref_type == vpiStructVar
|| ref_type == vpiEnumVar) {
auto typespec_h = vpi_handle(vpiTypedef, obj_h);
if (typespec_h) {
type = vpi_get(vpiType, typespec_h);
obj_h = typespec_h;
}
} else if (ref_type == vpiModport || ref_type == vpiInterface) {
// Special handling, generate only reference
vpiHandle iface_h = nullptr;
if (ref_type == vpiModport) {
iface_h = vpi_handle(vpiInterface, actual_h);
} else if (ref_type == vpiInterface) {
iface_h = actual_h;
}
std::string cellName, ifaceName;
if (auto s = vpi_get_str(vpiName, ref_h)) {
cellName = s;
sanitize_str(cellName);
}
if (auto s = vpi_get_str(vpiDefName, iface_h)) {
ifaceName = s;
sanitize_str(ifaceName);
}
return new AstIfaceRefDType(fl, cellName, ifaceName);
}
}
if (type == vpiEnumNet || type == vpiStructNet || type == vpiStructVar || type == vpiEnumVar) {
auto typespec_h = vpi_handle(vpiTypespec, obj_h);
if (typespec_h) {
type = vpi_get(vpiType, typespec_h);
obj_h = typespec_h;
}
}
AstBasicDTypeKwd keyword = AstBasicDTypeKwd::UNKNOWN;
switch (type) {
case vpiLogicNet:
case vpiLogicVar:
case vpiBitVar: {
if (auto typespec_h = vpi_handle(vpiTypespec, obj_h)) {
dtypep = getDType(fl, typespec_h, shared);
if (!dtypep)
v3error("Unable to handle vpiTypespec node in logic net, logic var or bit var");
} else {
AstBasicDTypeKwd keyword = get_kwd_for_type(type);
dtypep = new AstBasicDType(fl, keyword);
}
dtypep = applyPackedRanges(fl, obj_h, dtypep, shared);
break;
}
case vpiLogicTypespec:
case vpiBitTypespec:
case vpiIntTypespec:
case vpiLongIntTypespec:
case vpiIntegerTypespec:
case vpiShortIntTypespec:
case vpiShortRealTypespec:
case vpiByteTypespec:
case vpiRealTypespec:
case vpiStringTypespec:
case vpiChandleTypespec:
case vpiTimeTypespec:
case vpiArrayTypespec:
case vpiPackedArrayTypespec:
case vpiStructTypespec:
case vpiEnumTypespec:
case vpiUnionTypespec: {
if (vpiHandle alias_h = vpi_handle(vpiTypedefAlias, obj_h)) {
return getDType(fl, alias_h, shared);
}
std::string typespec_name = get_object_name(obj_h);
std::string full_type_name;
if (typespec_name != "") {
auto pos = typespec_name.rfind("::");
if (pos != std::string::npos) typespec_name = typespec_name.substr(pos + 2);
std::string package_name = "";
if (vpiHandle instance_h = vpi_handle(vpiInstance, obj_h)) {
if (vpi_get(vpiType, instance_h) == vpiPackage)
package_name = get_object_name(instance_h, {vpiDefName});
vpi_release_handle(instance_h);
}
if (package_name != "")
full_type_name = package_name + "::" + typespec_name;
else
full_type_name = typespec_name;
dtypep = get_type_reference(fl, typespec_name, full_type_name, shared);
break;
} else {
// Typespec without name, construct the type by process_typespec
dtypep = VN_CAST(process_typespec(obj_h, shared), NodeDType);
if (!dtypep) v3error("Unable to handle anonymous vpiTypespec node");
break;
}
}
case vpiIntegerNet:
case vpiTimeNet:
case vpiIntVar:
case vpiLongIntVar:
case vpiIntegerVar:
case vpiShortIntVar:
case vpiShortRealVar:
case vpiByteVar:
case vpiRealVar:
case vpiStringVar:
case vpiTimeVar:
case vpiChandleVar: {
AstBasicDTypeKwd keyword = get_kwd_for_type(type);
dtypep = new AstBasicDType(fl, keyword);
break;
}
case vpiEnumNet:
case vpiStructNet:
case vpiEnumVar:
case vpiStructVar: {
std::string type_string;
const uhdm_handle* const handle = (const uhdm_handle*)obj_h;
const UHDM::BaseClass* const object = (const UHDM::BaseClass*)handle->object;
std::string type_name = get_object_name(obj_h);
auto pos = type_name.rfind("::");
if (pos != std::string::npos) type_name = type_name.substr(pos + 2);
if (shared.visited_types_map.find(object) != shared.visited_types_map.end()) {
type_string = shared.visited_types_map[object];
dtypep = get_type_reference(fl, type_name, type_string, shared);
} else {
// Type not found or object pointer mismatch, but let's try to create a reference
// to be resolved later
// Simple reference only, prefix is not stored in name
UINFO(7, "No match found, creating ref to name" << type_name << std::endl);
dtypep = new AstRefDType(fl, type_name);
}
break;
}
case vpiPackedArrayNet:
case vpiPackedArrayVar: {
auto typespec_h = vpi_handle(vpiTypespec, obj_h);
if (typespec_h == 0) { // Try to get the type from one of the elements
auto itr = vpi_iterate(vpiElement, obj_h);
auto element_h = vpi_scan(itr);
typespec_h = vpi_handle(vpiTypespec, element_h);
}
if (typespec_h) {
dtypep = getDType(fl, typespec_h, shared);
} else {
v3error("Missing typespec for packed_array_var");
}
dtypep = applyPackedRanges(fl, obj_h, dtypep, shared);
break;
}
case vpiArrayVar: {
auto typespec_h = vpi_handle(vpiTypespec, obj_h);
if (typespec_h) {
dtypep = getDType(fl, typespec_h, shared);
} else { // Try to get the type from one of the elements
auto itr = vpi_iterate(vpiReg, obj_h);
auto member_h = vpi_scan(itr);
typespec_h = vpi_handle(vpiTypespec, member_h);
if (typespec_h) {
dtypep = getDType(fl, typespec_h, shared);
} else {
dtypep = getDType(fl, member_h, shared);
}
vpi_release_handle(itr);
vpi_release_handle(member_h);
}
vpi_release_handle(typespec_h);
dtypep = applyUnpackedRanges(make_fileline(obj_h), obj_h, dtypep, shared);
break;
}
case vpiArrayNet: {
vpiHandle itr = vpi_iterate(vpiNet, obj_h);
if (vpiHandle vpi_child_obj = vpi_scan(itr)) {
dtypep = getDType(fl, vpi_child_obj, shared);
vpi_release_handle(vpi_child_obj);
}
vpi_release_handle(itr);
dtypep = applyUnpackedRanges(make_fileline(obj_h), obj_h, dtypep, shared);
break;
}
case vpiRefVar: {
auto* name = vpi_get_str(vpiName, obj_h);
return new AstRefDType(fl, name);
}
default: v3error("Unknown object type: " << UHDM::VpiTypeName(obj_h));
}
return dtypep;
}
AstAlways* process_always(vpiHandle obj_h, UhdmShared& shared) {
VAlwaysKwd alwaysType;
// Which always type is it?
switch (vpi_get(vpiAlwaysType, obj_h)) {
case vpiAlways: {
alwaysType = VAlwaysKwd::ALWAYS;
break;
}
case vpiAlwaysFF: {
alwaysType = VAlwaysKwd::ALWAYS_FF;
break;
}
case vpiAlwaysLatch: {
alwaysType = VAlwaysKwd::ALWAYS_LATCH;
break;
}
case vpiAlwaysComb: {
alwaysType = VAlwaysKwd::ALWAYS_COMB;
break;
}
default: {
v3error("Unhandled always type");
break;
}
}
// Body of always statement
AstNode* bodyp = nullptr;
visit_one_to_one({vpiStmt}, obj_h, shared, [&](AstNode* nodep) { bodyp = nodep; });
return new AstAlways(make_fileline(obj_h), alwaysType, nullptr, bodyp);
}
AstTimingControl* process_event_control(vpiHandle obj_h, UhdmShared& shared) {
auto* sensesp = new AstSenTree(make_fileline(obj_h), nullptr);
visit_one_to_one({vpiCondition}, obj_h, shared, [&](AstNode* nodep) {
while (nodep) { // nodep may be a list, so go over it and wrap in SenItems
auto* nextp = nodep->nextp();
if (nextp) nextp->unlinkFrBackWithNext();
if (auto* senitemp = VN_CAST(nodep, SenItem)) { // already is a SenItem
sensesp->addSensesp(senitemp);
} else { // wrap this in a SenItem
sensesp->addSensesp(new AstSenItem(nodep->fileline(), VEdgeType::ET_ANYEDGE, nodep));
}
nodep = nextp;
}
});
// Body of statements
AstNode* bodyp = nullptr;
visit_one_to_one({vpiStmt}, obj_h, shared, [&](AstNode* nodep) { bodyp = nodep; });
return new AstTimingControl(make_fileline(obj_h), sensesp, bodyp);
}
AstNode* process_operation(vpiHandle obj_h, UhdmShared& shared, std::vector<AstNode*>&& operands) {
if (vpi_get(vpiReordered, obj_h)) {
// this field is present when Surelog changed the order of array parameters
// It happens when subarray is selected from multidimensional parameter
std::reverse(operands.begin(), operands.end());
}
auto operation = vpi_get(vpiOpType, obj_h);
switch (operation) {
case vpiBitNegOp: {
return new AstNot(make_fileline(obj_h), operands[0]);
}
case vpiNotOp: {
return new AstLogNot(make_fileline(obj_h), operands[0]);
}
case vpiBitAndOp: {
return new AstAnd(make_fileline(obj_h), operands[0], operands[1]);
}
case vpiListOp: {
AstNode* elementp = operands[0];
// First operand is assigned above, start from second
for (auto it = ++operands.begin(); it != operands.end(); it++) {
elementp->addNextNull(*it);
}
return elementp;
}
case vpiBitOrOp: {
return new AstOr(make_fileline(obj_h), operands[0], operands[1]);
}
case vpiBitXorOp: {
return new AstXor(make_fileline(obj_h), operands[0], operands[1]);
}
case vpiBitXnorOp: {
return new AstNot(make_fileline(obj_h), new AstXor(make_fileline(obj_h), operands[0], operands[1]));
}
case vpiImplyOp: {
// Unsupported by mainline verilator
// return new AstImplication(make_fileline(obj_h), operands[0], operands[1]);
make_fileline(obj_h)->v3error("Implication operator is unsupported");
}
case vpiPostIncOp:
case vpiPostDecOp: {
auto* onep = new AstConst(make_fileline(obj_h), 1);
AstNode* op = nullptr;
if (operation == vpiPostIncOp) {
op = new AstAdd(make_fileline(obj_h), operands[0], onep);
} else if (operation == vpiPostDecOp) {
op = new AstSub(make_fileline(obj_h), operands[0], onep);
}
auto* varp = get_referenceNode(make_fileline(obj_h), operands[0]->name(), shared);
return new AstAssign(make_fileline(obj_h), varp, op);
}
case vpiAssignmentOp: {
return new AstAssign(make_fileline(obj_h), operands[0], operands[1]);
}
case vpiUnaryAndOp: {
return new AstRedAnd(make_fileline(obj_h), operands[0]);
}
case vpiUnaryNandOp: {
auto* op = new AstRedAnd(make_fileline(obj_h), operands[0]);
return new AstNot(make_fileline(obj_h), op);
}
case vpiUnaryNorOp: {
auto* op = new AstRedOr(make_fileline(obj_h), operands[0]);
return new AstNot(make_fileline(obj_h), op);
}
case vpiUnaryOrOp: {
return new AstRedOr(make_fileline(obj_h), operands[0]);
}
case vpiUnaryXorOp: {
return new AstRedXor(make_fileline(obj_h), operands[0]);
}
case vpiUnaryXNorOp: {
return new AstNot(make_fileline(obj_h), new AstRedXor(make_fileline(obj_h), operands[0]));
}
case vpiEventOrOp: {
// Do not create a separate node
// Chain operand nodes instead
AstNode* eventOrNodep = nullptr;
for (auto op : operands) {
if (op) {
if (op->type() == AstType::en::atSenItem) {
// This is a Posedge/Negedge operation, keep this op