Skip to content

Commit f274cf6

Browse files
committed
LLVMCodeBuilder: Add addLoopIndex() method
1 parent b389f56 commit f274cf6

File tree

6 files changed

+29
-11
lines changed

6 files changed

+29
-11
lines changed

src/dev/engine/internal/icodebuilder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class ICodeBuilder
2323
virtual CompilerValue *addTargetFunctionCall(const std::string &functionName, Compiler::StaticType returnType, const Compiler::ArgTypes &argTypes, const Compiler::Args &args) = 0;
2424
virtual CompilerValue *addFunctionCallWithCtx(const std::string &functionName, Compiler::StaticType returnType, const Compiler::ArgTypes &argTypes, const Compiler::Args &args) = 0;
2525
virtual CompilerConstant *addConstValue(const Value &value) = 0;
26+
virtual CompilerValue *addLoopIndex() = 0;
2627
virtual CompilerValue *addVariableValue(Variable *variable) = 0;
2728
virtual CompilerValue *addListContents(List *list) = 0;
2829
virtual CompilerValue *addListItem(List *list, CompilerValue *index) = 0;

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,14 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
822822
break;
823823
}
824824

825+
case LLVMInstruction::Type::LoopIndex: {
826+
assert(!loops.empty());
827+
LLVMLoop &loop = loops.back();
828+
llvm::Value *index = m_builder.CreateLoad(m_builder.getInt64Ty(), loop.index);
829+
step.functionReturnReg->value = m_builder.CreateUIToFP(index, m_builder.getDoubleTy());
830+
break;
831+
}
832+
825833
case LLVMInstruction::Type::BeginWhileLoop: {
826834
assert(!loops.empty());
827835
LLVMLoop &loop = loops.back();
@@ -993,6 +1001,11 @@ CompilerConstant *LLVMCodeBuilder::addConstValue(const Value &value)
9931001
return static_cast<CompilerConstant *>(addReg(reg));
9941002
}
9951003

1004+
CompilerValue *LLVMCodeBuilder::addLoopIndex()
1005+
{
1006+
return createOp(LLVMInstruction::Type::LoopIndex, Compiler::StaticType::Number, {}, {});
1007+
}
1008+
9961009
CompilerValue *LLVMCodeBuilder::addVariableValue(Variable *variable)
9971010
{
9981011
LLVMInstruction ins(LLVMInstruction::Type::ReadVariable);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class LLVMCodeBuilder : public ICodeBuilder
3030
CompilerValue *addTargetFunctionCall(const std::string &functionName, Compiler::StaticType returnType, const Compiler::ArgTypes &argTypes, const Compiler::Args &args) override;
3131
CompilerValue *addFunctionCallWithCtx(const std::string &functionName, Compiler::StaticType returnType, const Compiler::ArgTypes &argTypes, const Compiler::Args &args) override;
3232
CompilerConstant *addConstValue(const Value &value) override;
33+
CompilerValue *addLoopIndex() override;
3334
CompilerValue *addVariableValue(Variable *variable) override;
3435
CompilerValue *addListContents(List *list) override;
3536
CompilerValue *addListItem(List *list, CompilerValue *index) override;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ struct LLVMInstruction
5858
BeginElse,
5959
EndIf,
6060
BeginRepeatLoop,
61+
LoopIndex,
6162
BeginWhileLoop,
6263
BeginRepeatUntilLoop,
6364
BeginLoopCondition,

test/dev/llvm/llvmcodebuilder_test.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2907,7 +2907,8 @@ TEST_F(LLVMCodeBuilderTest, RepeatLoop)
29072907
v = m_builder->addConstValue(2);
29082908
v = callConstFuncForType(ValueType::Number, v);
29092909
m_builder->beginRepeatLoop(v);
2910-
m_builder->addTargetFunctionCall("test_function_no_args", Compiler::StaticType::Void, {}, {});
2910+
CompilerValue *index = m_builder->addLoopIndex();
2911+
m_builder->addTargetFunctionCall("test_print_number", Compiler::StaticType::Void, { Compiler::StaticType::Number }, { index });
29112912
m_builder->endLoop();
29122913

29132914
// Nested
@@ -2928,8 +2929,8 @@ TEST_F(LLVMCodeBuilderTest, RepeatLoop)
29282929
v = m_builder->addConstValue(3);
29292930
m_builder->beginRepeatLoop(v);
29302931
{
2931-
v = m_builder->addConstValue(3);
2932-
m_builder->addTargetFunctionCall("test_function_1_arg", Compiler::StaticType::Void, { Compiler::StaticType::String }, { v });
2932+
index = m_builder->addLoopIndex();
2933+
m_builder->addTargetFunctionCall("test_print_number", Compiler::StaticType::Void, { Compiler::StaticType::Number }, { index });
29332934
}
29342935
m_builder->endLoop();
29352936
}
@@ -2950,20 +2951,20 @@ TEST_F(LLVMCodeBuilderTest, RepeatLoop)
29502951
"1_arg 1\n"
29512952
"1_arg 1\n"
29522953
"1_arg 1\n"
2953-
"no_args\n"
2954-
"no_args\n"
2954+
"0\n"
2955+
"1\n"
29552956
"1_arg 1\n"
29562957
"1_arg 1\n"
29572958
"1_arg 2\n"
2958-
"1_arg 3\n"
2959-
"1_arg 3\n"
2960-
"1_arg 3\n"
2959+
"0\n"
2960+
"1\n"
2961+
"2\n"
29612962
"1_arg 1\n"
29622963
"1_arg 1\n"
29632964
"1_arg 2\n"
2964-
"1_arg 3\n"
2965-
"1_arg 3\n"
2966-
"1_arg 3\n";
2965+
"0\n"
2966+
"1\n"
2967+
"2\n";
29672968

29682969
EXPECT_CALL(m_target, isStage).WillRepeatedly(Return(false));
29692970
testing::internal::CaptureStdout();

test/mocks/codebuildermock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class CodeBuilderMock : public ICodeBuilder
1313
MOCK_METHOD(CompilerValue *, addTargetFunctionCall, (const std::string &, Compiler::StaticType, const Compiler::ArgTypes &, const Compiler::Args &), (override));
1414
MOCK_METHOD(CompilerValue *, addFunctionCallWithCtx, (const std::string &, Compiler::StaticType, const Compiler::ArgTypes &, const Compiler::Args &), (override));
1515
MOCK_METHOD(CompilerConstant *, addConstValue, (const Value &), (override));
16+
MOCK_METHOD(CompilerValue *, addLoopIndex, (), (override));
1617
MOCK_METHOD(CompilerValue *, addVariableValue, (Variable *), (override));
1718
MOCK_METHOD(CompilerValue *, addListContents, (List *), (override));
1819
MOCK_METHOD(CompilerValue *, addListItem, (List *, CompilerValue *), (override));

0 commit comments

Comments
 (0)