Skip to content

Commit 34ce8b7

Browse files
committed
Add support for unknown function return type
1 parent 3709135 commit 34ce8b7

File tree

6 files changed

+50
-7
lines changed

6 files changed

+50
-7
lines changed

src/engine/internal/llvm/instructions/functions.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,19 @@ LLVMInstruction *Functions::buildFunctionCall(LLVMInstruction *ins)
4646

4747
// Args
4848
for (auto &arg : ins->args) {
49-
types.push_back(m_utils.getType(arg.first));
49+
types.push_back(m_utils.getType(arg.first, false));
5050
args.push_back(m_utils.castValue(arg.second, arg.first));
5151
}
5252

53-
llvm::Type *retType = m_utils.getType(ins->functionReturnReg ? ins->functionReturnReg->type() : Compiler::StaticType::Void);
53+
llvm::Type *retType = m_utils.getType(ins->functionReturnReg ? ins->functionReturnReg->type() : Compiler::StaticType::Void, true);
5454
llvm::Value *ret = m_builder.CreateCall(m_utils.functions().resolveFunction(ins->functionName, llvm::FunctionType::get(retType, types, false)), args);
5555

5656
if (ins->functionReturnReg) {
57-
ins->functionReturnReg->value = ret;
57+
if (ins->functionReturnReg->type() == Compiler::StaticType::Unknown) {
58+
ins->functionReturnReg->value = m_utils.addAlloca(retType);
59+
m_builder.CreateStore(ret, ins->functionReturnReg->value);
60+
} else
61+
ins->functionReturnReg->value = ret;
5862

5963
if (ins->functionReturnReg->type() == Compiler::StaticType::String)
6064
m_utils.freeStringLater(ins->functionReturnReg->value);

src/engine/internal/llvm/llvmbuildutils.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ llvm::Value *LLVMBuildUtils::castValue(LLVMRegister *reg, Compiler::StaticType t
496496
}
497497
}
498498

499-
llvm::Type *LLVMBuildUtils::getType(Compiler::StaticType type)
499+
llvm::Type *LLVMBuildUtils::getType(Compiler::StaticType type, bool isReturnType)
500500
{
501501
switch (type) {
502502
case Compiler::StaticType::Void:
@@ -515,7 +515,10 @@ llvm::Type *LLVMBuildUtils::getType(Compiler::StaticType type)
515515
return m_builder.getVoidTy()->getPointerTo();
516516

517517
case Compiler::StaticType::Unknown:
518-
return m_valueDataType->getPointerTo();
518+
if (isReturnType)
519+
return m_valueDataType;
520+
else
521+
return m_valueDataType->getPointerTo();
519522

520523
default:
521524
assert(false);

src/engine/internal/llvm/llvmbuildutils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class LLVMBuildUtils
8080

8181
llvm::Value *addAlloca(llvm::Type *type);
8282
llvm::Value *castValue(LLVMRegister *reg, Compiler::StaticType targetType);
83-
llvm::Type *getType(Compiler::StaticType type);
83+
llvm::Type *getType(Compiler::StaticType type, bool isReturnType);
8484
llvm::Value *isNaN(llvm::Value *num);
8585
llvm::Value *removeNaN(llvm::Value *num);
8686

src/engine/internal/llvm/llvmcodebuilder.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ CompilerValue *LLVMCodeBuilder::addFunctionCall(const std::string &functionName,
100100

101101
if (returnType != Compiler::StaticType::Void) {
102102
auto reg = std::make_shared<LLVMRegister>(returnType);
103-
reg->isRawValue = true;
103+
104+
if (returnType != Compiler::StaticType::Unknown)
105+
reg->isRawValue = true;
106+
104107
ins->functionReturnReg = reg.get();
105108
return addReg(reg, ins);
106109
}

test/llvm/llvmcodebuilder_test.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3301,3 +3301,28 @@ TEST_F(LLVMCodeBuilderTest, Reporters)
33013301
ASSERT_EQ(value_toPointer(&ret), &pointee);
33023302
value_free(&ret);
33033303
}
3304+
3305+
TEST_F(LLVMCodeBuilderTest, UnknownTypeReporter)
3306+
{
3307+
Sprite sprite;
3308+
auto var = std::make_shared<Variable>("", "");
3309+
var->setValue("Hello world!");
3310+
sprite.addVariable(var);
3311+
3312+
LLVMCodeBuilder *builder = m_utils.createReporterBuilder(&sprite);
3313+
3314+
CompilerValue *num = builder->addConstValue(5.2);
3315+
builder->addFunctionCall("test_const_unknown", Compiler::StaticType::Unknown, { Compiler::StaticType::Unknown }, { num });
3316+
3317+
auto code = builder->build();
3318+
3319+
Script script(&sprite, nullptr, nullptr);
3320+
script.setCode(code);
3321+
Thread thread1(&sprite, nullptr, &script);
3322+
auto ctx = code->createExecutionContext(&thread1);
3323+
3324+
ValueData ret = code->runReporter(ctx.get());
3325+
ASSERT_TRUE(value_isNumber(&ret));
3326+
ASSERT_EQ(value_toDouble(&ret), 5.2);
3327+
value_free(&ret);
3328+
}

test/llvm/testfunctions.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,14 @@ extern "C"
139139
return ret;
140140
}
141141

142+
ValueData test_const_unknown(const ValueData *v)
143+
{
144+
ValueData ret;
145+
value_init(&ret);
146+
value_assign_copy(&ret, v);
147+
return ret;
148+
}
149+
142150
const void *test_const_pointer(const void *v)
143151
{
144152
return v;

0 commit comments

Comments
 (0)