Skip to content

Commit 0f8399f

Browse files
committed
LLVMCodeAnalyzer: Reset types after procedure calls
1 parent 6998aec commit 0f8399f

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

src/engine/internal/llvm/llvmcodeanalyzer.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,29 @@ void LLVMCodeAnalyzer::analyzeScript(const LLVMInstructionList &script) const
102102
// Store the type in the return register
103103
// NOTE: Get list item returns empty string if index is out of range
104104
ins->functionReturnReg->setType(ins->targetType | Compiler::StaticType::String);
105+
} else if (isProcedureCall(ins)) {
106+
// Variables/lists may change in procedures
107+
for (auto &[var, type] : currentBranch->variableTypes) {
108+
if (type != Compiler::StaticType::Unknown) {
109+
type = Compiler::StaticType::Unknown;
110+
111+
if (typeAssignedInstructions.find(ins) == typeAssignedInstructions.cend())
112+
currentBranch->typeChanges = true;
113+
}
114+
}
115+
116+
for (auto &[list, type] : currentBranch->listTypes) {
117+
if (type != Compiler::StaticType::Unknown) {
118+
type = Compiler::StaticType::Unknown;
119+
120+
if (typeAssignedInstructions.find(ins) == typeAssignedInstructions.cend()) {
121+
typeAssignedInstructions.insert(ins);
122+
currentBranch->typeChanges = true;
123+
}
124+
}
125+
}
126+
127+
typeAssignedInstructions.insert(ins);
105128
}
106129

107130
ins = ins->next;
@@ -247,6 +270,11 @@ bool LLVMCodeAnalyzer::isListClear(const LLVMInstruction *ins) const
247270
return (ins->type == LLVMInstruction::Type::ClearList);
248271
}
249272

273+
bool LLVMCodeAnalyzer::isProcedureCall(const LLVMInstruction *ins) const
274+
{
275+
return (ins->type == LLVMInstruction::Type::CallProcedure);
276+
}
277+
250278
Compiler::StaticType LLVMCodeAnalyzer::writeType(LLVMInstruction *ins) const
251279
{
252280
assert(ins);

src/engine/internal/llvm/llvmcodeanalyzer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class LLVMCodeAnalyzer
4747
bool isListWrite(const LLVMInstruction *ins) const;
4848
bool isListClear(const LLVMInstruction *ins) const;
4949

50+
bool isProcedureCall(const LLVMInstruction *ins) const;
51+
5052
Compiler::StaticType writeType(LLVMInstruction *ins) const;
5153
};
5254

test/llvm/code_analyzer/list_type_analysis.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,39 @@ TEST(LLVMCodeAnalyzer_ListTypeAnalysis, ClearListOperation)
182182
ASSERT_EQ(appendList2->targetType, Compiler::StaticType::Void);
183183
}
184184

185+
TEST(LLVMCodeAnalyzer_ListTypeAnalysis, ProcedureCall)
186+
{
187+
LLVMCodeAnalyzer analyzer;
188+
LLVMInstructionList list;
189+
List targetList("", "");
190+
191+
auto clearList = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::ClearList, false);
192+
clearList->targetList = &targetList;
193+
list.addInstruction(clearList);
194+
195+
auto appendList1 = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::AppendToList, false);
196+
LLVMConstantRegister value1(Compiler::StaticType::String, "hello");
197+
appendList1->targetList = &targetList;
198+
appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 });
199+
list.addInstruction(appendList1);
200+
201+
auto procCall = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::CallProcedure, false);
202+
list.addInstruction(procCall);
203+
204+
auto appendList2 = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::AppendToList, false);
205+
LLVMConstantRegister value2(Compiler::StaticType::Number, 5.2);
206+
appendList2->targetList = &targetList;
207+
appendList2->args.push_back({ Compiler::StaticType::Unknown, &value2 });
208+
list.addInstruction(appendList2);
209+
210+
analyzer.analyzeScript(list);
211+
212+
ASSERT_EQ(appendList1->targetType, Compiler::StaticType::Void);
213+
214+
// Type unknown due to procedure call
215+
ASSERT_EQ(appendList2->targetType, Compiler::StaticType::Unknown);
216+
}
217+
185218
TEST(LLVMCodeAnalyzer_ListTypeAnalysis, MixedWriteOperationsSameType_AfterClear)
186219
{
187220
LLVMCodeAnalyzer analyzer;

test/llvm/code_analyzer/variable_type_analysis.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,41 @@ TEST(LLVMCodeAnalyzer_VariableTypeAnalysis, RepeatUntilLoop)
191191
ASSERT_EQ(setVar1->targetType, Compiler::StaticType::Number | Compiler::StaticType::Bool);
192192
}
193193

194+
TEST(LLVMCodeAnalyzer_VariableTypeAnalysis, ProcedureCallInLoop)
195+
{
196+
LLVMCodeAnalyzer analyzer;
197+
LLVMInstructionList list;
198+
Variable var("", "");
199+
200+
auto setVar1 = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::WriteVariable, false);
201+
LLVMConstantRegister value1(Compiler::StaticType::Number, 1.25);
202+
setVar1->targetVariable = &var;
203+
setVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 });
204+
list.addInstruction(setVar1);
205+
206+
auto loopStart = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::BeginRepeatLoop, false);
207+
list.addInstruction(loopStart);
208+
209+
auto setVar2 = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::WriteVariable, false);
210+
LLVMConstantRegister value2(Compiler::StaticType::Number, 5);
211+
setVar2->targetVariable = &var;
212+
setVar2->args.push_back({ Compiler::StaticType::Unknown, &value2 });
213+
list.addInstruction(setVar2);
214+
215+
auto procCall = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::CallProcedure, false);
216+
list.addInstruction(procCall);
217+
218+
auto loopEnd = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::EndLoop, false);
219+
list.addInstruction(loopEnd);
220+
221+
analyzer.analyzeScript(list);
222+
223+
ASSERT_EQ(setVar1->targetType, Compiler::StaticType::Unknown);
224+
225+
// Type unknown due to procedure call
226+
ASSERT_EQ(setVar2->targetType, Compiler::StaticType::Unknown);
227+
}
228+
194229
TEST(LLVMCodeAnalyzer_VariableTypeAnalysis, LoopMultipleWrites_UnknownType)
195230
{
196231
LLVMCodeAnalyzer analyzer;

0 commit comments

Comments
 (0)