diff --git a/lib/Conversion/ImportVerilog/Expressions.cpp b/lib/Conversion/ImportVerilog/Expressions.cpp index 813a14a194..d79f7c5099 100644 --- a/lib/Conversion/ImportVerilog/Expressions.cpp +++ b/lib/Conversion/ImportVerilog/Expressions.cpp @@ -361,9 +361,15 @@ struct RvalueExprVisitor { << value.getBitWidth() << " bits wide; only 64 supported"; return {}; } + auto intType = + moore::IntType::get(context.getContext(), value.getBitWidth(), + value.hasUnknown() ? moore::Domain::FourValued + : moore::Domain::TwoValued); auto truncValue = value.as().value(); - return builder.create(loc, cast(type), - truncValue); + Value result = builder.create(loc, intType, truncValue); + if (result.getType() != type) + result = builder.create(loc, type, result); + return result; } // Handle `'0`, `'1`, `'x`, and `'z` literals. diff --git a/test/Conversion/ImportVerilog/basic.sv b/test/Conversion/ImportVerilog/basic.sv index 2e314ddbda..55c55e3e37 100644 --- a/test/Conversion/ImportVerilog/basic.sv +++ b/test/Conversion/ImportVerilog/basic.sv @@ -997,6 +997,12 @@ module Conversion; // CHECK: [[TMP2:%.+]] = moore.conversion [[TMP1]] : !moore.i32 -> !moore.i19 // CHECK: %e = moore.variable [[TMP2]] bit signed [18:0] e = 19'(b); + + // Implicit conversion for literals. + // CHECK: [[TMP1:%.+]] = moore.constant 0 : i64 + // CHECK: [[TMP2:%.+]] = moore.conversion [[TMP1]] : !moore.i64 -> !moore.struct<{a: i32, b: i32}> + // CHECK: %f = moore.variable [[TMP2]] + struct packed { int a; int b; } f = '0; endmodule // CHECK-LABEL: moore.module @PortsTop