11// SPDX-License-Identifier: Apache-2.0
22
33#include < scratchcpp/compilerlocalvariable.h>
4+ #include < scratchcpp/blockprototype.h>
45
56#include " cppcodebuilder.h"
7+ #include " cppcompilercontext.h"
68#include " cppexecutablecode.h"
9+ #include " cppinstruction.h"
710#include " cppconstant.h"
811#include " ../typeutils.h"
912
@@ -12,12 +15,34 @@ using namespace libscratchcpp;
1215CppCodeBuilder::CppCodeBuilder (CppCompilerContext *ctx, BlockPrototype *procedurePrototype, Compiler::CodeType codeType) :
1316 m_ctx(ctx),
1417 m_procedurePrototype(procedurePrototype),
15- m_codeType(codeType)
18+ m_codeType(codeType),
19+ m_codeAnalyzer(ctx)
1620{
1721}
1822
1923std::shared_ptr<ExecutableCode> CppCodeBuilder::build ()
2024{
25+ if (!m_warp) {
26+ // Do not create coroutine if there are no yield instructions nor non-warp procedure calls
27+ if (!m_instructions.containsInstruction ([](const CppInstruction *ins) {
28+ return ins->type == CppInstruction::Type::Yield || (ins->type == CppInstruction::Type::CallProcedure && ins->procedurePrototype && !ins->procedurePrototype ->warp ());
29+ })) {
30+ m_warp = true ;
31+ }
32+
33+ // Only create coroutines in scripts
34+ if (m_codeType != Compiler::CodeType::Script)
35+ m_warp = true ;
36+ }
37+
38+ if (m_warp) {
39+ #ifdef ENABLE_CODE_ANALYZER
40+ // Analyze the script (type analysis, optimizations, etc.)
41+ // NOTE: Do this only for warp scripts
42+ m_codeAnalyzer.analyzeScript (m_instructions);
43+ #endif
44+ }
45+
2146 // Build recorded instructions
2247 CppInstruction *ins = m_instructions.first ();
2348
0 commit comments