Skip to content

Commit c06b6e6

Browse files
authored
Merge pull request #697 from scratchcpp/implement_generic_type_analyzer
Implement a generic type analyzer
2 parents 500fd4d + c14feca commit c06b6e6

91 files changed

Lines changed: 1995 additions & 1889 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎include/scratchcpp/compiler.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class LIBSCRATCHCPP_EXPORT Compiler
5353
using Args = std::vector<CompilerValue *>;
5454

5555
Compiler(CompilerContext *ctx);
56-
Compiler(IEngine *engine, Target *target);
56+
Compiler(IEngine *engine, Target *target, const std::unordered_set<std::string> &unsafeConstants = {});
5757
Compiler(const Compiler &) = delete;
5858

5959
IEngine *engine() const;
@@ -164,7 +164,7 @@ class LIBSCRATCHCPP_EXPORT Compiler
164164

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

167-
static std::shared_ptr<CompilerContext> createContext(IEngine *engine, Target *target);
167+
static std::shared_ptr<CompilerContext> createContext(IEngine *engine, Target *target, const std::unordered_set<std::string> &unsafeConstants);
168168

169169
private:
170170
spimpl::unique_impl_ptr<CompilerPrivate> impl;

‎include/scratchcpp/compilercontext.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#pragma once
44

5+
#include <unordered_set>
6+
57
#include "global.h"
68
#include "spimpl.h"
79

@@ -16,13 +18,15 @@ class CompilerContextPrivate;
1618
class LIBSCRATCHCPP_EXPORT CompilerContext
1719
{
1820
public:
19-
CompilerContext(IEngine *engine, Target *target);
21+
CompilerContext(IEngine *engine, Target *target, const std::unordered_set<std::string> &unsafeConstants = {});
2022
CompilerContext(const CompilerContext &) = delete;
2123
virtual ~CompilerContext() { }
2224

2325
IEngine *engine() const;
2426
Target *target() const;
2527

28+
const std::unordered_set<std::string> &unsafeConstants() const;
29+
2630
/*!
2731
* Optimizes compiled scripts ahead of time.
2832
* \see Compiler#preoptimize()

‎src/engine/CMakeLists.txt‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ target_sources(scratchcpp
2929
thread_p.h
3030
internal/engine.cpp
3131
internal/engine.h
32-
internal/icodebuilder.h
33-
internal/icodebuilderfactory.h
34-
internal/codebuilderfactory.cpp
35-
internal/codebuilderfactory.h
3632
internal/clock.cpp
3733
internal/clock.h
3834
internal/iclock.h
@@ -44,4 +40,4 @@ target_sources(scratchcpp
4440
internal/randomgenerator.cpp
4541
)
4642

47-
add_subdirectory(internal/llvm)
43+
add_subdirectory(internal/codegen)

‎src/engine/compiler.cpp‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
#include <scratchcpp/inputvalue.h>
99

1010
#include "compiler_p.h"
11-
#include "internal/icodebuilderfactory.h"
12-
#include "internal/icodebuilder.h"
11+
#include "internal/codegen/icodebuilderfactory.h"
12+
#include "internal/codegen/icodebuilder.h"
1313

1414
using namespace libscratchcpp;
1515

@@ -20,8 +20,8 @@ Compiler::Compiler(CompilerContext *ctx) :
2020
}
2121

2222
/*! Constructs Compiler using a new context for the given target. */
23-
Compiler::Compiler(IEngine *engine, Target *target) :
24-
impl(spimpl::make_unique_impl<CompilerPrivate>(engine, target))
23+
Compiler::Compiler(IEngine *engine, Target *target, const std::unordered_set<std::string> &unsafeConstants) :
24+
impl(spimpl::make_unique_impl<CompilerPrivate>(engine, target, unsafeConstants))
2525
{
2626
}
2727

@@ -751,8 +751,8 @@ const std::unordered_set<std::string> &Compiler::unsupportedBlocks() const
751751
}
752752

753753
/*! Creates a compiler context for the given target. */
754-
std::shared_ptr<CompilerContext> Compiler::createContext(IEngine *engine, Target *target)
754+
std::shared_ptr<CompilerContext> Compiler::createContext(IEngine *engine, Target *target, const std::unordered_set<std::string> &unsafeConstants)
755755
{
756756
CompilerPrivate::initBuilderFactory();
757-
return CompilerPrivate::builderFactory()->createCtx(engine, target);
757+
return CompilerPrivate::builderFactory()->createCtx(engine, target, unsafeConstants);
758758
}

