CIRCT 22.0.0git
Loading...
Searching...
No Matches
Expressions.cpp
Go to the documentation of this file.
1//===- Expressions.cpp - Slang expression conversion ----------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
11#include "mlir/IR/Operation.h"
12#include "mlir/IR/Value.h"
13#include "slang/ast/EvalContext.h"
14#include "slang/ast/SystemSubroutine.h"
15#include "slang/ast/types/AllTypes.h"
16#include "slang/syntax/AllSyntax.h"
17#include "llvm/ADT/ScopeExit.h"
18
19using namespace circt;
20using namespace ImportVerilog;
21using moore::Domain;
22
23/// Convert a Slang `SVInt` to a CIRCT `FVInt`.
24static FVInt convertSVIntToFVInt(const slang::SVInt &svint) {
25 if (svint.hasUnknown()) {
26 unsigned numWords = svint.getNumWords() / 2;
27 auto value = ArrayRef<uint64_t>(svint.getRawPtr(), numWords);
28 auto unknown = ArrayRef<uint64_t>(svint.getRawPtr() + numWords, numWords);
29 return FVInt(APInt(svint.getBitWidth(), value),
30 APInt(svint.getBitWidth(), unknown));
31 }
32 auto value = ArrayRef<uint64_t>(svint.getRawPtr(), svint.getNumWords());
33 return FVInt(APInt(svint.getBitWidth(), value));
34}
35
36/// Map an index into an array, with bounds `range`, to a bit offset of the
37/// underlying bit storage. This is a dynamic version of
38/// `slang::ConstantRange::translateIndex`.
39static Value getSelectIndex(Context &context, Location loc, Value index,
40 const slang::ConstantRange &range) {
41 auto &builder = context.builder;
42 auto indexType = cast<moore::UnpackedType>(index.getType());
43
44 // Compute offset first so we know if it is negative.
45 auto lo = range.lower();
46 auto hi = range.upper();
47 auto offset = range.isLittleEndian() ? lo : hi;
48
49 // If any bound is negative we need a signed index type.
50 const bool needSigned = (lo < 0) || (hi < 0);
51
52 // Magnitude over full range, not just the chosen offset.
53 const uint64_t maxAbs = std::max<uint64_t>(std::abs(lo), std::abs(hi));
54
55 // Bits needed from the range:
56 // - unsigned: ceil(log2(maxAbs + 1)) (ensure at least 1)
57 // - signed: ceil(log2(maxAbs)) + 1 sign bit (ensure at least 2 when neg)
58 unsigned want = needSigned
59 ? (llvm::Log2_64_Ceil(std::max<uint64_t>(1, maxAbs)) + 1)
60 : std::max<unsigned>(1, llvm::Log2_64_Ceil(maxAbs + 1));
61
62 // Keep at least as wide as the incoming index.
63 const unsigned bw = std::max<unsigned>(want, indexType.getBitSize().value());
64
65 auto intType =
66 moore::IntType::get(index.getContext(), bw, indexType.getDomain());
67 index = context.materializeConversion(intType, index, needSigned, loc);
68
69 if (offset == 0) {
70 if (range.isLittleEndian())
71 return index;
72 else
73 return moore::NegOp::create(builder, loc, index);
74 }
75
76 auto offsetConst =
77 moore::ConstantOp::create(builder, loc, intType, offset, needSigned);
78 if (range.isLittleEndian())
79 return moore::SubOp::create(builder, loc, index, offsetConst);
80 else
81 return moore::SubOp::create(builder, loc, offsetConst, index);
82}
83
84/// Get the currently active timescale as an integer number of femtoseconds.
86 static_assert(int(slang::TimeUnit::Seconds) == 0);
87 static_assert(int(slang::TimeUnit::Milliseconds) == 1);
88 static_assert(int(slang::TimeUnit::Microseconds) == 2);
89 static_assert(int(slang::TimeUnit::Nanoseconds) == 3);
90 static_assert(int(slang::TimeUnit::Picoseconds) == 4);
91 static_assert(int(slang::TimeUnit::Femtoseconds) == 5);
92
93 static_assert(int(slang::TimeScaleMagnitude::One) == 1);
94 static_assert(int(slang::TimeScaleMagnitude::Ten) == 10);
95 static_assert(int(slang::TimeScaleMagnitude::Hundred) == 100);
96
97 auto exp = static_cast<unsigned>(context.timeScale.base.unit);
98 assert(exp <= 5);
99 exp = 5 - exp;
100 auto scale = static_cast<uint64_t>(context.timeScale.base.magnitude);
101 while (exp-- > 0)
102 scale *= 1000;
103 return scale;
104}
105
107 const slang::ast::ClassPropertySymbol &expr) {
108 auto loc = context.convertLocation(expr.location);
109 auto builder = context.builder;
110 auto type = context.convertType(expr.getType());
111 auto fieldTy = cast<moore::UnpackedType>(type);
112 auto fieldRefTy = moore::RefType::get(fieldTy);
113
114 if (expr.lifetime == slang::ast::VariableLifetime::Static) {
115
116 // Variable may or may not have been hoisted already. Hoist if not.
117 if (!context.globalVariables.lookup(&expr)) {
118 if (failed(context.convertGlobalVariable(expr))) {
119 return {};
120 }
121 }
122 // Try the static variable after it has been hoisted.
123 if (auto globalOp = context.globalVariables.lookup(&expr))
124 return moore::GetGlobalVariableOp::create(builder, loc, globalOp);
125
126 mlir::emitError(loc) << "Failed to access static member variable "
127 << expr.name << " as a global variable";
128 return {};
129 }
130
131 // Get the scope's implicit this variable
132 mlir::Value instRef = context.getImplicitThisRef();
133 if (!instRef) {
134 mlir::emitError(loc) << "class property '" << expr.name
135 << "' referenced without an implicit 'this'";
136 return {};
137 }
138
139 auto fieldSym = mlir::FlatSymbolRefAttr::get(builder.getContext(), expr.name);
140
141 moore::ClassHandleType classTy =
142 cast<moore::ClassHandleType>(instRef.getType());
143
144 auto targetClassHandle =
145 context.getAncestorClassWithProperty(classTy, expr.name, loc);
146 if (!targetClassHandle)
147 return {};
148
149 auto upcastRef = context.materializeConversion(targetClassHandle, instRef,
150 false, instRef.getLoc());
151 if (!upcastRef)
152 return {};
153
154 Value fieldRef = moore::ClassPropertyRefOp::create(builder, loc, fieldRefTy,
155 upcastRef, fieldSym);
156 return fieldRef;
157}
158
159namespace {
160/// A visitor handling expressions that can be lowered as lvalue and rvalue.
161struct ExprVisitor {
162 Context &context;
163 Location loc;
164 OpBuilder &builder;
165 bool isLvalue;
166
167 ExprVisitor(Context &context, Location loc, bool isLvalue)
168 : context(context), loc(loc), builder(context.builder),
169 isLvalue(isLvalue) {}
170
171 /// Convert an expression either as an lvalue or rvalue, depending on whether
172 /// this is an lvalue or rvalue visitor. This is useful for projections such
173 /// as `a[i]`, where you want `a` as an lvalue if you want `a[i]` as an
174 /// lvalue, or `a` as an rvalue if you want `a[i]` as an rvalue.
175 Value convertLvalueOrRvalueExpression(const slang::ast::Expression &expr) {
176 if (isLvalue)
177 return context.convertLvalueExpression(expr);
178 return context.convertRvalueExpression(expr);
179 }
180
181 /// Handle single bit selections.
182 Value visit(const slang::ast::ElementSelectExpression &expr) {
183 auto type = context.convertType(*expr.type);
184 auto value = convertLvalueOrRvalueExpression(expr.value());
185 if (!type || !value)
186 return {};
187
188 // We only support indexing into a few select types for now.
189 auto derefType = value.getType();
190 if (isLvalue)
191 derefType = cast<moore::RefType>(derefType).getNestedType();
192
193 if (!isa<moore::IntType, moore::ArrayType, moore::UnpackedArrayType,
194 moore::QueueType>(derefType)) {
195 mlir::emitError(loc) << "unsupported expression: element select into "
196 << expr.value().type->toString() << "\n";
197 return {};
198 }
199
200 auto resultType =
201 isLvalue ? moore::RefType::get(cast<moore::UnpackedType>(type)) : type;
202 auto range = expr.value().type->getFixedRange();
203 if (auto *constValue = expr.selector().getConstant();
204 constValue && constValue->isInteger()) {
205 assert(!constValue->hasUnknown());
206 assert(constValue->size() <= 32);
207
208 auto lowBit = constValue->integer().as<uint32_t>().value();
209 if (isLvalue)
210 return llvm::TypeSwitch<Type, Value>(derefType)
211 .Case<moore::QueueType>([&](moore::QueueType) {
212 mlir::emitError(loc)
213 << "Unexpected LValue extract on Queue Type!";
214 return Value();
215 })
216 .Default([&](Type) {
217 return moore::ExtractRefOp::create(builder, loc, resultType,
218 value,
219 range.translateIndex(lowBit));
220 });
221 else
222 return llvm::TypeSwitch<Type, Value>(derefType)
223 .Case<moore::QueueType>([&](moore::QueueType) {
224 mlir::emitError(loc)
225 << "Unexpected RValue extract on Queue Type!";
226 return Value();
227 })
228 .Default([&](Type) {
229 return moore::ExtractOp::create(builder, loc, resultType, value,
230 range.translateIndex(lowBit));
231 });
232 }
233
234 auto lowBit = context.convertRvalueExpression(expr.selector());
235 if (!lowBit)
236 return {};
237 lowBit = getSelectIndex(context, loc, lowBit, range);
238 if (isLvalue)
239 return llvm::TypeSwitch<Type, Value>(derefType)
240 .Case<moore::QueueType>([&](moore::QueueType) {
241 return moore::DynQueueExtractRefOp::create(builder, loc, resultType,
242 value, lowBit);
243 })
244 .Default([&](Type) {
245 return moore::DynExtractRefOp::create(builder, loc, resultType,
246 value, lowBit);
247 });
248
249 else
250 return llvm::TypeSwitch<Type, Value>(derefType)
251 .Case<moore::QueueType>([&](moore::QueueType) {
252 return moore::DynQueueExtractOp::create(builder, loc, resultType,
253 value, lowBit);
254 })
255 .Default([&](Type) {
256 return moore::DynExtractOp::create(builder, loc, resultType, value,
257 lowBit);
258 });
259 }
260
261 /// Handle null assignments to variables.
262 /// Compare with IEEE 1800-2023 Table 6-7 - Default variable initial values
263 Value visit(const slang::ast::NullLiteral &expr) {
264 auto type = context.convertType(*expr.type);
265 if (isa<moore::ClassHandleType, moore::ChandleType, moore::EventType,
266 moore::NullType>(type))
267 return moore::NullOp::create(builder, loc);
268 mlir::emitError(loc) << "No null value definition found for value of type "
269 << type;
270 return {};
271 }
272
273 /// Handle range bit selections.
274 Value visit(const slang::ast::RangeSelectExpression &expr) {
275 auto type = context.convertType(*expr.type);
276 auto value = convertLvalueOrRvalueExpression(expr.value());
277 if (!type || !value)
278 return {};
279
280 std::optional<int32_t> constLeft;
281 std::optional<int32_t> constRight;
282 if (auto *constant = expr.left().getConstant())
283 constLeft = constant->integer().as<int32_t>();
284 if (auto *constant = expr.right().getConstant())
285 constRight = constant->integer().as<int32_t>();
286
287 // We currently require the right-hand-side of the range to be constant.
288 // This catches things like `[42:$]` which we don't support at the moment.
289 if (!constRight) {
290 mlir::emitError(loc)
291 << "unsupported expression: range select with non-constant bounds";
292 return {};
293 }
294
295 // We need to determine the right bound of the range. This is the address of
296 // the least significant bit of the underlying bit storage, which is the
297 // offset we want to pass to the extract op.
298 //
299 // The arrays [6:2] and [2:6] both have 5 bits worth of underlying storage.
300 // The left and right bound of the range only determine the addressing
301 // scheme of the storage bits:
302 //
303 // Storage bits: 4 3 2 1 0 <-- extract op works on storage bits
304 // [6:2] indices: 6 5 4 3 2 ("little endian" in Slang terms)
305 // [2:6] indices: 2 3 4 5 6 ("big endian" in Slang terms)
306 //
307 // Before we can extract, we need to map the range select left and right
308 // bounds from these indices to actual bit positions in the storage.
309
310 Value offsetDyn;
311 int32_t offsetConst = 0;
312 auto range = expr.value().type->getFixedRange();
313
314 using slang::ast::RangeSelectionKind;
315 if (expr.getSelectionKind() == RangeSelectionKind::Simple) {
316 // For a constant range [a:b], we want the offset of the lowest storage
317 // bit from which we are starting the extract. For a range [5:3] this is
318 // bit index 3; for a range [3:5] this is bit index 5. Both of these are
319 // later translated map to bit offset 1 (see bit indices above).
320 assert(constRight && "constness checked in slang");
321 offsetConst = *constRight;
322 } else {
323 // For an indexed range [a+:b] or [a-:b], determining the lowest storage
324 // bit is a bit more complicated. We start out with the base index `a`.
325 // This is the lower *index* of the range, but not the lower *storage bit
326 // position*.
327 //
328 // The range [a+:b] expands to [a+b-1:a] for a [6:2] range, or [a:a+b-1]
329 // for a [2:6] range. The range [a-:b] expands to [a:a-b+1] for a [6:2]
330 // range, or [a-b+1:a] for a [2:6] range.
331 if (constLeft) {
332 offsetConst = *constLeft;
333 } else {
334 offsetDyn = context.convertRvalueExpression(expr.left());
335 if (!offsetDyn)
336 return {};
337 }
338
339 // For a [a-:b] select on [2:6] and a [a+:b] select on [6:2], the range
340 // expands to [a-b+1:a] and [a+b-1:a]. In this case, the right bound which
341 // corresponds to the lower *storage bit offset*, is just `a` and there's
342 // no further tweaking to do.
343 int32_t offsetAdd = 0;
344
345 // For a [a-:b] select on [6:2], the range expands to [a:a-b+1]. We
346 // therefore have to take the `a` from above and adjust it by `-b+1` to
347 // arrive at the right bound.
348 if (expr.getSelectionKind() == RangeSelectionKind::IndexedDown &&
349 range.isLittleEndian()) {
350 assert(constRight && "constness checked in slang");
351 offsetAdd = 1 - *constRight;
352 }
353
354 // For a [a+:b] select on [2:6], the range expands to [a:a+b-1]. We
355 // therefore have to take the `a` from above and adjust it by `+b-1` to
356 // arrive at the right bound.
357 if (expr.getSelectionKind() == RangeSelectionKind::IndexedUp &&
358 !range.isLittleEndian()) {
359 assert(constRight && "constness checked in slang");
360 offsetAdd = *constRight - 1;
361 }
362
363 // Adjust the offset such that it matches the right bound of the range.
364 if (offsetAdd != 0) {
365 if (offsetDyn)
366 offsetDyn = moore::AddOp::create(
367 builder, loc, offsetDyn,
368 moore::ConstantOp::create(
369 builder, loc, cast<moore::IntType>(offsetDyn.getType()),
370 offsetAdd,
371 /*isSigned=*/offsetAdd < 0));
372 else
373 offsetConst += offsetAdd;
374 }
375 }
376
377 // Create a dynamic or constant extract. Use `getSelectIndex` and
378 // `ConstantRange::translateIndex` to map from the bit indices provided by
379 // the user to the actual storage bit position. Since `offset*` corresponds
380 // to the right bound of the range, which provides the index of the least
381 // significant selected storage bit, we get the bit offset at which we want
382 // to start extracting.
383 auto resultType =
384 isLvalue ? moore::RefType::get(cast<moore::UnpackedType>(type)) : type;
385
386 if (offsetDyn) {
387 offsetDyn = getSelectIndex(context, loc, offsetDyn, range);
388 if (isLvalue) {
389 return moore::DynExtractRefOp::create(builder, loc, resultType, value,
390 offsetDyn);
391 } else {
392 return moore::DynExtractOp::create(builder, loc, resultType, value,
393 offsetDyn);
394 }
395 } else {
396 offsetConst = range.translateIndex(offsetConst);
397 if (isLvalue) {
398 return moore::ExtractRefOp::create(builder, loc, resultType, value,
399 offsetConst);
400 } else {
401 return moore::ExtractOp::create(builder, loc, resultType, value,
402 offsetConst);
403 }
404 }
405 }
406
407 /// Handle concatenations.
408 Value visit(const slang::ast::ConcatenationExpression &expr) {
409 SmallVector<Value> operands;
410 if (expr.type->isString()) {
411 for (auto *operand : expr.operands()) {
412 assert(!isLvalue && "checked by Slang");
413 auto value = convertLvalueOrRvalueExpression(*operand);
414 if (!value)
415 return {};
416 value = context.materializeConversion(
417 moore::StringType::get(context.getContext()), value, false,
418 value.getLoc());
419 if (!value)
420 return {};
421 operands.push_back(value);
422 }
423 return moore::StringConcatOp::create(builder, loc, operands);
424 }
425 for (auto *operand : expr.operands()) {
426 // Handle empty replications like `{0{...}}` which may occur within
427 // concatenations. Slang assigns them a `void` type which we can check for
428 // here.
429 if (operand->type->isVoid())
430 continue;
431 auto value = convertLvalueOrRvalueExpression(*operand);
432 if (!value)
433 return {};
434 if (!isLvalue)
435 value = context.convertToSimpleBitVector(value);
436 if (!value)
437 return {};
438 operands.push_back(value);
439 }
440 if (isLvalue)
441 return moore::ConcatRefOp::create(builder, loc, operands);
442 else
443 return moore::ConcatOp::create(builder, loc, operands);
444 }
445
446 /// Handle member accesses.
447 Value visit(const slang::ast::MemberAccessExpression &expr) {
448 auto type = context.convertType(*expr.type);
449 if (!type)
450 return {};
451
452 auto *valueType = expr.value().type.get();
453 auto memberName = builder.getStringAttr(expr.member.name);
454
455 // Handle structs.
456 if (valueType->isStruct()) {
457 auto resultType =
458 isLvalue ? moore::RefType::get(cast<moore::UnpackedType>(type))
459 : type;
460 auto value = convertLvalueOrRvalueExpression(expr.value());
461 if (!value)
462 return {};
463
464 if (isLvalue)
465 return moore::StructExtractRefOp::create(builder, loc, resultType,
466 memberName, value);
467 return moore::StructExtractOp::create(builder, loc, resultType,
468 memberName, value);
469 }
470
471 // Handle unions.
472 if (valueType->isPackedUnion() || valueType->isUnpackedUnion()) {
473 auto resultType =
474 isLvalue ? moore::RefType::get(cast<moore::UnpackedType>(type))
475 : type;
476 auto value = convertLvalueOrRvalueExpression(expr.value());
477 if (!value)
478 return {};
479
480 if (isLvalue)
481 return moore::UnionExtractRefOp::create(builder, loc, resultType,
482 memberName, value);
483 return moore::UnionExtractOp::create(builder, loc, type, memberName,
484 value);
485 }
486
487 // Handle classes.
488 if (valueType->isClass()) {
489 auto valTy = context.convertType(*valueType);
490 if (!valTy)
491 return {};
492 auto targetTy = cast<moore::ClassHandleType>(valTy);
493
494 // `MemberAccessExpression`s may refer to either variables that may or may
495 // not be compile time constants, or to class parameters which are always
496 // elaboration-time constant.
497 //
498 // We distinguish these cases, and materialize a runtime member access
499 // for variables, but force constant conversion for parameter accesses.
500 //
501 // Also see this discussion:
502 // https://github.com/MikePopoloski/slang/issues/1641
503
504 if (expr.member.kind != slang::ast::SymbolKind::Parameter) {
505
506 // We need to pick the closest ancestor that declares a property with
507 // the relevant name. System Verilog explicitly enforces lexical
508 // shadowing, as shown in IEEE 1800-2023 Section 8.14 "Overridden
509 // members".
510 moore::ClassHandleType upcastTargetTy =
511 context.getAncestorClassWithProperty(targetTy, expr.member.name,
512 loc);
513 if (!upcastTargetTy)
514 return {};
515
516 // Convert the class handle to the required target type for property
517 // shadowing purposes.
518 Value baseVal =
519 context.convertRvalueExpression(expr.value(), upcastTargetTy);
520 if (!baseVal)
521 return {};
522
523 // @field and result type !moore.ref<T>.
524 auto fieldSym = mlir::FlatSymbolRefAttr::get(builder.getContext(),
525 expr.member.name);
526 auto fieldRefTy = moore::RefType::get(cast<moore::UnpackedType>(type));
527
528 // Produce a ref to the class property from the (possibly upcast)
529 // handle.
530 Value fieldRef = moore::ClassPropertyRefOp::create(
531 builder, loc, fieldRefTy, baseVal, fieldSym);
532
533 // If we need an RValue, read the reference, otherwise return
534 return isLvalue ? fieldRef
535 : moore::ReadOp::create(builder, loc, fieldRef);
536 }
537
538 slang::ConstantValue constVal;
539 if (auto param = expr.member.as_if<slang::ast::ParameterSymbol>()) {
540 constVal = param->getValue();
541 if (auto value = context.materializeConstant(constVal, *expr.type, loc))
542 return value;
543 }
544
545 mlir::emitError(loc) << "Parameter " << expr.member.name
546 << " has no constant value";
547 return {};
548 }
549
550 mlir::emitError(loc, "expression of type ")
551 << valueType->toString() << " has no member fields";
552 return {};
553 }
554};
555} // namespace
556
557//===----------------------------------------------------------------------===//
558// Rvalue Conversion
559//===----------------------------------------------------------------------===//
560
561// NOLINTBEGIN(misc-no-recursion)
562namespace {
563struct RvalueExprVisitor : public ExprVisitor {
564 RvalueExprVisitor(Context &context, Location loc)
565 : ExprVisitor(context, loc, /*isLvalue=*/false) {}
566 using ExprVisitor::visit;
567
568 // Handle references to the left-hand side of a parent assignment.
569 Value visit(const slang::ast::LValueReferenceExpression &expr) {
570 assert(!context.lvalueStack.empty() && "parent assignments push lvalue");
571 auto lvalue = context.lvalueStack.back();
572 return moore::ReadOp::create(builder, loc, lvalue);
573 }
574
575 // Handle named values, such as references to declared variables.
576 Value visit(const slang::ast::NamedValueExpression &expr) {
577 // Handle local variables.
578 if (auto value = context.valueSymbols.lookup(&expr.symbol)) {
579 if (isa<moore::RefType>(value.getType())) {
580 auto readOp = moore::ReadOp::create(builder, loc, value);
581 if (context.rvalueReadCallback)
582 context.rvalueReadCallback(readOp);
583 value = readOp.getResult();
584 }
585 return value;
586 }
587
588 // Handle global variables.
589 if (auto globalOp = context.globalVariables.lookup(&expr.symbol)) {
590 auto value = moore::GetGlobalVariableOp::create(builder, loc, globalOp);
591 return moore::ReadOp::create(builder, loc, value);
592 }
593
594 // We're reading a class property.
595 if (auto *const property =
596 expr.symbol.as_if<slang::ast::ClassPropertySymbol>()) {
597 auto fieldRef = visitClassProperty(context, *property);
598 return moore::ReadOp::create(builder, loc, fieldRef).getResult();
599 }
600
601 // Try to materialize constant values directly.
602 auto constant = context.evaluateConstant(expr);
603 if (auto value = context.materializeConstant(constant, *expr.type, loc))
604 return value;
605
606 // Otherwise some other part of ImportVerilog should have added an MLIR
607 // value for this expression's symbol to the `context.valueSymbols` table.
608 auto d = mlir::emitError(loc, "unknown name `") << expr.symbol.name << "`";
609 d.attachNote(context.convertLocation(expr.symbol.location))
610 << "no rvalue generated for " << slang::ast::toString(expr.symbol.kind);
611 return {};
612 }
613
614 // Handle hierarchical values, such as `x = Top.sub.var`.
615 Value visit(const slang::ast::HierarchicalValueExpression &expr) {
616 auto hierLoc = context.convertLocation(expr.symbol.location);
617 if (auto value = context.valueSymbols.lookup(&expr.symbol)) {
618 if (isa<moore::RefType>(value.getType())) {
619 auto readOp = moore::ReadOp::create(builder, hierLoc, value);
620 if (context.rvalueReadCallback)
621 context.rvalueReadCallback(readOp);
622 value = readOp.getResult();
623 }
624 return value;
625 }
626
627 // Emit an error for those hierarchical values not recorded in the
628 // `valueSymbols`.
629 auto d = mlir::emitError(loc, "unknown hierarchical name `")
630 << expr.symbol.name << "`";
631 d.attachNote(hierLoc) << "no rvalue generated for "
632 << slang::ast::toString(expr.symbol.kind);
633 return {};
634 }
635
636 // Handle type conversions (explicit and implicit).
637 Value visit(const slang::ast::ConversionExpression &expr) {
638 auto type = context.convertType(*expr.type);
639 if (!type)
640 return {};
641 return context.convertRvalueExpression(expr.operand(), type);
642 }
643
644 // Handle blocking and non-blocking assignments.
645 Value visit(const slang::ast::AssignmentExpression &expr) {
646 auto lhs = context.convertLvalueExpression(expr.left());
647 if (!lhs)
648 return {};
649
650 // Determine the right-hand side value of the assignment.
651 context.lvalueStack.push_back(lhs);
652 auto rhs = context.convertRvalueExpression(
653 expr.right(), cast<moore::RefType>(lhs.getType()).getNestedType());
654 context.lvalueStack.pop_back();
655 if (!rhs)
656 return {};
657
658 // If this is a blocking assignment, we can insert the delay/wait ops of the
659 // optional timing control directly in between computing the RHS and
660 // executing the assignment.
661 if (!expr.isNonBlocking()) {
662 if (expr.timingControl)
663 if (failed(context.convertTimingControl(*expr.timingControl)))
664 return {};
665 auto assignOp = moore::BlockingAssignOp::create(builder, loc, lhs, rhs);
666 if (context.variableAssignCallback)
667 context.variableAssignCallback(assignOp);
668 return rhs;
669 }
670
671 // For non-blocking assignments, we only support time delays for now.
672 if (expr.timingControl) {
673 // Handle regular time delays.
674 if (auto *ctrl = expr.timingControl->as_if<slang::ast::DelayControl>()) {
675 auto delay = context.convertRvalueExpression(
676 ctrl->expr, moore::TimeType::get(builder.getContext()));
677 if (!delay)
678 return {};
679 auto assignOp = moore::DelayedNonBlockingAssignOp::create(
680 builder, loc, lhs, rhs, delay);
681 if (context.variableAssignCallback)
682 context.variableAssignCallback(assignOp);
683 return rhs;
684 }
685
686 // All other timing controls are not supported.
687 auto loc = context.convertLocation(expr.timingControl->sourceRange);
688 mlir::emitError(loc)
689 << "unsupported non-blocking assignment timing control: "
690 << slang::ast::toString(expr.timingControl->kind);
691 return {};
692 }
693 auto assignOp = moore::NonBlockingAssignOp::create(builder, loc, lhs, rhs);
694 if (context.variableAssignCallback)
695 context.variableAssignCallback(assignOp);
696 return rhs;
697 }
698
699 // Helper function to convert an argument to a simple bit vector type, pass it
700 // to a reduction op, and optionally invert the result.
701 template <class ConcreteOp>
702 Value createReduction(Value arg, bool invert) {
703 arg = context.convertToSimpleBitVector(arg);
704 if (!arg)
705 return {};
706 Value result = ConcreteOp::create(builder, loc, arg);
707 if (invert)
708 result = moore::NotOp::create(builder, loc, result);
709 return result;
710 }
711
712 // Helper function to create pre and post increments and decrements.
713 Value createIncrement(Value arg, bool isInc, bool isPost) {
714 auto preValue = moore::ReadOp::create(builder, loc, arg);
715 Value postValue;
716 // Catch the special case where a signed 1 bit value (i1) is incremented,
717 // as +1 can not be expressed as a signed 1 bit value. For any 1-bit number
718 // negating is equivalent to incrementing.
719 if (moore::isIntType(preValue.getType(), 1)) {
720 postValue = moore::NotOp::create(builder, loc, preValue).getResult();
721 } else {
722
723 auto one = moore::ConstantOp::create(
724 builder, loc, cast<moore::IntType>(preValue.getType()), 1);
725 postValue =
726 isInc ? moore::AddOp::create(builder, loc, preValue, one).getResult()
727 : moore::SubOp::create(builder, loc, preValue, one).getResult();
728 auto assignOp =
729 moore::BlockingAssignOp::create(builder, loc, arg, postValue);
730 if (context.variableAssignCallback)
731 context.variableAssignCallback(assignOp);
732 }
733
734 if (isPost)
735 return preValue;
736 return postValue;
737 }
738
739 // Helper function to create pre and post increments and decrements.
740 Value createRealIncrement(Value arg, bool isInc, bool isPost) {
741 Value preValue = moore::ReadOp::create(builder, loc, arg);
742 Value postValue;
743
744 bool isTime = isa<moore::TimeType>(preValue.getType());
745 if (isTime)
746 preValue = context.materializeConversion(
747 moore::RealType::get(context.getContext(), moore::RealWidth::f64),
748 preValue, false, loc);
749
750 moore::RealType realTy =
751 llvm::dyn_cast<moore::RealType>(preValue.getType());
752 if (!realTy)
753 return {};
754
755 FloatAttr oneAttr;
756 if (realTy.getWidth() == moore::RealWidth::f32) {
757 oneAttr = builder.getFloatAttr(builder.getF32Type(), 1.0);
758 } else if (realTy.getWidth() == moore::RealWidth::f64) {
759 auto oneVal = isTime ? getTimeScaleInFemtoseconds(context) : 1.0;
760 oneAttr = builder.getFloatAttr(builder.getF64Type(), oneVal);
761 } else {
762 mlir::emitError(loc) << "cannot construct increment for " << realTy;
763 return {};
764 }
765 auto one = moore::ConstantRealOp::create(builder, loc, oneAttr);
766
767 postValue =
768 isInc
769 ? moore::AddRealOp::create(builder, loc, preValue, one).getResult()
770 : moore::SubRealOp::create(builder, loc, preValue, one).getResult();
771
772 if (isTime)
773 postValue = context.materializeConversion(
774 moore::TimeType::get(context.getContext()), postValue, false, loc);
775
776 auto assignOp =
777 moore::BlockingAssignOp::create(builder, loc, arg, postValue);
778
779 if (context.variableAssignCallback)
780 context.variableAssignCallback(assignOp);
781
782 if (isPost)
783 return preValue;
784 return postValue;
785 }
786
787 Value visitRealUOp(const slang::ast::UnaryExpression &expr) {
788 Type opFTy = context.convertType(*expr.operand().type);
789
790 using slang::ast::UnaryOperator;
791 Value arg;
792 if (expr.op == UnaryOperator::Preincrement ||
793 expr.op == UnaryOperator::Predecrement ||
794 expr.op == UnaryOperator::Postincrement ||
795 expr.op == UnaryOperator::Postdecrement)
796 arg = context.convertLvalueExpression(expr.operand());
797 else
798 arg = context.convertRvalueExpression(expr.operand(), opFTy);
799 if (!arg)
800 return {};
801
802 // Only covers expressions in 'else' branch above.
803 if (isa<moore::TimeType>(arg.getType()))
804 arg = context.materializeConversion(
805 moore::RealType::get(context.getContext(), moore::RealWidth::f64),
806 arg, false, loc);
807
808 switch (expr.op) {
809 // `+a` is simply `a`
810 case UnaryOperator::Plus:
811 return arg;
812 case UnaryOperator::Minus:
813 return moore::NegRealOp::create(builder, loc, arg);
814
815 case UnaryOperator::Preincrement:
816 return createRealIncrement(arg, true, false);
817 case UnaryOperator::Predecrement:
818 return createRealIncrement(arg, false, false);
819 case UnaryOperator::Postincrement:
820 return createRealIncrement(arg, true, true);
821 case UnaryOperator::Postdecrement:
822 return createRealIncrement(arg, false, true);
823
824 case UnaryOperator::LogicalNot:
825 arg = context.convertToBool(arg);
826 if (!arg)
827 return {};
828 return moore::NotOp::create(builder, loc, arg);
829
830 default:
831 mlir::emitError(loc) << "Unary operator " << slang::ast::toString(expr.op)
832 << " not supported with real values!\n";
833 return {};
834 }
835 }
836
837 // Handle unary operators.
838 Value visit(const slang::ast::UnaryExpression &expr) {
839 // First check whether we need real or integral BOps
840 const auto *floatType =
841 expr.operand().type->as_if<slang::ast::FloatingType>();
842 // If op is real-typed, treat as real BOp.
843 if (floatType)
844 return visitRealUOp(expr);
845
846 using slang::ast::UnaryOperator;
847 Value arg;
848 if (expr.op == UnaryOperator::Preincrement ||
849 expr.op == UnaryOperator::Predecrement ||
850 expr.op == UnaryOperator::Postincrement ||
851 expr.op == UnaryOperator::Postdecrement)
852 arg = context.convertLvalueExpression(expr.operand());
853 else
854 arg = context.convertRvalueExpression(expr.operand());
855 if (!arg)
856 return {};
857
858 switch (expr.op) {
859 // `+a` is simply `a`, but converted to a simple bit vector type since
860 // this is technically an arithmetic operation.
861 case UnaryOperator::Plus:
862 return context.convertToSimpleBitVector(arg);
863
864 case UnaryOperator::Minus:
865 arg = context.convertToSimpleBitVector(arg);
866 if (!arg)
867 return {};
868 return moore::NegOp::create(builder, loc, arg);
869
870 case UnaryOperator::BitwiseNot:
871 arg = context.convertToSimpleBitVector(arg);
872 if (!arg)
873 return {};
874 return moore::NotOp::create(builder, loc, arg);
875
876 case UnaryOperator::BitwiseAnd:
877 return createReduction<moore::ReduceAndOp>(arg, false);
878 case UnaryOperator::BitwiseOr:
879 return createReduction<moore::ReduceOrOp>(arg, false);
880 case UnaryOperator::BitwiseXor:
881 return createReduction<moore::ReduceXorOp>(arg, false);
882 case UnaryOperator::BitwiseNand:
883 return createReduction<moore::ReduceAndOp>(arg, true);
884 case UnaryOperator::BitwiseNor:
885 return createReduction<moore::ReduceOrOp>(arg, true);
886 case UnaryOperator::BitwiseXnor:
887 return createReduction<moore::ReduceXorOp>(arg, true);
888
889 case UnaryOperator::LogicalNot:
890 arg = context.convertToBool(arg);
891 if (!arg)
892 return {};
893 return moore::NotOp::create(builder, loc, arg);
894
895 case UnaryOperator::Preincrement:
896 return createIncrement(arg, true, false);
897 case UnaryOperator::Predecrement:
898 return createIncrement(arg, false, false);
899 case UnaryOperator::Postincrement:
900 return createIncrement(arg, true, true);
901 case UnaryOperator::Postdecrement:
902 return createIncrement(arg, false, true);
903 }
904
905 mlir::emitError(loc, "unsupported unary operator");
906 return {};
907 }
908
909 /// Handles logical operators (§11.4.7), assuming lhs/rhs are rvalues already.
910 Value buildLogicalBOp(slang::ast::BinaryOperator op, Value lhs, Value rhs,
911 std::optional<Domain> domain = std::nullopt) {
912 using slang::ast::BinaryOperator;
913 // TODO: These should short-circuit; RHS should be in a separate block.
914
915 if (domain) {
916 lhs = context.convertToBool(lhs, domain.value());
917 rhs = context.convertToBool(rhs, domain.value());
918 } else {
919 lhs = context.convertToBool(lhs);
920 rhs = context.convertToBool(rhs);
921 }
922
923 if (!lhs || !rhs)
924 return {};
925
926 switch (op) {
927 case BinaryOperator::LogicalAnd:
928 return moore::AndOp::create(builder, loc, lhs, rhs);
929
930 case BinaryOperator::LogicalOr:
931 return moore::OrOp::create(builder, loc, lhs, rhs);
932
933 case BinaryOperator::LogicalImplication: {
934 // (lhs -> rhs) == (!lhs || rhs)
935 auto notLHS = moore::NotOp::create(builder, loc, lhs);
936 return moore::OrOp::create(builder, loc, notLHS, rhs);
937 }
938
939 case BinaryOperator::LogicalEquivalence: {
940 // (lhs <-> rhs) == (lhs && rhs) || (!lhs && !rhs)
941 auto notLHS = moore::NotOp::create(builder, loc, lhs);
942 auto notRHS = moore::NotOp::create(builder, loc, rhs);
943 auto both = moore::AndOp::create(builder, loc, lhs, rhs);
944 auto notBoth = moore::AndOp::create(builder, loc, notLHS, notRHS);
945 return moore::OrOp::create(builder, loc, both, notBoth);
946 }
947
948 default:
949 llvm_unreachable("not a logical BinaryOperator");
950 }
951 }
952
953 Value visitHandleBOp(const slang::ast::BinaryExpression &expr) {
954 // Convert operands to the chosen target type.
955 auto lhs = context.convertRvalueExpression(expr.left());
956 if (!lhs)
957 return {};
958 auto rhs = context.convertRvalueExpression(expr.right());
959 if (!rhs)
960 return {};
961
962 using slang::ast::BinaryOperator;
963 switch (expr.op) {
964
965 case BinaryOperator::Equality:
966 return moore::HandleEqOp::create(builder, loc, lhs, rhs);
967 case BinaryOperator::Inequality:
968 return moore::HandleNeOp::create(builder, loc, lhs, rhs);
969 case BinaryOperator::CaseEquality:
970 return moore::HandleCaseEqOp::create(builder, loc, lhs, rhs);
971 case BinaryOperator::CaseInequality:
972 return moore::HandleCaseNeOp::create(builder, loc, lhs, rhs);
973
974 default:
975 mlir::emitError(loc)
976 << "Binary operator " << slang::ast::toString(expr.op)
977 << " not supported with class handle valued operands!\n";
978 return {};
979 }
980 }
981
982 Value visitRealBOp(const slang::ast::BinaryExpression &expr) {
983 // Convert operands to the chosen target type.
984 auto lhs = context.convertRvalueExpression(expr.left());
985 if (!lhs)
986 return {};
987 auto rhs = context.convertRvalueExpression(expr.right());
988 if (!rhs)
989 return {};
990
991 if (isa<moore::TimeType>(lhs.getType()) ||
992 isa<moore::TimeType>(rhs.getType())) {
993 lhs = context.materializeConversion(
994 moore::RealType::get(context.getContext(), moore::RealWidth::f64),
995 lhs, false, loc);
996 rhs = context.materializeConversion(
997 moore::RealType::get(context.getContext(), moore::RealWidth::f64),
998 rhs, false, loc);
999 }
1000
1001 using slang::ast::BinaryOperator;
1002 switch (expr.op) {
1003 case BinaryOperator::Add:
1004 return moore::AddRealOp::create(builder, loc, lhs, rhs);
1005 case BinaryOperator::Subtract:
1006 return moore::SubRealOp::create(builder, loc, lhs, rhs);
1007 case BinaryOperator::Multiply:
1008 return moore::MulRealOp::create(builder, loc, lhs, rhs);
1009 case BinaryOperator::Divide:
1010 return moore::DivRealOp::create(builder, loc, lhs, rhs);
1011 case BinaryOperator::Power:
1012 return moore::PowRealOp::create(builder, loc, lhs, rhs);
1013
1014 case BinaryOperator::Equality:
1015 return moore::EqRealOp::create(builder, loc, lhs, rhs);
1016 case BinaryOperator::Inequality:
1017 return moore::NeRealOp::create(builder, loc, lhs, rhs);
1018
1019 case BinaryOperator::GreaterThan:
1020 return moore::FgtOp::create(builder, loc, lhs, rhs);
1021 case BinaryOperator::LessThan:
1022 return moore::FltOp::create(builder, loc, lhs, rhs);
1023 case BinaryOperator::GreaterThanEqual:
1024 return moore::FgeOp::create(builder, loc, lhs, rhs);
1025 case BinaryOperator::LessThanEqual:
1026 return moore::FleOp::create(builder, loc, lhs, rhs);
1027
1028 case BinaryOperator::LogicalAnd:
1029 case BinaryOperator::LogicalOr:
1030 case BinaryOperator::LogicalImplication:
1031 case BinaryOperator::LogicalEquivalence:
1032 return buildLogicalBOp(expr.op, lhs, rhs);
1033
1034 default:
1035 mlir::emitError(loc) << "Binary operator "
1036 << slang::ast::toString(expr.op)
1037 << " not supported with real valued operands!\n";
1038 return {};
1039 }
1040 }
1041
1042 // Helper function to convert two arguments to a simple bit vector type and
1043 // pass them into a binary op.
1044 template <class ConcreteOp>
1045 Value createBinary(Value lhs, Value rhs) {
1046 lhs = context.convertToSimpleBitVector(lhs);
1047 if (!lhs)
1048 return {};
1049 rhs = context.convertToSimpleBitVector(rhs);
1050 if (!rhs)
1051 return {};
1052 return ConcreteOp::create(builder, loc, lhs, rhs);
1053 }
1054
1055 // Handle binary operators.
1056 Value visit(const slang::ast::BinaryExpression &expr) {
1057 // First check whether we need real or integral BOps
1058 const auto *rhsFloatType =
1059 expr.right().type->as_if<slang::ast::FloatingType>();
1060 const auto *lhsFloatType =
1061 expr.left().type->as_if<slang::ast::FloatingType>();
1062
1063 // If either arg is real-typed, treat as real BOp.
1064 if (rhsFloatType || lhsFloatType)
1065 return visitRealBOp(expr);
1066
1067 // Check whether we are comparing against a Class Handle or CHandle
1068 const auto rhsIsClass = expr.right().type->isClass();
1069 const auto lhsIsClass = expr.left().type->isClass();
1070 const auto rhsIsChandle = expr.right().type->isCHandle();
1071 const auto lhsIsChandle = expr.left().type->isCHandle();
1072 // If either arg is class handle-typed, treat as class handle BOp.
1073 if (rhsIsClass || lhsIsClass || rhsIsChandle || lhsIsChandle)
1074 return visitHandleBOp(expr);
1075
1076 auto lhs = context.convertRvalueExpression(expr.left());
1077 if (!lhs)
1078 return {};
1079 auto rhs = context.convertRvalueExpression(expr.right());
1080 if (!rhs)
1081 return {};
1082
1083 // Determine the domain of the result.
1084 Domain domain = Domain::TwoValued;
1085 if (expr.type->isFourState() || expr.left().type->isFourState() ||
1086 expr.right().type->isFourState())
1087 domain = Domain::FourValued;
1088
1089 using slang::ast::BinaryOperator;
1090 switch (expr.op) {
1091 case BinaryOperator::Add:
1092 return createBinary<moore::AddOp>(lhs, rhs);
1093 case BinaryOperator::Subtract:
1094 return createBinary<moore::SubOp>(lhs, rhs);
1095 case BinaryOperator::Multiply:
1096 return createBinary<moore::MulOp>(lhs, rhs);
1097 case BinaryOperator::Divide:
1098 if (expr.type->isSigned())
1099 return createBinary<moore::DivSOp>(lhs, rhs);
1100 else
1101 return createBinary<moore::DivUOp>(lhs, rhs);
1102 case BinaryOperator::Mod:
1103 if (expr.type->isSigned())
1104 return createBinary<moore::ModSOp>(lhs, rhs);
1105 else
1106 return createBinary<moore::ModUOp>(lhs, rhs);
1107 case BinaryOperator::Power: {
1108 // Slang casts the LHS and result of the `**` operator to a four-valued
1109 // type, since the operator can return X even for two-valued inputs. To
1110 // maintain uniform types across operands and results, cast the RHS to
1111 // that four-valued type as well.
1112 auto rhsCast = context.materializeConversion(
1113 lhs.getType(), rhs, expr.right().type->isSigned(), rhs.getLoc());
1114 if (expr.type->isSigned())
1115 return createBinary<moore::PowSOp>(lhs, rhsCast);
1116 else
1117 return createBinary<moore::PowUOp>(lhs, rhsCast);
1118 }
1119
1120 case BinaryOperator::BinaryAnd:
1121 return createBinary<moore::AndOp>(lhs, rhs);
1122 case BinaryOperator::BinaryOr:
1123 return createBinary<moore::OrOp>(lhs, rhs);
1124 case BinaryOperator::BinaryXor:
1125 return createBinary<moore::XorOp>(lhs, rhs);
1126 case BinaryOperator::BinaryXnor: {
1127 auto result = createBinary<moore::XorOp>(lhs, rhs);
1128 if (!result)
1129 return {};
1130 return moore::NotOp::create(builder, loc, result);
1131 }
1132
1133 case BinaryOperator::Equality:
1134 if (isa<moore::UnpackedArrayType>(lhs.getType()))
1135 return moore::UArrayCmpOp::create(
1136 builder, loc, moore::UArrayCmpPredicate::eq, lhs, rhs);
1137 else if (isa<moore::StringType>(lhs.getType()))
1138 return moore::StringCmpOp::create(
1139 builder, loc, moore::StringCmpPredicate::eq, lhs, rhs);
1140 else
1141 return createBinary<moore::EqOp>(lhs, rhs);
1142 case BinaryOperator::Inequality:
1143 if (isa<moore::UnpackedArrayType>(lhs.getType()))
1144 return moore::UArrayCmpOp::create(
1145 builder, loc, moore::UArrayCmpPredicate::ne, lhs, rhs);
1146 else if (isa<moore::StringType>(lhs.getType()))
1147 return moore::StringCmpOp::create(
1148 builder, loc, moore::StringCmpPredicate::ne, lhs, rhs);
1149 else
1150 return createBinary<moore::NeOp>(lhs, rhs);
1151 case BinaryOperator::CaseEquality:
1152 return createBinary<moore::CaseEqOp>(lhs, rhs);
1153 case BinaryOperator::CaseInequality:
1154 return createBinary<moore::CaseNeOp>(lhs, rhs);
1155 case BinaryOperator::WildcardEquality:
1156 return createBinary<moore::WildcardEqOp>(lhs, rhs);
1157 case BinaryOperator::WildcardInequality:
1158 return createBinary<moore::WildcardNeOp>(lhs, rhs);
1159
1160 case BinaryOperator::GreaterThanEqual:
1161 if (expr.left().type->isSigned())
1162 return createBinary<moore::SgeOp>(lhs, rhs);
1163 else if (isa<moore::StringType>(lhs.getType()))
1164 return moore::StringCmpOp::create(
1165 builder, loc, moore::StringCmpPredicate::ge, lhs, rhs);
1166 else
1167 return createBinary<moore::UgeOp>(lhs, rhs);
1168 case BinaryOperator::GreaterThan:
1169 if (expr.left().type->isSigned())
1170 return createBinary<moore::SgtOp>(lhs, rhs);
1171 else if (isa<moore::StringType>(lhs.getType()))
1172 return moore::StringCmpOp::create(
1173 builder, loc, moore::StringCmpPredicate::gt, lhs, rhs);
1174 else
1175 return createBinary<moore::UgtOp>(lhs, rhs);
1176 case BinaryOperator::LessThanEqual:
1177 if (expr.left().type->isSigned())
1178 return createBinary<moore::SleOp>(lhs, rhs);
1179 else if (isa<moore::StringType>(lhs.getType()))
1180 return moore::StringCmpOp::create(
1181 builder, loc, moore::StringCmpPredicate::le, lhs, rhs);
1182 else
1183 return createBinary<moore::UleOp>(lhs, rhs);
1184 case BinaryOperator::LessThan:
1185 if (expr.left().type->isSigned())
1186 return createBinary<moore::SltOp>(lhs, rhs);
1187 else if (isa<moore::StringType>(lhs.getType()))
1188 return moore::StringCmpOp::create(
1189 builder, loc, moore::StringCmpPredicate::lt, lhs, rhs);
1190 else
1191 return createBinary<moore::UltOp>(lhs, rhs);
1192
1193 case BinaryOperator::LogicalAnd:
1194 case BinaryOperator::LogicalOr:
1195 case BinaryOperator::LogicalImplication:
1196 case BinaryOperator::LogicalEquivalence:
1197 return buildLogicalBOp(expr.op, lhs, rhs, domain);
1198
1199 case BinaryOperator::LogicalShiftLeft:
1200 return createBinary<moore::ShlOp>(lhs, rhs);
1201 case BinaryOperator::LogicalShiftRight:
1202 return createBinary<moore::ShrOp>(lhs, rhs);
1203 case BinaryOperator::ArithmeticShiftLeft:
1204 return createBinary<moore::ShlOp>(lhs, rhs);
1205 case BinaryOperator::ArithmeticShiftRight: {
1206 // The `>>>` operator is an arithmetic right shift if the LHS operand is
1207 // signed, or a logical right shift if the operand is unsigned.
1208 lhs = context.convertToSimpleBitVector(lhs);
1209 rhs = context.convertToSimpleBitVector(rhs);
1210 if (!lhs || !rhs)
1211 return {};
1212 if (expr.type->isSigned())
1213 return moore::AShrOp::create(builder, loc, lhs, rhs);
1214 return moore::ShrOp::create(builder, loc, lhs, rhs);
1215 }
1216 }
1217
1218 mlir::emitError(loc, "unsupported binary operator");
1219 return {};
1220 }
1221
1222 // Handle `'0`, `'1`, `'x`, and `'z` literals.
1223 Value visit(const slang::ast::UnbasedUnsizedIntegerLiteral &expr) {
1224 return context.materializeSVInt(expr.getValue(), *expr.type, loc);
1225 }
1226
1227 // Handle integer literals.
1228 Value visit(const slang::ast::IntegerLiteral &expr) {
1229 return context.materializeSVInt(expr.getValue(), *expr.type, loc);
1230 }
1231
1232 // Handle time literals.
1233 Value visit(const slang::ast::TimeLiteral &expr) {
1234 // The time literal is expressed in the current time scale. Determine the
1235 // conversion factor to convert the literal from the current time scale into
1236 // femtoseconds, and round the scaled value to femtoseconds.
1237 double scale = getTimeScaleInFemtoseconds(context);
1238 double value = std::round(expr.getValue() * scale);
1239 assert(value >= 0.0);
1240
1241 // Check that the value does not exceed what we can represent in the IR.
1242 // Casting the maximum uint64 value to double changes its value from
1243 // 18446744073709551615 to 18446744073709551616, which makes the comparison
1244 // overestimate the largest number we can represent. To avoid this, round
1245 // the maximum value down to the closest number that only has the front 53
1246 // bits set. This matches the mantissa of a double, plus the implicit
1247 // leading 1, ensuring that we can accurately represent the limit.
1248 static constexpr uint64_t limit =
1249 (std::numeric_limits<uint64_t>::max() >> 11) << 11;
1250 if (value > limit) {
1251 mlir::emitError(loc) << "time value is larger than " << limit << " fs";
1252 return {};
1253 }
1254
1255 return moore::ConstantTimeOp::create(builder, loc,
1256 static_cast<uint64_t>(value));
1257 }
1258
1259 // Handle replications.
1260 Value visit(const slang::ast::ReplicationExpression &expr) {
1261 auto type = context.convertType(*expr.type);
1262 auto value = context.convertRvalueExpression(expr.concat());
1263 if (!value)
1264 return {};
1265 return moore::ReplicateOp::create(builder, loc, type, value);
1266 }
1267
1268 // Handle set membership operator.
1269 Value visit(const slang::ast::InsideExpression &expr) {
1270 auto lhs = context.convertToSimpleBitVector(
1271 context.convertRvalueExpression(expr.left()));
1272 if (!lhs)
1273 return {};
1274 // All conditions for determining whether it is inside.
1275 SmallVector<Value> conditions;
1276
1277 // Traverse open range list.
1278 for (const auto *listExpr : expr.rangeList()) {
1279 Value cond;
1280 // The open range list on the right-hand side of the inside operator is a
1281 // comma-separated list of expressions or ranges.
1282 if (const auto *openRange =
1283 listExpr->as_if<slang::ast::ValueRangeExpression>()) {
1284 // Handle ranges.
1285 auto lowBound = context.convertToSimpleBitVector(
1286 context.convertRvalueExpression(openRange->left()));
1287 auto highBound = context.convertToSimpleBitVector(
1288 context.convertRvalueExpression(openRange->right()));
1289 if (!lowBound || !highBound)
1290 return {};
1291 Value leftValue, rightValue;
1292 // Determine if the expression on the left-hand side is inclusively
1293 // within the range.
1294 if (openRange->left().type->isSigned() ||
1295 expr.left().type->isSigned()) {
1296 leftValue = moore::SgeOp::create(builder, loc, lhs, lowBound);
1297 } else {
1298 leftValue = moore::UgeOp::create(builder, loc, lhs, lowBound);
1299 }
1300 if (openRange->right().type->isSigned() ||
1301 expr.left().type->isSigned()) {
1302 rightValue = moore::SleOp::create(builder, loc, lhs, highBound);
1303 } else {
1304 rightValue = moore::UleOp::create(builder, loc, lhs, highBound);
1305 }
1306 cond = moore::AndOp::create(builder, loc, leftValue, rightValue);
1307 } else {
1308 // Handle expressions.
1309 if (!listExpr->type->isIntegral()) {
1310 if (listExpr->type->isUnpackedArray()) {
1311 mlir::emitError(
1312 loc, "unpacked arrays in 'inside' expressions not supported");
1313 return {};
1314 }
1315 mlir::emitError(
1316 loc, "only simple bit vectors supported in 'inside' expressions");
1317 return {};
1318 }
1319
1320 auto value = context.convertToSimpleBitVector(
1321 context.convertRvalueExpression(*listExpr));
1322 if (!value)
1323 return {};
1324 cond = moore::WildcardEqOp::create(builder, loc, lhs, value);
1325 }
1326 conditions.push_back(cond);
1327 }
1328
1329 // Calculate the final result by `or` op.
1330 auto result = conditions.back();
1331 conditions.pop_back();
1332 while (!conditions.empty()) {
1333 result = moore::OrOp::create(builder, loc, conditions.back(), result);
1334 conditions.pop_back();
1335 }
1336 return result;
1337 }
1338
1339 // Handle conditional operator `?:`.
1340 Value visit(const slang::ast::ConditionalExpression &expr) {
1341 auto type = context.convertType(*expr.type);
1342
1343 // Handle condition.
1344 if (expr.conditions.size() > 1) {
1345 mlir::emitError(loc)
1346 << "unsupported conditional expression with more than one condition";
1347 return {};
1348 }
1349 const auto &cond = expr.conditions[0];
1350 if (cond.pattern) {
1351 mlir::emitError(loc) << "unsupported conditional expression with pattern";
1352 return {};
1353 }
1354 auto value =
1355 context.convertToBool(context.convertRvalueExpression(*cond.expr));
1356 if (!value)
1357 return {};
1358 auto conditionalOp =
1359 moore::ConditionalOp::create(builder, loc, type, value);
1360
1361 // Create blocks for true region and false region.
1362 auto &trueBlock = conditionalOp.getTrueRegion().emplaceBlock();
1363 auto &falseBlock = conditionalOp.getFalseRegion().emplaceBlock();
1364
1365 OpBuilder::InsertionGuard g(builder);
1366
1367 // Handle left expression.
1368 builder.setInsertionPointToStart(&trueBlock);
1369 auto trueValue = context.convertRvalueExpression(expr.left(), type);
1370 if (!trueValue)
1371 return {};
1372 moore::YieldOp::create(builder, loc, trueValue);
1373
1374 // Handle right expression.
1375 builder.setInsertionPointToStart(&falseBlock);
1376 auto falseValue = context.convertRvalueExpression(expr.right(), type);
1377 if (!falseValue)
1378 return {};
1379 moore::YieldOp::create(builder, loc, falseValue);
1380
1381 return conditionalOp.getResult();
1382 }
1383
1384 /// Handle calls.
1385 Value visit(const slang::ast::CallExpression &expr) {
1386 // Try to materialize constant values directly.
1387 auto constant = context.evaluateConstant(expr);
1388 if (auto value = context.materializeConstant(constant, *expr.type, loc))
1389 return value;
1390
1391 return std::visit(
1392 [&](auto &subroutine) { return visitCall(expr, subroutine); },
1393 expr.subroutine);
1394 }
1395
1396 /// Get both the actual `this` argument of a method call and the required
1397 /// class type.
1398 std::pair<Value, moore::ClassHandleType>
1399 getMethodReceiverTypeHandle(const slang::ast::CallExpression &expr) {
1400
1401 moore::ClassHandleType handleTy;
1402 Value thisRef;
1403
1404 // Qualified call: t.m(...), extract from thisClass.
1405 if (const slang::ast::Expression *recvExpr = expr.thisClass()) {
1406 thisRef = context.convertRvalueExpression(*recvExpr);
1407 if (!thisRef)
1408 return {};
1409 } else {
1410 // Unqualified call inside a method body: try using implicit %this.
1411 thisRef = context.getImplicitThisRef();
1412 if (!thisRef) {
1413 mlir::emitError(loc) << "method '" << expr.getSubroutineName()
1414 << "' called without an object";
1415 return {};
1416 }
1417 }
1418 handleTy = cast<moore::ClassHandleType>(thisRef.getType());
1419 return {thisRef, handleTy};
1420 }
1421
1422 /// Build a method call including implicit this argument.
1423 mlir::CallOpInterface
1424 buildMethodCall(const slang::ast::SubroutineSymbol *subroutine,
1425 FunctionLowering *lowering,
1426 moore::ClassHandleType actualHandleTy, Value actualThisRef,
1427 SmallVector<Value> &arguments,
1428 SmallVector<Type> &resultTypes) {
1429
1430 // Get the expected receiver type from the lowered method
1431 auto funcTy = lowering->op.getFunctionType();
1432 auto expected0 = funcTy.getInput(0);
1433 auto expectedHdlTy = cast<moore::ClassHandleType>(expected0);
1434
1435 // Upcast the handle as necessary.
1436 auto implicitThisRef = context.materializeConversion(
1437 expectedHdlTy, actualThisRef, false, actualThisRef.getLoc());
1438
1439 // Build an argument list where the this reference is the first argument.
1440 SmallVector<Value> explicitArguments;
1441 explicitArguments.reserve(arguments.size() + 1);
1442 explicitArguments.push_back(implicitThisRef);
1443 explicitArguments.append(arguments.begin(), arguments.end());
1444
1445 // Method call: choose direct vs virtual.
1446 const bool isVirtual =
1447 (subroutine->flags & slang::ast::MethodFlags::Virtual) != 0;
1448
1449 if (!isVirtual) {
1450 auto calleeSym = lowering->op.getSymName();
1451 // Direct (non-virtual) call -> moore.class.call
1452 return mlir::func::CallOp::create(builder, loc, resultTypes, calleeSym,
1453 explicitArguments);
1454 }
1455
1456 auto funcName = subroutine->name;
1457 auto method = moore::VTableLoadMethodOp::create(
1458 builder, loc, funcTy, actualThisRef,
1459 SymbolRefAttr::get(context.getContext(), funcName));
1460 return mlir::func::CallIndirectOp::create(builder, loc, method,
1461 explicitArguments);
1462 }
1463
1464 /// Handle subroutine calls.
1465 Value visitCall(const slang::ast::CallExpression &expr,
1466 const slang::ast::SubroutineSymbol *subroutine) {
1467
1468 const bool isMethod = (subroutine->thisVar != nullptr);
1469
1470 auto *lowering = context.declareFunction(*subroutine);
1471 if (!lowering)
1472 return {};
1473 auto convertedFunction = context.convertFunction(*subroutine);
1474 if (failed(convertedFunction))
1475 return {};
1476
1477 // Convert the call arguments. Input arguments are converted to an rvalue.
1478 // All other arguments are converted to lvalues and passed into the function
1479 // by reference.
1480 SmallVector<Value> arguments;
1481 for (auto [callArg, declArg] :
1482 llvm::zip(expr.arguments(), subroutine->getArguments())) {
1483
1484 // Unpack the `<expr> = EmptyArgument` pattern emitted by Slang for output
1485 // and inout arguments.
1486 auto *expr = callArg;
1487 if (const auto *assign = expr->as_if<slang::ast::AssignmentExpression>())
1488 expr = &assign->left();
1489
1490 Value value;
1491 auto type = context.convertType(declArg->getType());
1492 if (declArg->direction == slang::ast::ArgumentDirection::In) {
1493 value = context.convertRvalueExpression(*expr, type);
1494 } else {
1495 Value lvalue = context.convertLvalueExpression(*expr);
1496 auto unpackedType = dyn_cast<moore::UnpackedType>(type);
1497 if (!unpackedType)
1498 return {};
1499 value =
1500 context.materializeConversion(moore::RefType::get(unpackedType),
1501 lvalue, expr->type->isSigned(), loc);
1502 }
1503 if (!value)
1504 return {};
1505 arguments.push_back(value);
1506 }
1507
1508 if (!lowering->isConverting && !lowering->captures.empty()) {
1509 auto materializeCaptureAtCall = [&](Value cap) -> Value {
1510 // Captures are expected to be moore::RefType.
1511 auto refTy = dyn_cast<moore::RefType>(cap.getType());
1512 if (!refTy) {
1513 lowering->op.emitError(
1514 "expected captured value to be moore::RefType");
1515 return {};
1516 }
1517
1518 // Expected case: the capture stems from a variable of any parent
1519 // scope. We need to walk up, since definition might be a couple regions
1520 // up.
1521 Region *capRegion = [&]() -> Region * {
1522 if (auto ba = dyn_cast<BlockArgument>(cap))
1523 return ba.getOwner()->getParent();
1524 if (auto *def = cap.getDefiningOp())
1525 return def->getParentRegion();
1526 return nullptr;
1527 }();
1528
1529 Region *callRegion =
1530 builder.getBlock() ? builder.getBlock()->getParent() : nullptr;
1531
1532 for (Region *r = callRegion; r; r = r->getParentRegion()) {
1533 if (r == capRegion) {
1534 // Safe to use the SSA value directly here.
1535 return cap;
1536 }
1537 }
1538
1539 // Otherwise we can’t legally rematerialize this capture here.
1540 lowering->op.emitError()
1541 << "cannot materialize captured ref at call site; non-symbol "
1542 << "source: "
1543 << (cap.getDefiningOp()
1544 ? cap.getDefiningOp()->getName().getStringRef()
1545 : "<block-arg>");
1546 return {};
1547 };
1548
1549 for (Value cap : lowering->captures) {
1550 Value mat = materializeCaptureAtCall(cap);
1551 if (!mat)
1552 return {};
1553 arguments.push_back(mat);
1554 }
1555 }
1556
1557 // Determine result types from the declared/converted func op.
1558 SmallVector<Type> resultTypes(
1559 lowering->op.getFunctionType().getResults().begin(),
1560 lowering->op.getFunctionType().getResults().end());
1561
1562 mlir::CallOpInterface callOp;
1563 if (isMethod) {
1564 // Class functions -> build func.call / func.indirect_call with implicit
1565 // this argument
1566 auto [thisRef, tyHandle] = getMethodReceiverTypeHandle(expr);
1567 callOp = buildMethodCall(subroutine, lowering, tyHandle, thisRef,
1568 arguments, resultTypes);
1569 } else {
1570 // Free function -> func.call
1571 callOp =
1572 mlir::func::CallOp::create(builder, loc, lowering->op, arguments);
1573 }
1574
1575 auto result = resultTypes.size() > 0 ? callOp->getOpResult(0) : Value{};
1576 // For calls to void functions we need to have a value to return from this
1577 // function. Create a dummy `unrealized_conversion_cast`, which will get
1578 // deleted again later on.
1579 if (resultTypes.size() == 0)
1580 return mlir::UnrealizedConversionCastOp::create(
1581 builder, loc, moore::VoidType::get(context.getContext()),
1582 ValueRange{})
1583 .getResult(0);
1584
1585 return result;
1586 }
1587
1588 /// Handle system calls.
1589 Value visitCall(const slang::ast::CallExpression &expr,
1590 const slang::ast::CallExpression::SystemCallInfo &info) {
1591 const auto &subroutine = *info.subroutine;
1592
1593 // $rose, $fell, $stable, $changed, and $past are only valid in
1594 // the context of properties and assertions. Those are treated in the
1595 // LTLDialect; treat them there instead.
1596 bool isAssertionCall =
1597 llvm::StringSwitch<bool>(subroutine.name)
1598 .Cases({"$rose", "$fell", "$stable", "$past"}, true)
1599 .Default(false);
1600
1601 if (isAssertionCall)
1602 return context.convertAssertionCallExpression(expr, info, loc);
1603
1604 auto args = expr.arguments();
1605
1606 FailureOr<Value> result;
1607 Value value;
1608 Value value2;
1609
1610 // $sformatf() and $sformat look like system tasks, but we handle string
1611 // formatting differently from expression evaluation, so handle them
1612 // separately.
1613 // According to IEEE 1800-2023 Section 21.3.3 "Formatting data to a
1614 // string" $sformatf works just like the string formatting but returns
1615 // a StringType.
1616 if (!subroutine.name.compare("$sformatf")) {
1617 // Create the FormatString
1618 auto fmtValue = context.convertFormatString(
1619 expr.arguments(), loc, moore::IntFormat::Decimal, false);
1620 if (failed(fmtValue))
1621 return {};
1622 return fmtValue.value();
1623 }
1624
1625 // Call the conversion function with the appropriate arity. These return one
1626 // of the following:
1627 //
1628 // - `failure()` if the system call was recognized but some error occurred
1629 // - `Value{}` if the system call was not recognized
1630 // - non-null `Value` result otherwise
1631 switch (args.size()) {
1632 case (0):
1633 result = context.convertSystemCallArity0(subroutine, loc);
1634 break;
1635
1636 case (1):
1637 value = context.convertRvalueExpression(*args[0]);
1638 if (!value)
1639 return {};
1640 result = context.convertSystemCallArity1(subroutine, loc, value);
1641 break;
1642
1643 case (2):
1644 value = context.convertRvalueExpression(*args[0]);
1645 value2 = context.convertRvalueExpression(*args[1]);
1646 if (!value || !value2)
1647 return {};
1648 result = context.convertSystemCallArity2(subroutine, loc, value, value2);
1649 break;
1650
1651 default:
1652 break;
1653 }
1654
1655 // If we have recognized the system call but the conversion has encountered
1656 // and already reported an error, simply return the usual null `Value` to
1657 // indicate failure.
1658 if (failed(result))
1659 return {};
1660
1661 // If we have recognized the system call and got a non-null `Value` result,
1662 // return that.
1663 if (*result) {
1664 auto ty = context.convertType(*expr.type);
1665 return context.materializeConversion(ty, *result, expr.type->isSigned(),
1666 loc);
1667 }
1668
1669 // Otherwise we didn't recognize the system call.
1670 mlir::emitError(loc) << "unsupported system call `" << subroutine.name
1671 << "`";
1672 return {};
1673 }
1674
1675 /// Handle string literals.
1676 Value visit(const slang::ast::StringLiteral &expr) {
1677 auto type = context.convertType(*expr.type);
1678 return moore::ConstantStringOp::create(builder, loc, type, expr.getValue());
1679 }
1680
1681 /// Handle real literals.
1682 Value visit(const slang::ast::RealLiteral &expr) {
1683 auto fTy = mlir::Float64Type::get(context.getContext());
1684 auto attr = mlir::FloatAttr::get(fTy, expr.getValue());
1685 return moore::ConstantRealOp::create(builder, loc, attr).getResult();
1686 }
1687
1688 /// Helper function to convert RValues at creation of a new Struct, Array or
1689 /// Int.
1690 FailureOr<SmallVector<Value>>
1691 convertElements(const slang::ast::AssignmentPatternExpressionBase &expr,
1692 std::variant<Type, ArrayRef<Type>> expectedTypes,
1693 unsigned replCount) {
1694 const auto &elts = expr.elements();
1695 const size_t elementCount = elts.size();
1696
1697 // Inspect the variant.
1698 const bool hasBroadcast =
1699 std::holds_alternative<Type>(expectedTypes) &&
1700 static_cast<bool>(std::get<Type>(expectedTypes)); // non-null Type
1701
1702 const bool hasPerElem =
1703 std::holds_alternative<ArrayRef<Type>>(expectedTypes) &&
1704 !std::get<ArrayRef<Type>>(expectedTypes).empty();
1705
1706 // If per-element types are provided, enforce arity.
1707 if (hasPerElem) {
1708 auto types = std::get<ArrayRef<Type>>(expectedTypes);
1709 if (types.size() != elementCount) {
1710 mlir::emitError(loc)
1711 << "assignment pattern arity mismatch: expected " << types.size()
1712 << " elements, got " << elementCount;
1713 return failure();
1714 }
1715 }
1716
1717 SmallVector<Value> converted;
1718 converted.reserve(elementCount * std::max(1u, replCount));
1719
1720 // Convert each element heuristically, no type is expected
1721 if (!hasBroadcast && !hasPerElem) {
1722 // No expected type info.
1723 for (const auto *elementExpr : elts) {
1724 Value v = context.convertRvalueExpression(*elementExpr);
1725 if (!v)
1726 return failure();
1727 converted.push_back(v);
1728 }
1729 } else if (hasBroadcast) {
1730 // Same expected type for all elements.
1731 Type want = std::get<Type>(expectedTypes);
1732 for (const auto *elementExpr : elts) {
1733 Value v = want ? context.convertRvalueExpression(*elementExpr, want)
1734 : context.convertRvalueExpression(*elementExpr);
1735 if (!v)
1736 return failure();
1737 converted.push_back(v);
1738 }
1739 } else { // hasPerElem, individual type is expected for each element
1740 auto types = std::get<ArrayRef<Type>>(expectedTypes);
1741 for (size_t i = 0; i < elementCount; ++i) {
1742 Type want = types[i];
1743 const auto *elementExpr = elts[i];
1744 Value v = want ? context.convertRvalueExpression(*elementExpr, want)
1745 : context.convertRvalueExpression(*elementExpr);
1746 if (!v)
1747 return failure();
1748 converted.push_back(v);
1749 }
1750 }
1751
1752 for (unsigned i = 1; i < replCount; ++i)
1753 converted.append(converted.begin(), converted.begin() + elementCount);
1754
1755 return converted;
1756 }
1757
1758 /// Handle assignment patterns.
1759 Value visitAssignmentPattern(
1760 const slang::ast::AssignmentPatternExpressionBase &expr,
1761 unsigned replCount = 1) {
1762 auto type = context.convertType(*expr.type);
1763 const auto &elts = expr.elements();
1764
1765 // Handle integers.
1766 if (auto intType = dyn_cast<moore::IntType>(type)) {
1767 auto elements = convertElements(expr, {}, replCount);
1768
1769 if (failed(elements))
1770 return {};
1771
1772 assert(intType.getWidth() == elements->size());
1773 std::reverse(elements->begin(), elements->end());
1774 return moore::ConcatOp::create(builder, loc, intType, *elements);
1775 }
1776
1777 // Handle packed structs.
1778 if (auto structType = dyn_cast<moore::StructType>(type)) {
1779 SmallVector<Type> expectedTy;
1780 expectedTy.reserve(structType.getMembers().size());
1781 for (auto member : structType.getMembers())
1782 expectedTy.push_back(member.type);
1783
1784 FailureOr<SmallVector<Value>> elements;
1785 if (expectedTy.size() == elts.size())
1786 elements = convertElements(expr, expectedTy, replCount);
1787 else
1788 elements = convertElements(expr, {}, replCount);
1789
1790 if (failed(elements))
1791 return {};
1792
1793 assert(structType.getMembers().size() == elements->size());
1794 return moore::StructCreateOp::create(builder, loc, structType, *elements);
1795 }
1796
1797 // Handle unpacked structs.
1798 if (auto structType = dyn_cast<moore::UnpackedStructType>(type)) {
1799 SmallVector<Type> expectedTy;
1800 expectedTy.reserve(structType.getMembers().size());
1801 for (auto member : structType.getMembers())
1802 expectedTy.push_back(member.type);
1803
1804 FailureOr<SmallVector<Value>> elements;
1805 if (expectedTy.size() == elts.size())
1806 elements = convertElements(expr, expectedTy, replCount);
1807 else
1808 elements = convertElements(expr, {}, replCount);
1809
1810 if (failed(elements))
1811 return {};
1812
1813 assert(structType.getMembers().size() == elements->size());
1814
1815 return moore::StructCreateOp::create(builder, loc, structType, *elements);
1816 }
1817
1818 // Handle packed arrays.
1819 if (auto arrayType = dyn_cast<moore::ArrayType>(type)) {
1820 auto elements =
1821 convertElements(expr, arrayType.getElementType(), replCount);
1822
1823 if (failed(elements))
1824 return {};
1825
1826 assert(arrayType.getSize() == elements->size());
1827 return moore::ArrayCreateOp::create(builder, loc, arrayType, *elements);
1828 }
1829
1830 // Handle unpacked arrays.
1831 if (auto arrayType = dyn_cast<moore::UnpackedArrayType>(type)) {
1832 auto elements =
1833 convertElements(expr, arrayType.getElementType(), replCount);
1834
1835 if (failed(elements))
1836 return {};
1837
1838 assert(arrayType.getSize() == elements->size());
1839 return moore::ArrayCreateOp::create(builder, loc, arrayType, *elements);
1840 }
1841
1842 mlir::emitError(loc) << "unsupported assignment pattern with type " << type;
1843 return {};
1844 }
1845
1846 Value visit(const slang::ast::SimpleAssignmentPatternExpression &expr) {
1847 return visitAssignmentPattern(expr);
1848 }
1849
1850 Value visit(const slang::ast::StructuredAssignmentPatternExpression &expr) {
1851 return visitAssignmentPattern(expr);
1852 }
1853
1854 Value visit(const slang::ast::ReplicatedAssignmentPatternExpression &expr) {
1855 auto count =
1856 context.evaluateConstant(expr.count()).integer().as<unsigned>();
1857 assert(count && "Slang guarantees constant non-zero replication count");
1858 return visitAssignmentPattern(expr, *count);
1859 }
1860
1861 Value visit(const slang::ast::StreamingConcatenationExpression &expr) {
1862 SmallVector<Value> operands;
1863 for (auto stream : expr.streams()) {
1864 auto operandLoc = context.convertLocation(stream.operand->sourceRange);
1865 if (!stream.constantWithWidth.has_value() && stream.withExpr) {
1866 mlir::emitError(operandLoc)
1867 << "Moore only support streaming "
1868 "concatenation with fixed size 'with expression'";
1869 return {};
1870 }
1871 Value value;
1872 if (stream.constantWithWidth.has_value()) {
1873 value = context.convertRvalueExpression(*stream.withExpr);
1874 auto type = cast<moore::UnpackedType>(value.getType());
1875 auto intType = moore::IntType::get(
1876 context.getContext(), type.getBitSize().value(), type.getDomain());
1877 // Do not care if it's signed, because we will not do expansion.
1878 value = context.materializeConversion(intType, value, false, loc);
1879 } else {
1880 value = context.convertRvalueExpression(*stream.operand);
1881 }
1882
1883 value = context.convertToSimpleBitVector(value);
1884 if (!value)
1885 return {};
1886 operands.push_back(value);
1887 }
1888 Value value;
1889
1890 if (operands.size() == 1) {
1891 // There must be at least one element, otherwise slang will report an
1892 // error.
1893 value = operands.front();
1894 } else {
1895 value = moore::ConcatOp::create(builder, loc, operands).getResult();
1896 }
1897
1898 if (expr.getSliceSize() == 0) {
1899 return value;
1900 }
1901
1902 auto type = cast<moore::IntType>(value.getType());
1903 SmallVector<Value> slicedOperands;
1904 auto iterMax = type.getWidth() / expr.getSliceSize();
1905 auto remainSize = type.getWidth() % expr.getSliceSize();
1906
1907 for (size_t i = 0; i < iterMax; i++) {
1908 auto extractResultType = moore::IntType::get(
1909 context.getContext(), expr.getSliceSize(), type.getDomain());
1910
1911 auto extracted = moore::ExtractOp::create(builder, loc, extractResultType,
1912 value, i * expr.getSliceSize());
1913 slicedOperands.push_back(extracted);
1914 }
1915 // Handle other wire
1916 if (remainSize) {
1917 auto extractResultType = moore::IntType::get(
1918 context.getContext(), remainSize, type.getDomain());
1919
1920 auto extracted =
1921 moore::ExtractOp::create(builder, loc, extractResultType, value,
1922 iterMax * expr.getSliceSize());
1923 slicedOperands.push_back(extracted);
1924 }
1925
1926 return moore::ConcatOp::create(builder, loc, slicedOperands);
1927 }
1928
1929 Value visit(const slang::ast::AssertionInstanceExpression &expr) {
1930 return context.convertAssertionExpression(expr.body, loc);
1931 }
1932
1933 // A new class expression can stand for one of two things:
1934 // 1) A call to the `new` method (ctor) of a class made outside the scope of
1935 // the class
1936 // 2) A call to the `super.new` method, i.e. the constructor of the base
1937 // class, within the scope of a class, more specifically, within the new
1938 // method override of a class.
1939 // In the first case we should emit an allocation and a call to the ctor if it
1940 // exists (it's optional in System Verilog), in the second case we should emit
1941 // a call to the parent's ctor (System Verilog only has single inheritance, so
1942 // super is always unambiguous), but no allocation, as the child class' new
1943 // invocation already allocated space for both its own and its parent's
1944 // properties.
1945 Value visit(const slang::ast::NewClassExpression &expr) {
1946 auto type = context.convertType(*expr.type);
1947 auto classTy = dyn_cast<moore::ClassHandleType>(type);
1948 Value newObj;
1949
1950 // We are calling new from within a new function, and it's pointing to
1951 // super. Check the implicit this ref to figure out the super class type.
1952 // Do not allocate a new object.
1953 if (!classTy && expr.isSuperClass) {
1954 newObj = context.getImplicitThisRef();
1955 if (!newObj || !newObj.getType() ||
1956 !isa<moore::ClassHandleType>(newObj.getType())) {
1957 mlir::emitError(loc) << "implicit this ref was not set while "
1958 "converting new class function";
1959 return {};
1960 }
1961 auto thisType = cast<moore::ClassHandleType>(newObj.getType());
1962 auto classDecl =
1963 cast<moore::ClassDeclOp>(*context.symbolTable.lookupNearestSymbolFrom(
1964 context.intoModuleOp, thisType.getClassSym()));
1965 auto baseClassSym = classDecl.getBase();
1966 classTy = circt::moore::ClassHandleType::get(context.getContext(),
1967 baseClassSym.value());
1968 } else {
1969 // We are calling from outside a class; allocate space for the object.
1970 newObj = moore::ClassNewOp::create(builder, loc, classTy, {});
1971 }
1972
1973 const auto *constructor = expr.constructorCall();
1974 // If there's no ctor, we are done.
1975 if (!constructor)
1976 return newObj;
1977
1978 if (const auto *callConstructor =
1979 constructor->as_if<slang::ast::CallExpression>())
1980 if (const auto *subroutine =
1981 std::get_if<const slang::ast::SubroutineSymbol *>(
1982 &callConstructor->subroutine)) {
1983 // Bit paranoid, but virtually free checks that new is a class method
1984 // and the subroutine has already been converted.
1985 if (!(*subroutine)->thisVar) {
1986 mlir::emitError(loc) << "Expected subroutine called by new to use an "
1987 "implicit this reference";
1988 return {};
1989 }
1990 if (failed(context.convertFunction(**subroutine)))
1991 return {};
1992 // Pass the newObj as the implicit this argument of the ctor.
1993 auto savedThis = context.currentThisRef;
1994 context.currentThisRef = newObj;
1995 llvm::scope_exit restoreThis(
1996 [&] { context.currentThisRef = savedThis; });
1997 // Emit a call to ctor
1998 if (!visitCall(*callConstructor, *subroutine))
1999 return {};
2000 // Return new handle
2001 return newObj;
2002 }
2003 return {};
2004 }
2005
2006 /// Emit an error for all other expressions.
2007 template <typename T>
2008 Value visit(T &&node) {
2009 mlir::emitError(loc, "unsupported expression: ")
2010 << slang::ast::toString(node.kind);
2011 return {};
2012 }
2013
2014 Value visitInvalid(const slang::ast::Expression &expr) {
2015 mlir::emitError(loc, "invalid expression");
2016 return {};
2017 }
2018};
2019} // namespace
2020
2021//===----------------------------------------------------------------------===//
2022// Lvalue Conversion
2023//===----------------------------------------------------------------------===//
2024
2025namespace {
2026struct LvalueExprVisitor : public ExprVisitor {
2027 LvalueExprVisitor(Context &context, Location loc)
2028 : ExprVisitor(context, loc, /*isLvalue=*/true) {}
2029 using ExprVisitor::visit;
2030
2031 // Handle named values, such as references to declared variables.
2032 Value visit(const slang::ast::NamedValueExpression &expr) {
2033 // Handle local variables.
2034 if (auto value = context.valueSymbols.lookup(&expr.symbol))
2035 return value;
2036
2037 // Handle global variables.
2038 if (auto globalOp = context.globalVariables.lookup(&expr.symbol))
2039 return moore::GetGlobalVariableOp::create(builder, loc, globalOp);
2040
2041 if (auto *const property =
2042 expr.symbol.as_if<slang::ast::ClassPropertySymbol>()) {
2043 return visitClassProperty(context, *property);
2044 }
2045
2046 auto d = mlir::emitError(loc, "unknown name `") << expr.symbol.name << "`";
2047 d.attachNote(context.convertLocation(expr.symbol.location))
2048 << "no lvalue generated for " << slang::ast::toString(expr.symbol.kind);
2049 return {};
2050 }
2051
2052 // Handle hierarchical values, such as `Top.sub.var = x`.
2053 Value visit(const slang::ast::HierarchicalValueExpression &expr) {
2054 // Handle local variables.
2055 if (auto value = context.valueSymbols.lookup(&expr.symbol))
2056 return value;
2057
2058 // Handle global variables.
2059 if (auto globalOp = context.globalVariables.lookup(&expr.symbol))
2060 return moore::GetGlobalVariableOp::create(builder, loc, globalOp);
2061
2062 // Emit an error for those hierarchical values not recorded in the
2063 // `valueSymbols`.
2064 auto d = mlir::emitError(loc, "unknown hierarchical name `")
2065 << expr.symbol.name << "`";
2066 d.attachNote(context.convertLocation(expr.symbol.location))
2067 << "no lvalue generated for " << slang::ast::toString(expr.symbol.kind);
2068 return {};
2069 }
2070
2071 Value visit(const slang::ast::StreamingConcatenationExpression &expr) {
2072 SmallVector<Value> operands;
2073 for (auto stream : expr.streams()) {
2074 auto operandLoc = context.convertLocation(stream.operand->sourceRange);
2075 if (!stream.constantWithWidth.has_value() && stream.withExpr) {
2076 mlir::emitError(operandLoc)
2077 << "Moore only support streaming "
2078 "concatenation with fixed size 'with expression'";
2079 return {};
2080 }
2081 Value value;
2082 if (stream.constantWithWidth.has_value()) {
2083 value = context.convertLvalueExpression(*stream.withExpr);
2084 auto type = cast<moore::UnpackedType>(
2085 cast<moore::RefType>(value.getType()).getNestedType());
2086 auto intType = moore::RefType::get(moore::IntType::get(
2087 context.getContext(), type.getBitSize().value(), type.getDomain()));
2088 // Do not care if it's signed, because we will not do expansion.
2089 value = context.materializeConversion(intType, value, false, loc);
2090 } else {
2091 value = context.convertLvalueExpression(*stream.operand);
2092 }
2093
2094 if (!value)
2095 return {};
2096 operands.push_back(value);
2097 }
2098 Value value;
2099 if (operands.size() == 1) {
2100 // There must be at least one element, otherwise slang will report an
2101 // error.
2102 value = operands.front();
2103 } else {
2104 value = moore::ConcatRefOp::create(builder, loc, operands).getResult();
2105 }
2106
2107 if (expr.getSliceSize() == 0) {
2108 return value;
2109 }
2110
2111 auto type = cast<moore::IntType>(
2112 cast<moore::RefType>(value.getType()).getNestedType());
2113 SmallVector<Value> slicedOperands;
2114 auto widthSum = type.getWidth();
2115 auto domain = type.getDomain();
2116 auto iterMax = widthSum / expr.getSliceSize();
2117 auto remainSize = widthSum % expr.getSliceSize();
2118
2119 for (size_t i = 0; i < iterMax; i++) {
2120 auto extractResultType = moore::RefType::get(moore::IntType::get(
2121 context.getContext(), expr.getSliceSize(), domain));
2122
2123 auto extracted = moore::ExtractRefOp::create(
2124 builder, loc, extractResultType, value, i * expr.getSliceSize());
2125 slicedOperands.push_back(extracted);
2126 }
2127 // Handle other wire
2128 if (remainSize) {
2129 auto extractResultType = moore::RefType::get(
2130 moore::IntType::get(context.getContext(), remainSize, domain));
2131
2132 auto extracted =
2133 moore::ExtractRefOp::create(builder, loc, extractResultType, value,
2134 iterMax * expr.getSliceSize());
2135 slicedOperands.push_back(extracted);
2136 }
2137
2138 return moore::ConcatRefOp::create(builder, loc, slicedOperands);
2139 }
2140
2141 /// Emit an error for all other expressions.
2142 template <typename T>
2143 Value visit(T &&node) {
2144 return context.convertRvalueExpression(node);
2145 }
2146
2147 Value visitInvalid(const slang::ast::Expression &expr) {
2148 mlir::emitError(loc, "invalid expression");
2149 return {};
2150 }
2151};
2152} // namespace
2153
2154//===----------------------------------------------------------------------===//
2155// Entry Points
2156//===----------------------------------------------------------------------===//
2157
2158Value Context::convertRvalueExpression(const slang::ast::Expression &expr,
2159 Type requiredType) {
2160 auto loc = convertLocation(expr.sourceRange);
2161 auto value = expr.visit(RvalueExprVisitor(*this, loc));
2162 if (value && requiredType)
2163 value =
2164 materializeConversion(requiredType, value, expr.type->isSigned(), loc);
2165 return value;
2166}
2167
2168Value Context::convertLvalueExpression(const slang::ast::Expression &expr) {
2169 auto loc = convertLocation(expr.sourceRange);
2170 return expr.visit(LvalueExprVisitor(*this, loc));
2171}
2172// NOLINTEND(misc-no-recursion)
2173
2174/// Helper function to convert a value to its "truthy" boolean value.
2175Value Context::convertToBool(Value value) {
2176 if (!value)
2177 return {};
2178 if (auto type = dyn_cast_or_null<moore::IntType>(value.getType()))
2179 if (type.getBitSize() == 1)
2180 return value;
2181 if (auto type = dyn_cast_or_null<moore::UnpackedType>(value.getType()))
2182 return moore::BoolCastOp::create(builder, value.getLoc(), value);
2183 mlir::emitError(value.getLoc(), "expression of type ")
2184 << value.getType() << " cannot be cast to a boolean";
2185 return {};
2186}
2187
2188/// Materialize a Slang real literal as a constant op.
2189Value Context::materializeSVReal(const slang::ConstantValue &svreal,
2190 const slang::ast::Type &astType,
2191 Location loc) {
2192 const auto *floatType = astType.as_if<slang::ast::FloatingType>();
2193 assert(floatType);
2194
2195 FloatAttr attr;
2196 if (svreal.isShortReal() &&
2197 floatType->floatKind == slang::ast::FloatingType::ShortReal) {
2198 attr = FloatAttr::get(builder.getF32Type(), svreal.shortReal().v);
2199 } else if (svreal.isReal() &&
2200 floatType->floatKind == slang::ast::FloatingType::Real) {
2201 attr = FloatAttr::get(builder.getF64Type(), svreal.real().v);
2202 } else {
2203 mlir::emitError(loc) << "invalid real constant";
2204 return {};
2205 }
2206
2207 return moore::ConstantRealOp::create(builder, loc, attr);
2208}
2209
2210/// Materialize a Slang string literal as a literal string constant op.
2211Value Context::materializeString(const slang::ConstantValue &stringLiteral,
2212 const slang::ast::Type &astType,
2213 Location loc) {
2214 slang::ConstantValue intVal = stringLiteral.convertToInt();
2215 auto effectiveWidth = intVal.getEffectiveWidth();
2216 if (!effectiveWidth)
2217 return {};
2218
2219 auto intTy = moore::IntType::getInt(getContext(), effectiveWidth.value());
2220
2221 if (astType.isString()) {
2222 auto immInt = moore::ConstantStringOp::create(builder, loc, intTy,
2223 stringLiteral.toString())
2224 .getResult();
2225 return moore::IntToStringOp::create(builder, loc, immInt).getResult();
2226 }
2227 return {};
2228}
2229
2230/// Materialize a Slang integer literal as a constant op.
2231Value Context::materializeSVInt(const slang::SVInt &svint,
2232 const slang::ast::Type &astType, Location loc) {
2233 auto type = convertType(astType);
2234 if (!type)
2235 return {};
2236
2237 bool typeIsFourValued = false;
2238 if (auto unpackedType = dyn_cast<moore::UnpackedType>(type))
2239 typeIsFourValued = unpackedType.getDomain() == moore::Domain::FourValued;
2240
2241 auto fvint = convertSVIntToFVInt(svint);
2242 auto intType = moore::IntType::get(getContext(), fvint.getBitWidth(),
2243 fvint.hasUnknown() || typeIsFourValued
2246 auto result = moore::ConstantOp::create(builder, loc, intType, fvint);
2247 return materializeConversion(type, result, astType.isSigned(), loc);
2248}
2249
2251 const slang::ConstantValue &constant,
2252 const slang::ast::FixedSizeUnpackedArrayType &astType, Location loc) {
2253
2254 auto type = convertType(astType);
2255 if (!type)
2256 return {};
2257
2258 // Check whether underlying type is an integer, if so, get bit width
2259 unsigned bitWidth;
2260 if (astType.elementType.isIntegral())
2261 bitWidth = astType.elementType.getBitWidth();
2262 else
2263 return {};
2264
2265 bool typeIsFourValued = false;
2266
2267 // Check whether the underlying type is four-valued
2268 if (auto unpackedType = dyn_cast<moore::UnpackedType>(type))
2269 typeIsFourValued = unpackedType.getDomain() == moore::Domain::FourValued;
2270 else
2271 return {};
2272
2273 auto domain =
2275
2276 // Construct the integer type this is an unpacked array of; if possible keep
2277 // it two-valued, unless any entry is four-valued or the underlying type is
2278 // four-valued
2279 auto intType = moore::IntType::get(getContext(), bitWidth, domain);
2280 // Construct the full array type from intType
2281 auto arrType = moore::UnpackedArrayType::get(
2282 getContext(), constant.elements().size(), intType);
2283
2284 llvm::SmallVector<mlir::Value> elemVals;
2285 moore::ConstantOp constOp;
2286
2287 mlir::OpBuilder::InsertionGuard guard(builder);
2288
2289 // Add one ConstantOp for every element in the array
2290 for (auto elem : constant.elements()) {
2291 FVInt fvInt = convertSVIntToFVInt(elem.integer());
2292 constOp = moore::ConstantOp::create(builder, loc, intType, fvInt);
2293 elemVals.push_back(constOp.getResult());
2294 }
2295
2296 // Take the result of each ConstantOp and concatenate them into an array (of
2297 // constant values).
2298 auto arrayOp = moore::ArrayCreateOp::create(builder, loc, arrType, elemVals);
2299
2300 return arrayOp.getResult();
2301}
2302
2303Value Context::materializeConstant(const slang::ConstantValue &constant,
2304 const slang::ast::Type &type, Location loc) {
2305
2306 if (auto *arr = type.as_if<slang::ast::FixedSizeUnpackedArrayType>())
2307 return materializeFixedSizeUnpackedArrayType(constant, *arr, loc);
2308 if (constant.isInteger())
2309 return materializeSVInt(constant.integer(), type, loc);
2310 if (constant.isReal() || constant.isShortReal())
2311 return materializeSVReal(constant, type, loc);
2312 if (constant.isString())
2313 return materializeString(constant, type, loc);
2314
2315 return {};
2316}
2317
2318slang::ConstantValue
2319Context::evaluateConstant(const slang::ast::Expression &expr) {
2320 using slang::ast::EvalFlags;
2321 slang::ast::EvalContext evalContext(
2322 slang::ast::ASTContext(compilation.getRoot(),
2323 slang::ast::LookupLocation::max),
2324 EvalFlags::CacheResults | EvalFlags::SpecparamsAllowed);
2325 return expr.eval(evalContext);
2326}
2327
2328/// Helper function to convert a value to its "truthy" boolean value and
2329/// convert it to the given domain.
2330Value Context::convertToBool(Value value, Domain domain) {
2331 value = convertToBool(value);
2332 if (!value)
2333 return {};
2334 auto type = moore::IntType::get(getContext(), 1, domain);
2335 return materializeConversion(type, value, false, value.getLoc());
2336}
2337
2339 if (!value)
2340 return {};
2341 if (isa<moore::IntType>(value.getType()))
2342 return value;
2343
2344 // Some operations in Slang's AST, for example bitwise or `|`, don't cast
2345 // packed struct/array operands to simple bit vectors but directly operate
2346 // on the struct/array. Since the corresponding IR ops operate only on
2347 // simple bit vectors, insert a conversion in this case.
2348 if (auto packed = dyn_cast<moore::PackedType>(value.getType()))
2349 if (auto sbvType = packed.getSimpleBitVector())
2350 return materializeConversion(sbvType, value, false, value.getLoc());
2351
2352 mlir::emitError(value.getLoc()) << "expression of type " << value.getType()
2353 << " cannot be cast to a simple bit vector";
2354 return {};
2355}
2356
2357/// Create the necessary operations to convert from a `PackedType` to the
2358/// corresponding simple bit vector `IntType`. This will apply special handling
2359/// to time values, which requires scaling by the local timescale.
2361 Location loc) {
2362 if (isa<moore::IntType>(value.getType()))
2363 return value;
2364
2365 auto &builder = context.builder;
2366 auto packedType = cast<moore::PackedType>(value.getType());
2367 auto intType = packedType.getSimpleBitVector();
2368 assert(intType);
2369
2370 // If we are converting from a time to an integer, divide the integer by the
2371 // timescale.
2372 if (isa<moore::TimeType>(packedType) &&
2374 value = builder.createOrFold<moore::TimeToLogicOp>(loc, value);
2375 auto scale = moore::ConstantOp::create(builder, loc, intType,
2377 return builder.createOrFold<moore::DivUOp>(loc, value, scale);
2378 }
2379
2380 // If this is an aggregate type, make sure that it does not contain any
2381 // `TimeType` fields. These require special conversion to ensure that the
2382 // local timescale is in effect.
2383 if (packedType.containsTimeType()) {
2384 mlir::emitError(loc) << "unsupported conversion: " << packedType
2385 << " cannot be converted to " << intType
2386 << "; contains a time type";
2387 return {};
2388 }
2389
2390 // Otherwise create a simple `PackedToSBVOp` for the conversion.
2391 return builder.createOrFold<moore::PackedToSBVOp>(loc, value);
2392}
2393
2394/// Create the necessary operations to convert from a simple bit vector
2395/// `IntType` to an equivalent `PackedType`. This will apply special handling to
2396/// time values, which requires scaling by the local timescale.
2398 moore::PackedType packedType,
2399 Value value, Location loc) {
2400 if (value.getType() == packedType)
2401 return value;
2402
2403 auto &builder = context.builder;
2404 auto intType = cast<moore::IntType>(value.getType());
2405 assert(intType && intType == packedType.getSimpleBitVector());
2406
2407 // If we are converting from an integer to a time, multiply the integer by the
2408 // timescale.
2409 if (isa<moore::TimeType>(packedType) &&
2411 auto scale = moore::ConstantOp::create(builder, loc, intType,
2413 value = builder.createOrFold<moore::MulOp>(loc, value, scale);
2414 return builder.createOrFold<moore::LogicToTimeOp>(loc, value);
2415 }
2416
2417 // If this is an aggregate type, make sure that it does not contain any
2418 // `TimeType` fields. These require special conversion to ensure that the
2419 // local timescale is in effect.
2420 if (packedType.containsTimeType()) {
2421 mlir::emitError(loc) << "unsupported conversion: " << intType
2422 << " cannot be converted to " << packedType
2423 << "; contains a time type";
2424 return {};
2425 }
2426
2427 // Otherwise create a simple `PackedToSBVOp` for the conversion.
2428 return builder.createOrFold<moore::SBVToPackedOp>(loc, packedType, value);
2429}
2430
2431/// Check whether the actual handle is a subclass of another handle type
2432/// and return a properly upcast version if so.
2433static mlir::Value maybeUpcastHandle(Context &context, mlir::Value actualHandle,
2434 moore::ClassHandleType expectedHandleTy) {
2435 auto loc = actualHandle.getLoc();
2436
2437 auto actualTy = actualHandle.getType();
2438 auto actualHandleTy = dyn_cast<moore::ClassHandleType>(actualTy);
2439 if (!actualHandleTy) {
2440 mlir::emitError(loc) << "expected a !moore.class<...> value, got "
2441 << actualTy;
2442 return {};
2443 }
2444
2445 // Fast path: already the expected handle type.
2446 if (actualHandleTy == expectedHandleTy)
2447 return actualHandle;
2448
2449 if (!context.isClassDerivedFrom(actualHandleTy, expectedHandleTy)) {
2450 mlir::emitError(loc)
2451 << "receiver class " << actualHandleTy.getClassSym()
2452 << " is not the same as, or derived from, expected base class "
2453 << expectedHandleTy.getClassSym().getRootReference();
2454 return {};
2455 }
2456
2457 // Only implicit upcasting is allowed - down casting should never be implicit.
2458 auto casted = moore::ClassUpcastOp::create(context.builder, loc,
2459 expectedHandleTy, actualHandle)
2460 .getResult();
2461 return casted;
2462}
2463
2464Value Context::materializeConversion(Type type, Value value, bool isSigned,
2465 Location loc) {
2466 // Nothing to do if the types are already equal.
2467 if (type == value.getType())
2468 return value;
2469
2470 // Handle packed types which can be converted to a simple bit vector. This
2471 // allows us to perform resizing and domain casting on that bit vector.
2472 auto dstPacked = dyn_cast<moore::PackedType>(type);
2473 auto srcPacked = dyn_cast<moore::PackedType>(value.getType());
2474 auto dstInt = dstPacked ? dstPacked.getSimpleBitVector() : moore::IntType();
2475 auto srcInt = srcPacked ? srcPacked.getSimpleBitVector() : moore::IntType();
2476
2477 if (dstInt && srcInt) {
2478 // Convert the value to a simple bit vector if it isn't one already.
2479 value = materializePackedToSBVConversion(*this, value, loc);
2480 if (!value)
2481 return {};
2482
2483 // Create truncation or sign/zero extension ops depending on the source and
2484 // destination width.
2485 auto resizedType = moore::IntType::get(
2486 value.getContext(), dstInt.getWidth(), srcPacked.getDomain());
2487 if (dstInt.getWidth() < srcInt.getWidth()) {
2488 value = builder.createOrFold<moore::TruncOp>(loc, resizedType, value);
2489 } else if (dstInt.getWidth() > srcInt.getWidth()) {
2490 if (isSigned)
2491 value = builder.createOrFold<moore::SExtOp>(loc, resizedType, value);
2492 else
2493 value = builder.createOrFold<moore::ZExtOp>(loc, resizedType, value);
2494 }
2495
2496 // Convert the domain if needed.
2497 if (dstInt.getDomain() != srcInt.getDomain()) {
2498 if (dstInt.getDomain() == moore::Domain::TwoValued)
2499 value = builder.createOrFold<moore::LogicToIntOp>(loc, value);
2500 else if (dstInt.getDomain() == moore::Domain::FourValued)
2501 value = builder.createOrFold<moore::IntToLogicOp>(loc, value);
2502 }
2503
2504 // Convert the value from a simple bit vector back to the packed type.
2505 value = materializeSBVToPackedConversion(*this, dstPacked, value, loc);
2506 if (!value)
2507 return {};
2508
2509 assert(value.getType() == type);
2510 return value;
2511 }
2512
2513 // Convert from FormatStringType to StringType
2514 if (isa<moore::StringType>(type) &&
2515 isa<moore::FormatStringType>(value.getType())) {
2516 return builder.createOrFold<moore::FormatStringToStringOp>(loc, value);
2517 }
2518
2519 // Convert from StringType to FormatStringType
2520 if (isa<moore::FormatStringType>(type) &&
2521 isa<moore::StringType>(value.getType())) {
2522 return builder.createOrFold<moore::FormatStringOp>(loc, value);
2523 }
2524
2525 // Handle Real To Int conversion
2526 if (isa<moore::IntType>(type) && isa<moore::RealType>(value.getType())) {
2527 auto twoValInt = builder.createOrFold<moore::RealToIntOp>(
2528 loc, dyn_cast<moore::IntType>(type).getTwoValued(), value);
2529
2530 if (dyn_cast<moore::IntType>(type).getDomain() == moore::Domain::FourValued)
2531 return materializePackedToSBVConversion(*this, twoValInt, loc);
2532 return twoValInt;
2533 }
2534
2535 // Handle Int to Real conversion
2536 if (isa<moore::RealType>(type) && isa<moore::IntType>(value.getType())) {
2537 Value twoValInt;
2538 // Check if int needs to be converted to two-valued first
2539 if (dyn_cast<moore::IntType>(value.getType()).getDomain() ==
2541 twoValInt = value;
2542 else
2543 twoValInt = materializeConversion(
2544 dyn_cast<moore::IntType>(value.getType()).getTwoValued(), value, true,
2545 loc);
2546
2547 if (isSigned)
2548 return builder.createOrFold<moore::SIntToRealOp>(loc, type, twoValInt);
2549 return builder.createOrFold<moore::UIntToRealOp>(loc, type, twoValInt);
2550 }
2551
2552 auto getBuiltinFloatType = [&](moore::RealType type) -> Type {
2553 if (type.getWidth() == moore::RealWidth::f32)
2554 return mlir::Float32Type::get(builder.getContext());
2555
2556 return mlir::Float64Type::get(builder.getContext());
2557 };
2558
2559 // Handle f64/f32 to time conversion
2560 if (isa<moore::TimeType>(type) && isa<moore::RealType>(value.getType())) {
2561 auto intType =
2562 moore::IntType::get(builder.getContext(), 64, Domain::TwoValued);
2563 Type floatType =
2564 getBuiltinFloatType(cast<moore::RealType>(value.getType()));
2565 auto scale = moore::ConstantRealOp::create(
2566 builder, loc, value.getType(),
2567 FloatAttr::get(floatType, getTimeScaleInFemtoseconds(*this)));
2568 auto scaled = builder.createOrFold<moore::MulRealOp>(loc, value, scale);
2569 auto asInt = moore::RealToIntOp::create(builder, loc, intType, scaled);
2570 auto asLogic = moore::IntToLogicOp::create(builder, loc, asInt);
2571 return moore::LogicToTimeOp::create(builder, loc, asLogic);
2572 }
2573
2574 // Handle time to f64/f32 conversion
2575 if (isa<moore::RealType>(type) && isa<moore::TimeType>(value.getType())) {
2576 auto asLogic = moore::TimeToLogicOp::create(builder, loc, value);
2577 auto asInt = moore::LogicToIntOp::create(builder, loc, asLogic);
2578 auto asReal = moore::UIntToRealOp::create(builder, loc, type, asInt);
2579 Type floatType = getBuiltinFloatType(cast<moore::RealType>(type));
2580 auto scale = moore::ConstantRealOp::create(
2581 builder, loc, type,
2582 FloatAttr::get(floatType, getTimeScaleInFemtoseconds(*this)));
2583 return moore::DivRealOp::create(builder, loc, asReal, scale);
2584 }
2585
2586 // Handle Int to String
2587 if (isa<moore::StringType>(type)) {
2588 if (auto intType = dyn_cast<moore::IntType>(value.getType())) {
2589 if (intType.getDomain() == moore::Domain::FourValued)
2590 value = moore::LogicToIntOp::create(builder, loc, value);
2591 return moore::IntToStringOp::create(builder, loc, value);
2592 }
2593 }
2594
2595 // Handle String to Int
2596 if (auto intType = dyn_cast<moore::IntType>(type)) {
2597 if (isa<moore::StringType>(value.getType())) {
2598 value = moore::StringToIntOp::create(builder, loc, intType.getTwoValued(),
2599 value);
2600
2601 if (intType.getDomain() == moore::Domain::FourValued)
2602 return moore::IntToLogicOp::create(builder, loc, value);
2603
2604 return value;
2605 }
2606 }
2607
2608 // Handle Int to FormatString
2609 if (isa<moore::FormatStringType>(type)) {
2610 auto asStr = materializeConversion(moore::StringType::get(getContext()),
2611 value, isSigned, loc);
2612 if (!asStr)
2613 return {};
2614 return moore::FormatStringOp::create(builder, loc, asStr, {}, {}, {});
2615 }
2616
2617 if (isa<moore::RealType>(type) && isa<moore::RealType>(value.getType()))
2618 return builder.createOrFold<moore::ConvertRealOp>(loc, type, value);
2619
2620 if (isa<moore::ClassHandleType>(type) &&
2621 isa<moore::ClassHandleType>(value.getType()))
2622 return maybeUpcastHandle(*this, value, cast<moore::ClassHandleType>(type));
2623
2624 // TODO: Handle other conversions with dedicated ops.
2625 if (value.getType() != type)
2626 value = moore::ConversionOp::create(builder, loc, type, value);
2627 return value;
2628}
2629
2630FailureOr<Value>
2631Context::convertSystemCallArity0(const slang::ast::SystemSubroutine &subroutine,
2632 Location loc) {
2633
2634 auto systemCallRes =
2635 llvm::StringSwitch<std::function<FailureOr<Value>()>>(subroutine.name)
2636 .Case("$urandom",
2637 [&]() -> Value {
2638 return moore::UrandomBIOp::create(builder, loc, nullptr);
2639 })
2640 .Case("$random",
2641 [&]() -> Value {
2642 return moore::RandomBIOp::create(builder, loc, nullptr);
2643 })
2644 .Case(
2645 "$time",
2646 [&]() -> Value { return moore::TimeBIOp::create(builder, loc); })
2647 .Case(
2648 "$stime",
2649 [&]() -> Value { return moore::TimeBIOp::create(builder, loc); })
2650 .Case(
2651 "$realtime",
2652 [&]() -> Value { return moore::TimeBIOp::create(builder, loc); })
2653 .Default([&]() -> Value { return {}; });
2654 return systemCallRes();
2655}
2656
2657FailureOr<Value>
2658Context::convertSystemCallArity1(const slang::ast::SystemSubroutine &subroutine,
2659 Location loc, Value value) {
2660 auto systemCallRes =
2661 llvm::StringSwitch<std::function<FailureOr<Value>()>>(subroutine.name)
2662 // Signed and unsigned system functions.
2663 .Case("$signed", [&]() { return value; })
2664 .Case("$unsigned", [&]() { return value; })
2665
2666 // Math functions in SystemVerilog.
2667 .Case("$clog2",
2668 [&]() -> FailureOr<Value> {
2669 value = convertToSimpleBitVector(value);
2670 if (!value)
2671 return failure();
2672 return (Value)moore::Clog2BIOp::create(builder, loc, value);
2673 })
2674 .Case("$ln",
2675 [&]() -> Value {
2676 return moore::LnBIOp::create(builder, loc, value);
2677 })
2678 .Case("$log10",
2679 [&]() -> Value {
2680 return moore::Log10BIOp::create(builder, loc, value);
2681 })
2682 .Case("$sin",
2683 [&]() -> Value {
2684 return moore::SinBIOp::create(builder, loc, value);
2685 })
2686 .Case("$cos",
2687 [&]() -> Value {
2688 return moore::CosBIOp::create(builder, loc, value);
2689 })
2690 .Case("$tan",
2691 [&]() -> Value {
2692 return moore::TanBIOp::create(builder, loc, value);
2693 })
2694 .Case("$exp",
2695 [&]() -> Value {
2696 return moore::ExpBIOp::create(builder, loc, value);
2697 })
2698 .Case("$sqrt",
2699 [&]() -> Value {
2700 return moore::SqrtBIOp::create(builder, loc, value);
2701 })
2702 .Case("$floor",
2703 [&]() -> Value {
2704 return moore::FloorBIOp::create(builder, loc, value);
2705 })
2706 .Case("$ceil",
2707 [&]() -> Value {
2708 return moore::CeilBIOp::create(builder, loc, value);
2709 })
2710 .Case("$asin",
2711 [&]() -> Value {
2712 return moore::AsinBIOp::create(builder, loc, value);
2713 })
2714 .Case("$acos",
2715 [&]() -> Value {
2716 return moore::AcosBIOp::create(builder, loc, value);
2717 })
2718 .Case("$atan",
2719 [&]() -> Value {
2720 return moore::AtanBIOp::create(builder, loc, value);
2721 })
2722 .Case("$sinh",
2723 [&]() -> Value {
2724 return moore::SinhBIOp::create(builder, loc, value);
2725 })
2726 .Case("$cosh",
2727 [&]() -> Value {
2728 return moore::CoshBIOp::create(builder, loc, value);
2729 })
2730 .Case("$tanh",
2731 [&]() -> Value {
2732 return moore::TanhBIOp::create(builder, loc, value);
2733 })
2734 .Case("$asinh",
2735 [&]() -> Value {
2736 return moore::AsinhBIOp::create(builder, loc, value);
2737 })
2738 .Case("$acosh",
2739 [&]() -> Value {
2740 return moore::AcoshBIOp::create(builder, loc, value);
2741 })
2742 .Case("$atanh",
2743 [&]() -> Value {
2744 return moore::AtanhBIOp::create(builder, loc, value);
2745 })
2746 .Case("$urandom",
2747 [&]() -> Value {
2748 return moore::UrandomBIOp::create(builder, loc, value);
2749 })
2750 .Case("$random",
2751 [&]() -> Value {
2752 return moore::RandomBIOp::create(builder, loc, value);
2753 })
2754 .Case("$realtobits",
2755 [&]() -> Value {
2756 return moore::RealtobitsBIOp::create(builder, loc, value);
2757 })
2758 .Case("$bitstoreal",
2759 [&]() -> Value {
2760 return moore::BitstorealBIOp::create(builder, loc, value);
2761 })
2762 .Case("$shortrealtobits",
2763 [&]() -> Value {
2764 return moore::ShortrealtobitsBIOp::create(builder, loc,
2765 value);
2766 })
2767 .Case("$bitstoshortreal",
2768 [&]() -> Value {
2769 return moore::BitstoshortrealBIOp::create(builder, loc,
2770 value);
2771 })
2772 .Case("len",
2773 [&]() -> Value {
2774 if (isa<moore::StringType>(value.getType()))
2775 return moore::StringLenOp::create(builder, loc, value);
2776 return {};
2777 })
2778 .Case("toupper",
2779 [&]() -> Value {
2780 return moore::StringToUpperOp::create(builder, loc, value);
2781 })
2782 .Case("size",
2783 [&]() -> Value {
2784 if (isa<moore::QueueType>(value.getType()))
2785 return moore::QueueSizeBIOp::create(builder, loc, value);
2786 return {};
2787 })
2788 .Case("tolower",
2789 [&]() -> Value {
2790 return moore::StringToLowerOp::create(builder, loc, value);
2791 })
2792 .Case("pop_back",
2793 [&]() -> Value {
2794 if (isa<moore::QueueType>(value.getType()))
2795 return moore::QueuePopBackOp::create(builder, loc, value);
2796 return {};
2797 })
2798 .Case("pop_front",
2799 [&]() -> Value {
2800 if (isa<moore::QueueType>(value.getType()))
2801 return moore::QueuePopFrontOp::create(builder, loc, value);
2802 return {};
2803 })
2804 .Default([&]() -> Value { return {}; });
2805 return systemCallRes();
2806}
2807
2808FailureOr<Value>
2809Context::convertSystemCallArity2(const slang::ast::SystemSubroutine &subroutine,
2810 Location loc, Value value1, Value value2) {
2811 auto systemCallRes =
2812 llvm::StringSwitch<std::function<FailureOr<Value>()>>(subroutine.name)
2813 .Case("getc",
2814 [&]() -> Value {
2815 return moore::StringGetCOp::create(builder, loc, value1,
2816 value2);
2817 })
2818 .Case("push_back",
2819 [&]() -> Value {
2820 if (isa<moore::QueueType>(value1.getType()))
2821 return moore::QueuePushBackOp::create(builder, loc, value1,
2822 value2);
2823 return {};
2824 })
2825 .Case("push_front",
2826 [&]() -> Value {
2827 if (isa<moore::QueueType>(value1.getType()))
2828 return moore::QueuePushFrontOp::create(builder, loc, value1,
2829 value2);
2830 return {};
2831 })
2832 .Default([&]() -> Value { return {}; });
2833 return systemCallRes();
2834}
2835
2836// Resolve any (possibly nested) SymbolRefAttr to an op from the root.
2837static mlir::Operation *resolve(Context &context, mlir::SymbolRefAttr sym) {
2838 return context.symbolTable.lookupNearestSymbolFrom(context.intoModuleOp, sym);
2839}
2840
2841bool Context::isClassDerivedFrom(const moore::ClassHandleType &actualTy,
2842 const moore::ClassHandleType &baseTy) {
2843 if (!actualTy || !baseTy)
2844 return false;
2845
2846 mlir::SymbolRefAttr actualSym = actualTy.getClassSym();
2847 mlir::SymbolRefAttr baseSym = baseTy.getClassSym();
2848
2849 if (actualSym == baseSym)
2850 return true;
2851
2852 auto *op = resolve(*this, actualSym);
2853 auto decl = llvm::dyn_cast_or_null<moore::ClassDeclOp>(op);
2854 // Walk up the inheritance chain via ClassDeclOp::$base (SymbolRefAttr).
2855 while (decl) {
2856 mlir::SymbolRefAttr curBase = decl.getBaseAttr();
2857 if (!curBase)
2858 break;
2859 if (curBase == baseSym)
2860 return true;
2861 decl = llvm::dyn_cast_or_null<moore::ClassDeclOp>(resolve(*this, curBase));
2862 }
2863 return false;
2864}
2865
2866moore::ClassHandleType
2867Context::getAncestorClassWithProperty(const moore::ClassHandleType &actualTy,
2868 llvm::StringRef fieldName, Location loc) {
2869 // Start at the actual class symbol.
2870 mlir::SymbolRefAttr classSym = actualTy.getClassSym();
2871
2872 while (classSym) {
2873 // Resolve the class declaration from the root symbol table owner.
2874 auto *op = resolve(*this, classSym);
2875 auto decl = llvm::dyn_cast_or_null<moore::ClassDeclOp>(op);
2876 if (!decl)
2877 break;
2878
2879 // Scan the class body for a property with the requested symbol name.
2880 for (auto &block : decl.getBody()) {
2881 for (auto &opInBlock : block) {
2882 if (auto prop =
2883 llvm::dyn_cast<moore::ClassPropertyDeclOp>(&opInBlock)) {
2884 if (prop.getSymName() == fieldName) {
2885 // Found a declaring ancestor: return its handle type.
2886 return moore::ClassHandleType::get(actualTy.getContext(), classSym);
2887 }
2888 }
2889 }
2890 }
2891
2892 // Not found here—climb to the base class (if any) and continue.
2893 classSym = decl.getBaseAttr(); // may be null; loop ends if so
2894 }
2895
2896 // No ancestor declares that property.
2897 mlir::emitError(loc) << "unknown property `" << fieldName << "`";
2898 return {};
2899}
assert(baseType &&"element must be base type")
static std::unique_ptr< Context > context
static Value materializeSBVToPackedConversion(Context &context, moore::PackedType packedType, Value value, Location loc)
Create the necessary operations to convert from a simple bit vector IntType to an equivalent PackedTy...
static mlir::Value maybeUpcastHandle(Context &context, mlir::Value actualHandle, moore::ClassHandleType expectedHandleTy)
Check whether the actual handle is a subclass of another handle type and return a properly upcast ver...
static mlir::Operation * resolve(Context &context, mlir::SymbolRefAttr sym)
static Value visitClassProperty(Context &context, const slang::ast::ClassPropertySymbol &expr)
static uint64_t getTimeScaleInFemtoseconds(Context &context)
Get the currently active timescale as an integer number of femtoseconds.
static Value materializePackedToSBVConversion(Context &context, Value value, Location loc)
Create the necessary operations to convert from a PackedType to the corresponding simple bit vector I...
static Value getSelectIndex(Context &context, Location loc, Value index, const slang::ConstantRange &range)
Map an index into an array, with bounds range, to a bit offset of the underlying bit storage.
static FVInt convertSVIntToFVInt(const slang::SVInt &svint)
Convert a Slang SVInt to a CIRCT FVInt.
Four-valued arbitrary precision integers.
Definition FVInt.h:37
A packed SystemVerilog type.
Definition MooreTypes.h:153
bool containsTimeType() const
Check if this is a TimeType, or an aggregate that contains a nested TimeType.
IntType getSimpleBitVector() const
Get the simple bit vector type equivalent to this packed type.
void info(Twine message)
Definition LSPUtils.cpp:20
Domain
The number of values each bit of a type can assume.
Definition MooreTypes.h:49
@ FourValued
Four-valued types such as logic or integer.
@ TwoValued
Two-valued types such as bit or int.
bool isIntType(Type type, unsigned width)
Check if a type is an IntType type of the given width.
@ f32
A standard 32-Bit floating point number ("float")
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.
Value materializeFixedSizeUnpackedArrayType(const slang::ConstantValue &constant, const slang::ast::FixedSizeUnpackedArrayType &astType, Location loc)
Helper function to materialize an unpacked array of SVInts as an SSA value.
bool isClassDerivedFrom(const moore::ClassHandleType &actualTy, const moore::ClassHandleType &baseTy)
Checks whether one class (actualTy) is derived from another class (baseTy).
Type convertType(const slang::ast::Type &type, LocationAttr loc={})
Convert a slang type into an MLIR type.
Definition Types.cpp:206
Value materializeSVInt(const slang::SVInt &svint, const slang::ast::Type &type, Location loc)
Helper function to materialize an SVInt as an SSA value.
Value materializeSVReal(const slang::ConstantValue &svreal, const slang::ast::Type &type, Location loc)
Helper function to materialize a real value as an SSA value.
Value convertToBool(Value value)
Helper function to convert a value to its "truthy" boolean value.
moore::ClassHandleType getAncestorClassWithProperty(const moore::ClassHandleType &actualTy, StringRef fieldName, Location loc)
Tries to find the closest base class of actualTy that carries a property with name fieldName.
Value convertRvalueExpression(const slang::ast::Expression &expr, Type requiredType={})
FailureOr< Value > convertSystemCallArity0(const slang::ast::SystemSubroutine &subroutine, Location loc)
Convert system function calls only have arity-0.
Value convertToSimpleBitVector(Value value)
Helper function to convert a value to its simple bit vector representation, if it has one.
Value materializeString(const slang::ConstantValue &string, const slang::ast::Type &astType, Location loc)
Helper function to materialize a string as an SSA value.
FailureOr< Value > convertSystemCallArity1(const slang::ast::SystemSubroutine &subroutine, Location loc, Value value)
Convert system function calls only have arity-1.
MLIRContext * getContext()
Return the MLIR context.
FailureOr< Value > convertSystemCallArity2(const slang::ast::SystemSubroutine &subroutine, Location loc, Value value1, Value value2)
Convert system function calls with arity-2.
Location convertLocation(slang::SourceLocation loc)
Convert a slang SourceLocation into an MLIR Location.