Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions include/scratchcpp/executioncontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

#pragma once

#include "global.h"
#include <stack>
#include <vector>

#include "string_pool.h"
#include "spimpl.h"

namespace libscratchcpp
Expand All @@ -13,6 +16,7 @@ class IEngine;
class Promise;
class IStackTimer;
class IRandomGenerator;
struct StringPtr;
class ExecutionContextPrivate;

/*! \brief The ExecutionContext represents the execution context of a target (can be a clone) with variables, lists, etc. */
Expand All @@ -21,7 +25,7 @@ class LIBSCRATCHCPP_EXPORT ExecutionContext
public:
ExecutionContext(Thread *thread);
ExecutionContext(const ExecutionContext &) = delete;
virtual ~ExecutionContext() { }
virtual ~ExecutionContext();

Thread *thread() const;
IEngine *engine() const;
Expand All @@ -35,8 +39,50 @@ class LIBSCRATCHCPP_EXPORT ExecutionContext
IRandomGenerator *rng() const;
void setRng(IRandomGenerator *newRng);

inline StringPtr **pushStringStack(size_t stringCount)
{
m_stringStackCleared = false;
m_stringStack.push({});

auto &strings = m_stringStack.top();

for (size_t i = 0; i < stringCount; i++) {
StringPtr *str = string_pool_new();
strings.push_back(str);
}

return strings.data();
}

inline void popStringStack()
{
if (m_stringStackCleared) // avoid UB when popping after killing a thread
return;

assert(!m_stringStack.empty()); // check only in debug mode to maximize performance

const auto &strings = m_stringStack.top();

for (StringPtr *str : strings)
string_pool_free(str);

m_stringStack.pop();
}

inline void clearStringStack()
{
while (!m_stringStack.empty()) {
popStringStack();
}

m_stringStackCleared = true;
}

private:
spimpl::unique_impl_ptr<ExecutionContextPrivate> impl;

std::stack<std::vector<StringPtr *>> m_stringStack;
bool m_stringStackCleared = false;
};

} // namespace libscratchcpp
1 change: 1 addition & 0 deletions include/scratchcpp/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class LIBSCRATCHCPP_EXPORT Thread
public:
Thread(Target *target, IEngine *engine, Script *script);
Thread(const Thread &) = delete;
~Thread();

Target *target() const;
IEngine *engine() const;
Expand Down
7 changes: 7 additions & 0 deletions src/engine/executioncontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ ExecutionContext::ExecutionContext(Thread *thread) :
{
}

/*! Destroys ExecutionContext. */
ExecutionContext::~ExecutionContext()
{
// Deallocate strings
clearStringStack();
}

