Skip to content

Commit 8db2ec3

Browse files
committed
Add support for mixed types in function return values
1 parent 1d59bbe commit 8db2ec3

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ LLVMInstruction *Functions::buildFunctionCall(LLVMInstruction *ins)
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-
if (ins->functionReturnReg->type() == Compiler::StaticType::Unknown) {
57+
if (m_utils.isSingleType(ins->functionReturnReg->type()))
58+
ins->functionReturnReg->value = ret;
59+
else {
5860
ins->functionReturnReg->value = m_utils.addAlloca(retType);
5961
m_builder.CreateStore(ret, ins->functionReturnReg->value);
60-
} else
61-
ins->functionReturnReg->value = ret;
62+
}
6263

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

src/engine/internal/llvm/llvmbuildutils.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,12 @@ Compiler::StaticType LLVMBuildUtils::mapType(ValueType type)
383383
return TYPE_MAP[type];
384384
}
385385

386+
bool LLVMBuildUtils::isSingleType(Compiler::StaticType type)
387+
{
388+
// Check if the type is a power of 2 (only one bit set)
389+
return (type & (static_cast<Compiler::StaticType>(static_cast<int>(type) - 1))) == static_cast<Compiler::StaticType>(0);
390+
}
391+
386392
llvm::Value *LLVMBuildUtils::addAlloca(llvm::Type *type)
387393
{
388394
// Add an alloca to the entry block because allocas must be there (to avoid stack overflow)
@@ -509,15 +515,12 @@ llvm::Type *LLVMBuildUtils::getType(Compiler::StaticType type, bool isReturnType
509515
case Compiler::StaticType::Pointer:
510516
return m_builder.getVoidTy()->getPointerTo();
511517

512-
case Compiler::StaticType::Unknown:
518+
default:
519+
// Multiple types
513520
if (isReturnType)
514521
return m_valueDataType;
515522
else
516523
return m_valueDataType->getPointerTo();
517-
518-
default:
519-
assert(false);
520-
return nullptr;
521524
}
522525
}
523526

src/engine/internal/llvm/llvmbuildutils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class LLVMBuildUtils
7575

7676
static Compiler::StaticType optimizeRegisterType(const LLVMRegister *reg);
7777
static Compiler::StaticType mapType(ValueType type);
78+
static bool isSingleType(Compiler::StaticType type);
7879

7980
llvm::Value *addAlloca(llvm::Type *type);
8081
llvm::Value *castValue(LLVMRegister *reg, Compiler::StaticType targetType);

src/engine/internal/llvm/llvmcodebuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ CompilerValue *LLVMCodeBuilder::addFunctionCall(const std::string &functionName,
101101
if (returnType != Compiler::StaticType::Void) {
102102
auto reg = std::make_shared<LLVMRegister>(returnType);
103103

104-
if (returnType != Compiler::StaticType::Unknown)
104+
if (m_utils.isSingleType(returnType))
105105
reg->isRawValue = true;
106106

107107
ins->functionReturnReg = reg.get();

0 commit comments

Comments
 (0)