Skip to content

Commit e708e70

Browse files
committed
CppCodeBuilder: Add code analyzer
1 parent 9741bf1 commit e708e70

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

‎src/engine/internal/codegen/cpp/cppcodebuilder.cpp‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
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;
1215
CppCodeBuilder::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

1923
std::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

‎src/engine/internal/codegen/cpp/cppcodebuilder.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "../icodebuilder.h"
66
#include "cppinstructionlist.h"
77
#include "instructions/instructionbuilder.h"
8+
#include "../codeanalyzer.h"
89

910
namespace libscratchcpp
1011
{
@@ -117,6 +118,8 @@ class CppCodeBuilder : public ICodeBuilder
117118
std::vector<std::shared_ptr<CompilerLocalVariable>> m_localVars;
118119
CppInstructionList m_instructions;
119120
cppins::InstructionBuilder m_instructionBuilder;
121+
CodeAnalyzer m_codeAnalyzer;
122+
bool m_warp = false;
120123
};
121124

122125
} // namespace libscratchcpp

0 commit comments

Comments
 (0)