Skip to content

Commit f10c765

Browse files
committed
Add support for casting of constants to mixed type values
1 parent a06b40b commit f10c765

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/engine/internal/llvm/llvmbuildutils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ llvm::Value *LLVMBuildUtils::addAlloca(llvm::Type *type)
402402
llvm::Value *LLVMBuildUtils::castValue(LLVMRegister *reg, Compiler::StaticType targetType)
403403
{
404404
if (reg->isConst()) {
405-
if (targetType == Compiler::StaticType::Unknown)
405+
if (!isSingleType(targetType))
406406
return createValue(reg);
407407
else
408408
return castConstValue(reg->constValue(), targetType);

test/llvm/llvmcodebuilder_test.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,30 @@ TEST_F(LLVMCodeBuilderTest, ConstCasting)
196196
ASSERT_EQ(testing::internal::GetCapturedStdout(), expected);
197197
}
198198

199+
TEST_F(LLVMCodeBuilderTest, ConstCasting_ToMixedTypes)
200+
{
201+
LLVMCodeBuilder *builder = m_utils.createBuilder(true);
202+
203+
CompilerValue *v = builder->addConstValue(true);
204+
builder->addFunctionCall("test_print_unknown", Compiler::StaticType::Void, { Compiler::StaticType::Number | Compiler::StaticType::Bool }, { v });
205+
v = builder->addConstValue(-24.156);
206+
builder->addFunctionCall("test_print_unknown", Compiler::StaticType::Void, { Compiler::StaticType::Number | Compiler::StaticType::Bool }, { v });
207+
208+
auto code = builder->build();
209+
Script script(&m_utils.target(), nullptr, nullptr);
210+
script.setCode(code);
211+
Thread thread(&m_utils.target(), nullptr, &script);
212+
auto ctx = code->createExecutionContext(&thread);
213+
214+
static const std::string expected =
215+
"true\n"
216+
"-24.156\n";
217+
218+
testing::internal::CaptureStdout();
219+
code->run(ctx.get());
220+
ASSERT_EQ(testing::internal::GetCapturedStdout(), expected);
221+
}
222+
199223
TEST_F(LLVMCodeBuilderTest, RawValueCasting)
200224
{
201225
LLVMCodeBuilder *builder = m_utils.createBuilder(true);

0 commit comments

Comments
 (0)