Skip to content

Commit 9567a04

Browse files
committed
Refactor compiler to use shared context for each target
1 parent dde8dad commit 9567a04

22 files changed

+568
-413
lines changed

include/scratchcpp/dev/compiler.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace libscratchcpp
1212
{
1313

14+
class CompilerContext;
1415
class IEngine;
1516
class Target;
1617
class ExecutableCode;
@@ -39,6 +40,7 @@ class LIBSCRATCHCPP_EXPORT Compiler
3940
using ArgTypes = std::vector<StaticType>;
4041
using Args = std::vector<CompilerValue *>;
4142

43+
Compiler(CompilerContext *ctx);
4244
Compiler(IEngine *engine, Target *target);
4345
Compiler(const Compiler &) = delete;
4446

@@ -132,6 +134,8 @@ class LIBSCRATCHCPP_EXPORT Compiler
132134

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

137+
static std::shared_ptr<CompilerContext> createContext(IEngine *engine, Target *target);
138+
135139
private:
136140
CompilerValue *addInput(Input *input);
137141

src/dev/engine/compiler.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22

33
#include <scratchcpp/dev/compiler.h>
4+
#include <scratchcpp/dev/compilercontext.h>
45
#include <scratchcpp/dev/compilerconstant.h>
56
#include <scratchcpp/block.h>
67
#include <scratchcpp/input.h>
@@ -12,7 +13,13 @@
1213

1314
using namespace libscratchcpp;
1415

15-
/*! Constructs Compiler. */
16+
/*! Constructs Compiler using the given context. */
17+
Compiler::Compiler(CompilerContext *ctx) :
18+
impl(spimpl::make_unique_impl<CompilerPrivate>(ctx))
19+
{
20+
}
21+
22+
/*! Constructs Compiler using a new context for the given target. */
1623
Compiler::Compiler(IEngine *engine, Target *target) :
1724
impl(spimpl::make_unique_impl<CompilerPrivate>(engine, target))
1825
{
@@ -21,13 +28,13 @@ Compiler::Compiler(IEngine *engine, Target *target) :
2128
/*! Returns the Engine of the project. */
2229
IEngine *Compiler::engine() const
2330
{
24-
return impl->engine;
31+
return impl->ctx->engine();
2532
}
2633

2734
/*! Returns the Target of this compiler. */
2835
Target *Compiler::target() const
2936
{
30-
return impl->target;
37+
return impl->ctx->target();
3138
}
3239

3340
/*! Returns currently compiled block. */
@@ -39,7 +46,7 @@ std::shared_ptr<libscratchcpp::Block> Compiler::block() const
3946
/*! Compiles the script starting with the given block. */
4047
std::shared_ptr<ExecutableCode> Compiler::compile(std::shared_ptr<Block> startBlock)
4148
{
42-
impl->builder = impl->builderFactory->create(impl->target, startBlock->id(), false);
49+
impl->builder = impl->builderFactory->create(impl->ctx, false);
4350
impl->substackTree.clear();
4451
impl->substackHit = false;
4552
impl->emptySubstack = false;
@@ -570,6 +577,13 @@ const std::unordered_set<std::string> &Compiler::unsupportedBlocks() const
570577
return impl->unsupportedBlocks;
571578
}
572579

580+
/*! Creates a compiler context for the given target. */
581+
std::shared_ptr<CompilerContext> Compiler::createContext(IEngine *engine, Target *target)
582+
{
583+
CompilerPrivate::initBuilderFactory();
584+
return CompilerPrivate::builderFactory->createCtx(engine, target);
585+
}
586+
573587
CompilerValue *Compiler::addInput(Input *input)
574588
{
575589
if (!input)

src/dev/engine/compiler_p.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// SPDX-License-Identifier: Apache-2.0
22

3+
#include <scratchcpp/dev/compiler.h>
34
#include <scratchcpp/block.h>
45

56
#include "compiler_p.h"
@@ -9,9 +10,21 @@
910

1011
using namespace libscratchcpp;
1112

13+
CompilerPrivate::CompilerPrivate(CompilerContext *ctx) :
14+
ctx(ctx)
15+
{
16+
assert(ctx);
17+
initBuilderFactory();
18+
}
19+
1220
CompilerPrivate::CompilerPrivate(IEngine *engine, Target *target) :
13-
engine(engine),
14-
target(target)
21+
ctxPtr(Compiler::createContext(engine, target)),
22+
ctx(ctxPtr.get())
23+
{
24+
initBuilderFactory();
25+
}
26+
27+
void CompilerPrivate::initBuilderFactory()
1528
{
1629
if (!builderFactory)
1730
builderFactory = CodeBuilderFactory::instance().get();

src/dev/engine/compiler_p.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace libscratchcpp
1010
{
1111

12+
class CompilerContext;
1213
class IEngine;
1314
class Target;
1415
class Block;
@@ -24,12 +25,15 @@ struct CompilerPrivate
2425
IfStatement
2526
};
2627

28+
CompilerPrivate(CompilerContext *ctx);
2729
CompilerPrivate(IEngine *engine, Target *target);
2830

31+
static void initBuilderFactory();
32+
2933
void substackEnd();
3034

31-
IEngine *engine = nullptr;
32-
Target *target = nullptr;
35+
std::shared_ptr<CompilerContext> ctxPtr; // for self-managed contexts
36+
CompilerContext *ctx = nullptr;
3337

3438
std::shared_ptr<Block> block;
3539
int customIfStatementCount = 0;

src/dev/engine/internal/codebuilderfactory.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "codebuilderfactory.h"
44
#include "llvm/llvmcodebuilder.h"
5+
#include "llvm/llvmcompilercontext.h"
56

67
using namespace libscratchcpp;
78

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

15-
std::shared_ptr<ICodeBuilder> CodeBuilderFactory::create(Target *target, const std::string &id, bool warp) const
16+
std::shared_ptr<ICodeBuilder> CodeBuilderFactory::create(CompilerContext *ctx, bool warp) const
1617
{
17-
return std::make_shared<LLVMCodeBuilder>(target, id, warp);
18+
assert(dynamic_cast<LLVMCompilerContext *>(ctx));
19+
return std::make_shared<LLVMCodeBuilder>(static_cast<LLVMCompilerContext *>(ctx), warp);
20+
}
21+
22+
std::shared_ptr<CompilerContext> CodeBuilderFactory::createCtx(IEngine *engine, Target *target) const
23+
{
24+
auto ptr = std::make_shared<LLVMCompilerContext>(engine, target);
25+
return ptr;
1826
}

src/dev/engine/internal/codebuilderfactory.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ class CodeBuilderFactory : public ICodeBuilderFactory
1111
{
1212
public:
1313
static std::shared_ptr<CodeBuilderFactory> instance();
14-
std::shared_ptr<ICodeBuilder> create(Target *target, const std::string &id, bool warp) const override;
14+
std::shared_ptr<ICodeBuilder> create(CompilerContext *ctx, bool warp) const override;
15+
std::shared_ptr<CompilerContext> createCtx(IEngine *engine, Target *target) const override;
1516

1617
private:
1718
static std::shared_ptr<CodeBuilderFactory> m_instance;

src/dev/engine/internal/icodebuilderfactory.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ namespace libscratchcpp
88
{
99

1010
class ICodeBuilder;
11+
class CompilerContext;
1112
class Target;
13+
class IEngine;
1214

1315
class ICodeBuilderFactory
1416
{
1517
public:
1618
virtual ~ICodeBuilderFactory() { }
1719

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

2124
} // namespace libscratchcpp

src/dev/engine/internal/llvm/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ target_sources(scratchcpp
1616
llvmtypes.cpp
1717
llvmtypes.h
1818
llvmfunctions.cpp
19+
llvmcompilercontext.cpp
20+
llvmcompilercontext.h
1921
llvmexecutablecode.cpp
2022
llvmexecutablecode.h
2123
llvmexecutioncontext.cpp

0 commit comments

Comments
 (0)