Skip to content

Commit

Permalink
Refactor compiler to use shared context for each target
Browse files Browse the repository at this point in the history
  • Loading branch information
adazem009 committed Dec 30, 2024
1 parent dde8dad commit 9567a04
Show file tree
Hide file tree
Showing 22 changed files with 568 additions and 413 deletions.
4 changes: 4 additions & 0 deletions include/scratchcpp/dev/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace libscratchcpp
{

class CompilerContext;
class IEngine;
class Target;
class ExecutableCode;
Expand Down Expand Up @@ -39,6 +40,7 @@ class LIBSCRATCHCPP_EXPORT Compiler
using ArgTypes = std::vector<StaticType>;
using Args = std::vector<CompilerValue *>;

Compiler(CompilerContext *ctx);
Compiler(IEngine *engine, Target *target);
Compiler(const Compiler &) = delete;

Expand Down Expand Up @@ -132,6 +134,8 @@ class LIBSCRATCHCPP_EXPORT Compiler

const std::unordered_set<std::string> &unsupportedBlocks() const;

static std::shared_ptr<CompilerContext> createContext(IEngine *engine, Target *target);

private:
CompilerValue *addInput(Input *input);

Expand Down
22 changes: 18 additions & 4 deletions src/dev/engine/compiler.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: Apache-2.0

#include <scratchcpp/dev/compiler.h>
#include <scratchcpp/dev/compilercontext.h>
#include <scratchcpp/dev/compilerconstant.h>
#include <scratchcpp/block.h>
#include <scratchcpp/input.h>
Expand All @@ -12,7 +13,13 @@

using namespace libscratchcpp;

/*! Constructs Compiler. */
/*! Constructs Compiler using the given context. */
Compiler::Compiler(CompilerContext *ctx) :
impl(spimpl::make_unique_impl<CompilerPrivate>(ctx))
{
}

/*! Constructs Compiler using a new context for the given target. */
Compiler::Compiler(IEngine *engine, Target *target) :
impl(spimpl::make_unique_impl<CompilerPrivate>(engine, target))
{
Expand All @@ -21,13 +28,13 @@ Compiler::Compiler(IEngine *engine, Target *target) :
/*! Returns the Engine of the project. */
IEngine *Compiler::engine() const
{
return impl->engine;
return impl->ctx->engine();
}

/*! Returns the Target of this compiler. */
Target *Compiler::target() const
{
return impl->target;
return impl->ctx->target();
}

/*! Returns currently compiled block. */
Expand All @@ -39,7 +46,7 @@ std::shared_ptr<libscratchcpp::Block> Compiler::block() const
/*! Compiles the script starting with the given block. */
std::shared_ptr<ExecutableCode> Compiler::compile(std::shared_ptr<Block> startBlock)
{
impl->builder = impl->builderFactory->create(impl->target, startBlock->id(), false);
impl->builder = impl->builderFactory->create(impl->ctx, false);
impl->substackTree.clear();
impl->substackHit = false;
impl->emptySubstack = false;
Expand Down Expand Up @@ -570,6 +577,13 @@ const std::unordered_set<std::string> &Compiler::unsupportedBlocks() const
return impl->unsupportedBlocks;
}

/*! Creates a compiler context for the given target. */
std::shared_ptr<CompilerContext> Compiler::createContext(IEngine *engine, Target *target)
{
CompilerPrivate::initBuilderFactory();
return CompilerPrivate::builderFactory->createCtx(engine, target);
}

CompilerValue *Compiler::addInput(Input *input)
{
if (!input)
Expand Down
17 changes: 15 additions & 2 deletions src/dev/engine/compiler_p.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: Apache-2.0

#include <scratchcpp/dev/compiler.h>
#include <scratchcpp/block.h>

#include "compiler_p.h"
Expand All @@ -9,9 +10,21 @@

using namespace libscratchcpp;

CompilerPrivate::CompilerPrivate(CompilerContext *ctx) :
ctx(ctx)
{
assert(ctx);
initBuilderFactory();
}

CompilerPrivate::CompilerPrivate(IEngine *engine, Target *target) :
engine(engine),
target(target)
ctxPtr(Compiler::createContext(engine, target)),
ctx(ctxPtr.get())
{
initBuilderFactory();
}

void CompilerPrivate::initBuilderFactory()
{
if (!builderFactory)
builderFactory = CodeBuilderFactory::instance().get();
Expand Down
8 changes: 6 additions & 2 deletions src/dev/engine/compiler_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace libscratchcpp
{

class CompilerContext;
class IEngine;
class Target;
class Block;
Expand All @@ -24,12 +25,15 @@ struct CompilerPrivate
IfStatement
};

CompilerPrivate(CompilerContext *ctx);
CompilerPrivate(IEngine *engine, Target *target);

static void initBuilderFactory();

void substackEnd();

IEngine *engine = nullptr;
Target *target = nullptr;
std::shared_ptr<CompilerContext> ctxPtr; // for self-managed contexts
CompilerContext *ctx = nullptr;

std::shared_ptr<Block> block;
int customIfStatementCount = 0;
Expand Down
12 changes: 10 additions & 2 deletions src/dev/engine/internal/codebuilderfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "codebuilderfactory.h"
#include "llvm/llvmcodebuilder.h"
#include "llvm/llvmcompilercontext.h"

using namespace libscratchcpp;

Expand All @@ -12,7 +13,14 @@ std::shared_ptr<CodeBuilderFactory> CodeBuilderFactory::instance()
return m_instance;
}

std::shared_ptr<ICodeBuilder> CodeBuilderFactory::create(Target *target, const std::string &id, bool warp) const
std::shared_ptr<ICodeBuilder> CodeBuilderFactory::create(CompilerContext *ctx, bool warp) const
{
return std::make_shared<LLVMCodeBuilder>(target, id, warp);
assert(dynamic_cast<LLVMCompilerContext *>(ctx));
return std::make_shared<LLVMCodeBuilder>(static_cast<LLVMCompilerContext *>(ctx), warp);
}

std::shared_ptr<CompilerContext> CodeBuilderFactory::createCtx(IEngine *engine, Target *target) const
{
auto ptr = std::make_shared<LLVMCompilerContext>(engine, target);
return ptr;
}
3 changes: 2 additions & 1 deletion src/dev/engine/internal/codebuilderfactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class CodeBuilderFactory : public ICodeBuilderFactory
{
public:
static std::shared_ptr<CodeBuilderFactory> instance();
std::shared_ptr<ICodeBuilder> create(Target *target, const std::string &id, bool warp) const override;
std::shared_ptr<ICodeBuilder> create(CompilerContext *ctx, bool warp) const override;
std::shared_ptr<CompilerContext> createCtx(IEngine *engine, Target *target) const override;

private:
static std::shared_ptr<CodeBuilderFactory> m_instance;
Expand Down
5 changes: 4 additions & 1 deletion src/dev/engine/internal/icodebuilderfactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ namespace libscratchcpp
{

class ICodeBuilder;
class CompilerContext;
class Target;
class IEngine;

class ICodeBuilderFactory
{
public:
virtual ~ICodeBuilderFactory() { }

virtual std::shared_ptr<ICodeBuilder> create(Target *target, const std::string &id, bool warp) const = 0;
virtual std::shared_ptr<ICodeBuilder> create(CompilerContext *ctx, bool warp) const = 0;
virtual std::shared_ptr<CompilerContext> createCtx(IEngine *engine, Target *target) const = 0;
};

} // namespace libscratchcpp
2 changes: 2 additions & 0 deletions src/dev/engine/internal/llvm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ target_sources(scratchcpp
llvmtypes.cpp
llvmtypes.h
llvmfunctions.cpp
llvmcompilercontext.cpp
llvmcompilercontext.h
llvmexecutablecode.cpp
llvmexecutablecode.h
llvmexecutioncontext.cpp
Expand Down
Loading

0 comments on commit 9567a04

Please sign in to comment.