10 #include "slang/ast/SystemSubroutine.h"
11 #include "slang/syntax/AllSyntax.h"
13 using namespace circt;
14 using namespace ImportVerilog;
19 if (svint.hasUnknown()) {
20 unsigned numWords = svint.getNumWords() / 2;
21 auto value = ArrayRef<uint64_t>(svint.getRawPtr(), numWords);
22 auto unknown = ArrayRef<uint64_t>(svint.getRawPtr() + numWords, numWords);
23 return FVInt(APInt(svint.getBitWidth(), value),
24 APInt(svint.getBitWidth(), unknown));
26 auto value = ArrayRef<uint64_t>(svint.getRawPtr(), svint.getNumWords());
27 return FVInt(APInt(svint.getBitWidth(), value));
32 struct RvalueExprVisitor {
37 RvalueExprVisitor(
Context &context, Location loc)
38 : context(context), loc(loc), builder(context.builder) {}
41 Value visit(
const slang::ast::LValueReferenceExpression &expr) {
44 return builder.create<moore::ReadOp>(loc, lvalue);
48 Value visit(
const slang::ast::NamedValueExpression &expr) {
49 if (
auto value = context.
valueSymbols.lookup(&expr.symbol)) {
50 if (isa<moore::RefType>(value.getType())) {
51 auto readOp = builder.create<moore::ReadOp>(loc, value);
54 value = readOp.getResult();
66 auto d = mlir::emitError(loc,
"unknown name `") << expr.symbol.name <<
"`";
73 Value visit(
const slang::ast::HierarchicalValueExpression &expr) {
75 if (
auto value = context.
valueSymbols.lookup(&expr.symbol)) {
76 if (isa<moore::RefType>(value.getType())) {
77 auto readOp = builder.create<moore::ReadOp>(hierLoc, value);
80 value = readOp.getResult();
87 auto d = mlir::emitError(loc,
"unknown hierarchical name `")
88 << expr.symbol.name <<
"`";
89 d.attachNote(hierLoc) <<
"no rvalue generated for "
95 Value visit(
const slang::ast::ConversionExpression &expr) {
103 Value visit(
const slang::ast::AssignmentExpression &expr) {
110 expr.right(), cast<moore::RefType>(lhs.getType()).getNestedType());
115 if (expr.timingControl) {
117 mlir::emitError(loc,
"delayed assignments not supported");
121 if (expr.isNonBlocking())
122 builder.create<moore::NonBlockingAssignOp>(loc, lhs, rhs);
124 builder.create<moore::BlockingAssignOp>(loc, lhs, rhs);
130 template <
class ConcreteOp>
131 Value createReduction(Value arg,
bool invert) {
135 Value result = builder.create<ConcreteOp>(loc, arg);
137 result = builder.create<moore::NotOp>(loc, result);
142 Value createIncrement(Value arg,
bool isInc,
bool isPost) {
143 auto preValue = builder.create<moore::ReadOp>(loc, arg);
144 auto one = builder.create<moore::ConstantOp>(
145 loc, cast<moore::IntType>(preValue.getType()), 1);
147 isInc ? builder.create<moore::AddOp>(loc, preValue, one).getResult()
148 : builder.create<moore::SubOp>(loc, preValue, one).getResult();
149 builder.create<moore::BlockingAssignOp>(loc, arg, postValue);
156 Value visit(
const slang::ast::UnaryExpression &expr) {
157 using slang::ast::UnaryOperator;
159 if (expr.op == UnaryOperator::Preincrement ||
160 expr.op == UnaryOperator::Predecrement ||
161 expr.op == UnaryOperator::Postincrement ||
162 expr.op == UnaryOperator::Postdecrement)
172 case UnaryOperator::Plus:
175 case UnaryOperator::Minus:
179 return builder.create<moore::NegOp>(loc, arg);
181 case UnaryOperator::BitwiseNot:
185 return builder.create<moore::NotOp>(loc, arg);
187 case UnaryOperator::BitwiseAnd:
188 return createReduction<moore::ReduceAndOp>(arg,
false);
189 case UnaryOperator::BitwiseOr:
190 return createReduction<moore::ReduceOrOp>(arg,
false);
191 case UnaryOperator::BitwiseXor:
192 return createReduction<moore::ReduceXorOp>(arg,
false);
193 case UnaryOperator::BitwiseNand:
194 return createReduction<moore::ReduceAndOp>(arg,
true);
195 case UnaryOperator::BitwiseNor:
196 return createReduction<moore::ReduceOrOp>(arg,
true);
197 case UnaryOperator::BitwiseXnor:
198 return createReduction<moore::ReduceXorOp>(arg,
true);
200 case UnaryOperator::LogicalNot:
204 return builder.create<moore::NotOp>(loc, arg);
206 case UnaryOperator::Preincrement:
207 return createIncrement(arg,
true,
false);
208 case UnaryOperator::Predecrement:
209 return createIncrement(arg,
false,
false);
210 case UnaryOperator::Postincrement:
211 return createIncrement(arg,
true,
true);
212 case UnaryOperator::Postdecrement:
213 return createIncrement(arg,
false,
true);
216 mlir::emitError(loc,
"unsupported unary operator");
222 template <
class ConcreteOp>
223 Value createBinary(Value lhs, Value rhs) {
230 return builder.create<ConcreteOp>(loc, lhs, rhs);
234 Value visit(
const slang::ast::BinaryExpression &expr) {
243 Domain domain = Domain::TwoValued;
244 if (expr.type->isFourState() || expr.left().type->isFourState() ||
245 expr.right().type->isFourState())
246 domain = Domain::FourValued;
248 using slang::ast::BinaryOperator;
250 case BinaryOperator::Add:
251 return createBinary<moore::AddOp>(lhs, rhs);
252 case BinaryOperator::Subtract:
253 return createBinary<moore::SubOp>(lhs, rhs);
254 case BinaryOperator::Multiply:
255 return createBinary<moore::MulOp>(lhs, rhs);
256 case BinaryOperator::Divide:
257 if (expr.type->isSigned())
258 return createBinary<moore::DivSOp>(lhs, rhs);
260 return createBinary<moore::DivUOp>(lhs, rhs);
261 case BinaryOperator::Mod:
262 if (expr.type->isSigned())
263 return createBinary<moore::ModSOp>(lhs, rhs);
265 return createBinary<moore::ModUOp>(lhs, rhs);
266 case BinaryOperator::Power: {
272 builder.create<moore::ConversionOp>(loc, lhs.getType(), rhs);
273 if (expr.type->isSigned())
274 return createBinary<moore::PowSOp>(lhs, rhsCast);
276 return createBinary<moore::PowUOp>(lhs, rhsCast);
279 case BinaryOperator::BinaryAnd:
280 return createBinary<moore::AndOp>(lhs, rhs);
281 case BinaryOperator::BinaryOr:
282 return createBinary<moore::OrOp>(lhs, rhs);
283 case BinaryOperator::BinaryXor:
284 return createBinary<moore::XorOp>(lhs, rhs);
285 case BinaryOperator::BinaryXnor: {
286 auto result = createBinary<moore::XorOp>(lhs, rhs);
289 return builder.create<moore::NotOp>(loc, result);
292 case BinaryOperator::Equality:
293 return createBinary<moore::EqOp>(lhs, rhs);
294 case BinaryOperator::Inequality:
295 return createBinary<moore::NeOp>(lhs, rhs);
296 case BinaryOperator::CaseEquality:
297 return createBinary<moore::CaseEqOp>(lhs, rhs);
298 case BinaryOperator::CaseInequality:
299 return createBinary<moore::CaseNeOp>(lhs, rhs);
300 case BinaryOperator::WildcardEquality:
301 return createBinary<moore::WildcardEqOp>(lhs, rhs);
302 case BinaryOperator::WildcardInequality:
303 return createBinary<moore::WildcardNeOp>(lhs, rhs);
305 case BinaryOperator::GreaterThanEqual:
306 if (expr.left().type->isSigned())
307 return createBinary<moore::SgeOp>(lhs, rhs);
309 return createBinary<moore::UgeOp>(lhs, rhs);
310 case BinaryOperator::GreaterThan:
311 if (expr.left().type->isSigned())
312 return createBinary<moore::SgtOp>(lhs, rhs);
314 return createBinary<moore::UgtOp>(lhs, rhs);
315 case BinaryOperator::LessThanEqual:
316 if (expr.left().type->isSigned())
317 return createBinary<moore::SleOp>(lhs, rhs);
319 return createBinary<moore::UleOp>(lhs, rhs);
320 case BinaryOperator::LessThan:
321 if (expr.left().type->isSigned())
322 return createBinary<moore::SltOp>(lhs, rhs);
324 return createBinary<moore::UltOp>(lhs, rhs);
327 case BinaryOperator::LogicalAnd: {
336 return builder.create<moore::AndOp>(loc, lhs, rhs);
338 case BinaryOperator::LogicalOr: {
347 return builder.create<moore::OrOp>(loc, lhs, rhs);
349 case BinaryOperator::LogicalImplication: {
357 auto notLHS = builder.create<moore::NotOp>(loc, lhs);
358 return builder.create<moore::OrOp>(loc, notLHS, rhs);
360 case BinaryOperator::LogicalEquivalence: {
368 auto notLHS = builder.create<moore::NotOp>(loc, lhs);
369 auto notRHS = builder.create<moore::NotOp>(loc, rhs);
370 auto both = builder.create<moore::AndOp>(loc, lhs, rhs);
371 auto notBoth = builder.create<moore::AndOp>(loc, notLHS, notRHS);
372 return builder.create<moore::OrOp>(loc, both, notBoth);
375 case BinaryOperator::LogicalShiftLeft:
376 return createBinary<moore::ShlOp>(lhs, rhs);
377 case BinaryOperator::LogicalShiftRight:
378 return createBinary<moore::ShrOp>(lhs, rhs);
379 case BinaryOperator::ArithmeticShiftLeft:
380 return createBinary<moore::ShlOp>(lhs, rhs);
381 case BinaryOperator::ArithmeticShiftRight: {
388 if (expr.type->isSigned())
389 return builder.create<moore::AShrOp>(loc, lhs, rhs);
390 return builder.create<moore::ShrOp>(loc, lhs, rhs);
394 mlir::emitError(loc,
"unsupported binary operator");
399 Value visit(
const slang::ast::UnbasedUnsizedIntegerLiteral &expr) {
404 Value visit(
const slang::ast::IntegerLiteral &expr) {
409 Value visit(
const slang::ast::ConcatenationExpression &expr) {
410 SmallVector<Value> operands;
411 for (
auto *operand : expr.operands()) {
416 operands.push_back(value);
418 return builder.create<moore::ConcatOp>(loc, operands);
422 Value visit(
const slang::ast::ReplicationExpression &expr) {
424 if (isa<moore::VoidType>(type))
430 return builder.create<moore::ReplicateOp>(loc, type, value);
434 Value visit(
const slang::ast::ElementSelectExpression &expr) {
439 if (
auto *constValue = expr.selector().constant) {
440 assert(!constValue->hasUnknown());
441 assert(constValue->size() <= 32);
443 auto lowBit = constValue->integer().as<uint32_t>().value();
444 return builder.create<moore::ExtractOp>(loc, type, value, lowBit);
449 return builder.create<moore::DynExtractOp>(loc, type, value, lowBit);
453 Value visit(
const slang::ast::RangeSelectExpression &expr) {
460 uint32_t constLowBit;
461 auto *leftConst = expr.left().constant;
462 auto *rightConst = expr.right().constant;
464 assert(!leftConst->hasUnknown());
465 assert(leftConst->size() <= 32);
468 assert(!rightConst->hasUnknown());
469 assert(rightConst->size() <= 32);
472 if (expr.getSelectionKind() == slang::ast::RangeSelectionKind::Simple) {
473 if (leftConst && rightConst) {
475 auto lhs = leftConst->integer().as<uint32_t>().value();
476 auto rhs = rightConst->integer().as<uint32_t>().value();
477 constLowBit = lhs < rhs ? lhs : rhs;
479 mlir::emitError(loc,
"unsupported a variable as the index in the")
483 }
else if (expr.getSelectionKind() ==
484 slang::ast::RangeSelectionKind::IndexedDown) {
489 auto subtrahend = leftConst->integer().as<uint32_t>().value();
491 expr.right().constant->integer().as<uint32_t>().value();
492 constLowBit = subtrahend - sliceWidth - 1;
495 auto subtrahendType = cast<moore::UnpackedType>(subtrahend.getType());
497 subtrahendType.getBitSize().value(),
498 subtrahendType.getDomain());
500 expr.right().constant->integer().as<uint32_t>().value() - 1;
502 builder.create<moore::ConstantOp>(loc, intType, sliceWidth);
503 dynLowBit = builder.create<moore::SubOp>(loc, subtrahend, minuend);
508 constLowBit = leftConst->integer().as<uint32_t>().value();
512 if (leftConst && rightConst)
513 return builder.create<moore::ExtractOp>(loc, type, value, constLowBit);
514 return builder.create<moore::DynExtractOp>(loc, type, value, dynLowBit);
517 Value visit(
const slang::ast::MemberAccessExpression &expr) {
519 auto valueType = expr.value().type;
523 if (valueType->isStruct()) {
524 return builder.create<moore::StructExtractOp>(
525 loc, type, builder.getStringAttr(expr.member.name), value);
527 if (valueType->isPackedUnion() || valueType->isUnpackedUnion()) {
528 return builder.create<moore::UnionExtractOp>(
529 loc, type, builder.getStringAttr(expr.member.name), value);
531 mlir::emitError(loc,
"expression of type ")
532 << value.getType() <<
" cannot be accessed";
537 Value visit(
const slang::ast::InsideExpression &expr) {
543 SmallVector<Value> conditions;
546 for (
const auto *listExpr : expr.rangeList()) {
550 if (
const auto *openRange =
551 listExpr->as_if<slang::ast::OpenRangeExpression>()) {
557 if (!lowBound || !highBound)
559 Value leftValue, rightValue;
562 if (openRange->left().type->isSigned() ||
563 expr.left().type->isSigned()) {
564 leftValue = builder.create<moore::SgeOp>(loc, lhs, lowBound);
566 leftValue = builder.create<moore::UgeOp>(loc, lhs, lowBound);
568 if (openRange->right().type->isSigned() ||
569 expr.left().type->isSigned()) {
570 rightValue = builder.create<moore::SleOp>(loc, lhs, highBound);
572 rightValue = builder.create<moore::UleOp>(loc, lhs, highBound);
574 cond = builder.create<moore::AndOp>(loc, leftValue, rightValue);
577 if (!listExpr->type->isSimpleBitVector()) {
578 if (listExpr->type->isUnpackedArray()) {
580 loc,
"unpacked arrays in 'inside' expressions not supported");
584 loc,
"only simple bit vectors supported in 'inside' expressions");
591 cond = builder.create<moore::WildcardEqOp>(loc, lhs, value);
593 conditions.push_back(cond);
597 auto result = conditions.back();
598 conditions.pop_back();
599 while (!conditions.empty()) {
600 result = builder.create<moore::OrOp>(loc, conditions.back(), result);
601 conditions.pop_back();
607 Value visit(
const slang::ast::ConditionalExpression &expr) {
611 if (expr.conditions.size() > 1) {
613 <<
"unsupported conditional expression with more than one condition";
616 const auto &cond = expr.conditions[0];
618 mlir::emitError(loc) <<
"unsupported conditional expression with pattern";
625 auto conditionalOp = builder.create<moore::ConditionalOp>(loc, type, value);
628 auto &trueBlock = conditionalOp.getTrueRegion().emplaceBlock();
629 auto &falseBlock = conditionalOp.getFalseRegion().emplaceBlock();
631 OpBuilder::InsertionGuard g(builder);
634 builder.setInsertionPointToStart(&trueBlock);
638 builder.create<moore::YieldOp>(loc, trueValue);
641 builder.setInsertionPointToStart(&falseBlock);
645 builder.create<moore::YieldOp>(loc, falseValue);
647 return conditionalOp.getResult();
651 Value visit(
const slang::ast::CallExpression &expr) {
653 if (expr.thisClass()) {
654 mlir::emitError(loc,
"unsupported class method call");
664 [&](
auto &subroutine) {
return visitCall(expr, subroutine); },
669 Value visitCall(
const slang::ast::CallExpression &expr,
670 const slang::ast::SubroutineSymbol *subroutine) {
678 SmallVector<Value> arguments;
679 for (
auto [callArg, declArg] :
680 llvm::zip(expr.arguments(), subroutine->getArguments())) {
684 auto *expr = callArg;
685 if (
const auto *assign = expr->as_if<slang::ast::AssignmentExpression>())
686 expr = &assign->left();
689 if (declArg->direction == slang::ast::ArgumentDirection::In)
695 arguments.push_back(value);
700 builder.create<mlir::func::CallOp>(loc, lowering->op, arguments);
705 if (callOp.getNumResults() == 0)
707 .create<mlir::UnrealizedConversionCastOp>(
711 return callOp.getResult(0);
715 Value visitCall(
const slang::ast::CallExpression &expr,
716 const slang::ast::CallExpression::SystemCallInfo &info) {
717 const auto &subroutine = *info.subroutine;
718 auto args = expr.arguments();
720 if (subroutine.name ==
"$signed" || subroutine.name ==
"$unsigned")
723 if (subroutine.name ==
"$clog2") {
728 return builder.create<moore::Clog2BIOp>(loc, value);
731 mlir::emitError(loc) <<
"unsupported system call `" << subroutine.name
737 Value visit(
const slang::ast::StringLiteral &expr) {
739 return builder.create<moore::StringConstantOp>(loc, type, expr.getValue());
743 Value visitAssignmentPattern(
744 const slang::ast::AssignmentPatternExpressionBase &expr,
745 unsigned replCount = 1) {
749 auto elementCount = expr.elements().size();
750 SmallVector<Value> elements;
751 elements.reserve(replCount * elementCount);
752 for (
auto elementExpr : expr.elements()) {
756 elements.push_back(value);
758 for (
unsigned replIdx = 1; replIdx < replCount; ++replIdx)
759 for (
unsigned elementIdx = 0; elementIdx < elementCount; ++elementIdx)
760 elements.push_back(elements[elementIdx]);
763 if (
auto intType = dyn_cast<moore::IntType>(type)) {
764 assert(intType.getWidth() == elements.size());
765 std::reverse(elements.begin(), elements.end());
766 return builder.create<moore::ConcatOp>(loc, intType, elements);
770 if (
auto structType = dyn_cast<moore::StructType>(type)) {
771 assert(structType.getMembers().size() == elements.size());
772 return builder.create<moore::StructCreateOp>(loc, structType, elements);
776 if (
auto structType = dyn_cast<moore::UnpackedStructType>(type)) {
777 assert(structType.getMembers().size() == elements.size());
778 return builder.create<moore::StructCreateOp>(loc, structType, elements);
782 if (
auto arrayType = dyn_cast<moore::ArrayType>(type)) {
783 assert(arrayType.getSize() == elements.size());
784 return builder.create<moore::ArrayCreateOp>(loc, arrayType, elements);
788 if (
auto arrayType = dyn_cast<moore::UnpackedArrayType>(type)) {
789 assert(arrayType.getSize() == elements.size());
790 return builder.create<moore::ArrayCreateOp>(loc, arrayType, elements);
793 mlir::emitError(loc) <<
"unsupported assignment pattern with type " << type;
797 Value visit(
const slang::ast::SimpleAssignmentPatternExpression &expr) {
798 return visitAssignmentPattern(expr);
801 Value visit(
const slang::ast::StructuredAssignmentPatternExpression &expr) {
802 return visitAssignmentPattern(expr);
805 Value visit(
const slang::ast::ReplicatedAssignmentPatternExpression &expr) {
808 assert(count &&
"Slang guarantees constant non-zero replication count");
809 return visitAssignmentPattern(expr, *count);
812 Value visit(
const slang::ast::StreamingConcatenationExpression &expr) {
813 SmallVector<Value> operands;
814 for (
auto stream : expr.streams()) {
815 auto operandLoc = context.
convertLocation(stream.operand->sourceRange);
816 if (!stream.constantWithWidth.has_value() && stream.withExpr) {
817 mlir::emitError(operandLoc)
818 <<
"Moore only support streaming "
819 "concatenation with fixed size 'with expression'";
823 if (stream.constantWithWidth.has_value()) {
825 auto type = cast<moore::UnpackedType>(value.getType());
827 context.
getContext(), type.getBitSize().value(), type.getDomain());
840 operands.push_back(value);
844 if (operands.size() == 1) {
847 value = operands.front();
849 value = builder.create<moore::ConcatOp>(loc, operands).getResult();
852 if (expr.sliceSize == 0) {
856 auto type = cast<moore::IntType>(value.getType());
857 SmallVector<Value> slicedOperands;
858 auto iterMax = type.getWidth() / expr.sliceSize;
859 auto remainSize = type.getWidth() % expr.sliceSize;
861 for (
size_t i = 0; i < iterMax; i++) {
863 context.
getContext(), expr.sliceSize, type.getDomain());
865 auto extracted = builder.create<moore::ExtractOp>(
866 loc, extractResultType, value, i * expr.sliceSize);
867 slicedOperands.push_back(extracted);
872 context.
getContext(), remainSize, type.getDomain());
874 auto extracted = builder.create<moore::ExtractOp>(
875 loc, extractResultType, value, iterMax * expr.sliceSize);
876 slicedOperands.push_back(extracted);
879 return builder.create<moore::ConcatOp>(loc, slicedOperands);
883 template <
typename T>
884 Value visit(T &&node) {
885 mlir::emitError(loc,
"unsupported expression: ")
890 Value visitInvalid(
const slang::ast::Expression &expr) {
891 mlir::emitError(loc,
"invalid expression");
898 struct LvalueExprVisitor {
903 LvalueExprVisitor(
Context &context, Location loc)
904 : context(context), loc(loc), builder(context.builder) {}
907 Value visit(
const slang::ast::NamedValueExpression &expr) {
908 if (
auto value = context.
valueSymbols.lookup(&expr.symbol))
910 auto d = mlir::emitError(loc,
"unknown name `") << expr.symbol.name <<
"`";
917 Value visit(
const slang::ast::HierarchicalValueExpression &expr) {
918 if (
auto value = context.
valueSymbols.lookup(&expr.symbol))
923 auto d = mlir::emitError(loc,
"unknown hierarchical name `")
924 << expr.symbol.name <<
"`";
931 Value visit(
const slang::ast::ConcatenationExpression &expr) {
932 SmallVector<Value> operands;
933 for (
auto *operand : expr.operands()) {
937 operands.push_back(value);
939 return builder.create<moore::ConcatRefOp>(loc, operands);
943 Value visit(
const slang::ast::ElementSelectExpression &expr) {
948 if (
auto *constValue = expr.selector().constant) {
949 assert(!constValue->hasUnknown());
950 assert(constValue->size() <= 32);
952 auto lowBit = constValue->integer().as<uint32_t>().value();
953 return builder.create<moore::ExtractRefOp>(
960 return builder.create<moore::DynExtractRefOp>(
966 Value visit(
const slang::ast::RangeSelectExpression &expr) {
973 uint32_t constLowBit;
974 auto *leftConst = expr.left().constant;
975 auto *rightConst = expr.right().constant;
977 assert(!leftConst->hasUnknown());
978 assert(leftConst->size() <= 32);
981 assert(!rightConst->hasUnknown());
982 assert(rightConst->size() <= 32);
985 if (expr.getSelectionKind() == slang::ast::RangeSelectionKind::Simple) {
986 if (leftConst && rightConst) {
988 auto lhs = leftConst->integer().as<uint32_t>().value();
989 auto rhs = rightConst->integer().as<uint32_t>().value();
990 constLowBit = lhs < rhs ? lhs : rhs;
992 mlir::emitError(loc,
"unsupported a variable as the index in the")
996 }
else if (expr.getSelectionKind() ==
997 slang::ast::RangeSelectionKind::IndexedDown) {
1002 auto subtrahend = leftConst->integer().as<uint32_t>().value();
1004 expr.right().constant->integer().as<uint32_t>().value();
1005 constLowBit = subtrahend - sliceWidth - 1;
1008 auto subtrahendType = cast<moore::UnpackedType>(subtrahend.getType());
1010 subtrahendType.getBitSize().value(),
1011 subtrahendType.getDomain());
1013 expr.right().constant->integer().as<uint32_t>().value() - 1;
1015 builder.create<moore::ConstantOp>(loc, intType, sliceWidth);
1016 dynLowBit = builder.create<moore::SubOp>(loc, subtrahend, minuend);
1021 constLowBit = leftConst->integer().as<uint32_t>().value();
1025 if (leftConst && rightConst)
1026 return builder.create<moore::ExtractRefOp>(
1029 return builder.create<moore::DynExtractRefOp>(
1034 Value visit(
const slang::ast::StreamingConcatenationExpression &expr) {
1035 SmallVector<Value> operands;
1036 for (
auto stream : expr.streams()) {
1037 auto operandLoc = context.
convertLocation(stream.operand->sourceRange);
1038 if (!stream.constantWithWidth.has_value() && stream.withExpr) {
1039 mlir::emitError(operandLoc)
1040 <<
"Moore only support streaming "
1041 "concatenation with fixed size 'with expression'";
1045 if (stream.constantWithWidth.has_value()) {
1047 auto type = cast<moore::UnpackedType>(
1048 cast<moore::RefType>(value.getType()).getNestedType());
1050 context.
getContext(), type.getBitSize().value(), type.getDomain()));
1059 operands.push_back(value);
1062 if (operands.size() == 1) {
1065 value = operands.front();
1067 value = builder.create<moore::ConcatRefOp>(loc, operands).getResult();
1070 if (expr.sliceSize == 0) {
1074 auto type = cast<moore::IntType>(
1075 cast<moore::RefType>(value.getType()).getNestedType());
1076 SmallVector<Value> slicedOperands;
1077 auto widthSum = type.getWidth();
1078 auto domain = type.getDomain();
1079 auto iterMax = widthSum / expr.sliceSize;
1080 auto remainSize = widthSum % expr.sliceSize;
1082 for (
size_t i = 0; i < iterMax; i++) {
1086 auto extracted = builder.create<moore::ExtractRefOp>(
1087 loc, extractResultType, value, i * expr.sliceSize);
1088 slicedOperands.push_back(extracted);
1095 auto extracted = builder.create<moore::ExtractRefOp>(
1096 loc, extractResultType, value, iterMax * expr.sliceSize);
1097 slicedOperands.push_back(extracted);
1100 return builder.create<moore::ConcatRefOp>(loc, slicedOperands);
1103 Value visit(
const slang::ast::MemberAccessExpression &expr) {
1105 auto valueType = expr.value().type;
1107 if (!type || !value)
1109 if (valueType->isStruct()) {
1110 return builder.create<moore::StructExtractRefOp>(
1112 builder.getStringAttr(expr.member.name), value);
1114 if (valueType->isPackedUnion() || valueType->isUnpackedUnion()) {
1115 return builder.create<moore::UnionExtractRefOp>(
1117 builder.getStringAttr(expr.member.name), value);
1119 mlir::emitError(loc,
"expression of type ")
1120 << value.getType() <<
" cannot be accessed";
1125 template <
typename T>
1126 Value visit(T &&node) {
1130 Value visitInvalid(
const slang::ast::Expression &expr) {
1131 mlir::emitError(loc,
"invalid expression");
1138 Type requiredType) {
1140 auto value = expr.visit(RvalueExprVisitor(*
this, loc));
1141 if (value && requiredType)
1149 return expr.visit(LvalueExprVisitor(*
this, loc));
1157 if (
auto type = dyn_cast_or_null<moore::IntType>(value.getType()))
1158 if (type.getBitSize() == 1)
1160 if (
auto type = dyn_cast_or_null<moore::UnpackedType>(value.getType()))
1161 return builder.create<moore::BoolCastOp>(value.getLoc(), value);
1162 mlir::emitError(value.getLoc(),
"expression of type ")
1163 << value.getType() <<
" cannot be cast to a boolean";
1169 const slang::ast::Type &astType, Location loc) {
1174 bool typeIsFourValued =
false;
1175 if (
auto unpackedType = dyn_cast<moore::UnpackedType>(type))
1180 fvint.hasUnknown() || typeIsFourValued
1183 Value result =
builder.create<moore::ConstantOp>(loc, intType, fvint);
1184 if (result.getType() != type)
1185 result =
builder.create<moore::ConversionOp>(loc, type, result);
1190 const slang::ast::Type &type, Location loc) {
1191 if (constant.isInteger())
1196 slang::ConstantValue
1198 using slang::ast::EvalFlags;
1199 slang::ast::EvalContext evalContext(
1200 compilation, EvalFlags::CacheResults | EvalFlags::SpecparamsAllowed);
1201 return expr.eval(evalContext);
1211 if (value.getType() == type)
1213 return builder.create<moore::ConversionOp>(value.getLoc(), type, value);
1219 if (isa<moore::IntType>(value.getType()))
1226 if (
auto packed = dyn_cast<moore::PackedType>(value.getType())) {
1227 if (
auto bits = packed.getBitSize()) {
1230 return builder.create<moore::ConversionOp>(value.getLoc(), sbvType,
1235 mlir::emitError(value.getLoc()) <<
"expression of type " << value.getType()
1236 <<
" cannot be cast to a simple bit vector";
1242 if (type == value.getType())
1244 auto dstPacked = dyn_cast<moore::PackedType>(type);
1245 auto srcPacked = dyn_cast<moore::PackedType>(value.getType());
1248 if (dstPacked && srcPacked && dstPacked.getBitSize() &&
1249 srcPacked.getBitSize() &&
1250 *dstPacked.getBitSize() != *srcPacked.getBitSize()) {
1251 auto dstWidth = *dstPacked.getBitSize();
1252 auto srcWidth = *srcPacked.getBitSize();
1256 srcPacked.getDomain());
1257 if (value.getType() != srcWidthType)
1258 value =
builder.create<moore::ConversionOp>(value.getLoc(), srcWidthType,
1264 srcPacked.getDomain());
1265 if (dstWidth < srcWidth) {
1266 value =
builder.create<moore::TruncOp>(loc, dstWidthType, value);
1267 }
else if (dstWidth > srcWidth) {
1269 value =
builder.create<moore::SExtOp>(loc, dstWidthType, value);
1271 value =
builder.create<moore::ZExtOp>(loc, dstWidthType, value);
1275 if (value.getType() != type)
1276 value =
builder.create<moore::ConversionOp>(loc, type, value);
assert(baseType &&"element must be base type")
static FVInt convertSVIntToFVInt(const slang::SVInt &svint)
Convert a Slang SVInt to a CIRCT FVInt.
const char * toString(Flow flow)
Four-valued arbitrary precision integers.
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
Domain
The number of values each bit of a type can assume.
@ FourValued
Four-valued types such as logic or integer.
@ TwoValued
Two-valued types such as bit or int.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
A helper class to facilitate the conversion from a Slang AST to MLIR operations.
Value materializeConversion(Type type, Value value, bool isSigned, Location loc)
Helper function to insert the necessary operations to cast a value from one type to another.
Value convertLvalueExpression(const slang::ast::Expression &expr)
Value materializeConstant(const slang::ConstantValue &constant, const slang::ast::Type &type, Location loc)
Helper function to materialize a ConstantValue as an SSA value.
slang::ConstantValue evaluateConstant(const slang::ast::Expression &expr)
Evaluate the constant value of an expression.
slang::ast::Compilation & compilation
OpBuilder builder
The builder used to create IR operations.
std::function< void(moore::ReadOp)> rvalueReadCallback
A listener called for every variable or net being read.
Type convertType(const slang::ast::Type &type, LocationAttr loc={})
Convert a slang type into an MLIR type.
Value materializeSVInt(const slang::SVInt &svint, const slang::ast::Type &type, Location loc)
Helper function to materialize an SVInt as an SSA value.
Value convertToBool(Value value)
Helper function to convert a value to its "truthy" boolean value.
ValueSymbols valueSymbols
Value convertRvalueExpression(const slang::ast::Expression &expr, Type requiredType={})
Value convertToSimpleBitVector(Value value)
Helper function to convert a value to its simple bit vector representation, if it has one.
FunctionLowering * declareFunction(const slang::ast::SubroutineSymbol &subroutine)
Convert a function and its arguments to a function declaration in the IR.
MLIRContext * getContext()
Return the MLIR context.
SmallVector< Value > lvalueStack
A stack of assignment left-hand side values.
Location convertLocation(slang::SourceLocation loc)
Convert a slang SourceLocation into an MLIR Location.