Skip to content

Commit 3f4d19f

Browse files
committed
LLVMTypeAnalyzer: Use Variable instead of LLVMVariablePtr
1 parent ad367dc commit 3f4d19f

File tree

4 files changed

+140
-348
lines changed

4 files changed

+140
-348
lines changed

src/engine/internal/llvm/llvmtypeanalyzer.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22

33
#include "llvmtypeanalyzer.h"
44
#include "llvminstruction.h"
5-
#include "llvmvariableptr.h"
65

76
using namespace libscratchcpp;
87

98
static const std::unordered_set<LLVMInstruction::Type>
109
BEGIN_LOOP_INSTRUCTIONS = { LLVMInstruction::Type::BeginRepeatLoop, LLVMInstruction::Type::BeginWhileLoop, LLVMInstruction::Type::BeginRepeatUntilLoop };
1110

12-
Compiler::StaticType LLVMTypeAnalyzer::variableType(LLVMVariablePtr *varPtr, LLVMInstruction *pos, Compiler::StaticType previousType) const
11+
Compiler::StaticType LLVMTypeAnalyzer::variableType(Variable *var, LLVMInstruction *pos, Compiler::StaticType previousType) const
1312
{
14-
if (!varPtr || !pos)
13+
if (!var || !pos)
1514
return Compiler::StaticType::Unknown;
1615

1716
// Check the last write operation before the instruction
@@ -36,7 +35,7 @@ Compiler::StaticType LLVMTypeAnalyzer::variableType(LLVMVariablePtr *varPtr, LLV
3635
firstElseBranch = ins;
3736
ins = skipBranch(ins);
3837
continue;
39-
} else if (isVariableWrite(ins, varPtr)) {
38+
} else if (isVariableWrite(ins, var)) {
4039
if (level <= 0) { // ignore nested branches (they're handled by the branch analyzer)
4140
write = ins;
4241
break;
@@ -57,8 +56,8 @@ Compiler::StaticType LLVMTypeAnalyzer::variableType(LLVMVariablePtr *varPtr, LLV
5756
Compiler::StaticType elseBranchType = previousType;
5857

5958
if (!ignoreWriteAfterPos) {
60-
firstBranchType = variableTypeAfterBranch(varPtr, firstBranch, previousType);
61-
elseBranchType = variableTypeAfterBranch(varPtr, firstElseBranch, previousType);
59+
firstBranchType = variableTypeAfterBranch(var, firstBranch, previousType);
60+
elseBranchType = variableTypeAfterBranch(var, firstElseBranch, previousType);
6261
}
6362

6463
if (typesMatch(firstBranchType, elseBranchType))
@@ -74,9 +73,9 @@ Compiler::StaticType LLVMTypeAnalyzer::variableType(LLVMVariablePtr *varPtr, LLV
7473
return previousType;
7574
}
7675

77-
Compiler::StaticType LLVMTypeAnalyzer::variableTypeAfterBranch(LLVMVariablePtr *varPtr, LLVMInstruction *start, Compiler::StaticType previousType) const
76+
Compiler::StaticType LLVMTypeAnalyzer::variableTypeAfterBranch(Variable *var, LLVMInstruction *start, Compiler::StaticType previousType) const
7877
{
79-
if (!varPtr || !start)
78+
if (!var || !start)
8079
return previousType;
8180

8281
assert(isLoopStart(start) || isIfStart(start) || isElse(start));
@@ -103,10 +102,10 @@ Compiler::StaticType LLVMTypeAnalyzer::variableTypeAfterBranch(LLVMVariablePtr *
103102

104103
// Process the branch from end
105104
bool write = false; // only used internally (the compiler doesn't need this)
106-
return variableTypeAfterBranchFromEnd(varPtr, ins, previousType, write);
105+
return variableTypeAfterBranchFromEnd(var, ins, previousType, write);
107106
}
108107

109-
Compiler::StaticType LLVMTypeAnalyzer::variableTypeAfterBranchFromEnd(LLVMVariablePtr *varPtr, LLVMInstruction *end, Compiler::StaticType previousType, bool &write) const
108+
Compiler::StaticType LLVMTypeAnalyzer::variableTypeAfterBranchFromEnd(Variable *var, LLVMInstruction *end, Compiler::StaticType previousType, bool &write) const
110109
{
111110
// Find the last write instruction
112111
LLVMInstruction *ins = end->previous;
@@ -115,7 +114,7 @@ Compiler::StaticType LLVMTypeAnalyzer::variableTypeAfterBranchFromEnd(LLVMVariab
115114
while (ins && !isLoopStart(ins) && !isIfStart(ins)) {
116115
if (isLoopEnd(ins) || isIfEnd(ins) || isElse(ins)) {
117116
// Process the nested loop or if statement
118-
Compiler::StaticType ret = variableTypeAfterBranchFromEnd(varPtr, ins, previousType, write);
117+
Compiler::StaticType ret = variableTypeAfterBranchFromEnd(var, ins, previousType, write);
119118

120119
if (typesMatch(ret, previousType)) {
121120
if (write)
@@ -127,7 +126,7 @@ Compiler::StaticType LLVMTypeAnalyzer::variableTypeAfterBranchFromEnd(LLVMVariab
127126

128127
if (isElse(ins)) {
129128
// Process if branch (the else branch is already processed)
130-
ret = variableTypeAfterBranchFromEnd(varPtr, ins, previousType, write);
129+
ret = variableTypeAfterBranchFromEnd(var, ins, previousType, write);
131130

132131
if (typesMatch(ret, previousType)) {
133132
if (write) {
@@ -141,7 +140,7 @@ Compiler::StaticType LLVMTypeAnalyzer::variableTypeAfterBranchFromEnd(LLVMVariab
141140

142141
ins = skipBranch(ins);
143142
}
144-
} else if (isVariableWrite(ins, varPtr)) {
143+
} else if (isVariableWrite(ins, var)) {
145144
// Variable write instruction
146145
Compiler::StaticType writeType = writeValueType(ins);
147146
write = true;
@@ -209,9 +208,9 @@ bool LLVMTypeAnalyzer::isIfEnd(LLVMInstruction *ins) const
209208
return (ins->type == LLVMInstruction::Type::EndIf);
210209
}
211210

212-
bool LLVMTypeAnalyzer::isVariableWrite(LLVMInstruction *ins, LLVMVariablePtr *varPtr) const
211+
bool LLVMTypeAnalyzer::isVariableWrite(LLVMInstruction *ins, Variable *var) const
213212
{
214-
return (ins->type == LLVMInstruction::Type::WriteVariable && ins->workVariable == varPtr->var);
213+
return (ins->type == LLVMInstruction::Type::WriteVariable && ins->workVariable == var);
215214
}
216215

217216
Compiler::StaticType LLVMTypeAnalyzer::optimizeRegisterType(LLVMRegister *reg) const

src/engine/internal/llvm/llvmtypeanalyzer.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,24 @@
55
namespace libscratchcpp
66
{
77

8-
struct LLVMVariablePtr;
98
struct LLVMInstruction;
109
struct LLVMRegister;
1110

1211
class LLVMTypeAnalyzer
1312
{
1413
public:
15-
Compiler::StaticType variableType(LLVMVariablePtr *varPtr, LLVMInstruction *pos, Compiler::StaticType previousType) const;
16-
Compiler::StaticType variableTypeAfterBranch(LLVMVariablePtr *varPtr, LLVMInstruction *start, Compiler::StaticType previousType) const;
14+
Compiler::StaticType variableType(Variable *var, LLVMInstruction *pos, Compiler::StaticType previousType) const;
15+
Compiler::StaticType variableTypeAfterBranch(Variable *var, LLVMInstruction *start, Compiler::StaticType previousType) const;
1716

1817
private:
19-
Compiler::StaticType variableTypeAfterBranchFromEnd(LLVMVariablePtr *varPtr, LLVMInstruction *end, Compiler::StaticType previousType, bool &write) const;
18+
Compiler::StaticType variableTypeAfterBranchFromEnd(Variable *var, LLVMInstruction *end, Compiler::StaticType previousType, bool &write) const;
2019
LLVMInstruction *skipBranch(LLVMInstruction *pos) const;
2120
bool isLoopStart(LLVMInstruction *ins) const;
2221
bool isLoopEnd(LLVMInstruction *ins) const;
2322
bool isIfStart(LLVMInstruction *ins) const;
2423
bool isElse(LLVMInstruction *ins) const;
2524
bool isIfEnd(LLVMInstruction *ins) const;
26-
bool isVariableWrite(LLVMInstruction *ins, LLVMVariablePtr *varPtr) const;
25+
bool isVariableWrite(LLVMInstruction *ins, Variable *var) const;
2726

2827
Compiler::StaticType optimizeRegisterType(LLVMRegister *reg) const;
2928
Compiler::StaticType writeValueType(LLVMInstruction *ins) const;

0 commit comments

Comments
 (0)