Skip to content

Commit beb0f98

Browse files
committed
LLVMCodeBuilder: Add more function call parameter options
1 parent 4309e90 commit beb0f98

File tree

8 files changed

+144
-85
lines changed

8 files changed

+144
-85
lines changed

src/dev/engine/internal/icodebuilder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class ICodeBuilder
2020
virtual std::shared_ptr<ExecutableCode> finalize() = 0;
2121

2222
virtual CompilerValue *addFunctionCall(const std::string &functionName, Compiler::StaticType returnType, const Compiler::ArgTypes &argTypes, const Compiler::Args &args) = 0;
23+
virtual CompilerValue *addTargetFunctionCall(const std::string &functionName, Compiler::StaticType returnType, const Compiler::ArgTypes &argTypes, const Compiler::Args &args) = 0;
24+
virtual CompilerValue *addFunctionCallWithCtx(const std::string &functionName, Compiler::StaticType returnType, const Compiler::ArgTypes &argTypes, const Compiler::Args &args) = 0;
2325
virtual CompilerConstant *addConstValue(const Value &value) = 0;
2426
virtual CompilerValue *addVariableValue(Variable *variable) = 0;
2527
virtual CompilerValue *addListContents(List *list) = 0;

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,17 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
118118
std::vector<llvm::Type *> types;
119119
std::vector<llvm::Value *> args;
120120

121+
// Add execution context arg
122+
if (step.functionCtxArg) {
123+
types.push_back(llvm::PointerType::get(llvm::Type::getInt8Ty(m_ctx), 0));
124+
args.push_back(executionContextPtr);
125+
}
126+
121127
// Add target pointer arg
122-
types.push_back(llvm::PointerType::get(llvm::Type::getInt8Ty(m_ctx), 0));
123-
args.push_back(targetPtr);
128+
if (step.functionTargetArg) {
129+
types.push_back(llvm::PointerType::get(llvm::Type::getInt8Ty(m_ctx), 0));
130+
args.push_back(targetPtr);
131+
}
124132

125133
// Args
126134
for (auto &arg : step.args) {
@@ -951,6 +959,20 @@ CompilerValue *LLVMCodeBuilder::addFunctionCall(const std::string &functionName,
951959
return nullptr;
952960
}
953961

962+
CompilerValue *LLVMCodeBuilder::addTargetFunctionCall(const std::string &functionName, Compiler::StaticType returnType, const Compiler::ArgTypes &argTypes, const Compiler::Args &args)
963+
{
964+
CompilerValue *ret = addFunctionCall(functionName, returnType, argTypes, args);
965+
m_instructions.back().functionTargetArg = true;
966+
return ret;
967+
}
968+
969+
CompilerValue *LLVMCodeBuilder::addFunctionCallWithCtx(const std::string &functionName, Compiler::StaticType returnType, const Compiler::ArgTypes &argTypes, const Compiler::Args &args)
970+
{
971+
CompilerValue *ret = addFunctionCall(functionName, returnType, argTypes, args);
972+
m_instructions.back().functionCtxArg = true;
973+
return ret;
974+
}
975+
954976
CompilerConstant *LLVMCodeBuilder::addConstValue(const Value &value)
955977
{
956978
auto constReg = std::make_shared<LLVMConstantRegister>(TYPE_MAP[value.type()], value);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class LLVMCodeBuilder : public ICodeBuilder
2727
std::shared_ptr<ExecutableCode> finalize() override;
2828

2929
CompilerValue *addFunctionCall(const std::string &functionName, Compiler::StaticType returnType, const Compiler::ArgTypes &argTypes, const Compiler::Args &args) override;
30+
CompilerValue *addTargetFunctionCall(const std::string &functionName, Compiler::StaticType returnType, const Compiler::ArgTypes &argTypes, const Compiler::Args &args) override;
31+
CompilerValue *addFunctionCallWithCtx(const std::string &functionName, Compiler::StaticType returnType, const Compiler::ArgTypes &argTypes, const Compiler::Args &args) override;
3032
CompilerConstant *addConstValue(const Value &value) override;
3133
CompilerValue *addVariableValue(Variable *variable) override;
3234
CompilerValue *addListContents(List *list) override;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ struct LLVMInstruction
7373
std::string functionName;
7474
std::vector<std::pair<Compiler::StaticType, LLVMRegister *>> args; // target type, register
7575
LLVMRegister *functionReturnReg = nullptr;
76+
bool functionTargetArg = false; // whether to add target ptr to function parameters
77+
bool functionCtxArg = false; // whether to add execution context ptr to function parameters
7678
Variable *workVariable = nullptr; // for variables
7779
List *workList = nullptr; // for lists
7880
};

test/dev/llvm/llvmcodebuilder_test.cpp

Lines changed: 72 additions & 57 deletions
Large diffs are not rendered by default.

test/dev/llvm/testfunctions.cpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ extern "C"
2525
std::cout << s1 << " " << s2 << std::endl;
2626
}
2727

28+
void test_empty_function()
29+
{
30+
std::cout << "empty" << std::endl;
31+
}
32+
33+
void test_ctx_function(ExecutionContext *ctx, const char *arg)
34+
{
35+
std::cout << ctx << std::endl;
36+
std::cout << arg << std::endl;
37+
}
38+
2839
void test_function_no_args(Target *target)
2940
{
3041
target->isStage();
@@ -67,69 +78,69 @@ extern "C"
6778
return value_toCString(&v.data());
6879
}
6980

