@@ -26,7 +26,7 @@ static std::unordered_map<ValueType, Compiler::StaticType>
2626static const std::unordered_set<LLVMInstruction::Type>
2727 VAR_LIST_READ_INSTRUCTIONS = { LLVMInstruction::Type::ReadVariable, LLVMInstruction::Type::GetListItem, LLVMInstruction::Type::GetListItemIndex, LLVMInstruction::Type::ListContainsItem };
2828
29- LLVMCodeBuilder::LLVMCodeBuilder (LLVMCompilerContext *ctx, BlockPrototype *procedurePrototype, bool isPredicate ) :
29+ LLVMCodeBuilder::LLVMCodeBuilder (LLVMCompilerContext *ctx, BlockPrototype *procedurePrototype, Compiler::CodeType codeType ) :
3030 m_ctx(ctx),
3131 m_target(ctx->target ()),
3232 m_llvmCtx(*ctx->llvmCtx ()),
@@ -35,7 +35,7 @@ LLVMCodeBuilder::LLVMCodeBuilder(LLVMCompilerContext *ctx, BlockPrototype *proce
3535 m_procedurePrototype(procedurePrototype),
3636 m_defaultWarp(procedurePrototype ? procedurePrototype->warp () : false),
3737 m_warp(m_defaultWarp),
38- m_isPredicate(isPredicate )
38+ m_codeType(codeType )
3939{
4040 initTypes ();
4141 createVariableMap ();
@@ -56,8 +56,8 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
5656 if (it == m_instructions.end ())
5757 m_warp = true ;
5858
59- // Do not create coroutine in hat predicates
60- if (m_isPredicate )
59+ // Only create coroutines in scripts
60+ if (m_codeType != Compiler::CodeType::Script )
6161 m_warp = true ;
6262 }
6363
@@ -1319,15 +1319,20 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
13191319 // End and verify the function
13201320 llvm::PointerType *pointerType = llvm::PointerType::get (llvm::Type::getInt8Ty (m_llvmCtx), 0 );
13211321
1322- if (m_isPredicate) {
1323- // Use last instruction return value
1324- assert (!m_instructions.empty ());
1325- m_builder.CreateRet (m_instructions.back ()->functionReturnReg ->value );
1326- } else {
1327- if (m_warp)
1328- m_builder.CreateRet (llvm::ConstantPointerNull::get (pointerType));
1329- else
1330- coro->end ();
1322+ switch (m_codeType) {
1323+ case Compiler::CodeType::Script:
1324+ if (m_warp)
1325+ m_builder.CreateRet (llvm::ConstantPointerNull::get (pointerType));
1326+ else
1327+ coro->end ();
1328+ break ;
1329+
1330+ // TODO: Implement reporter code type
1331+ case Compiler::CodeType::HatPredicate:
1332+ // Use last instruction return value
1333+ assert (!m_instructions.empty ());
1334+ m_builder.CreateRet (m_instructions.back ()->functionReturnReg ->value );
1335+ break ;
13311336 }
13321337
13331338 verifyFunction (m_function);
@@ -1349,7 +1354,7 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
13491354
13501355 verifyFunction (resumeFunc);
13511356
1352- return std::make_shared<LLVMExecutableCode>(m_ctx, m_function->getName ().str (), resumeFunc->getName ().str (), m_isPredicate );
1357+ return std::make_shared<LLVMExecutableCode>(m_ctx, m_function->getName ().str (), resumeFunc->getName ().str (), m_codeType );
13531358}
13541359
13551360CompilerValue *LLVMCodeBuilder::addFunctionCall (const std::string &functionName, Compiler::StaticType returnType, const Compiler::ArgTypes &argTypes, const Compiler::Args &args)
@@ -2019,7 +2024,20 @@ void LLVMCodeBuilder::popLoopScope()
20192024
20202025std::string LLVMCodeBuilder::getMainFunctionName (BlockPrototype *procedurePrototype)
20212026{
2022- return procedurePrototype ? " proc." + procedurePrototype->procCode () : (m_isPredicate ? " predicate" : " script" );
2027+ std::string name;
2028+
2029+ switch (m_codeType) {
2030+ case Compiler::CodeType::Script:
2031+ name = " script" ;
2032+ break ;
2033+
2034+ // TODO: Implement reporter code type
2035+ case Compiler::CodeType::HatPredicate:
2036+ name = " predicate" ;
2037+ break ;
2038+ }
2039+
2040+ return procedurePrototype ? " proc." + procedurePrototype->procCode () : name;
20232041}
20242042
20252043std::string LLVMCodeBuilder::getResumeFunctionName (BlockPrototype *procedurePrototype)
@@ -2046,7 +2064,20 @@ llvm::FunctionType *LLVMCodeBuilder::getMainFunctionType(BlockPrototype *procedu
20462064 }
20472065 }
20482066
2049- return llvm::FunctionType::get (m_isPredicate ? m_builder.getInt1Ty () : pointerType, argTypes, false );
2067+ llvm::Type *retType = nullptr ;
2068+
2069+ switch (m_codeType) {
2070+ case Compiler::CodeType::Script:
2071+ retType = pointerType;
2072+ break ;
2073+
2074+ // TODO: Implement reporter code type
2075+ case Compiler::CodeType::HatPredicate:
2076+ retType = m_builder.getInt1Ty ();
2077+ break ;
2078+ }
2079+
2080+ return llvm::FunctionType::get (retType, argTypes, false );
20502081}
20512082
20522083llvm::Function *LLVMCodeBuilder::getOrCreateFunction (const std::string &name, llvm::FunctionType *type)
0 commit comments