Skip to content

Commit d991b67

Browse files
committed
LLVMCodeBuilder: Add support for dynamically typed parameters
1 parent e24fdd8 commit d991b67

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

src/engine/internal/llvm/llvmcodebuilder.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,8 +2169,12 @@ void LLVMCodeBuilder::freeScopeHeap()
21692169

21702170
llvm::Value *LLVMCodeBuilder::castValue(LLVMRegister *reg, Compiler::StaticType targetType)
21712171
{
2172-
if (reg->isConst())
2173-
return castConstValue(reg->constValue(), targetType);
2172+
if (reg->isConst()) {
2173+
if (targetType == Compiler::StaticType::Unknown)
2174+
return createValue(reg);
2175+
else
2176+
return castConstValue(reg->constValue(), targetType);
2177+
}
21742178

21752179
if (reg->isRawValue)
21762180
return castRawValue(reg, targetType);
@@ -2391,6 +2395,9 @@ llvm::Type *LLVMCodeBuilder::getType(Compiler::StaticType type)
23912395
case Compiler::StaticType::Pointer:
23922396
return m_builder.getVoidTy()->getPointerTo();
23932397

2398+
case Compiler::StaticType::Unknown:
2399+
return m_valueDataType->getPointerTo();
2400+
23942401
default:
23952402
assert(false);
23962403
return nullptr;

test/llvm/llvmcodebuilder_test.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,24 @@ TEST_F(LLVMCodeBuilderTest, FunctionCalls)
477477
{ Compiler::StaticType::String, Compiler::StaticType::String, Compiler::StaticType::String },
478478
{ v, v1, v2 });
479479
m_builder->addTargetFunctionCall("test_function_1_arg", Compiler::StaticType::Void, { Compiler::StaticType::String }, { v });
480+
481+
v = m_builder->addConstValue(123);
482+
v = m_builder->addFunctionCall("test_const_number", Compiler::StaticType::Number, { Compiler::StaticType::Number }, { v });
483+
m_builder->addFunctionCall("test_print_number", Compiler::StaticType::Void, { Compiler::StaticType::Number }, { v });
484+
485+
v = m_builder->addConstValue(true);
486+
v = m_builder->addFunctionCall("test_const_bool", Compiler::StaticType::Bool, { Compiler::StaticType::Bool }, { v });
487+
m_builder->addFunctionCall("test_print_bool", Compiler::StaticType::Void, { Compiler::StaticType::Bool }, { v });
488+
489+
v = m_builder->addConstValue(321.5);
490+
m_builder->addFunctionCall("test_print_unknown", Compiler::StaticType::Void, { Compiler::StaticType::Unknown }, { v });
491+
492+
v = m_builder->addConstValue("test");
493+
m_builder->addFunctionCall("test_print_unknown", Compiler::StaticType::Void, { Compiler::StaticType::Unknown }, { v });
494+
495+
v = m_builder->addConstValue(true);
496+
m_builder->addFunctionCall("test_print_unknown", Compiler::StaticType::Void, { Compiler::StaticType::Unknown }, { v });
497+
480498
auto code = m_builder->finalize();
481499
Script script(&m_target, nullptr, nullptr);
482500
script.setCode(code);
@@ -497,7 +515,12 @@ TEST_F(LLVMCodeBuilderTest, FunctionCalls)
497515
"1_arg_ret 1\n"
498516
"3_args 1_arg_output 2 3\n"
499517
"3_args test 4 5\n"
500-
"1_arg 3_args_output\n";
518+
"1_arg 3_args_output\n"
519+
"123\n"
520+
"1\n"
521+
"321.5\n"
522+
"test\n"
523+
"true\n";
501524

502525
EXPECT_CALL(m_target, isStage()).Times(7);
503526
testing::internal::CaptureStdout();

test/llvm/testfunctions.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,11 @@ extern "C"
182182
{
183183
std::cout << v << std::endl;
184184
}
185+
186+
void test_print_unknown(const ValueData *v)
187+
{
188+
std::string str;
189+
value_toString(v, &str);
190+
std::cout << str << std::endl;
191+
}
185192
}

test/llvm/testfunctions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ extern "C"
4747
void test_print_bool(bool v);
4848
void test_print_string(const StringPtr *v);
4949
void test_print_pointer(const void *v);
50+
void test_print_unknown(const ValueData *v);
5051
}
5152

5253
} // namespace libscratchcpp

0 commit comments

Comments
 (0)