Skip to content

Commit

Permalink
[WIP][MooreToCore] Fix conversion to vector of bit
Browse files Browse the repository at this point in the history
$ ./bin/circt-verilog /tmp/string_bit_array.sv
module {
  hw.module @top() {
    %0 = llhd.constant_time <0ns, 0d, 1e>
    %c0_i80 = hw.constant 0 : i80
    %c6_i4 = hw.constant 6 : i4
    %c0_i112 = hw.constant 0 : i112
    %a = llhd.sig %c0_i112 : i112
    llhd.process {
      %1 = builtin.unrealized_conversion_cast %c6_i4 : i4 to i32
      %2 = comb.concat %c0_i80, %1 : i80, i32
      llhd.drv %a, %2 after %0 : !hw.inout<i112>
      llhd.halt
    }
    hw.output
  }
}
  • Loading branch information
jpinot committed Oct 27, 2024
1 parent d171d4a commit a1747a3
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions lib/Conversion/MooreToCore/MooreToCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,23 +558,19 @@ struct ConstantOpConv : public OpConversionPattern<ConstantOp> {
struct StringConstantOpConv : public OpConversionPattern<StringConstantOp> {
using OpConversionPattern::OpConversionPattern;
LogicalResult
matchAndRewrite(StringConstantOp op, OpAdaptor adaptor,
matchAndRewrite(moore::StringConstantOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
// Retrieve the string constant from the op
StringRef stringValue = op.getValue();

// Define the target bit width and radix
unsigned radix = 10; // For decimal interpretation; change to 16 for hex, etc.

// Parse the string as an integer using StringRef::getAsInteger
APInt value;
stringValue.getAsInteger(radix, value);
auto type = rewriter.getIntegerType(value.getBitWidth());

/* rewriter.replaceOpWithNewOp<hw::ConstantOp>(op, type, value); */
rewriter.replaceOpWithNewOp<hw::ConstantOp>(
op, type, rewriter.getIntegerAttr(type, value));

StringRef str = op.getValue();
const auto byteSize = sizeof(uint8_t);
const unsigned byteWidth = str.size() * byteSize;
APInt value(byteWidth, 0);
for (size_t i = 0; i < str.size(); ++i) {
uint8_t asciiChar = static_cast<uint8_t>(str[i]);
value |= APInt(byteWidth, asciiChar) << (byteSize * (str.size() - 1 - i));
}
auto type = IntegerType::get(rewriter.getContext(), byteWidth);
rewriter.replaceOpWithNewOp<hw::ConstantOp>( op, type,
rewriter.getIntegerAttr(type, value));
return success();
}
};
Expand Down

0 comments on commit a1747a3

Please sign in to comment.