Skip to content

Commit 4b32859

Browse files
committed
LLVMCodeBuilder: Fix constants in reporter blocks/hat predicates
1 parent 75edf4b commit 4b32859

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

src/engine/internal/llvm/llvmcodebuilder.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,16 +1328,22 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
13281328
break;
13291329

13301330
case Compiler::CodeType::Reporter: {
1331-
// Use last instruction return value and create a ValueData instance
1332-
assert(!m_instructions.empty());
1333-
LLVMRegister *ret = m_instructions.back()->functionReturnReg;
1334-
m_builder.CreateRet(m_builder.CreateLoad(m_valueDataType, createNewValue(ret))); // return copy
1331+
// Use last instruction return value (or last constant) and create a ValueData instance
1332+
assert(!m_instructions.empty() || m_lastConstValue);
1333+
LLVMRegister *ret = m_instructions.empty() ? m_lastConstValue : m_instructions.back()->functionReturnReg;
1334+
llvm::Value *copy = createNewValue(ret);
1335+
m_builder.CreateRet(m_builder.CreateLoad(m_valueDataType, copy));
13351336
break;
13361337
}
1338+
13371339
case Compiler::CodeType::HatPredicate:
1338-
// Use last instruction return value
1339-
assert(!m_instructions.empty());
1340-
m_builder.CreateRet(m_instructions.back()->functionReturnReg->value);
1340+
// Use last instruction return value (or last constant)
1341+
assert(!m_instructions.empty() || m_lastConstValue);
1342+
1343+
if (m_instructions.empty())
1344+
m_builder.CreateRet(castValue(m_lastConstValue, Compiler::StaticType::Bool));
1345+
else
1346+
m_builder.CreateRet(castValue(m_instructions.back()->functionReturnReg, Compiler::StaticType::Bool));
13411347
break;
13421348
}
13431349

@@ -1403,6 +1409,7 @@ CompilerConstant *LLVMCodeBuilder::addConstValue(const Value &value)
14031409
{
14041410
auto constReg = std::make_shared<LLVMConstantRegister>(TYPE_MAP[value.type()], value);
14051411
auto reg = std::reinterpret_pointer_cast<LLVMRegister>(constReg);
1412+
m_lastConstValue = reg.get();
14061413
return static_cast<CompilerConstant *>(static_cast<CompilerValue *>(addReg(reg, nullptr)));
14071414
}
14081415

@@ -3154,6 +3161,7 @@ llvm::Value *LLVMCodeBuilder::createStringComparison(LLVMRegister *arg1, LLVMReg
31543161
std::string str1 = arg1->constValue().toString();
31553162
std::string str2 = arg2->constValue().toString();
31563163
result = strcasecmp(str1.c_str(), str2.c_str()) == 0;
3164+
result = string_compare_case_insensitive(str1, str2) == 0;
31573165
}
31583166

31593167
return m_builder.getInt1(result);

src/engine/internal/llvm/llvmcodebuilder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ class LLVMCodeBuilder : public ICodeBuilder
237237
std::vector<std::shared_ptr<LLVMInstruction>> m_instructions;
238238
std::vector<std::shared_ptr<LLVMRegister>> m_regs;
239239
std::vector<std::shared_ptr<CompilerLocalVariable>> m_localVars;
240+
LLVMRegister *m_lastConstValue = nullptr; // for reporters and hat predicates
240241
BlockPrototype *m_procedurePrototype = nullptr;
241242
bool m_defaultWarp = false;
242243
bool m_warp = false;

test/llvm/llvmcodebuilder_test.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6007,15 +6007,14 @@ TEST_F(LLVMCodeBuilderTest, HatPredicates)
60076007
// Predicate 1
60086008
createPredicateBuilder(&sprite);
60096009

6010-
CompilerValue *v = m_builder->addConstValue(true);
6011-
m_builder->addFunctionCall("test_const_bool", Compiler::StaticType::Bool, { Compiler::StaticType::Bool }, { v });
6010+
m_builder->addConstValue(true);
60126011

60136012
auto code1 = m_builder->finalize();
60146013

60156014
// Predicate 2
60166015
createPredicateBuilder(&sprite);
60176016

6018-
v = m_builder->addConstValue(false);
6017+
CompilerValue *v = m_builder->addConstValue(false);
60196018
m_builder->addFunctionCall("test_const_bool", Compiler::StaticType::Bool, { Compiler::StaticType::Bool }, { v });
60206019

60216020
auto code2 = m_builder->finalize();
@@ -6045,15 +6044,14 @@ TEST_F(LLVMCodeBuilderTest, Reporters)
60456044
// Reporter 1
60466045
createReporterBuilder(&sprite);
60476046

6048-
CompilerValue *v = m_builder->addConstValue(-45.23);
6049-
m_builder->addFunctionCall("test_const_number", Compiler::StaticType::Number, { Compiler::StaticType::Number }, { v });
6047+
m_builder->addConstValue(-45.23);
60506048

60516049
auto code1 = m_builder->finalize();
60526050

60536051
// Reporter 2
60546052
createReporterBuilder(&sprite);
60556053

6056-
v = m_builder->addConstValue("test");
6054+
CompilerValue *v = m_builder->addConstValue("test");
60576055
m_builder->addFunctionCall("test_const_string", Compiler::StaticType::String, { Compiler::StaticType::String }, { v });
60586056

60596057
auto code2 = m_builder->finalize();

0 commit comments

Comments
 (0)