Skip to content

Commit b73c5e1

Browse files
committed
LLVMCodeBuilder: Store loop variable write instructions
1 parent 268c51a commit b73c5e1

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/engine/internal/llvm/llvmcodebuilder.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,11 @@ void LLVMCodeBuilder::createVariableWrite(Variable *variable, CompilerValue *val
15481548
if (m_variablePtrs.find(variable) == m_variablePtrs.cend())
15491549
m_variablePtrs[variable] = LLVMVariablePtr();
15501550

1551+
if (m_loopScope >= 0) {
1552+
auto scope = m_loopScopes[m_loopScope];
1553+
m_variablePtrs[variable].loopVariableWrites[scope].push_back(m_instructions.back());
1554+
}
1555+
15511556
m_variableInstructions.push_back(m_instructions.back());
15521557
}
15531558

src/engine/internal/llvm/llvmvariableptr.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#pragma once
44

55
#include <scratchcpp/compiler.h>
6+
#include <unordered_map>
67

78
namespace llvm
89
{
@@ -14,13 +15,19 @@ class Value;
1415
namespace libscratchcpp
1516
{
1617

18+
class LLVMLoopScope;
19+
class LLVMInstruction;
20+
1721
struct LLVMVariablePtr
1822
{
1923
llvm::Value *stackPtr = nullptr;
2024
llvm::Value *heapPtr = nullptr;
2125
Compiler::StaticType type = Compiler::StaticType::Unknown;
2226
bool onStack = false;
2327
bool changed = false;
28+
29+
// Used in build phase to check the type safety of variables in loops
30+
std::unordered_map<std::shared_ptr<LLVMLoopScope>, std::vector<std::shared_ptr<LLVMInstruction>>> loopVariableWrites; // loop scope, write instructions
2431
};
2532

2633
} // namespace libscratchcpp

0 commit comments

Comments
 (0)