Skip to content

Commit e1aa392

Browse files
committed
LLVMTypeAnalyzer: Make variableTypeAfterBranchFromEnd() generic
* for variables and lists
1 parent a76435b commit e1aa392

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/engine/internal/llvm/llvmtypeanalyzer.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,11 @@ Compiler::StaticType LLVMTypeAnalyzer::variableTypeAfterBranch(Variable *var, LL
131131

132132
// Process the branch from end
133133
bool write = false; // only used internally (the compiler doesn't need this)
134-
return variableTypeAfterBranchFromEnd(var, ins, previousType, write, visitedInstructions);
134+
return typeAfterBranchFromEnd(var, end, previousType, write, visitedInstructions);
135135
}
136136

137-
Compiler::StaticType LLVMTypeAnalyzer::variableTypeAfterBranchFromEnd(Variable *var, LLVMInstruction *end, Compiler::StaticType previousType, bool &write, InstructionSet &visitedInstructions) const
137+
Compiler::StaticType
138+
LLVMTypeAnalyzer::typeAfterBranchFromEnd(std::variant<Variable *, List *> varOrList, LLVMInstruction *end, Compiler::StaticType previousType, bool &write, InstructionSet &visitedInstructions) const
138139
{
139140
// Find the last write instruction
140141
LLVMInstruction *ins = end->previous;
@@ -143,7 +144,7 @@ Compiler::StaticType LLVMTypeAnalyzer::variableTypeAfterBranchFromEnd(Variable *
143144
while (ins && !isLoopStart(ins) && !isIfStart(ins)) {
144145
if (isLoopEnd(ins) || isIfEnd(ins) || isElse(ins)) {
145146
// Process the nested loop or if statement
146-
Compiler::StaticType ret = variableTypeAfterBranchFromEnd(var, ins, previousType, write, visitedInstructions);
147+
Compiler::StaticType ret = typeAfterBranchFromEnd(varOrList, ins, previousType, write, visitedInstructions);
147148

148149
if (typesMatch(ret, previousType)) {
149150
if (write)
@@ -155,7 +156,7 @@ Compiler::StaticType LLVMTypeAnalyzer::variableTypeAfterBranchFromEnd(Variable *
155156

156157
if (isElse(ins)) {
157158
// Process if branch (the else branch is already processed)
158-
ret = variableTypeAfterBranchFromEnd(var, ins, previousType, write, visitedInstructions);
159+
ret = typeAfterBranchFromEnd(varOrList, ins, previousType, write, visitedInstructions);
159160

160161
if (typesMatch(ret, previousType)) {
161162
if (write) {
@@ -169,7 +170,7 @@ Compiler::StaticType LLVMTypeAnalyzer::variableTypeAfterBranchFromEnd(Variable *
169170

170171
ins = skipBranch(ins);
171172
}
172-
} else if (isVariableWrite(ins, var) && !isWriteNoOp(ins)) {
173+
} else if (std::holds_alternative<Variable *>(varOrList), isVariableWrite(ins, std::get<Variable *>(varOrList)) && !isWriteNoOp(ins)) {
173174
// Variable write instruction
174175
Compiler::StaticType writeType = writeValueType(ins, visitedInstructions);
175176
write = true;

src/engine/internal/llvm/llvmtypeanalyzer.h

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

33
#include <scratchcpp/compiler.h>
4+
#include <variant>
45

56
namespace libscratchcpp
67
{
@@ -19,8 +20,11 @@ class LLVMTypeAnalyzer
1920

2021
Compiler::StaticType variableType(Variable *var, LLVMInstruction *pos, Compiler::StaticType previousType, InstructionSet &visitedInstructions) const;
2122
Compiler::StaticType variableTypeAfterBranch(Variable *var, LLVMInstruction *start, Compiler::StaticType previousType, InstructionSet &visitedInstructions) const;
22-
Compiler::StaticType variableTypeAfterBranchFromEnd(Variable *var, LLVMInstruction *end, Compiler::StaticType previousType, bool &write, InstructionSet &visitedInstructions) const;
2323

24+
Compiler::StaticType
25+
typeAfterBranchFromEnd(std::variant<Variable *, List *> varOrList, LLVMInstruction *end, Compiler::StaticType previousType, bool &write, InstructionSet &visitedInstructions) const;
26+
27+
LLVMInstruction *branchEnd(LLVMInstruction *start) const;
2428
LLVMInstruction *skipBranch(LLVMInstruction *pos) const;
2529

2630
bool isLoopStart(LLVMInstruction *ins) const;

0 commit comments

Comments
 (0)