@@ -79,14 +79,24 @@ void LLVMBuildUtils::init(llvm::Function *function, BlockPrototype *procedurePro
7979 reg->intValue = llvm::ConstantInt::get (m_builder.getInt64Ty (), reg->constValue ().toDouble (), true );
8080 }
8181
82+ // Create string count variable (LLVM should optimize it into a constant later)
83+ m_stringCountVar = m_builder.CreateAlloca (m_builder.getInt64Ty (), nullptr , " stringCount" );
84+
85+ // Create string count block (string count is known after all instructions are processed)
86+ m_stringCountBlock = llvm::BasicBlock::Create (m_llvmCtx, " getStringCount" , m_function);
87+ m_builder.CreateBr (m_stringCountBlock);
88+ // NOTE: This block is terminated later
89+
8290 // Create string allocation block
91+ m_builder.SetInsertPoint (m_stringCountBlock);
8392 m_stringAllocaBlock = llvm::BasicBlock::Create (m_llvmCtx, " stringAlloca" , m_function);
84- m_builder. CreateBr (m_stringAllocaBlock);
93+ // NOTE: This block is terminated later
8594
86- // Get string array
95+ // Push string stack (load string array)
8796 m_builder.SetInsertPoint (m_stringAllocaBlock);
88- m_stringArray = m_builder.CreateCall (m_functions.resolve_llvm_get_string_array (), { m_executionContextPtr, m_functionIdValue });
89- // NOTE: This block is terminated later
97+
98+ llvm::Value *stringCount = m_builder.CreateLoad (m_builder.getInt64Ty (), m_stringCountVar, " strCount" );
99+ m_stringArray = m_builder.CreateCall (m_functions.resolve_llvm_push_string_stack (), { m_executionContextPtr, stringCount });
90100
91101 // Create next block
92102 m_stringAllocaNextBlock = llvm::BasicBlock::Create (m_llvmCtx, " entry.next" , m_function);
@@ -146,11 +156,13 @@ void LLVMBuildUtils::init(llvm::Function *function, BlockPrototype *procedurePro
146156
147157void LLVMBuildUtils::end (LLVMInstruction *lastInstruction, LLVMRegister *lastConstant)
148158{
149- // Terminate string allocation block
150- if (m_stringCount == 0 )
151- m_stringAllocaBlock->erase (m_stringAllocaBlock->begin (), m_stringAllocaBlock->end ());
152-
159+ // Terminate string count and allocation blocks
153160 llvm::BasicBlock *previousBlock = m_builder.GetInsertBlock ();
161+ m_builder.SetInsertPoint (m_stringCountBlock);
162+ m_builder.CreateStore (m_builder.getInt64 (m_stringCount), m_stringCountVar);
163+
164+ m_builder.CreateBr (m_stringAllocaBlock);
165+
154166 m_builder.SetInsertPoint (m_stringAllocaBlock);
155167 m_builder.CreateBr (m_stringAllocaNextBlock);
156168 m_builder.SetInsertPoint (previousBlock);
@@ -165,6 +177,7 @@ void LLVMBuildUtils::end(LLVMInstruction *lastInstruction, LLVMRegister *lastCon
165177
166178 // End branch
167179 m_builder.SetInsertPoint (m_endBranch);
180+ m_builder.CreateCall (m_functions.resolve_llvm_pop_string_stack (), { m_executionContextPtr });
168181
169182 llvm::PointerType *pointerType = llvm::PointerType::get (llvm::Type::getInt8Ty (m_llvmCtx), 0 );
170183
@@ -198,6 +211,7 @@ void LLVMBuildUtils::end(LLVMInstruction *lastInstruction, LLVMRegister *lastCon
198211
199212 // Thread end branch (stop the entire thread, including procedure callers)
200213 m_builder.SetInsertPoint (m_endThreadBranch);
214+ m_builder.CreateCall (m_functions.resolve_llvm_pop_string_stack (), { m_executionContextPtr });
201215
202216 switch (m_codeType) {
203217 case Compiler::CodeType::Script:
@@ -497,7 +511,7 @@ llvm::Value *LLVMBuildUtils::addAlloca(llvm::Type *type)
497511
498512llvm::Value *LLVMBuildUtils::addStringAlloca ()
499513{
500- // All temporary strings are allocated before the script is called
514+ // All temporary strings are allocated when the string stack is pushed
501515 llvm::BasicBlock *block = m_builder.GetInsertBlock ();
502516 m_builder.SetInsertPoint (m_stringAllocaBlock);
503517
@@ -1418,7 +1432,9 @@ void LLVMBuildUtils::createSuspend()
14181432 }
14191433
14201434 syncVariables ();
1435+
14211436 m_coroutine->createSuspend ();
1437+
14221438 reloadVariables ();
14231439 reloadLists ();
14241440
0 commit comments