70-
bool test_equals(Target *target, const char *a, const char *b)
81+
bool test_equals(const char *a, const char *b)
7182
{
7283
return strcmp(a, b) == 0;
7384
}
7485

75-
void test_unreachable(Target *target)
86+
void test_unreachable()
7687
{
7788
std::cout << "error: unreachable reached" << std::endl;
7889
exit(1);
7990
}
8091

81-
bool test_lower_than(Target *target, double a, double b)
92+
bool test_lower_than(double a, double b)
8293
{
8394
return a < b;
8495
}
8596

86-
double test_const_number(Target *target, double v)
97+
double test_const_number(double v)
8798
{
8899
return v;
89100
}
90101

91-
bool test_const_bool(Target *target, bool v)
102+
bool test_const_bool(bool v)
92103
{
93104
return v;
94105
}
95106

96-
char *test_const_string(Target *target, const char *v)
107+
char *test_const_string(const char *v)
97108
{
98109
Value value(v);
99110
return value_toCString(&value.data());
100111
}
101112

102-
bool test_not(Target *target, bool arg)
113+
bool test_not(bool arg)
103114
{
104115
return !arg;
105116
}
106117

107-
void test_reset_counter(Target *target)
118+
void test_reset_counter()
108119
{
109120
counter = 0;
110121
}
111122

112-
void test_increment_counter(Target *target)
123+
void test_increment_counter()
113124
{
114125
counter++;
115126
}
116127

117-
double test_get_counter(Target *target)
128+
double test_get_counter()
118129
{
119130
return counter;
120131
}
121132

122-
void test_print_number(Target *target, double v)
133+
void test_print_number(double v)
123134
{
124135
std::cout << v << std::endl;
125136
}
126137

127-
void test_print_bool(Target *target, bool v)
138+
void test_print_bool(bool v)
128139
{
129140
std::cout << v << std::endl;
130141
}
131142

132-
void test_print_string(Target *target, const char *v)
143+
void test_print_string(const char *v)
133144
{
134145
std::cout << v << std::endl;
135146
}

test/dev/llvm/testfunctions.h

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,32 @@ extern "C"
1414
void test_function(TestMock *mock, ExecutionContext *ctx, Target *target, ValueData **varData, List **listData);
1515
void test_print_function(ValueData *arg1, ValueData *arg2);
1616

17+
void test_empty_function();
18+
void test_ctx_function(ExecutionContext *ctx, const char *arg);
19+
1720
void test_function_no_args(Target *target);
1821
char *test_function_no_args_ret(Target *target);
1922
void test_function_1_arg(Target *target, const char *arg1);
2023
char *test_function_1_arg_ret(Target *target, const char *arg1);
2124
void test_function_3_args(Target *target, const char *arg1, const char *arg2, const char *arg3);
2225
char *test_function_3_args_ret(Target *target, const char *arg1, const char *arg2, const char *arg3);
2326

24-
bool test_equals(Target *target, const char *a, const char *b);
25-
bool test_lower_than(Target *target, double a, double b);
26-
bool test_not(Target *target, bool arg);
27-
double test_const_number(Target *target, double v);
28-
bool test_const_bool(Target *target, bool v);
29-
char *test_const_string(Target *target, const char *v);
27+
bool test_equals(const char *a, const char *b);
28+
bool test_lower_than(double a, double b);
29+
bool test_not(bool arg);
30+
double test_const_number(double v);
31+
bool test_const_bool(bool v);
32+
char *test_const_string(const char *v);
3033

31-
void test_unreachable(Target *target);
34+
void test_unreachable();
3235

33-
void test_reset_counter(Target *target);
34-
void test_increment_counter(Target *target);
35-
double test_get_counter(Target *target);
36+
void test_reset_counter();
37+
void test_increment_counter();
38+
double test_get_counter();
3639

37-
void test_print_number(Target *target, double v);
38-
void test_print_bool(Target *target, bool v);
39-
void test_print_string(Target *target, const char *v);
40+
void test_print_number(double v);
41+
void test_print_bool(bool v);
42+
void test_print_string(const char *v);
4043
}
4144

4245
} // namespace libscratchcpp

test/mocks/codebuildermock.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class CodeBuilderMock : public ICodeBuilder
1010
public:
1111
MOCK_METHOD(std::shared_ptr<ExecutableCode>, finalize, (), (override));
1212
MOCK_METHOD(CompilerValue *, addFunctionCall, (const std::string &, Compiler::StaticType, const Compiler::ArgTypes &, const Compiler::Args &), (override));
13+
MOCK_METHOD(CompilerValue *, addTargetFunctionCall, (const std::string &, Compiler::StaticType, const Compiler::ArgTypes &, const Compiler::Args &), (override));
14+
MOCK_METHOD(CompilerValue *, addFunctionCallWithCtx, (const std::string &, Compiler::StaticType, const Compiler::ArgTypes &, const Compiler::Args &), (override));
1315
MOCK_METHOD(CompilerConstant *, addConstValue, (const Value &), (override));
1416
MOCK_METHOD(CompilerValue *, addVariableValue, (Variable *), (override));
1517
MOCK_METHOD(CompilerValue *, addListContents, (List *), (override));

0 commit comments

Comments
 (0)