/*! Returns the thread of this context. */
Thread *ExecutionContext::thread() const
{
Expand Down
34 changes: 25 additions & 9 deletions src/engine/internal/codegen/llvm/llvmbuildutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,24 @@ void LLVMBuildUtils::init(llvm::Function *function, BlockPrototype *procedurePro
reg->intValue = llvm::ConstantInt::get(m_builder.getInt64Ty(), reg->constValue().toDouble(), true);
}

// Create string count variable (LLVM should optimize it into a constant later)
m_stringCountVar = m_builder.CreateAlloca(m_builder.getInt64Ty(), nullptr, "stringCount");

// Create string count block (string count is known after all instructions are processed)
m_stringCountBlock = llvm::BasicBlock::Create(m_llvmCtx, "getStringCount", m_function);
m_builder.CreateBr(m_stringCountBlock);
// NOTE: This block is terminated later

// Create string allocation block
m_builder.SetInsertPoint(m_stringCountBlock);
m_stringAllocaBlock = llvm::BasicBlock::Create(m_llvmCtx, "stringAlloca", m_function);
m_builder.CreateBr(m_stringAllocaBlock);
// NOTE: This block is terminated later

// Get string array
// Push string stack (load string array)
m_builder.SetInsertPoint(m_stringAllocaBlock);
m_stringArray = m_builder.CreateCall(m_functions.resolve_llvm_get_string_array(), { m_executionContextPtr, m_functionIdValue });
// NOTE: This block is terminated later

llvm::Value *stringCount = m_builder.CreateLoad(m_builder.getInt64Ty(), m_stringCountVar, "strCount");
m_stringArray = m_builder.CreateCall(m_functions.resolve_llvm_push_string_stack(), { m_executionContextPtr, stringCount });

// Create next block
m_stringAllocaNextBlock = llvm::BasicBlock::Create(m_llvmCtx, "entry.next", m_function);
Expand Down Expand Up @@ -146,11 +156,13 @@ void LLVMBuildUtils::init(llvm::Function *function, BlockPrototype *procedurePro

void LLVMBuildUtils::end(LLVMInstruction *lastInstruction, LLVMRegister *lastConstant)
{
// Terminate string allocation block
if (m_stringCount == 0)
m_stringAllocaBlock->erase(m_stringAllocaBlock->begin(), m_stringAllocaBlock->end());

// Terminate string count and allocation blocks
llvm::BasicBlock *previousBlock = m_builder.GetInsertBlock();
m_builder.SetInsertPoint(m_stringCountBlock);
m_builder.CreateStore(m_builder.getInt64(m_stringCount), m_stringCountVar);

m_builder.CreateBr(m_stringAllocaBlock);

m_builder.SetInsertPoint(m_stringAllocaBlock);
m_builder.CreateBr(m_stringAllocaNextBlock);
m_builder.SetInsertPoint(previousBlock);
Expand All @@ -165,6 +177,7 @@ void LLVMBuildUtils::end(LLVMInstruction *lastInstruction, LLVMRegister *lastCon

// End branch
m_builder.SetInsertPoint(m_endBranch);
m_builder.CreateCall(m_functions.resolve_llvm_pop_string_stack(), { m_executionContextPtr });

llvm::PointerType *pointerType = llvm::PointerType::get(llvm::Type::getInt8Ty(m_llvmCtx), 0);

Expand Down Expand Up @@ -198,6 +211,7 @@ void LLVMBuildUtils::end(LLVMInstruction *lastInstruction, LLVMRegister *lastCon

// Thread end branch (stop the entire thread, including procedure callers)
m_builder.SetInsertPoint(m_endThreadBranch);
m_builder.CreateCall(m_functions.resolve_llvm_pop_string_stack(), { m_executionContextPtr });

switch (m_codeType) {
case Compiler::CodeType::Script:
Expand Down Expand Up @@ -497,7 +511,7 @@ llvm::Value *LLVMBuildUtils::addAlloca(llvm::Type *type)

llvm::Value *LLVMBuildUtils::addStringAlloca()
{
// All temporary strings are allocated before the script is called
// All temporary strings are allocated when the string stack is pushed
llvm::BasicBlock *block = m_builder.GetInsertBlock();
m_builder.SetInsertPoint(m_stringAllocaBlock);

Expand Down Expand Up @@ -1418,7 +1432,9 @@ void LLVMBuildUtils::createSuspend()
}

syncVariables();

m_coroutine->createSuspend();

reloadVariables();
reloadLists();

Expand Down
2 changes: 2 additions & 0 deletions src/engine/internal/codegen/llvm/llvmbuildutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,10 @@ class LIBSCRATCHCPP_TEST_EXPORT LLVMBuildUtils
std::vector<LLVMIfStatement> m_ifStatements;
std::vector<LLVMLoop> m_loops;

llvm::BasicBlock *m_stringCountBlock = nullptr;
llvm::BasicBlock *m_stringAllocaBlock = nullptr;
llvm::BasicBlock *m_stringAllocaNextBlock = nullptr;
llvm::Value *m_stringCountVar = nullptr;
llvm::Value *m_stringArray = nullptr;
size_t m_stringCount = 0;
};
Expand Down
2 changes: 1 addition & 1 deletion src/engine/internal/codegen/llvm/llvmcodebuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::build()
m_utils.end(m_instructions.empty() ? nullptr : m_instructions.last(), m_lastConstValue);
verifyFunction(m_function);

return std::make_shared<LLVMExecutableCode>(m_ctx, m_utils.scriptFunctionId(), m_function->getName().str(), m_ctx->coroutineResumeFunction()->getName().str(), m_utils.stringCount(), m_codeType);
return std::make_shared<LLVMExecutableCode>(m_ctx, m_utils.scriptFunctionId(), m_function->getName().str(), m_ctx->coroutineResumeFunction()->getName().str(), m_codeType);
}

CompilerValue *LLVMCodeBuilder::addFunctionCall(const std::string &functionName, Compiler::StaticType returnType, const Compiler::ArgTypes &argTypes, const Compiler::Args &args)
Expand Down
14 changes: 1 addition & 13 deletions src/engine/internal/codegen/llvm/llvmexecutablecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,11 @@ using namespace libscratchcpp;

static const void *END_THREAD_SENTINEL = (void *)0x1;

LLVMExecutableCode::LLVMExecutableCode(
LLVMCompilerContext *ctx,
function_id_t functionId,
const std::string &mainFunctionName,
const std::string &resumeFunctionName,
size_t stringCount,
Compiler::CodeType codeType) :
LLVMExecutableCode::LLVMExecutableCode(LLVMCompilerContext *ctx, function_id_t functionId, const std::string &mainFunctionName, const std::string &resumeFunctionName, Compiler::CodeType codeType) :
m_ctx(ctx),
m_functionId(functionId),
m_mainFunctionName(mainFunctionName),
m_resumeFunctionName(resumeFunctionName),
m_stringCount(stringCount),
m_codeType(codeType)
{
assert(m_ctx);
Expand Down Expand Up @@ -153,11 +146,6 @@ function_id_t LLVMExecutableCode::functionId() const
return m_functionId;
}

size_t LLVMExecutableCode::stringCount() const
{
return m_stringCount;
}

LLVMExecutionContext *LLVMExecutableCode::getContext(ExecutionContext *context)
{
assert(dynamic_cast<LLVMExecutionContext *>(context));
Expand Down
10 changes: 1 addition & 9 deletions src/engine/internal/codegen/llvm/llvmexecutablecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ class LLVMExecutionContext;
class LIBSCRATCHCPP_TEST_EXPORT LLVMExecutableCode : public ExecutableCode
{
public:
LLVMExecutableCode(
LLVMCompilerContext *ctx,
function_id_t functionId,
const std::string &mainFunctionName,
const std::string &resumeFunctionName,
size_t stringCount,
Compiler::CodeType codeType);
LLVMExecutableCode(LLVMCompilerContext *ctx, function_id_t functionId, const std::string &mainFunctionName, const std::string &resumeFunctionName, Compiler::CodeType codeType);

void run(ExecutionContext *context) override;
ValueData runReporter(ExecutionContext *context) override;
Expand All @@ -37,7 +31,6 @@ class LIBSCRATCHCPP_TEST_EXPORT LLVMExecutableCode : public ExecutableCode
std::shared_ptr<ExecutionContext> createExecutionContext(Thread *thread) const override;

function_id_t functionId() const;
size_t stringCount() const;

private:
using MainFunctionType = void *(*)(ExecutionContext *, Target *, ValueData **, List **);
Expand All @@ -52,7 +45,6 @@ class LIBSCRATCHCPP_TEST_EXPORT LLVMExecutableCode : public ExecutableCode
std::string m_mainFunctionName;
std::string m_predicateFunctionName;
std::string m_resumeFunctionName;
size_t m_stringCount = 0;
Compiler::CodeType m_codeType;

mutable std::variant<MainFunctionType, ReporterFunctionType, PredicateFunctionType> m_mainFunction;
Expand Down
25 changes: 0 additions & 25 deletions src/engine/internal/codegen/llvm/llvmexecutioncontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,6 @@ LLVMExecutionContext::LLVMExecutionContext(LLVMCompilerContext *compilerCtx, Thr
ExecutionContext(thread),
m_compilerCtx(compilerCtx)
{
// Allocate strings for all scripts in the target
// TODO: Allocate strings for this script and procedures it calls
const auto &codeMap = compilerCtx->codeMap();

for (const auto &[functionId, code] : codeMap) {
size_t count = code->stringCount();

if (count > 0) {
m_stringVectors[functionId] = {};

auto &strings = m_stringVectors[functionId];
strings.reserve(count);

for (size_t i = 0; i < count; i++)
strings.push_back(string_pool_new());

m_stringArrays[functionId] = strings.data();
}
}
}

LLVMExecutionContext::~LLVMExecutionContext()
Expand All @@ -41,12 +22,6 @@ LLVMExecutionContext::~LLVMExecutionContext()
} else
assert(false);
}

// Deallocate strings
for (const auto &[functionId, strings] : m_stringVectors) {
for (StringPtr *str : strings)
string_pool_free(str);
}
}

void *LLVMExecutionContext::coroutineHandle() const
Expand Down
9 changes: 0 additions & 9 deletions src/engine/internal/codegen/llvm/llvmexecutioncontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,10 @@ class LIBSCRATCHCPP_TEST_EXPORT LLVMExecutionContext : public ExecutionContext
bool finished() const;
void setFinished(bool newFinished);

inline StringPtr **getStringArray(function_id_t functionId)
{
assert(m_stringArrays.find(functionId) != m_stringArrays.cend());
return m_stringArrays[functionId];
}

private:
LLVMCompilerContext *m_compilerCtx = nullptr;
void *m_coroutineHandle = nullptr;
bool m_finished = false;

std::unordered_map<function_id_t, std::vector<StringPtr *>> m_stringVectors;
std::unordered_map<function_id_t, StringPtr **> m_stringArrays;
};

} // namespace libscratchcpp
23 changes: 18 additions & 5 deletions src/engine/internal/codegen/llvm/llvmfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ extern "C"
return ctx->rng()->randint(from, to);
}

LIBSCRATCHCPP_EXPORT StringPtr **llvm_get_string_array(ExecutionContext *ctx, function_id_t functionId)
LIBSCRATCHCPP_EXPORT StringPtr **llvm_push_string_stack(ExecutionContext *ctx, size_t stringCount)
{
return static_cast<LLVMExecutionContext *>(ctx)->getStringArray(functionId);
return ctx->pushStringStack(stringCount);
}

LIBSCRATCHCPP_EXPORT void llvm_pop_string_stack(ExecutionContext *ctx)
{
return ctx->popStringStack();
}

LIBSCRATCHCPP_EXPORT void llvm_mark_thread_as_finished(ExecutionContext *ctx)
Expand Down Expand Up @@ -290,14 +295,22 @@ llvm::FunctionCallee LLVMFunctions::resolve_llvm_random_bool()
return resolveFunction("llvm_random_bool", llvm::FunctionType::get(m_builder->getDoubleTy(), { pointerType, m_builder->getInt1Ty(), m_builder->getInt1Ty() }, false));
}

