Skip to content

Add support for dynamically typed parameters #636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/engine/internal/llvm/llvmcodebuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2169,8 +2169,12 @@ void LLVMCodeBuilder::freeScopeHeap()

llvm::Value *LLVMCodeBuilder::castValue(LLVMRegister *reg, Compiler::StaticType targetType)
{
if (reg->isConst())
return castConstValue(reg->constValue(), targetType);
if (reg->isConst()) {
if (targetType == Compiler::StaticType::Unknown)
return createValue(reg);
else
return castConstValue(reg->constValue(), targetType);
}

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

case Compiler::StaticType::Unknown:
return m_valueDataType->getPointerTo();

default:
assert(false);
return nullptr;
Expand Down
25 changes: 24 additions & 1 deletion test/llvm/llvmcodebuilder_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,24 @@ TEST_F(LLVMCodeBuilderTest, FunctionCalls)
{ Compiler::StaticType::String, Compiler::StaticType::String, Compiler::StaticType::String },
{ v, v1, v2 });
m_builder->addTargetFunctionCall("test_function_1_arg", Compiler::StaticType::Void, { Compiler::StaticType::String }, { v });

v = m_builder->addConstValue(123);
v = m_builder->addFunctionCall("test_const_number", Compiler::StaticType::Number, { Compiler::StaticType::Number }, { v });
m_builder->addFunctionCall("test_print_number", Compiler::StaticType::Void, { Compiler::StaticType::Number }, { v });

v = m_builder->addConstValue(true);
v = m_builder->addFunctionCall("test_const_bool", Compiler::StaticType::Bool, { Compiler::StaticType::Bool }, { v });
m_builder->addFunctionCall("test_print_bool", Compiler::StaticType::Void, { Compiler::StaticType::Bool }, { v });

v = m_builder->addConstValue(321.5);
m_builder->addFunctionCall("test_print_unknown", Compiler::StaticType::Void, { Compiler::StaticType::Unknown }, { v });

v = m_builder->addConstValue("test");
m_builder->addFunctionCall("test_print_unknown", Compiler::StaticType::Void, { Compiler::StaticType::Unknown }, { v });

v = m_builder->addConstValue(true);
m_builder->addFunctionCall("test_print_unknown", Compiler::StaticType::Void, { Compiler::StaticType::Unknown }, { v });

auto code = m_builder->finalize();
Script script(&m_target, nullptr, nullptr);
script.setCode(code);
Expand All @@ -497,7 +515,12 @@ TEST_F(LLVMCodeBuilderTest, FunctionCalls)
"1_arg_ret 1\n"
"3_args 1_arg_output 2 3\n"
"3_args test 4 5\n"
"1_arg 3_args_output\n";
"1_arg 3_args_output\n"
"123\n"
"1\n"
"321.5\n"
"test\n"
"true\n";

EXPECT_CALL(m_target, isStage()).Times(7);
testing::internal::CaptureStdout();
Expand Down
7 changes: 7 additions & 0 deletions test/llvm/testfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,11 @@ extern "C"
{
std::cout << v << std::endl;
}

void test_print_unknown(const ValueData *v)
{
std::string str;
value_toString(v, &str);
std::cout << str << std::endl;
}
}
1 change: 1 addition & 0 deletions test/llvm/testfunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ extern "C"
void test_print_bool(bool v);
void test_print_string(const StringPtr *v);
void test_print_pointer(const void *v);
void test_print_unknown(const ValueData *v);
}

} // namespace libscratchcpp