11#include < scratchcpp/target.h>
2+ #include < scratchcpp/value_functions.h>
23#include < dev/engine/internal/llvmexecutablecode.h>
34#include < dev/engine/internal/llvmexecutioncontext.h>
45#include < llvm/Support/TargetSelect.h>
@@ -52,6 +53,13 @@ class LLVMExecutableCodeTest : public testing::Test
5253 m_builder->CreateCall (func, { mockPtr, mainFunc->getArg (0 ) });
5354 }
5455
56+ void addTestPrintFunction (llvm::Value *arg1, llvm::Value *arg2)
57+ {
58+ auto ptrType = llvm::PointerType::get (llvm::Type::getInt8Ty (m_ctx), 0 );
59+ auto func = m_module->getOrInsertFunction (" test_print_function" , llvm::FunctionType::get (m_builder->getVoidTy (), { ptrType, ptrType }, false ));
60+ m_builder->CreateCall (func, { arg1, arg2 });
61+ }
62+
5563 llvm::LLVMContext m_ctx;
5664 std::unique_ptr<llvm::Module> m_module;
5765 std::unique_ptr<llvm::IRBuilder<>> m_builder;
@@ -62,7 +70,8 @@ class LLVMExecutableCodeTest : public testing::Test
6270TEST_F (LLVMExecutableCodeTest, NoFunctions)
6371{
6472 LLVMExecutionContext ctx (&m_target);
65- LLVMExecutableCode code (std::move (m_module));
73+ std::vector<std::unique_ptr<ValueData>> constValues;
74+ LLVMExecutableCode code (std::move (m_module), constValues);
6675 ASSERT_TRUE (code.isFinished (&ctx));
6776
6877 code.run (&ctx);
@@ -82,7 +91,8 @@ TEST_F(LLVMExecutableCodeTest, SingleFunction)
8291 endFunction (0 );
8392
8493 LLVMExecutionContext ctx (&m_target);
85- LLVMExecutableCode code (std::move (m_module));
94+ std::vector<std::unique_ptr<ValueData>> constValues;
95+ LLVMExecutableCode code (std::move (m_module), constValues);
8696 ASSERT_FALSE (code.isFinished (&ctx));
8797
8898 EXPECT_CALL (m_mock, f (&m_target));
@@ -114,7 +124,8 @@ TEST_F(LLVMExecutableCodeTest, MultipleFunctions)
114124 }
115125
116126 LLVMExecutionContext ctx (&m_target);
117- LLVMExecutableCode code (std::move (m_module));
127+ std::vector<std::unique_ptr<ValueData>> constValues;
128+ LLVMExecutableCode code (std::move (m_module), constValues);
118129 ASSERT_FALSE (code.isFinished (&ctx));
119130
120131 for (int i = 0 ; i < count; i++) {
@@ -125,3 +136,36 @@ TEST_F(LLVMExecutableCodeTest, MultipleFunctions)
125136
126137 ASSERT_TRUE (code.isFinished (&ctx));
127138}
139+
140+ TEST_F (LLVMExecutableCodeTest, ConstValues)
141+ {
142+ beginFunction (0 );
143+ std::vector<std::unique_ptr<ValueData>> constValues;
144+
145+ for (int i = 0 ; i < 2 ; i++) {
146+ std::unique_ptr<ValueData> value = std::make_unique<ValueData>();
147+ value_init (value.get ());
148+ value_assign_int (value.get (), i + 1 );
149+ constValues.push_back (std::move (value));
150+ }
151+
152+ auto ptrType = llvm::PointerType::get (llvm::Type::getInt8Ty (m_ctx), 0 );
153+ llvm::Value *intAddress = m_builder->getInt64 ((uintptr_t )constValues[0 ].get ());
154+ llvm::Value *ptr1 = m_builder->CreateIntToPtr (intAddress, ptrType);
155+
156+ intAddress = m_builder->getInt64 ((uintptr_t )constValues[1 ].get ());
157+ llvm::Value *ptr2 = m_builder->CreateIntToPtr (intAddress, ptrType);
158+
159+ addTestPrintFunction (ptr1, ptr2);
160+ endFunction (0 );
161+
162+ LLVMExecutionContext ctx (&m_target);
163+ LLVMExecutableCode code (std::move (m_module), constValues);
164+ ASSERT_TRUE (constValues.empty ());
165+ ASSERT_FALSE (code.isFinished (&ctx));
166+
167+ testing::internal::CaptureStdout ();
168+ code.run (&ctx);
169+ ASSERT_EQ (testing::internal::GetCapturedStdout (), " 1 2\n " );
170+ ASSERT_TRUE (code.isFinished (&ctx));
171+ }
0 commit comments