Skip to content

Commit 45567c7

Browse files
committed
Use unsafe constants from CompilerContext
1 parent b2eb2d2 commit 45567c7

4 files changed

Lines changed: 11 additions & 57 deletions

File tree

‎src/engine/internal/codegen/llvm/llvmbuildutils.cpp‎

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,35 +42,6 @@ LLVMBuildUtils::LLVMBuildUtils(LLVMCompilerContext *ctx, llvm::IRBuilder<> &buil
4242
initTypes();
4343
createVariableMap();
4444
createListMap();
45-
46-
// Find unsafe numeric string constants in costume and sound names
47-
if (m_target) {
48-
IEngine *engine = m_target->engine();
49-
50-
if (engine) {
51-
auto checkConstant = [this](const std::string &str) {
52-
Value stringValue(str);
53-
Value numberValue(stringValue.toDouble());
54-
55-
if (stringValue.isValidNumber() && str == numberValue.toString())
56-
m_unsafeConstants.insert(str);
57-
};
58-
59-
const auto &targets = engine->targets();
60-
bool found = false;
61-
62-
for (const auto &target : targets) {
63-
const auto &costumes = target->costumes();
64-
const auto &sounds = target->sounds();
65-
66-
for (const auto &costume : costumes)
67-
checkConstant(costume->name());
68-
69-
for (const auto &sound : sounds)
70-
checkConstant(sound->name());
71-
}
72-
}
73-
}
7445
}
7546

7647
void LLVMBuildUtils::init(llvm::Function *function, BlockPrototype *procedurePrototype, bool warp, const std::vector<std::shared_ptr<LLVMRegister>> &regs)
@@ -522,7 +493,9 @@ Compiler::StaticType LLVMBuildUtils::optimizeRegisterType(const LLVMRegister *re
522493
std::string str = value.toString();
523494

524495
// Apply this optimization only if the number matches the string and the constant is safe
525-
if (value.isValidNumber() && numberValue.toString() == str && m_unsafeConstants.find(str) == m_unsafeConstants.cend())
496+
const auto &unsafeConstants = m_ctx->unsafeConstants();
497+
498+
if (value.isValidNumber() && numberValue.toString() == str && unsafeConstants.find(str) == unsafeConstants.cend())
526499
ret = Compiler::StaticType::Number;
527500
}
528501

‎src/engine/internal/codegen/llvm/llvmbuildutils.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,6 @@ class LIBSCRATCHCPP_TEST_EXPORT LLVMBuildUtils
180180
bool m_warp = false;
181181
Compiler::CodeType m_codeType = Compiler::CodeType::Script;
182182

183-
std::unordered_set<std::string> m_unsafeConstants;
184-
185183
llvm::Value *m_executionContextPtr = nullptr;
186184
llvm::Value *m_targetPtr = nullptr;
187185
llvm::Value *m_targetVariables = nullptr;

‎test/llvm/code_analyzer/list_type_analysis.cpp‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include <scratchcpp/project.h>
2-
#include <scratchcpp/sprite.h>
3-
#include <scratchcpp/costume.h>
4-
#include <scratchcpp/sound.h>
2+
#include <scratchcpp/target.h>
53
#include <scratchcpp/iengine.h>
64
#include <scratchcpp/list.h>
75
#include <engine/internal/codegen/llvm/llvmcodeanalyzer.h>
@@ -22,17 +20,11 @@ class LLVMCodeAnalyzer_ListTypeAnalysis : public testing::Test
2220
{
2321
auto engine = m_project.engine();
2422
m_target = std::make_shared<Target>();
25-
m_spriteWithUnsafeConstants = std::make_shared<Sprite>();
2623

27-
auto costume = std::make_shared<Costume>(m_unsafeCostumeNumConstant, "", "");
28-
m_spriteWithUnsafeConstants->addCostume(costume);
24+
engine->setTargets({ m_target });
2925

30-
auto sound = std::make_shared<Sound>(m_unsafeSoundNumConstant, "", "");
31-
m_spriteWithUnsafeConstants->addSound(sound);
32-
33-
engine->setTargets({ m_target, m_spriteWithUnsafeConstants });
34-
35-
m_ctx = std::make_unique<LLVMCompilerContext>(engine.get(), m_target.get());
26+
const std::unordered_set<std::string> unsafeConstants = { m_unsafeCostumeNumConstant, m_unsafeSoundNumConstant };
27+
m_ctx = std::make_unique<LLVMCompilerContext>(engine.get(), m_target.get(), unsafeConstants);
3628
m_builder = std::make_unique<llvm::IRBuilder<>>(*m_ctx->llvmCtx());
3729
m_utils = std::make_unique<LLVMBuildUtils>(m_ctx.get(), *m_builder, Compiler::CodeType::Script);
3830
m_analyzer = std::make_unique<LLVMCodeAnalyzer>(*m_utils);

‎test/llvm/code_analyzer/variable_type_analysis.cpp‎

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include <scratchcpp/project.h>
2-
#include <scratchcpp/sprite.h>
3-
#include <scratchcpp/costume.h>
4-
#include <scratchcpp/sound.h>
2+
#include <scratchcpp/target.h>
53
#include <scratchcpp/iengine.h>
64
#include <scratchcpp/variable.h>
75
#include <engine/internal/codegen/llvm/llvmcodeanalyzer.h>
@@ -22,17 +20,11 @@ class LLVMCodeAnalyzer_VariableTypeAnalysis : public testing::Test
2220
{
2321
auto engine = m_project.engine();
2422
m_target = std::make_shared<Target>();
25-
m_spriteWithUnsafeConstants = std::make_shared<Sprite>();
2623

27-
auto costume = std::make_shared<Costume>(m_unsafeCostumeNumConstant, "", "");
28-
m_spriteWithUnsafeConstants->addCostume(costume);
24+
engine->setTargets({ m_target });
2925

30-
auto sound = std::make_shared<Sound>(m_unsafeSoundNumConstant, "", "");
31-
m_spriteWithUnsafeConstants->addSound(sound);
32-
33-
engine->setTargets({ m_target, m_spriteWithUnsafeConstants });
34-
35-
m_ctx = std::make_unique<LLVMCompilerContext>(engine.get(), m_target.get());
26+
const std::unordered_set<std::string> unsafeConstants = { m_unsafeCostumeNumConstant, m_unsafeSoundNumConstant };
27+
m_ctx = std::make_unique<LLVMCompilerContext>(engine.get(), m_target.get(), unsafeConstants);
3628
m_builder = std::make_unique<llvm::IRBuilder<>>(*m_ctx->llvmCtx());
3729
m_utils = std::make_unique<LLVMBuildUtils>(m_ctx.get(), *m_builder, Compiler::CodeType::Script);
3830
m_analyzer = std::make_unique<LLVMCodeAnalyzer>(*m_utils);
@@ -47,7 +39,6 @@ class LLVMCodeAnalyzer_VariableTypeAnalysis : public testing::Test
4739
private:
4840
Project m_project;
4941
std::shared_ptr<Target> m_target;
50-
std::shared_ptr<Sprite> m_spriteWithUnsafeConstants;
5142
std::unique_ptr<LLVMCompilerContext> m_ctx;
5243
std::unique_ptr<llvm::IRBuilder<>> m_builder;
5344
std::unique_ptr<LLVMBuildUtils> m_utils;

0 commit comments

Comments
 (0)