Skip to content

Commit 4309e90

Browse files
committed
Add execution context parameter to LLVM function
1 parent b88a148 commit 4309e90

File tree

8 files changed

+21
-19
lines changed

8 files changed

+21
-19
lines changed

src/dev/engine/internal/llvm/llvmcodebuilder.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,14 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
5858
m_builder.setFastMathFlags(fmf);
5959

6060
// Create function
61-
// void *f(Target *, ValueData **, List **)
61+
// void *f(ExecutionContext *, Target *, ValueData **, List **)
6262
llvm::PointerType *pointerType = llvm::PointerType::get(llvm::Type::getInt8Ty(m_ctx), 0);
63-
llvm::FunctionType *funcType = llvm::FunctionType::get(pointerType, { pointerType, pointerType, pointerType }, false);
63+
llvm::FunctionType *funcType = llvm::FunctionType::get(pointerType, { pointerType, pointerType, pointerType, pointerType }, false);
6464
llvm::Function *func = llvm::Function::Create(funcType, llvm::Function::ExternalLinkage, "f", m_module.get());
65-
llvm::Value *targetPtr = func->getArg(0);
66-
llvm::Value *targetVariables = func->getArg(1);
67-
llvm::Value *targetLists = func->getArg(2);
65+
llvm::Value *executionContextPtr = func->getArg(0);
66+
llvm::Value *targetPtr = func->getArg(1);
67+
llvm::Value *targetVariables = func->getArg(2);
68+
llvm::Value *targetLists = func->getArg(3);
6869

6970
llvm::BasicBlock *entry = llvm::BasicBlock::Create(m_ctx, "entry", func);
7071
m_builder.SetInsertPoint(entry);

