From d991b67eb10ea29c7ca3fa6c81ec102dcc08dfa9 Mon Sep 17 00:00:00 2001 From: adazem009 <68537469+adazem009@users.noreply.github.com> Date: Tue, 18 Mar 2025 13:43:22 +0100 Subject: [PATCH] LLVMCodeBuilder: Add support for dynamically typed parameters --- src/engine/internal/llvm/llvmcodebuilder.cpp | 11 +++++++-- test/llvm/llvmcodebuilder_test.cpp | 25 +++++++++++++++++++- test/llvm/testfunctions.cpp | 7 ++++++ test/llvm/testfunctions.h | 1 + 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/engine/internal/llvm/llvmcodebuilder.cpp b/src/engine/internal/llvm/llvmcodebuilder.cpp index a26546ff..88163353 100644 --- a/src/engine/internal/llvm/llvmcodebuilder.cpp +++ b/src/engine/internal/llvm/llvmcodebuilder.cpp @@ -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); @@ -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; diff --git a/test/llvm/llvmcodebuilder_test.cpp b/test/llvm/llvmcodebuilder_test.cpp index a73162fc..47030a26 100644 --- a/test/llvm/llvmcodebuilder_test.cpp +++ b/test/llvm/llvmcodebuilder_test.cpp @@ -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); @@ -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(); diff --git a/test/llvm/testfunctions.cpp b/test/llvm/testfunctions.cpp index 7eaf04a0..93536af9 100644 --- a/test/llvm/testfunctions.cpp +++ b/test/llvm/testfunctions.cpp @@ -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; + } } diff --git a/test/llvm/testfunctions.h b/test/llvm/testfunctions.h index 94a878d6..da210be2 100644 --- a/test/llvm/testfunctions.h +++ b/test/llvm/testfunctions.h @@ -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