‎src/engine/compiler_p.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
#include "compiler_p.h"
77

8-
#include "internal/codebuilderfactory.h"
9-
#include "internal/icodebuilder.h"
8+
#include "internal/codegen/codebuilderfactory.h"
9+
#include "internal/codegen/icodebuilder.h"
1010

1111
using namespace libscratchcpp;
1212

@@ -17,8 +17,8 @@ CompilerPrivate::CompilerPrivate(CompilerContext *ctx) :
1717
initBuilderFactory();
1818
}
1919

20-
CompilerPrivate::CompilerPrivate(IEngine *engine, Target *target) :
21-
ctxPtr(Compiler::createContext(engine, target)),
20+
CompilerPrivate::CompilerPrivate(IEngine *engine, Target *target, const std::unordered_set<std::string> &unsafeConstants) :
21+
ctxPtr(Compiler::createContext(engine, target, unsafeConstants)),
2222
ctx(ctxPtr.get())
2323
{
2424
initBuilderFactory();

‎src/engine/compiler_p.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class LIBSCRATCHCPP_TEST_EXPORT CompilerPrivate
2929
};
3030

3131
CompilerPrivate(CompilerContext *ctx);
32-
CompilerPrivate(IEngine *engine, Target *target);
32+
CompilerPrivate(IEngine *engine, Target *target, const std::unordered_set<std::string> &unsafeConstants);
3333

3434
static void initBuilderFactory();
3535

‎src/engine/compilercontext.cpp‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
using namespace libscratchcpp;
88

99
/*! Constructs CompilerContext. */
10-
CompilerContext::CompilerContext(IEngine *engine, Target *target) :
11-
impl(spimpl::make_unique_impl<CompilerContextPrivate>(engine, target))
10+
CompilerContext::CompilerContext(IEngine *engine, Target *target, const std::unordered_set<std::string> &unsafeConstants) :
11+
impl(spimpl::make_unique_impl<CompilerContextPrivate>(engine, target, unsafeConstants))
1212
{
1313
}
1414

@@ -23,3 +23,9 @@ Target *CompilerContext::target() const
2323
{
2424
return impl->target;
2525
}
26+
27+
/*! Returns the list of unsafe constants (used internally during optimization). */
28+
const std::unordered_set<std::string> &CompilerContext::unsafeConstants() const
29+
{
30+
return impl->unsafeConstants;
31+
}

‎src/engine/compilercontext_p.cpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
using namespace libscratchcpp;
66

7-
CompilerContextPrivate::CompilerContextPrivate(IEngine *engine, Target *target) :
7+
CompilerContextPrivate::CompilerContextPrivate(IEngine *engine, Target *target, const std::unordered_set<std::string> &unsafeConstants) :
88
engine(engine),
9-
target(target)
9+
target(target),
10+
unsafeConstants(unsafeConstants)
1011
{
1112
}

‎src/engine/compilercontext_p.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
#pragma once
44

5+
#include <unordered_set>
6+
#include <string>
7+
58
namespace libscratchcpp
69
{
710

@@ -10,10 +13,11 @@ class Target;
1013

1114
struct CompilerContextPrivate
1215
{
13-
CompilerContextPrivate(IEngine *engine, Target *target);
16+
CompilerContextPrivate(IEngine *engine, Target *target, const std::unordered_set<std::string> &unsafeConstants);
1417

1518
IEngine *engine = nullptr;
1619
Target *target = nullptr;
20+
const std::unordered_set<std::string> unsafeConstants;
1721
};
1822

1923
} // namespace libscratchcpp
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
target_sources(scratchcpp
2+
PRIVATE
3+
icodebuilder.h
4+
icodebuilderfactory.h
5+
codebuilderfactory.cpp
6+
codebuilderfactory.h
7+
instructionbase.h
8+
instruction.h
9+
instructionlistbase.cpp
10+
instructionlistbase.h
11+
instructionlist.h
12+
codeanalyzer.cpp
13+
codeanalyzer.h
14+
optimizationutils.cpp
15+
optimizationutils.h
16+
)
17+
18+
add_subdirectory(llvm)

0 commit comments

Comments
 (0)