src/dev/engine/internal/llvm/llvmexecutablecode.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void LLVMExecutableCode::run(ExecutionContext *context)
5555
ctx->setFinished(done);
5656
} else {
5757
Target *target = ctx->target();
58-
void *handle = m_mainFunction(target, target->variableData(), target->listData());
58+
void *handle = m_mainFunction(context, target, target->variableData(), target->listData());
5959

6060
if (!handle)
6161
ctx->setFinished(true);

src/dev/engine/internal/llvm/llvmexecutablecode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class LLVMExecutableCode : public ExecutableCode
3333
private:
3434
uint64_t lookupFunction(const std::string &name);
3535

36-
using MainFunctionType = void *(*)(Target *, ValueData **, List **);
36+
using MainFunctionType = void *(*)(ExecutionContext *, Target *, ValueData **, List **);
3737
using ResumeFunctionType = bool (*)(void *);
3838

3939
static LLVMExecutionContext *getContext(ExecutionContext *context);

test/dev/llvm/llvmcodebuilder_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class LLVMCodeBuilderTest : public testing::Test
5252

5353
void SetUp() override
5454
{
55-
test_function(nullptr, nullptr, nullptr, nullptr); // force dependency
55+
test_function(nullptr, nullptr, nullptr, nullptr, nullptr); // force dependency
5656
}
5757

5858
void createBuilder(Target *target, bool warp) { m_builder = std::make_unique<LLVMCodeBuilder>(target, "test", warp); }

test/dev/llvm/llvmexecutablecode_test.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class LLVMExecutableCodeTest : public testing::Test
2020
{
2121
m_module = std::make_unique<llvm::Module>("test", m_ctx);
2222
m_builder = std::make_unique<llvm::IRBuilder<>>(m_ctx);
23-
test_function(nullptr, nullptr, nullptr, nullptr); // force dependency
23+
test_function(nullptr, nullptr, nullptr, nullptr, nullptr); // force dependency
2424

2525
llvm::InitializeNativeTarget();
2626
llvm::InitializeNativeTargetAsmPrinter();
@@ -31,9 +31,9 @@ class LLVMExecutableCodeTest : public testing::Test
3131

3232
llvm::Function *beginMainFunction()
3333
{
34-
// void *f(Target *, ValueData **, List **)
34+
// void *f(ExecutionContext *, Target *, ValueData **, List **)
3535
llvm::Type *pointerType = llvm::PointerType::get(llvm::Type::getInt8Ty(m_ctx), 0);
36-
llvm::FunctionType *funcType = llvm::FunctionType::get(pointerType, { pointerType, pointerType, pointerType }, false);
36+
llvm::FunctionType *funcType = llvm::FunctionType::get(pointerType, { pointerType, pointerType, pointerType, pointerType }, false);
3737
llvm::Function *func = llvm::Function::Create(funcType, llvm::Function::ExternalLinkage, "f", m_module.get());
3838

3939
llvm::BasicBlock *entry = llvm::BasicBlock::Create(m_ctx, "entry", func);
@@ -57,12 +57,12 @@ class LLVMExecutableCodeTest : public testing::Test
5757
void addTestFunction(llvm::Function *mainFunc)
5858
{
5959
auto ptrType = llvm::PointerType::get(llvm::Type::getInt8Ty(m_ctx), 0);
60-
auto func = m_module->getOrInsertFunction("test_function", llvm::FunctionType::get(m_builder->getVoidTy(), { ptrType, ptrType, ptrType, ptrType }, false));
60+
auto func = m_module->getOrInsertFunction("test_function", llvm::FunctionType::get(m_builder->getVoidTy(), { ptrType, ptrType, ptrType, ptrType, ptrType }, false));
6161

6262
llvm::Constant *mockInt = llvm::ConstantInt::get(llvm::Type::getInt64Ty(m_ctx), (uintptr_t)&m_mock, false);
6363
llvm::Constant *mockPtr = llvm::ConstantExpr::getIntToPtr(mockInt, ptrType);
6464

65-
m_builder->CreateCall(func, { mockPtr, mainFunc->getArg(0), mainFunc->getArg(1), mainFunc->getArg(2) });
65+
m_builder->CreateCall(func, { mockPtr, mainFunc->getArg(0), mainFunc->getArg(1), mainFunc->getArg(2), mainFunc->getArg(3) });
6666
}
6767

6868
void addTestPrintFunction(llvm::Value *arg1, llvm::Value *arg2)
@@ -110,7 +110,7 @@ TEST_F(LLVMExecutableCodeTest, MainFunction)
110110
auto ctx = code.createExecutionContext(&m_target);
111111
ASSERT_FALSE(code.isFinished(ctx.get()));
112112

113-
EXPECT_CALL(m_mock, f(&m_target, m_target.variableData(), m_target.listData()));
113+
EXPECT_CALL(m_mock, f(ctx.get(), &m_target, m_target.variableData(), m_target.listData()));
114114
code.run(ctx.get());
115115
ASSERT_TRUE(code.isFinished(ctx.get()));
116116

@@ -135,7 +135,7 @@ TEST_F(LLVMExecutableCodeTest, MainFunction)
135135
ASSERT_FALSE(code.isFinished(anotherCtx.get()));
136136
ASSERT_TRUE(code.isFinished(ctx.get()));
137137

138-
EXPECT_CALL(m_mock, f(&anotherTarget, anotherTarget.variableData(), anotherTarget.listData()));
138+
EXPECT_CALL(m_mock, f(anotherCtx.get(), &anotherTarget, anotherTarget.variableData(), anotherTarget.listData()));
139139
code.run(anotherCtx.get());
140140
ASSERT_TRUE(code.isFinished(anotherCtx.get()));
141141
ASSERT_TRUE(code.isFinished(ctx.get()));

test/dev/llvm/testfunctions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ static int counter = 0;
1111

1212
extern "C"
1313
{
14-
void test_function(TestMock *mock, Target *target, ValueData **varData, List **listData)
14+
void test_function(TestMock *mock, ExecutionContext *ctx, Target *target, ValueData **varData, List **listData)
1515
{
1616
if (mock)
17-
mock->f(target, varData, listData);
17+
mock->f(ctx, target, varData, listData);
1818
}
1919

2020
void test_print_function(ValueData *arg1, ValueData *arg2)

test/dev/llvm/testfunctions.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ class TestMock;
77
class Target;
88
class ValueData;
99
class List;
10+
class ExecutionContext;
1011

1112
extern "C"
1213
{
13-
void test_function(TestMock *mock, Target *target, ValueData **varData, List **listData);
14+
void test_function(TestMock *mock, ExecutionContext *ctx, Target *target, ValueData **varData, List **listData);
1415
void test_print_function(ValueData *arg1, ValueData *arg2);
1516

1617
void test_function_no_args(Target *target);

test/dev/llvm/testmock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class List;
1212
class TestMock
1313
{
1414
public:
15-
MOCK_METHOD(void, f, (Target *, ValueData **, List **));
15+
MOCK_METHOD(void, f, (ExecutionContext * ctx, Target *, ValueData **, List **));
1616
};
1717

1818
} // namespace libscratchcpp

0 commit comments

Comments
 (0)