@@ -20,6 +20,7 @@ namespace libscratchcpp
20
20
21
21
class LLVMCompilerContext ;
22
22
class LLVMConstantRegister ;
23
+ class LLVMLoopScope ;
23
24
24
25
class LLVMCodeBuilder : public ICodeBuilder
25
26
{
@@ -119,22 +120,24 @@ class LLVMCodeBuilder : public ICodeBuilder
119
120
void createListMap ();
120
121
void pushScopeLevel ();
121
122
void popScopeLevel ();
123
+ void pushLoopScope (bool buildPhase);
124
+ void popLoopScope ();
122
125
123
126
std::string getMainFunctionName (BlockPrototype *procedurePrototype);
124
127
std::string getResumeFunctionName (BlockPrototype *procedurePrototype);
125
128
llvm::FunctionType *getMainFunctionType (BlockPrototype *procedurePrototype);
126
129
llvm::Function *getOrCreateFunction (const std::string &name, llvm::FunctionType *type);
127
130
void verifyFunction (llvm::Function *func);
128
131
129
- LLVMRegister *addReg (std::shared_ptr<LLVMRegister> reg);
132
+ LLVMRegister *addReg (std::shared_ptr<LLVMRegister> reg, std::shared_ptr<LLVMInstruction> ins );
130
133
131
134
llvm::Value *addAlloca (llvm::Type *type);
132
135
void freeLater (llvm::Value *value);
133
136
void freeScopeHeap ();
134
137
llvm::Value *castValue (LLVMRegister *reg, Compiler::StaticType targetType);
135
138
llvm::Value *castRawValue (LLVMRegister *reg, Compiler::StaticType targetType);
136
139
llvm::Constant *castConstValue (const Value &value, Compiler::StaticType targetType);
137
- Compiler::StaticType optimizeRegisterType (LLVMRegister *reg);
140
+ Compiler::StaticType optimizeRegisterType (LLVMRegister *reg) const ;
138
141
llvm::Type *getType (Compiler::StaticType type);
139
142
Compiler::StaticType getProcedureArgType (BlockPrototype::ArgType type);
140
143
llvm::Value *isNaN (llvm::Value *num);
@@ -146,9 +149,15 @@ class LLVMCodeBuilder : public ICodeBuilder
146
149
void reloadVariables (llvm::Value *targetVariables);
147
150
void reloadLists ();
148
151
void updateListDataPtr (const LLVMListPtr &listPtr);
152
+ bool isVarOrListTypeSafe (std::shared_ptr<LLVMInstruction> ins, Compiler::StaticType expectedType) const ;
153
+ bool isVarOrListTypeSafe (std::shared_ptr<LLVMInstruction> ins, Compiler::StaticType expectedType, std::unordered_set<LLVMInstruction *> &log, int &c) const ;
154
+ bool isVarOrListWriteResultTypeSafe (std::shared_ptr<LLVMInstruction> ins, Compiler::StaticType expectedType, bool ignoreSavedType, std::unordered_set<LLVMInstruction *> &log, int &c) const ;
149
155
156
+ LLVMRegister *createOp (LLVMInstruction::Type type, Compiler::StaticType retType, Compiler::StaticType argType, const Compiler::Args &args);
157
+ LLVMRegister *createOp (LLVMInstruction::Type type, Compiler::StaticType retType, const Compiler::ArgTypes &argTypes = {}, const Compiler::Args &args = {});
150
158
LLVMRegister *createOp (const LLVMInstruction &ins, Compiler::StaticType retType, Compiler::StaticType argType, const Compiler::Args &args);
151
159
LLVMRegister *createOp (const LLVMInstruction &ins, Compiler::StaticType retType, const Compiler::ArgTypes &argTypes = {}, const Compiler::Args &args = {});
160
+ std::shared_ptr<LLVMLoopScope> currentLoopScope () const ;
152
161
153
162
void createValueStore (LLVMRegister *reg, llvm::Value *targetPtr, Compiler::StaticType sourceType, Compiler::StaticType targetType);
154
163
void createReusedValueStore (LLVMRegister *reg, llvm::Value *targetPtr, Compiler::StaticType sourceType, Compiler::StaticType targetType);
@@ -215,14 +224,21 @@ class LLVMCodeBuilder : public ICodeBuilder
215
224
llvm::StructType *m_valueDataType = nullptr ;
216
225
llvm::FunctionType *m_resumeFuncType = nullptr ;
217
226
218
- std::vector<LLVMInstruction> m_instructions;
227
+ std::vector<std::shared_ptr< LLVMInstruction> > m_instructions;
219
228
std::vector<std::shared_ptr<LLVMRegister>> m_regs;
220
229
std::vector<std::shared_ptr<CompilerLocalVariable>> m_localVars;
221
230
BlockPrototype *m_procedurePrototype = nullptr ;
222
231
bool m_defaultWarp = false ;
223
232
bool m_warp = false ;
224
233
int m_defaultArgCount = 0 ;
225
234
235
+ long m_loopScope = -1 ; // index
236
+ std::vector<std::shared_ptr<LLVMLoopScope>> m_loopScopes;
237
+ long m_loopScopeCounter = 0 ; // replacement for m_loopScopes size in build phase
238
+ std::vector<long > m_loopScopeTree;
239
+ bool m_loopCondition = false ; // whether we're currently compiling a loop condition
240
+ std::vector<std::shared_ptr<LLVMInstruction>> m_variableInstructions;
241
+ std::vector<std::shared_ptr<LLVMInstruction>> m_listInstructions;
226
242
std::vector<std::vector<llvm::Value *>> m_heap; // scopes
227
243
228
244
std::shared_ptr<ExecutableCode> m_output;
0 commit comments