-
Notifications
You must be signed in to change notification settings - Fork 341
/
Copy pathExpressions.cpp
1420 lines (1280 loc) · 52.1 KB
/
Expressions.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
//===- Expressions.cpp - Slang expression conversion ----------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "ImportVerilogInternals.h"
#include "slang/ast/SystemSubroutine.h"
#include "slang/syntax/AllSyntax.h"
using namespace circt;
using namespace ImportVerilog;
using moore::Domain;
/// Convert a Slang `SVInt` to a CIRCT `FVInt`.
static FVInt convertSVIntToFVInt(const slang::SVInt &svint) {
if (svint.hasUnknown()) {
unsigned numWords = svint.getNumWords() / 2;
auto value = ArrayRef<uint64_t>(svint.getRawPtr(), numWords);
auto unknown = ArrayRef<uint64_t>(svint.getRawPtr() + numWords, numWords);
return FVInt(APInt(svint.getBitWidth(), value),
APInt(svint.getBitWidth(), unknown));
}
auto value = ArrayRef<uint64_t>(svint.getRawPtr(), svint.getNumWords());
return FVInt(APInt(svint.getBitWidth(), value));
}
// NOLINTBEGIN(misc-no-recursion)
namespace {
struct RvalueExprVisitor {
Context &context;
Location loc;
OpBuilder &builder;
RvalueExprVisitor(Context &context, Location loc)
: context(context), loc(loc), builder(context.builder) {}
// Handle references to the left-hand side of a parent assignment.
Value visit(const slang::ast::LValueReferenceExpression &expr) {
assert(!context.lvalueStack.empty() && "parent assignments push lvalue");
auto lvalue = context.lvalueStack.back();
return builder.create<moore::ReadOp>(loc, lvalue);
}
// Handle named values, such as references to declared variables.
Value visit(const slang::ast::NamedValueExpression &expr) {
if (auto value = context.valueSymbols.lookup(&expr.symbol)) {
if (isa<moore::RefType>(value.getType())) {
auto readOp = builder.create<moore::ReadOp>(loc, value);
if (context.rvalueReadCallback)
context.rvalueReadCallback(readOp);
value = readOp.getResult();
}
return value;
}
// Try to materialize constant values directly.
auto constant = context.evaluateConstant(expr);
if (auto value = context.materializeConstant(constant, *expr.type, loc))
return value;
// Otherwise some other part of ImportVerilog should have added an MLIR
// value for this expression's symbol to the `context.valueSymbols` table.
auto d = mlir::emitError(loc, "unknown name `") << expr.symbol.name << "`";
d.attachNote(context.convertLocation(expr.symbol.location))
<< "no rvalue generated for " << slang::ast::toString(expr.symbol.kind);
return {};
}
// Handle hierarchical values, such as `x = Top.sub.var`.
Value visit(const slang::ast::HierarchicalValueExpression &expr) {
auto hierLoc = context.convertLocation(expr.symbol.location);
if (auto value = context.valueSymbols.lookup(&expr.symbol)) {
if (isa<moore::RefType>(value.getType())) {
auto readOp = builder.create<moore::ReadOp>(hierLoc, value);
if (context.rvalueReadCallback)
context.rvalueReadCallback(readOp);
value = readOp.getResult();
}
return value;
}
// Emit an error for those hierarchical values not recorded in the
// `valueSymbols`.
auto d = mlir::emitError(loc, "unknown hierarchical name `")
<< expr.symbol.name << "`";
d.attachNote(hierLoc) << "no rvalue generated for "
<< slang::ast::toString(expr.symbol.kind);
return {};
}
// Handle type conversions (explicit and implicit).
Value visit(const slang::ast::ConversionExpression &expr) {
auto type = context.convertType(*expr.type);
if (!type)
return {};
return context.convertRvalueExpression(expr.operand(), type);
}
// Handle blocking and non-blocking assignments.
Value visit(const slang::ast::AssignmentExpression &expr) {
auto lhs = context.convertLvalueExpression(expr.left());
if (!lhs)
return {};
context.lvalueStack.push_back(lhs);
auto rhs = context.convertRvalueExpression(
expr.right(), cast<moore::RefType>(lhs.getType()).getNestedType());
context.lvalueStack.pop_back();
if (!rhs)
return {};
if (expr.timingControl) {
auto loc = context.convertLocation(expr.timingControl->sourceRange);
mlir::emitError(loc, "delayed assignments not supported");
return {};
}
if (expr.isNonBlocking())
builder.create<moore::NonBlockingAssignOp>(loc, lhs, rhs);
else
builder.create<moore::BlockingAssignOp>(loc, lhs, rhs);
return rhs;
}
// Helper function to convert an argument to a simple bit vector type, pass it
// to a reduction op, and optionally invert the result.
template <class ConcreteOp>
Value createReduction(Value arg, bool invert) {
arg = context.convertToSimpleBitVector(arg);
if (!arg)
return {};
Value result = builder.create<ConcreteOp>(loc, arg);
if (invert)
result = builder.create<moore::NotOp>(loc, result);
return result;
}
// Helper function to create pre and post increments and decrements.
Value createIncrement(Value arg, bool isInc, bool isPost) {
auto preValue = builder.create<moore::ReadOp>(loc, arg);
auto one = builder.create<moore::ConstantOp>(
loc, cast<moore::IntType>(preValue.getType()), 1);
auto postValue =
isInc ? builder.create<moore::AddOp>(loc, preValue, one).getResult()
: builder.create<moore::SubOp>(loc, preValue, one).getResult();
builder.create<moore::BlockingAssignOp>(loc, arg, postValue);
if (isPost)
return preValue;
return postValue;
}
// Handle unary operators.
Value visit(const slang::ast::UnaryExpression &expr) {
using slang::ast::UnaryOperator;
Value arg;
if (expr.op == UnaryOperator::Preincrement ||
expr.op == UnaryOperator::Predecrement ||
expr.op == UnaryOperator::Postincrement ||
expr.op == UnaryOperator::Postdecrement)
arg = context.convertLvalueExpression(expr.operand());
else
arg = context.convertRvalueExpression(expr.operand());
if (!arg)
return {};
switch (expr.op) {
// `+a` is simply `a`, but converted to a simple bit vector type since
// this is technically an arithmetic operation.
case UnaryOperator::Plus:
return context.convertToSimpleBitVector(arg);
case UnaryOperator::Minus:
arg = context.convertToSimpleBitVector(arg);
if (!arg)
return {};
return builder.create<moore::NegOp>(loc, arg);
case UnaryOperator::BitwiseNot:
arg = context.convertToSimpleBitVector(arg);
if (!arg)
return {};
return builder.create<moore::NotOp>(loc, arg);
case UnaryOperator::BitwiseAnd:
return createReduction<moore::ReduceAndOp>(arg, false);
case UnaryOperator::BitwiseOr:
return createReduction<moore::ReduceOrOp>(arg, false);
case UnaryOperator::BitwiseXor:
return createReduction<moore::ReduceXorOp>(arg, false);
case UnaryOperator::BitwiseNand:
return createReduction<moore::ReduceAndOp>(arg, true);
case UnaryOperator::BitwiseNor:
return createReduction<moore::ReduceOrOp>(arg, true);
case UnaryOperator::BitwiseXnor:
return createReduction<moore::ReduceXorOp>(arg, true);
case UnaryOperator::LogicalNot:
arg = context.convertToBool(arg);
if (!arg)
return {};
return builder.create<moore::NotOp>(loc, arg);
case UnaryOperator::Preincrement:
return createIncrement(arg, true, false);
case UnaryOperator::Predecrement:
return createIncrement(arg, false, false);
case UnaryOperator::Postincrement:
return createIncrement(arg, true, true);
case UnaryOperator::Postdecrement:
return createIncrement(arg, false, true);
}
mlir::emitError(loc, "unsupported unary operator");
return {};
}
// Helper function to convert two arguments to a simple bit vector type and
// pass them into a binary op.
template <class ConcreteOp>
Value createBinary(Value lhs, Value rhs) {
lhs = context.convertToSimpleBitVector(lhs);
if (!lhs)
return {};
rhs = context.convertToSimpleBitVector(rhs);
if (!rhs)
return {};
return builder.create<ConcreteOp>(loc, lhs, rhs);
}
// Handle binary operators.
Value visit(const slang::ast::BinaryExpression &expr) {
auto lhs = context.convertRvalueExpression(expr.left());
if (!lhs)
return {};
auto rhs = context.convertRvalueExpression(expr.right());
if (!rhs)
return {};
// Determine the domain of the result.
Domain domain = Domain::TwoValued;
if (expr.type->isFourState() || expr.left().type->isFourState() ||
expr.right().type->isFourState())
domain = Domain::FourValued;
using slang::ast::BinaryOperator;
switch (expr.op) {
case BinaryOperator::Add:
return createBinary<moore::AddOp>(lhs, rhs);
case BinaryOperator::Subtract:
return createBinary<moore::SubOp>(lhs, rhs);
case BinaryOperator::Multiply:
return createBinary<moore::MulOp>(lhs, rhs);
case BinaryOperator::Divide:
if (expr.type->isSigned())
return createBinary<moore::DivSOp>(lhs, rhs);
else
return createBinary<moore::DivUOp>(lhs, rhs);
case BinaryOperator::Mod:
if (expr.type->isSigned())
return createBinary<moore::ModSOp>(lhs, rhs);
else
return createBinary<moore::ModUOp>(lhs, rhs);
case BinaryOperator::Power: {
// Slang casts the LHS and result of the `**` operator to a four-valued
// type, since the operator can return X even for two-valued inputs. To
// maintain uniform types across operands and results, cast the RHS to
// that four-valued type as well.
auto rhsCast =
builder.create<moore::ConversionOp>(loc, lhs.getType(), rhs);
if (expr.type->isSigned())
return createBinary<moore::PowSOp>(lhs, rhsCast);
else
return createBinary<moore::PowUOp>(lhs, rhsCast);
}
case BinaryOperator::BinaryAnd:
return createBinary<moore::AndOp>(lhs, rhs);
case BinaryOperator::BinaryOr:
return createBinary<moore::OrOp>(lhs, rhs);
case BinaryOperator::BinaryXor:
return createBinary<moore::XorOp>(lhs, rhs);
case BinaryOperator::BinaryXnor: {
auto result = createBinary<moore::XorOp>(lhs, rhs);
if (!result)
return {};
return builder.create<moore::NotOp>(loc, result);
}
case BinaryOperator::Equality:
return createBinary<moore::EqOp>(lhs, rhs);
case BinaryOperator::Inequality:
return createBinary<moore::NeOp>(lhs, rhs);
case BinaryOperator::CaseEquality:
return createBinary<moore::CaseEqOp>(lhs, rhs);
case BinaryOperator::CaseInequality:
return createBinary<moore::CaseNeOp>(lhs, rhs);
case BinaryOperator::WildcardEquality:
return createBinary<moore::WildcardEqOp>(lhs, rhs);
case BinaryOperator::WildcardInequality:
return createBinary<moore::WildcardNeOp>(lhs, rhs);
case BinaryOperator::GreaterThanEqual:
if (expr.left().type->isSigned())
return createBinary<moore::SgeOp>(lhs, rhs);
else
return createBinary<moore::UgeOp>(lhs, rhs);
case BinaryOperator::GreaterThan:
if (expr.left().type->isSigned())
return createBinary<moore::SgtOp>(lhs, rhs);
else
return createBinary<moore::UgtOp>(lhs, rhs);
case BinaryOperator::LessThanEqual:
if (expr.left().type->isSigned())
return createBinary<moore::SleOp>(lhs, rhs);
else
return createBinary<moore::UleOp>(lhs, rhs);
case BinaryOperator::LessThan:
if (expr.left().type->isSigned())
return createBinary<moore::SltOp>(lhs, rhs);
else
return createBinary<moore::UltOp>(lhs, rhs);
// See IEEE 1800-2017 § 11.4.7 "Logical operators".
case BinaryOperator::LogicalAnd: {
// TODO: This should short-circuit. Put the RHS code into a separate
// block.
lhs = context.convertToBool(lhs, domain);
if (!lhs)
return {};
rhs = context.convertToBool(rhs, domain);
if (!rhs)
return {};
return builder.create<moore::AndOp>(loc, lhs, rhs);
}
case BinaryOperator::LogicalOr: {
// TODO: This should short-circuit. Put the RHS code into a separate
// block.
lhs = context.convertToBool(lhs, domain);
if (!lhs)
return {};
rhs = context.convertToBool(rhs, domain);
if (!rhs)
return {};
return builder.create<moore::OrOp>(loc, lhs, rhs);
}
case BinaryOperator::LogicalImplication: {
// `(lhs -> rhs)` equivalent to `(!lhs || rhs)`.
lhs = context.convertToBool(lhs, domain);
if (!lhs)
return {};
rhs = context.convertToBool(rhs, domain);
if (!rhs)
return {};
auto notLHS = builder.create<moore::NotOp>(loc, lhs);
return builder.create<moore::OrOp>(loc, notLHS, rhs);
}
case BinaryOperator::LogicalEquivalence: {
// `(lhs <-> rhs)` equivalent to `(lhs && rhs) || (!lhs && !rhs)`.
lhs = context.convertToBool(lhs, domain);
if (!lhs)
return {};
rhs = context.convertToBool(rhs, domain);
if (!rhs)
return {};
auto notLHS = builder.create<moore::NotOp>(loc, lhs);
auto notRHS = builder.create<moore::NotOp>(loc, rhs);
auto both = builder.create<moore::AndOp>(loc, lhs, rhs);
auto notBoth = builder.create<moore::AndOp>(loc, notLHS, notRHS);
return builder.create<moore::OrOp>(loc, both, notBoth);
}
case BinaryOperator::LogicalShiftLeft:
return createBinary<moore::ShlOp>(lhs, rhs);
case BinaryOperator::LogicalShiftRight:
return createBinary<moore::ShrOp>(lhs, rhs);
case BinaryOperator::ArithmeticShiftLeft:
return createBinary<moore::ShlOp>(lhs, rhs);
case BinaryOperator::ArithmeticShiftRight: {
// The `>>>` operator is an arithmetic right shift if the LHS operand is
// signed, or a logical right shift if the operand is unsigned.
lhs = context.convertToSimpleBitVector(lhs);
rhs = context.convertToSimpleBitVector(rhs);
if (!lhs || !rhs)
return {};
if (expr.type->isSigned())
return builder.create<moore::AShrOp>(loc, lhs, rhs);
return builder.create<moore::ShrOp>(loc, lhs, rhs);
}
}
mlir::emitError(loc, "unsupported binary operator");
return {};
}
// Handle `'0`, `'1`, `'x`, and `'z` literals.
Value visit(const slang::ast::UnbasedUnsizedIntegerLiteral &expr) {
return context.materializeSVInt(expr.getValue(), *expr.type, loc);
}
// Handle integer literals.
Value visit(const slang::ast::IntegerLiteral &expr) {
return context.materializeSVInt(expr.getValue(), *expr.type, loc);
}
// Handle concatenations.
Value visit(const slang::ast::ConcatenationExpression &expr) {
SmallVector<Value> operands;
for (auto *operand : expr.operands()) {
auto value = context.convertRvalueExpression(*operand);
if (!value)
continue;
value = context.convertToSimpleBitVector(value);
operands.push_back(value);
}
return builder.create<moore::ConcatOp>(loc, operands);
}
// Handle replications.
Value visit(const slang::ast::ReplicationExpression &expr) {
auto type = context.convertType(*expr.type);
if (isa<moore::VoidType>(type))
return {};
auto value = context.convertRvalueExpression(expr.concat());
if (!value)
return {};
return builder.create<moore::ReplicateOp>(loc, type, value);
}
Value getSelectIndex(Value index, const slang::ConstantRange &range) const {
auto indexType = cast<moore::UnpackedType>(index.getType());
auto bw = std::max(llvm::Log2_32_Ceil(std::max(std::abs(range.lower()),
std::abs(range.upper()))),
indexType.getBitSize().value());
auto intType =
moore::IntType::get(index.getContext(), bw, indexType.getDomain());
if (range.isLittleEndian()) {
if (range.lower() == 0)
return index;
Value newIndex =
builder.createOrFold<moore::ConversionOp>(loc, intType, index);
Value offset = builder.create<moore::ConstantOp>(
loc, intType, range.lower(), /*isSigned = */ range.lower() < 0);
return builder.createOrFold<moore::SubOp>(loc, newIndex, offset);
}
if (range.upper() == 0)
return builder.createOrFold<moore::NegOp>(loc, index);
Value newIndex =
builder.createOrFold<moore::ConversionOp>(loc, intType, index);
Value offset = builder.create<moore::ConstantOp>(
loc, intType, range.upper(), /* isSigned = */ range.upper() < 0);
return builder.createOrFold<moore::SubOp>(loc, offset, newIndex);
}
// Handle single bit selections.
Value visit(const slang::ast::ElementSelectExpression &expr) {
auto type = context.convertType(*expr.type);
auto value = context.convertRvalueExpression(expr.value());
if (!type || !value)
return {};
auto range = expr.value().type->getFixedRange();
if (auto *constValue = expr.selector().constant) {
assert(!constValue->hasUnknown());
assert(constValue->size() <= 32);
auto lowBit = constValue->integer().as<uint32_t>().value();
return builder.create<moore::ExtractOp>(loc, type, value,
range.translateIndex(lowBit));
}
auto lowBit = context.convertRvalueExpression(expr.selector());
if (!lowBit)
return {};
return builder.create<moore::DynExtractOp>(loc, type, value,
getSelectIndex(lowBit, range));
}
// Handle range bits selections.
Value visit(const slang::ast::RangeSelectExpression &expr) {
auto type = context.convertType(*expr.type);
auto value = context.convertRvalueExpression(expr.value());
if (!type || !value)
return {};
Value dynLowBit;
uint32_t constLowBit;
auto *leftConst = expr.left().constant;
auto *rightConst = expr.right().constant;
if (leftConst) {
assert(!leftConst->hasUnknown());
assert(leftConst->size() <= 32);
}
if (rightConst) {
assert(!rightConst->hasUnknown());
assert(rightConst->size() <= 32);
}
if (expr.getSelectionKind() == slang::ast::RangeSelectionKind::Simple) {
if (leftConst && rightConst) {
// Estimate whether is big endian or little endian.
auto lhs = leftConst->integer().as<uint32_t>().value();
auto rhs = rightConst->integer().as<uint32_t>().value();
constLowBit = lhs < rhs ? lhs : rhs;
} else {
mlir::emitError(loc, "unsupported a variable as the index in the")
<< slang::ast::toString(expr.getSelectionKind()) << "kind";
return {};
}
} else if (expr.getSelectionKind() ==
slang::ast::RangeSelectionKind::IndexedDown) {
// IndexedDown: arr[7-:8]. It's equivalent to arr[7:0] or arr[0:7]
// depending on little endian or bit endian. No matter which situation,
// the low bit must be "0".
if (leftConst) {
auto subtrahend = leftConst->integer().as<uint32_t>().value();
auto sliceWidth =
expr.right().constant->integer().as<uint32_t>().value();
constLowBit = subtrahend - sliceWidth - 1;
} else {
auto subtrahend = context.convertRvalueExpression(expr.left());
auto subtrahendType = cast<moore::UnpackedType>(subtrahend.getType());
auto intType = moore::IntType::get(context.getContext(),
subtrahendType.getBitSize().value(),
subtrahendType.getDomain());
auto sliceWidth =
expr.right().constant->integer().as<uint32_t>().value() - 1;
auto minuend = builder.create<moore::ConstantOp>(
loc, intType, sliceWidth, expr.left().type->isSigned());
dynLowBit = builder.create<moore::SubOp>(loc, subtrahend, minuend);
}
} else {
// IndexedUp: arr[0+:8]. "0" is the low bit, "8" is the bits slice width.
if (leftConst)
constLowBit = leftConst->integer().as<uint32_t>().value();
else
dynLowBit = context.convertRvalueExpression(expr.left());
}
auto range = expr.value().type->getFixedRange();
if (leftConst && rightConst)
return builder.create<moore::ExtractOp>(
loc, type, value, range.translateIndex(constLowBit));
return builder.create<moore::DynExtractOp>(
loc, type, value, getSelectIndex(dynLowBit, range));
}
Value visit(const slang::ast::MemberAccessExpression &expr) {
auto type = context.convertType(*expr.type);
auto valueType = expr.value().type;
auto value = context.convertRvalueExpression(expr.value());
if (!type || !value)
return {};
if (valueType->isStruct()) {
return builder.create<moore::StructExtractOp>(
loc, type, builder.getStringAttr(expr.member.name), value);
}
if (valueType->isPackedUnion() || valueType->isUnpackedUnion()) {
return builder.create<moore::UnionExtractOp>(
loc, type, builder.getStringAttr(expr.member.name), value);
}
mlir::emitError(loc, "expression of type ")
<< value.getType() << " cannot be accessed";
return {};
}
// Handle set membership operator.
Value visit(const slang::ast::InsideExpression &expr) {
auto lhs = context.convertToSimpleBitVector(
context.convertRvalueExpression(expr.left()));
if (!lhs)
return {};
// All conditions for determining whether it is inside.
SmallVector<Value> conditions;
// Traverse open range list.
for (const auto *listExpr : expr.rangeList()) {
Value cond;
// The open range list on the right-hand side of the inside operator is a
// comma-separated list of expressions or ranges.
if (const auto *openRange =
listExpr->as_if<slang::ast::OpenRangeExpression>()) {
// Handle ranges.
auto lowBound = context.convertToSimpleBitVector(
context.convertRvalueExpression(openRange->left()));
auto highBound = context.convertToSimpleBitVector(
context.convertRvalueExpression(openRange->right()));
if (!lowBound || !highBound)
return {};
Value leftValue, rightValue;
// Determine if the expression on the left-hand side is inclusively
// within the range.
if (openRange->left().type->isSigned() ||
expr.left().type->isSigned()) {
leftValue = builder.create<moore::SgeOp>(loc, lhs, lowBound);
} else {
leftValue = builder.create<moore::UgeOp>(loc, lhs, lowBound);
}
if (openRange->right().type->isSigned() ||
expr.left().type->isSigned()) {
rightValue = builder.create<moore::SleOp>(loc, lhs, highBound);
} else {
rightValue = builder.create<moore::UleOp>(loc, lhs, highBound);
}
cond = builder.create<moore::AndOp>(loc, leftValue, rightValue);
} else {
// Handle expressions.
if (!listExpr->type->isSimpleBitVector()) {
if (listExpr->type->isUnpackedArray()) {
mlir::emitError(
loc, "unpacked arrays in 'inside' expressions not supported");
return {};
}
mlir::emitError(
loc, "only simple bit vectors supported in 'inside' expressions");
return {};
}
auto value = context.convertToSimpleBitVector(
context.convertRvalueExpression(*listExpr));
if (!value)
return {};
cond = builder.create<moore::WildcardEqOp>(loc, lhs, value);
}
conditions.push_back(cond);
}
// Calculate the final result by `or` op.
auto result = conditions.back();
conditions.pop_back();
while (!conditions.empty()) {
result = builder.create<moore::OrOp>(loc, conditions.back(), result);
conditions.pop_back();
}
return result;
}
// Handle conditional operator `?:`.
Value visit(const slang::ast::ConditionalExpression &expr) {
auto type = context.convertType(*expr.type);
// Handle condition.
if (expr.conditions.size() > 1) {
mlir::emitError(loc)
<< "unsupported conditional expression with more than one condition";
return {};
}
const auto &cond = expr.conditions[0];
if (cond.pattern) {
mlir::emitError(loc) << "unsupported conditional expression with pattern";
return {};
}
auto value =
context.convertToBool(context.convertRvalueExpression(*cond.expr));
if (!value)
return {};
auto conditionalOp = builder.create<moore::ConditionalOp>(loc, type, value);
// Create blocks for true region and false region.
auto &trueBlock = conditionalOp.getTrueRegion().emplaceBlock();
auto &falseBlock = conditionalOp.getFalseRegion().emplaceBlock();
OpBuilder::InsertionGuard g(builder);
// Handle left expression.
builder.setInsertionPointToStart(&trueBlock);
auto trueValue = context.convertRvalueExpression(expr.left(), type);
if (!trueValue)
return {};
builder.create<moore::YieldOp>(loc, trueValue);
// Handle right expression.
builder.setInsertionPointToStart(&falseBlock);
auto falseValue = context.convertRvalueExpression(expr.right(), type);
if (!falseValue)
return {};
builder.create<moore::YieldOp>(loc, falseValue);
return conditionalOp.getResult();
}
/// Handle calls.
Value visit(const slang::ast::CallExpression &expr) {
// Class method calls are currently not supported.
if (expr.thisClass()) {
mlir::emitError(loc, "unsupported class method call");
return {};
}
// Try to materialize constant values directly.
auto constant = context.evaluateConstant(expr);
if (auto value = context.materializeConstant(constant, *expr.type, loc))
return value;
return std::visit(
[&](auto &subroutine) { return visitCall(expr, subroutine); },
expr.subroutine);
}
/// Handle subroutine calls.
Value visitCall(const slang::ast::CallExpression &expr,
const slang::ast::SubroutineSymbol *subroutine) {
auto *lowering = context.declareFunction(*subroutine);
if (!lowering)
return {};
// Convert the call arguments. Input arguments are converted to an rvalue.
// All other arguments are converted to lvalues and passed into the function
// by reference.
SmallVector<Value> arguments;
for (auto [callArg, declArg] :
llvm::zip(expr.arguments(), subroutine->getArguments())) {
// Unpack the `<expr> = EmptyArgument` pattern emitted by Slang for output
// and inout arguments.
auto *expr = callArg;
if (const auto *assign = expr->as_if<slang::ast::AssignmentExpression>())
expr = &assign->left();
Value value;
if (declArg->direction == slang::ast::ArgumentDirection::In)
value = context.convertRvalueExpression(*expr);
else
value = context.convertLvalueExpression(*expr);
if (!value)
return {};
arguments.push_back(value);
}
// Create the call.
auto callOp =
builder.create<mlir::func::CallOp>(loc, lowering->op, arguments);
// For calls to void functions we need to have a value to return from this
// function. Create a dummy `unrealized_conversion_cast`, which will get
// deleted again later on.
if (callOp.getNumResults() == 0)
return builder
.create<mlir::UnrealizedConversionCastOp>(
loc, moore::VoidType::get(context.getContext()), ValueRange{})
.getResult(0);
return callOp.getResult(0);
}
/// Handle system calls.
Value visitCall(const slang::ast::CallExpression &expr,
const slang::ast::CallExpression::SystemCallInfo &info) {
const auto &subroutine = *info.subroutine;
auto args = expr.arguments();
if (args.size() == 1) {
auto value = context.convertRvalueExpression(*args[0]);
if (!value)
return {};
auto result = context.convertSystemCallArity1(subroutine, loc, value);
if (failed(result))
return {};
if (*result)
return *result;
}
mlir::emitError(loc) << "unsupported system call `" << subroutine.name
<< "`";
return {};
}
/// Handle string literals.
Value visit(const slang::ast::StringLiteral &expr) {
auto type = context.convertType(*expr.type);
return builder.create<moore::StringConstantOp>(loc, type, expr.getValue());
}
/// Handle real literals.
Value visit(const slang::ast::RealLiteral &expr) {
return builder.create<moore::RealLiteralOp>(
loc, builder.getF64FloatAttr(expr.getValue()));
}
/// Handle assignment patterns.
Value visitAssignmentPattern(
const slang::ast::AssignmentPatternExpressionBase &expr,
unsigned replCount = 1) {
auto type = context.convertType(*expr.type);
// Convert the individual elements first.
auto elementCount = expr.elements().size();
SmallVector<Value> elements;
elements.reserve(replCount * elementCount);
for (auto elementExpr : expr.elements()) {
auto value = context.convertRvalueExpression(*elementExpr);
if (!value)
return {};
elements.push_back(value);
}
for (unsigned replIdx = 1; replIdx < replCount; ++replIdx)
for (unsigned elementIdx = 0; elementIdx < elementCount; ++elementIdx)
elements.push_back(elements[elementIdx]);
// Handle integers.
if (auto intType = dyn_cast<moore::IntType>(type)) {
assert(intType.getWidth() == elements.size());
std::reverse(elements.begin(), elements.end());
return builder.create<moore::ConcatOp>(loc, intType, elements);
}
// Handle packed structs.
if (auto structType = dyn_cast<moore::StructType>(type)) {
assert(structType.getMembers().size() == elements.size());
return builder.create<moore::StructCreateOp>(loc, structType, elements);
}
// Handle unpacked structs.
if (auto structType = dyn_cast<moore::UnpackedStructType>(type)) {
assert(structType.getMembers().size() == elements.size());
return builder.create<moore::StructCreateOp>(loc, structType, elements);
}
// Handle packed arrays.
if (auto arrayType = dyn_cast<moore::ArrayType>(type)) {
assert(arrayType.getSize() == elements.size());
return builder.create<moore::ArrayCreateOp>(loc, arrayType, elements);
}
// Handle unpacked arrays.
if (auto arrayType = dyn_cast<moore::UnpackedArrayType>(type)) {
assert(arrayType.getSize() == elements.size());
return builder.create<moore::ArrayCreateOp>(loc, arrayType, elements);
}
mlir::emitError(loc) << "unsupported assignment pattern with type " << type;
return {};
}
Value visit(const slang::ast::SimpleAssignmentPatternExpression &expr) {
return visitAssignmentPattern(expr);
}
Value visit(const slang::ast::StructuredAssignmentPatternExpression &expr) {
return visitAssignmentPattern(expr);
}
Value visit(const slang::ast::ReplicatedAssignmentPatternExpression &expr) {
auto count =
context.evaluateConstant(expr.count()).integer().as<unsigned>();
assert(count && "Slang guarantees constant non-zero replication count");
return visitAssignmentPattern(expr, *count);
}
Value visit(const slang::ast::StreamingConcatenationExpression &expr) {
SmallVector<Value> operands;
for (auto stream : expr.streams()) {
auto operandLoc = context.convertLocation(stream.operand->sourceRange);
if (!stream.constantWithWidth.has_value() && stream.withExpr) {
mlir::emitError(operandLoc)
<< "Moore only support streaming "
"concatenation with fixed size 'with expression'";
return {};
}
Value value;
if (stream.constantWithWidth.has_value()) {
value = context.convertRvalueExpression(*stream.withExpr);
auto type = cast<moore::UnpackedType>(value.getType());
auto intType = moore::IntType::get(
context.getContext(), type.getBitSize().value(), type.getDomain());
// Do not care if it's signed, because we will not do expansion.
value = context.materializeConversion(intType, value, false, loc);
} else {
value = context.convertRvalueExpression(*stream.operand);
}
if (!value)
return {};
value = context.convertToSimpleBitVector(value);
if (!value) {
return {};
}
operands.push_back(value);
}
Value value;
if (operands.size() == 1) {
// There must be at least one element, otherwise slang will report an
// error.
value = operands.front();
} else {
value = builder.create<moore::ConcatOp>(loc, operands).getResult();
}
if (expr.sliceSize == 0) {
return value;
}
auto type = cast<moore::IntType>(value.getType());
SmallVector<Value> slicedOperands;
auto iterMax = type.getWidth() / expr.sliceSize;
auto remainSize = type.getWidth() % expr.sliceSize;
for (size_t i = 0; i < iterMax; i++) {
auto extractResultType = moore::IntType::get(
context.getContext(), expr.sliceSize, type.getDomain());
auto extracted = builder.create<moore::ExtractOp>(
loc, extractResultType, value, i * expr.sliceSize);
slicedOperands.push_back(extracted);
}
// Handle other wire
if (remainSize) {
auto extractResultType = moore::IntType::get(
context.getContext(), remainSize, type.getDomain());
auto extracted = builder.create<moore::ExtractOp>(
loc, extractResultType, value, iterMax * expr.sliceSize);
slicedOperands.push_back(extracted);
}
return builder.create<moore::ConcatOp>(loc, slicedOperands);
}
/// Emit an error for all other expressions.
template <typename T>
Value visit(T &&node) {
mlir::emitError(loc, "unsupported expression: ")
<< slang::ast::toString(node.kind);
return {};
}
Value visitInvalid(const slang::ast::Expression &expr) {
mlir::emitError(loc, "invalid expression");
return {};
}
};
} // namespace
namespace {
struct LvalueExprVisitor {
Context &context;
Location loc;
OpBuilder &builder;
LvalueExprVisitor(Context &context, Location loc)
: context(context), loc(loc), builder(context.builder) {}
// Handle named values, such as references to declared variables.
Value visit(const slang::ast::NamedValueExpression &expr) {
if (auto value = context.valueSymbols.lookup(&expr.symbol))
return value;
auto d = mlir::emitError(loc, "unknown name `") << expr.symbol.name << "`";
d.attachNote(context.convertLocation(expr.symbol.location))
<< "no lvalue generated for " << slang::ast::toString(expr.symbol.kind);
return {};
}
// Handle hierarchical values, such as `Top.sub.var = x`.
Value visit(const slang::ast::HierarchicalValueExpression &expr) {
if (auto value = context.valueSymbols.lookup(&expr.symbol))
return value;
// Emit an error for those hierarchical values not recorded in the
// `valueSymbols`.
auto d = mlir::emitError(loc, "unknown hierarchical name `")
<< expr.symbol.name << "`";
d.attachNote(context.convertLocation(expr.symbol.location))
<< "no lvalue generated for " << slang::ast::toString(expr.symbol.kind);
return {};
}
// Handle concatenations.
Value visit(const slang::ast::ConcatenationExpression &expr) {
SmallVector<Value> operands;
for (auto *operand : expr.operands()) {
auto value = context.convertLvalueExpression(*operand);
if (!value)
continue;
operands.push_back(value);
}
return builder.create<moore::ConcatRefOp>(loc, operands);
}
// Handle single bit selections.
Value visit(const slang::ast::ElementSelectExpression &expr) {
auto type = context.convertType(*expr.type);
auto value = context.convertLvalueExpression(expr.value());
if (!type || !value)
return {};
if (auto *constValue = expr.selector().constant) {
assert(!constValue->hasUnknown());
assert(constValue->size() <= 32);
auto lowBit = constValue->integer().as<uint32_t>().value();
return builder.create<moore::ExtractRefOp>(
loc, moore::RefType::get(cast<moore::UnpackedType>(type)), value,
lowBit);
}
auto lowBit = context.convertRvalueExpression(expr.selector());
if (!lowBit)
return {};