Skip to content

Commit b88a148

Browse files
committed
LLVMCodeBuilder: Implement list contents
1 parent a1decd9 commit b88a148

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,15 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
623623
break;
624624
}
625625

626+
case LLVMInstruction::Type::GetListContents: {
627+
assert(step.args.size() == 0);
628+
const LLVMListPtr &listPtr = m_listPtrs[step.workList];
629+
llvm::Value *ptr = m_builder.CreateCall(resolve_list_to_string(), listPtr.ptr);
630+
m_heap.push_back(ptr); // deallocate later
631+
step.functionReturnReg->value = ptr;
632+
break;
633+
}
634+
626635
case LLVMInstruction::Type::GetListItem: {
627636
assert(step.args.size() == 1);
628637
const auto &arg = step.args[0];
@@ -964,7 +973,10 @@ CompilerValue *LLVMCodeBuilder::addVariableValue(Variable *variable)
964973

965974
CompilerValue *LLVMCodeBuilder::addListContents(List *list)
966975
{
967-
return addConstValue(Value());
976+
LLVMInstruction ins(LLVMInstruction::Type::GetListContents);
977+
ins.workList = list;
978+
m_listPtrs[list] = LLVMListPtr();
979+
return createOp(ins, Compiler::StaticType::String);
968980
}
969981

970982
CompilerValue *LLVMCodeBuilder::addListItem(List *list, CompilerValue *index)
@@ -2261,6 +2273,12 @@ llvm::FunctionCallee LLVMCodeBuilder::resolve_list_alloc_size_ptr()
22612273
return resolveFunction("list_alloc_size_ptr", llvm::FunctionType::get(m_builder.getInt64Ty()->getPointerTo()->getPointerTo(), { listPtr }, false));
22622274
}
22632275

2276+
llvm::FunctionCallee LLVMCodeBuilder::resolve_list_to_string()
2277+
{
2278+
llvm::Type *pointerType = llvm::PointerType::get(llvm::Type::getInt8Ty(m_ctx), 0);
2279+
return resolveFunction("list_to_string", llvm::FunctionType::get(pointerType, { pointerType }, false));
2280+
}
2281+
22642282
llvm::FunctionCallee LLVMCodeBuilder::resolve_strcasecmp()
22652283
{
22662284
llvm::Type *pointerType = llvm::PointerType::get(llvm::Type::getInt8Ty(m_ctx), 0);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ class LLVMCodeBuilder : public ICodeBuilder
160160
llvm::FunctionCallee resolve_list_data();
161161
llvm::FunctionCallee resolve_list_size_ptr();
162162
llvm::FunctionCallee resolve_list_alloc_size_ptr();
163+
llvm::FunctionCallee resolve_list_to_string();
163164
llvm::FunctionCallee resolve_strcasecmp();
164165

165166
Target *m_target = nullptr;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ struct LLVMInstruction
4848
AppendToList,
4949
InsertToList,
5050
ListReplace,
51+
GetListContents,
5152
GetListItem,
5253
GetListSize,
5354
GetListItemIndex,

test/dev/llvm/llvmcodebuilder_test.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,6 +1801,54 @@ TEST_F(LLVMCodeBuilderTest, ListReplace)
18011801
ASSERT_EQ(localList->toString(), "3 ipsum true hello world");
18021802
}
18031803

1804+
TEST_F(LLVMCodeBuilderTest, GetListContents)
1805+
{
1806+
EngineMock engine;
1807+
Stage stage;
1808+
Sprite sprite;
1809+
sprite.setEngine(&engine);
1810+
EXPECT_CALL(engine, stage()).WillRepeatedly(Return(&stage));
1811+
1812+
std::unordered_map<List *, std::string> strings;
1813+
1814+
auto globalList = std::make_shared<List>("", "");
1815+
stage.addList(globalList);
1816+
1817+
auto localList = std::make_shared<List>("", "");
1818+
sprite.addList(localList);
1819+
1820+
globalList->append(1);
1821+
globalList->append(2);
1822+
globalList->append(3);
1823+
1824+
localList->append("Lorem");
1825+
localList->append("ipsum");
1826+
localList->append("dolor");
1827+
localList->append("sit");
1828+
strings[localList.get()] = localList->toString();
1829+
1830+
createBuilder(&sprite, true);
1831+
1832+
CompilerValue *v = m_builder->addListContents(globalList.get());
1833+
m_builder->addFunctionCall("test_print_string", Compiler::StaticType::Void, { Compiler::StaticType::String }, { v });
1834+
1835+
v = m_builder->addListContents(localList.get());
1836+
m_builder->addFunctionCall("test_print_string", Compiler::StaticType::Void, { Compiler::StaticType::String }, { v });
1837+
1838+
static const std::string expected =
1839+
"123\n"
1840+
"Lorem ipsum dolor sit\n";
1841+
1842+
auto code = m_builder->finalize();
1843+
auto ctx = code->createExecutionContext(&sprite);
1844+
testing::internal::CaptureStdout();
1845+
code->run(ctx.get());
1846+
ASSERT_EQ(testing::internal::GetCapturedStdout(), expected);
1847+
1848+
ASSERT_EQ(globalList->toString(), "123");
1849+
ASSERT_EQ(localList->toString(), "Lorem ipsum dolor sit");
1850+
}
1851+
18041852
TEST_F(LLVMCodeBuilderTest, GetListItem)
18051853
{
18061854
EngineMock engine;

0 commit comments

Comments
 (0)