|
| 1 | +#include <scratchcpp/target.h> |
| 2 | +#include <dev/engine/internal/llvmexecutablecode.h> |
| 3 | +#include <dev/engine/internal/llvmexecutioncontext.h> |
| 4 | +#include <llvm/Support/TargetSelect.h> |
| 5 | +#include <llvm/IR/IRBuilder.h> |
| 6 | +#include <gmock/gmock.h> |
| 7 | + |
| 8 | +#include "testmock.h" |
| 9 | +#include "testfunctions.h" |
| 10 | + |
| 11 | +using namespace libscratchcpp; |
| 12 | + |
| 13 | +class LLVMExecutableCodeTest : public testing::Test |
| 14 | +{ |
| 15 | + public: |
| 16 | + void SetUp() override |
| 17 | + { |
| 18 | + m_module = std::make_unique<llvm::Module>("test", m_ctx); |
| 19 | + m_builder = std::make_unique<llvm::IRBuilder<>>(m_ctx); |
| 20 | + test_function(nullptr, nullptr); // force dependency |
| 21 | + |
| 22 | + llvm::InitializeNativeTarget(); |
| 23 | + llvm::InitializeNativeTargetAsmPrinter(); |
| 24 | + llvm::InitializeNativeTargetAsmParser(); |
| 25 | + } |
| 26 | + |
| 27 | + llvm::Function *beginFunction(size_t index) |
| 28 | + { |
| 29 | + // size_t f#(Target *) |
| 30 | + llvm::FunctionType *funcType = llvm::FunctionType::get(m_builder->getInt64Ty(), llvm::PointerType::get(llvm::Type::getInt8Ty(m_ctx), 0), false); |
| 31 | + llvm::Function *func = llvm::Function::Create(funcType, llvm::Function::ExternalLinkage, "f" + std::to_string(index), m_module.get()); |
| 32 | + |
| 33 | + llvm::BasicBlock *entry = llvm::BasicBlock::Create(m_ctx, "entry", func); |
| 34 | + m_builder->SetInsertPoint(entry); |
| 35 | + return func; |
| 36 | + } |
| 37 | + |
| 38 | + void endFunction(size_t index) |
| 39 | + { |
| 40 | + // Return next function index |
| 41 | + m_builder->CreateRet(m_builder->getInt64(index + 1)); |
| 42 | + } |
| 43 | + |
| 44 | + void addTestFunction(llvm::Function *mainFunc) |
| 45 | + { |
| 46 | + auto ptrType = llvm::PointerType::get(llvm::Type::getInt8Ty(m_ctx), 0); |
| 47 | + auto func = m_module->getOrInsertFunction("test_function", llvm::FunctionType::get(m_builder->getVoidTy(), { ptrType, ptrType }, false)); |
| 48 | + |
| 49 | + llvm::Constant *mockInt = llvm::ConstantInt::get(llvm::Type::getInt64Ty(m_ctx), (uintptr_t)&m_mock, false); |
| 50 | + llvm::Constant *mockPtr = llvm::ConstantExpr::getIntToPtr(mockInt, ptrType); |
| 51 | + |
| 52 | + m_builder->CreateCall(func, { mockPtr, mainFunc->getArg(0) }); |
| 53 | + } |
| 54 | + |
| 55 | + llvm::LLVMContext m_ctx; |
| 56 | + std::unique_ptr<llvm::Module> m_module; |
| 57 | + std::unique_ptr<llvm::IRBuilder<>> m_builder; |
| 58 | + Target m_target; |
| 59 | + TestMock m_mock; |
| 60 | +}; |
| 61 | + |
| 62 | +TEST_F(LLVMExecutableCodeTest, NoFunctions) |
| 63 | +{ |
| 64 | + LLVMExecutionContext ctx(&m_target); |
| 65 | + LLVMExecutableCode code(std::move(m_module)); |
| 66 | + ASSERT_TRUE(code.isFinished(&ctx)); |
| 67 | + |
| 68 | + code.run(&ctx); |
| 69 | + ASSERT_TRUE(code.isFinished(&ctx)); |
| 70 | + |
| 71 | + code.kill(&ctx); |
| 72 | + ASSERT_TRUE(code.isFinished(&ctx)); |
| 73 | + |
| 74 | + code.reset(&ctx); |
| 75 | + ASSERT_TRUE(code.isFinished(&ctx)); |
| 76 | +} |
| 77 | + |
| 78 | +TEST_F(LLVMExecutableCodeTest, SingleFunction) |
| 79 | +{ |
| 80 | + auto f = beginFunction(0); |
| 81 | + addTestFunction(f); |
| 82 | + endFunction(0); |
| 83 | + |
| 84 | + LLVMExecutionContext ctx(&m_target); |
| 85 | + LLVMExecutableCode code(std::move(m_module)); |
| 86 | + ASSERT_FALSE(code.isFinished(&ctx)); |
| 87 | + |
| 88 | + EXPECT_CALL(m_mock, f(&m_target)); |
| 89 | + code.run(&ctx); |
| 90 | + ASSERT_TRUE(code.isFinished(&ctx)); |
| 91 | + |
| 92 | + code.kill(&ctx); |
| 93 | + ASSERT_TRUE(code.isFinished(&ctx)); |
| 94 | + |
| 95 | + code.reset(&ctx); |
| 96 | + ASSERT_FALSE(code.isFinished(&ctx)); |
| 97 | + |
| 98 | + code.kill(&ctx); |
| 99 | + ASSERT_TRUE(code.isFinished(&ctx)); |
| 100 | + |
| 101 | + EXPECT_CALL(m_mock, f).Times(0); |
| 102 | + code.run(&ctx); |
| 103 | + ASSERT_TRUE(code.isFinished(&ctx)); |
| 104 | +} |
| 105 | + |
| 106 | +TEST_F(LLVMExecutableCodeTest, MultipleFunctions) |
| 107 | +{ |
| 108 | + static const int count = 5; |
| 109 | + |
| 110 | + for (int i = 0; i < count; i++) { |
| 111 | + auto f = beginFunction(i); |
| 112 | + addTestFunction(f); |
| 113 | + endFunction(i); |
| 114 | + } |
| 115 | + |
| 116 | + LLVMExecutionContext ctx(&m_target); |
| 117 | + LLVMExecutableCode code(std::move(m_module)); |
| 118 | + ASSERT_FALSE(code.isFinished(&ctx)); |
| 119 | + |
| 120 | + for (int i = 0; i < count; i++) { |
| 121 | + ASSERT_FALSE(code.isFinished(&ctx)); |
| 122 | + EXPECT_CALL(m_mock, f(&m_target)); |
| 123 | + code.run(&ctx); |
| 124 | + } |
| 125 | + |
| 126 | + ASSERT_TRUE(code.isFinished(&ctx)); |
| 127 | +} |
0 commit comments