@@ -20,6 +20,7 @@ namespace libscratchcpp
2020
2121class LLVMCompilerContext ;
2222class LLVMConstantRegister ;
23+ class LLVMLoopScope ;
2324
2425class LLVMCodeBuilder : public ICodeBuilder
2526{
@@ -119,22 +120,24 @@ class LLVMCodeBuilder : public ICodeBuilder
119120 void createListMap ();
120121 void pushScopeLevel ();
121122 void popScopeLevel ();
123+ void pushLoopScope (bool buildPhase);
124+ void popLoopScope ();
122125
123126 std::string getMainFunctionName (BlockPrototype *procedurePrototype);
124127 std::string getResumeFunctionName (BlockPrototype *procedurePrototype);
125128 llvm::FunctionType *getMainFunctionType (BlockPrototype *procedurePrototype);
126129 llvm::Function *getOrCreateFunction (const std::string &name, llvm::FunctionType *type);
127130 void verifyFunction (llvm::Function *func);
128131
129- LLVMRegister *addReg (std::shared_ptr<LLVMRegister> reg);
132+ LLVMRegister *addReg (std::shared_ptr<LLVMRegister> reg, std::shared_ptr<LLVMInstruction> ins );
130133
131134 llvm::Value *addAlloca (llvm::Type *type);
132135 void freeLater (llvm::Value *value);
133136 void freeScopeHeap ();
134137 llvm::Value *castValue (LLVMRegister *reg, Compiler::StaticType targetType);
135138 llvm::Value *castRawValue (LLVMRegister *reg, Compiler::StaticType targetType);
136139 llvm::Constant *castConstValue (const Value &value, Compiler::StaticType targetType);
137- Compiler::StaticType optimizeRegisterType (LLVMRegister *reg);
140+ Compiler::StaticType optimizeRegisterType (LLVMRegister *reg) const ;
138141 llvm::Type *getType (Compiler::StaticType type);
139142 Compiler::StaticType getProcedureArgType (BlockPrototype::ArgType type);
140143 llvm::Value *isNaN (llvm::Value *num);
@@ -146,9 +149,15 @@ class LLVMCodeBuilder : public ICodeBuilder
146149 void reloadVariables (llvm::Value *targetVariables);
147150 void reloadLists ();
148151 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 ;
149155
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 = {});
150158 LLVMRegister *createOp (const LLVMInstruction &ins, Compiler::StaticType retType, Compiler::StaticType argType, const Compiler::Args &args);
151159 LLVMRegister *createOp (const LLVMInstruction &ins, Compiler::StaticType retType, const Compiler::ArgTypes &argTypes = {}, const Compiler::Args &args = {});
160+ std::shared_ptr<LLVMLoopScope> currentLoopScope () const ;
152161
153162 void createValueStore (LLVMRegister *reg, llvm::Value *targetPtr, Compiler::StaticType sourceType, Compiler::StaticType targetType);
154163 void createReusedValueStore (LLVMRegister *reg, llvm::Value *targetPtr, Compiler::StaticType sourceType, Compiler::StaticType targetType);
@@ -215,14 +224,21 @@ class LLVMCodeBuilder : public ICodeBuilder
215224 llvm::StructType *m_valueDataType = nullptr ;
216225 llvm::FunctionType *m_resumeFuncType = nullptr ;
217226
218- std::vector<LLVMInstruction> m_instructions;
227+ std::vector<std::shared_ptr< LLVMInstruction> > m_instructions;
219228 std::vector<std::shared_ptr<LLVMRegister>> m_regs;
220229 std::vector<std::shared_ptr<CompilerLocalVariable>> m_localVars;
221230 BlockPrototype *m_procedurePrototype = nullptr ;
222231 bool m_defaultWarp = false ;
223232 bool m_warp = false ;
224233 int m_defaultArgCount = 0 ;
225234
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;
226242 std::vector<std::vector<llvm::Value *>> m_heap; // scopes
227243
228244 std::shared_ptr<ExecutableCode> m_output;
0 commit comments