llvm::FunctionCallee LLVMFunctions::resolve_llvm_get_string_array()
llvm::FunctionCallee LLVMFunctions::resolve_llvm_push_string_stack()
{
llvm::Type *stringPtr = m_stringPtrType->getPointerTo();
llvm::Type *pointerType = llvm::PointerType::get(llvm::Type::getInt8Ty(*m_ctx->llvmCtx()), 0);

llvm::FunctionCallee callee = resolveFunction("llvm_get_string_array", llvm::FunctionType::get(stringPtr->getPointerTo(), { pointerType, m_functionIdType }, false));
llvm::FunctionCallee callee = resolveFunction("llvm_push_string_stack", llvm::FunctionType::get(stringPtr->getPointerTo(), { pointerType, m_builder->getInt64Ty() }, false));
llvm::Function *func = llvm::cast<llvm::Function>(callee.getCallee());
return callee;
}

llvm::FunctionCallee LLVMFunctions::resolve_llvm_pop_string_stack()
{
llvm::Type *pointerType = llvm::PointerType::get(llvm::Type::getInt8Ty(*m_ctx->llvmCtx()), 0);

llvm::FunctionCallee callee = resolveFunction("llvm_pop_string_stack", llvm::FunctionType::get(m_builder->getVoidTy(), { pointerType }, false));
llvm::Function *func = llvm::cast<llvm::Function>(callee.getCallee());
func->addFnAttr(llvm::Attribute::ReadOnly);
return callee;
}

Expand Down
3 changes: 2 additions & 1 deletion src/engine/internal/codegen/llvm/llvmfunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class LLVMFunctions
llvm::FunctionCallee resolve_llvm_random_double();
llvm::FunctionCallee resolve_llvm_random_int64();
llvm::FunctionCallee resolve_llvm_random_bool();
llvm::FunctionCallee resolve_llvm_get_string_array();
llvm::FunctionCallee resolve_llvm_push_string_stack();
llvm::FunctionCallee resolve_llvm_pop_string_stack();
llvm::FunctionCallee resolve_llvm_mark_thread_as_finished();
llvm::FunctionCallee resolve_llvm_is_thread_finished();
llvm::FunctionCallee resolve_llvm_print();
Expand Down
Loading
Loading