diff --git a/include/scratchcpp/compiler.h b/include/scratchcpp/compiler.h index 1d7b22b61..626635528 100644 --- a/include/scratchcpp/compiler.h +++ b/include/scratchcpp/compiler.h @@ -53,7 +53,7 @@ class LIBSCRATCHCPP_EXPORT Compiler using Args = std::vector; Compiler(CompilerContext *ctx); - Compiler(IEngine *engine, Target *target); + Compiler(IEngine *engine, Target *target, const std::unordered_set &unsafeConstants = {}); Compiler(const Compiler &) = delete; IEngine *engine() const; @@ -164,7 +164,7 @@ class LIBSCRATCHCPP_EXPORT Compiler const std::unordered_set &unsupportedBlocks() const; - static std::shared_ptr createContext(IEngine *engine, Target *target); + static std::shared_ptr createContext(IEngine *engine, Target *target, const std::unordered_set &unsafeConstants); private: spimpl::unique_impl_ptr impl; diff --git a/include/scratchcpp/compilercontext.h b/include/scratchcpp/compilercontext.h index 1b3351995..ed8376617 100644 --- a/include/scratchcpp/compilercontext.h +++ b/include/scratchcpp/compilercontext.h @@ -2,6 +2,8 @@ #pragma once +#include + #include "global.h" #include "spimpl.h" @@ -16,13 +18,15 @@ class CompilerContextPrivate; class LIBSCRATCHCPP_EXPORT CompilerContext { public: - CompilerContext(IEngine *engine, Target *target); + CompilerContext(IEngine *engine, Target *target, const std::unordered_set &unsafeConstants = {}); CompilerContext(const CompilerContext &) = delete; virtual ~CompilerContext() { } IEngine *engine() const; Target *target() const; + const std::unordered_set &unsafeConstants() const; + /*! * Optimizes compiled scripts ahead of time. * \see Compiler#preoptimize() diff --git a/src/engine/CMakeLists.txt b/src/engine/CMakeLists.txt index d4aa8ed52..b1a84308a 100644 --- a/src/engine/CMakeLists.txt +++ b/src/engine/CMakeLists.txt @@ -29,10 +29,6 @@ target_sources(scratchcpp thread_p.h internal/engine.cpp internal/engine.h - internal/icodebuilder.h - internal/icodebuilderfactory.h - internal/codebuilderfactory.cpp - internal/codebuilderfactory.h internal/clock.cpp internal/clock.h internal/iclock.h @@ -44,4 +40,4 @@ target_sources(scratchcpp internal/randomgenerator.cpp ) -add_subdirectory(internal/llvm) +add_subdirectory(internal/codegen) diff --git a/src/engine/compiler.cpp b/src/engine/compiler.cpp index fd4b41b72..799c0ae14 100644 --- a/src/engine/compiler.cpp +++ b/src/engine/compiler.cpp @@ -8,8 +8,8 @@ #include #include "compiler_p.h" -#include "internal/icodebuilderfactory.h" -#include "internal/icodebuilder.h" +#include "internal/codegen/icodebuilderfactory.h" +#include "internal/codegen/icodebuilder.h" using namespace libscratchcpp; @@ -20,8 +20,8 @@ Compiler::Compiler(CompilerContext *ctx) : } /*! Constructs Compiler using a new context for the given target. */ -Compiler::Compiler(IEngine *engine, Target *target) : - impl(spimpl::make_unique_impl(engine, target)) +Compiler::Compiler(IEngine *engine, Target *target, const std::unordered_set &unsafeConstants) : + impl(spimpl::make_unique_impl(engine, target, unsafeConstants)) { } @@ -751,8 +751,8 @@ const std::unordered_set &Compiler::unsupportedBlocks() const } /*! Creates a compiler context for the given target. */ -std::shared_ptr Compiler::createContext(IEngine *engine, Target *target) +std::shared_ptr Compiler::createContext(IEngine *engine, Target *target, const std::unordered_set &unsafeConstants) { CompilerPrivate::initBuilderFactory(); - return CompilerPrivate::builderFactory()->createCtx(engine, target); + return CompilerPrivate::builderFactory()->createCtx(engine, target, unsafeConstants); } diff --git a/src/engine/compiler_p.cpp b/src/engine/compiler_p.cpp index fce12ab7f..a6b4c8b51 100644 --- a/src/engine/compiler_p.cpp +++ b/src/engine/compiler_p.cpp @@ -5,8 +5,8 @@ #include "compiler_p.h" -#include "internal/codebuilderfactory.h" -#include "internal/icodebuilder.h" +#include "internal/codegen/codebuilderfactory.h" +#include "internal/codegen/icodebuilder.h" using namespace libscratchcpp; @@ -17,8 +17,8 @@ CompilerPrivate::CompilerPrivate(CompilerContext *ctx) : initBuilderFactory(); } -CompilerPrivate::CompilerPrivate(IEngine *engine, Target *target) : - ctxPtr(Compiler::createContext(engine, target)), +CompilerPrivate::CompilerPrivate(IEngine *engine, Target *target, const std::unordered_set &unsafeConstants) : + ctxPtr(Compiler::createContext(engine, target, unsafeConstants)), ctx(ctxPtr.get()) { initBuilderFactory(); diff --git a/src/engine/compiler_p.h b/src/engine/compiler_p.h index c0d076606..a366d82ca 100644 --- a/src/engine/compiler_p.h +++ b/src/engine/compiler_p.h @@ -29,7 +29,7 @@ class LIBSCRATCHCPP_TEST_EXPORT CompilerPrivate }; CompilerPrivate(CompilerContext *ctx); - CompilerPrivate(IEngine *engine, Target *target); + CompilerPrivate(IEngine *engine, Target *target, const std::unordered_set &unsafeConstants); static void initBuilderFactory(); diff --git a/src/engine/compilercontext.cpp b/src/engine/compilercontext.cpp index fa78ad57a..8d16a44dc 100644 --- a/src/engine/compilercontext.cpp +++ b/src/engine/compilercontext.cpp @@ -7,8 +7,8 @@ using namespace libscratchcpp; /*! Constructs CompilerContext. */ -CompilerContext::CompilerContext(IEngine *engine, Target *target) : - impl(spimpl::make_unique_impl(engine, target)) +CompilerContext::CompilerContext(IEngine *engine, Target *target, const std::unordered_set &unsafeConstants) : + impl(spimpl::make_unique_impl(engine, target, unsafeConstants)) { } @@ -23,3 +23,9 @@ Target *CompilerContext::target() const { return impl->target; } + +/*! Returns the list of unsafe constants (used internally during optimization). */ +const std::unordered_set &CompilerContext::unsafeConstants() const +{ + return impl->unsafeConstants; +} diff --git a/src/engine/compilercontext_p.cpp b/src/engine/compilercontext_p.cpp index 5f4c42f25..181470d59 100644 --- a/src/engine/compilercontext_p.cpp +++ b/src/engine/compilercontext_p.cpp @@ -4,8 +4,9 @@ using namespace libscratchcpp; -CompilerContextPrivate::CompilerContextPrivate(IEngine *engine, Target *target) : +CompilerContextPrivate::CompilerContextPrivate(IEngine *engine, Target *target, const std::unordered_set &unsafeConstants) : engine(engine), - target(target) + target(target), + unsafeConstants(unsafeConstants) { } diff --git a/src/engine/compilercontext_p.h b/src/engine/compilercontext_p.h index 5a97a56a1..a12c73f09 100644 --- a/src/engine/compilercontext_p.h +++ b/src/engine/compilercontext_p.h @@ -2,6 +2,9 @@ #pragma once +#include +#include + namespace libscratchcpp { @@ -10,10 +13,11 @@ class Target; struct CompilerContextPrivate { - CompilerContextPrivate(IEngine *engine, Target *target); + CompilerContextPrivate(IEngine *engine, Target *target, const std::unordered_set &unsafeConstants); IEngine *engine = nullptr; Target *target = nullptr; + const std::unordered_set unsafeConstants; }; } // namespace libscratchcpp diff --git a/src/engine/internal/codegen/CMakeLists.txt b/src/engine/internal/codegen/CMakeLists.txt new file mode 100644 index 000000000..bb278efb3 --- /dev/null +++ b/src/engine/internal/codegen/CMakeLists.txt @@ -0,0 +1,18 @@ +target_sources(scratchcpp + PRIVATE + icodebuilder.h + icodebuilderfactory.h + codebuilderfactory.cpp + codebuilderfactory.h + instructionbase.h + instruction.h + instructionlistbase.cpp + instructionlistbase.h + instructionlist.h + codeanalyzer.cpp + codeanalyzer.h + optimizationutils.cpp + optimizationutils.h +) + +add_subdirectory(llvm) diff --git a/src/engine/internal/llvm/llvmcodeanalyzer.cpp b/src/engine/internal/codegen/codeanalyzer.cpp similarity index 76% rename from src/engine/internal/llvm/llvmcodeanalyzer.cpp rename to src/engine/internal/codegen/codeanalyzer.cpp index 1e594d75d..fdb26b02c 100644 --- a/src/engine/internal/llvm/llvmcodeanalyzer.cpp +++ b/src/engine/internal/codegen/codeanalyzer.cpp @@ -1,27 +1,29 @@ // SPDX-License-Identifier: Apache-2.0 -#include "llvmcodeanalyzer.h" -#include "llvminstructionlist.h" -#include "llvminstruction.h" -#include "llvmbuildutils.h" +#include + +#include "codeanalyzer.h" +#include "instructionlistbase.h" +#include "instructionbase.h" +#include "optimizationutils.h" using namespace libscratchcpp; // NOTE: The loop condition in repeat until and while loops is considered a part of the loop body -static const std::unordered_set BEGIN_LOOP_INSTRUCTIONS = { LLVMInstruction::Type::BeginRepeatLoop, LLVMInstruction::Type::BeginLoopCondition }; +static const std::unordered_set BEGIN_LOOP_INSTRUCTIONS = { InstructionBase::Type::BeginRepeatLoop, InstructionBase::Type::BeginLoopCondition }; -static const std::unordered_set LIST_WRITE_INSTRUCTIONS = { LLVMInstruction::Type::AppendToList, LLVMInstruction::Type::InsertToList, LLVMInstruction::Type::ListReplace }; +static const std::unordered_set LIST_WRITE_INSTRUCTIONS = { InstructionBase::Type::AppendToList, InstructionBase::Type::InsertToList, InstructionBase::Type::ListReplace }; -LLVMCodeAnalyzer::LLVMCodeAnalyzer(const LLVMBuildUtils &utils) : - m_utils(utils) +CodeAnalyzer::CodeAnalyzer(CompilerContext *ctx) : + m_ctx(ctx) { } -void LLVMCodeAnalyzer::analyzeScript(const LLVMInstructionList &script) const +void CodeAnalyzer::analyzeScript(const InstructionListBase &script) const { - std::unordered_set typeAssignedInstructions; + std::unordered_set typeAssignedInstructions; std::vector> branches; - LLVMInstruction *ins = script.first(); + InstructionBase *ins = script.first(); auto topBranch = std::make_unique(); topBranch->start = ins; @@ -155,7 +157,7 @@ void LLVMCodeAnalyzer::analyzeScript(const LLVMInstructionList &script) const assert(branches.back().get() == topBranchPtr); } -void LLVMCodeAnalyzer::updateVariableType(Branch *branch, LLVMInstruction *ins, std::unordered_set &typeAssignedInstructions, bool isWrite) const +void CodeAnalyzer::updateVariableType(Branch *branch, InstructionBase *ins, std::unordered_set &typeAssignedInstructions, bool isWrite) const { auto it = branch->variableTypes.find(ins->targetVariable); @@ -182,7 +184,7 @@ void LLVMCodeAnalyzer::updateVariableType(Branch *branch, LLVMInstruction *ins, } } -void LLVMCodeAnalyzer::updateListType(Branch *branch, LLVMInstruction *ins, std::unordered_set &typeAssignedInstructions, bool isWrite) const +void CodeAnalyzer::updateListType(Branch *branch, InstructionBase *ins, std::unordered_set &typeAssignedInstructions, bool isWrite) const { auto it = branch->listTypes.find(ins->targetList); @@ -211,7 +213,7 @@ void LLVMCodeAnalyzer::updateListType(Branch *branch, LLVMInstruction *ins, std: } } -void LLVMCodeAnalyzer::mergeVariableTypes(Branch *branch, Branch *previousBranch) const +void CodeAnalyzer::mergeVariableTypes(Branch *branch, Branch *previousBranch) const { for (const auto &[var, type] : branch->variableTypes) { auto it = previousBranch->variableTypes.find(var); @@ -223,13 +225,13 @@ void LLVMCodeAnalyzer::mergeVariableTypes(Branch *branch, Branch *previousBranch } } -void LLVMCodeAnalyzer::overrideVariableTypes(Branch *branch, Branch *previousBranch) const +void CodeAnalyzer::overrideVariableTypes(Branch *branch, Branch *previousBranch) const { for (const auto &[var, type] : branch->variableTypes) previousBranch->variableTypes[var] = type; } -void LLVMCodeAnalyzer::mergeListTypes(Branch *branch, Branch *previousBranch, bool firstUnknown) const +void CodeAnalyzer::mergeListTypes(Branch *branch, Branch *previousBranch, bool firstUnknown) const { for (const auto &[list, type] : branch->listTypes) { auto it = previousBranch->listTypes.find(list); @@ -241,69 +243,69 @@ void LLVMCodeAnalyzer::mergeListTypes(Branch *branch, Branch *previousBranch, bo } } -bool LLVMCodeAnalyzer::isLoopStart(const LLVMInstruction *ins) const +bool CodeAnalyzer::isLoopStart(const InstructionBase *ins) const { return (BEGIN_LOOP_INSTRUCTIONS.find(ins->type) != BEGIN_LOOP_INSTRUCTIONS.cend()); } -bool LLVMCodeAnalyzer::isLoopEnd(const LLVMInstruction *ins) const +bool CodeAnalyzer::isLoopEnd(const InstructionBase *ins) const { - return (ins->type == LLVMInstruction::Type::EndLoop); + return (ins->type == InstructionBase::Type::EndLoop); } -bool LLVMCodeAnalyzer::isIfStart(const LLVMInstruction *ins) const +bool CodeAnalyzer::isIfStart(const InstructionBase *ins) const { - return (ins->type == LLVMInstruction::Type::BeginIf); + return (ins->type == InstructionBase::Type::BeginIf); } -bool LLVMCodeAnalyzer::isElse(const LLVMInstruction *ins) const +bool CodeAnalyzer::isElse(const InstructionBase *ins) const { - return (ins->type == LLVMInstruction::Type::BeginElse); + return (ins->type == InstructionBase::Type::BeginElse); } -bool LLVMCodeAnalyzer::isIfEnd(const LLVMInstruction *ins) const +bool CodeAnalyzer::isIfEnd(const InstructionBase *ins) const { - return (ins->type == LLVMInstruction::Type::EndIf); + return (ins->type == InstructionBase::Type::EndIf); } -bool LLVMCodeAnalyzer::isVariableRead(const LLVMInstruction *ins) const +bool CodeAnalyzer::isVariableRead(const InstructionBase *ins) const { - return (ins->type == LLVMInstruction::Type::ReadVariable); + return (ins->type == InstructionBase::Type::ReadVariable); } -bool LLVMCodeAnalyzer::isVariableWrite(const LLVMInstruction *ins) const +bool CodeAnalyzer::isVariableWrite(const InstructionBase *ins) const { - return (ins->type == LLVMInstruction::Type::WriteVariable); + return (ins->type == InstructionBase::Type::WriteVariable); } -bool LLVMCodeAnalyzer::isListRead(const LLVMInstruction *ins) const +bool CodeAnalyzer::isListRead(const InstructionBase *ins) const { - return (ins->type == LLVMInstruction::Type::GetListItem); + return (ins->type == InstructionBase::Type::GetListItem); } -bool LLVMCodeAnalyzer::isListWrite(const LLVMInstruction *ins) const +bool CodeAnalyzer::isListWrite(const InstructionBase *ins) const { return (LIST_WRITE_INSTRUCTIONS.find(ins->type) != LIST_WRITE_INSTRUCTIONS.cend()); } -bool LLVMCodeAnalyzer::isListClear(const LLVMInstruction *ins) const +bool CodeAnalyzer::isListClear(const InstructionBase *ins) const { - return (ins->type == LLVMInstruction::Type::ClearList); + return (ins->type == InstructionBase::Type::ClearList); } -bool LLVMCodeAnalyzer::isProcedureCall(const LLVMInstruction *ins) const +bool CodeAnalyzer::isProcedureCall(const InstructionBase *ins) const { - return (ins->type == LLVMInstruction::Type::CallProcedure); + return (ins->type == InstructionBase::Type::CallProcedure); } -Compiler::StaticType LLVMCodeAnalyzer::writeType(LLVMInstruction *ins) const +Compiler::StaticType CodeAnalyzer::writeType(InstructionBase *ins) const { assert(ins); assert(!ins->args.empty()); auto &arg = ins->args.back(); // value is always the last argument in variable/list write instructions - const LLVMRegister *argReg = arg.second; + const CompilerValue *argValue = arg.second; - Compiler::StaticType ret = m_utils.optimizeRegisterType(argReg); + Compiler::StaticType ret = OptimizationUtils::optimizeValueType(m_ctx, argValue); arg.first = ret; // store the return type in the value argument return ret; } diff --git a/src/engine/internal/codegen/codeanalyzer.h b/src/engine/internal/codegen/codeanalyzer.h new file mode 100644 index 000000000..9e6f19397 --- /dev/null +++ b/src/engine/internal/codegen/codeanalyzer.h @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include +#include + +#include "test_export.h" + +namespace libscratchcpp +{ + +class InstructionListBase; +class InstructionBase; + +class LIBSCRATCHCPP_TEST_EXPORT CodeAnalyzer +{ + public: + CodeAnalyzer(CompilerContext *ctx); + CodeAnalyzer(const CodeAnalyzer &) = delete; + + void analyzeScript(const InstructionListBase &script) const; + + private: + struct Branch + { + InstructionBase *start = nullptr; + bool typeChanges = false; + std::unordered_map variableTypes; + std::unordered_map listTypes; + + std::unique_ptr elseBranch; + bool isAtElse = false; // whether we're currently processing the else branch + }; + + void updateVariableType(Branch *branch, InstructionBase *ins, std::unordered_set &typeAssignedInstructions, bool isWrite) const; + void updateListType(Branch *branch, InstructionBase *ins, std::unordered_set &typeAssignedInstructions, bool isWrite) const; + + void mergeVariableTypes(Branch *branch, Branch *previousBranch) const; + void overrideVariableTypes(Branch *branch, Branch *previousBranch) const; + void mergeListTypes(Branch *branch, Branch *previousBranch, bool firstUnknown) const; + + bool isLoopStart(const InstructionBase *ins) const; + bool isLoopEnd(const InstructionBase *ins) const; + bool isIfStart(const InstructionBase *ins) const; + bool isElse(const InstructionBase *ins) const; + bool isIfEnd(const InstructionBase *ins) const; + + bool isVariableRead(const InstructionBase *ins) const; + bool isVariableWrite(const InstructionBase *ins) const; + + bool isListRead(const InstructionBase *ins) const; + bool isListWrite(const InstructionBase *ins) const; + bool isListClear(const InstructionBase *ins) const; + + bool isProcedureCall(const InstructionBase *ins) const; + + Compiler::StaticType writeType(InstructionBase *ins) const; + + private: + CompilerContext *m_ctx; +}; + +} // namespace libscratchcpp diff --git a/src/engine/internal/codebuilderfactory.cpp b/src/engine/internal/codegen/codebuilderfactory.cpp similarity index 87% rename from src/engine/internal/codebuilderfactory.cpp rename to src/engine/internal/codegen/codebuilderfactory.cpp index 853f283cd..fc2985de5 100644 --- a/src/engine/internal/codebuilderfactory.cpp +++ b/src/engine/internal/codegen/codebuilderfactory.cpp @@ -19,8 +19,8 @@ std::shared_ptr CodeBuilderFactory::create(CompilerContext *ctx, B return std::make_shared(static_cast(ctx), procedurePrototype, codeType); } -std::shared_ptr CodeBuilderFactory::createCtx(IEngine *engine, Target *target) const +std::shared_ptr CodeBuilderFactory::createCtx(IEngine *engine, Target *target, const std::unordered_set &unsafeConstants) const { - auto ptr = std::make_shared(engine, target); + auto ptr = std::make_shared(engine, target, unsafeConstants); return ptr; } diff --git a/src/engine/internal/codebuilderfactory.h b/src/engine/internal/codegen/codebuilderfactory.h similarity index 86% rename from src/engine/internal/codebuilderfactory.h rename to src/engine/internal/codegen/codebuilderfactory.h index 8218f3bc2..955e5fe5e 100644 --- a/src/engine/internal/codebuilderfactory.h +++ b/src/engine/internal/codegen/codebuilderfactory.h @@ -13,7 +13,7 @@ class LIBSCRATCHCPP_TEST_EXPORT CodeBuilderFactory : public ICodeBuilderFactory public: static std::shared_ptr instance(); std::shared_ptr create(CompilerContext *ctx, BlockPrototype *procedurePrototype, Compiler::CodeType codeType) const override; - std::shared_ptr createCtx(IEngine *engine, Target *target) const override; + std::shared_ptr createCtx(IEngine *engine, Target *target, const std::unordered_set &unsafeConstants = {}) const override; private: static std::shared_ptr m_instance; diff --git a/src/engine/internal/icodebuilder.h b/src/engine/internal/codegen/icodebuilder.h similarity index 100% rename from src/engine/internal/icodebuilder.h rename to src/engine/internal/codegen/icodebuilder.h diff --git a/src/engine/internal/icodebuilderfactory.h b/src/engine/internal/codegen/icodebuilderfactory.h similarity index 84% rename from src/engine/internal/icodebuilderfactory.h rename to src/engine/internal/codegen/icodebuilderfactory.h index de0b7b8df..419cf50e9 100644 --- a/src/engine/internal/icodebuilderfactory.h +++ b/src/engine/internal/codegen/icodebuilderfactory.h @@ -16,7 +16,7 @@ class ICodeBuilderFactory virtual ~ICodeBuilderFactory() { } virtual std::shared_ptr create(CompilerContext *ctx, BlockPrototype *procedurePrototype = nullptr, Compiler::CodeType codeType = Compiler::CodeType::Script) const = 0; - virtual std::shared_ptr createCtx(IEngine *engine, Target *target) const = 0; + virtual std::shared_ptr createCtx(IEngine *engine, Target *target, const std::unordered_set &unsafeConstants = {}) const = 0; }; } // namespace libscratchcpp diff --git a/src/engine/internal/codegen/instruction.h b/src/engine/internal/codegen/instruction.h new file mode 100644 index 000000000..6820945a7 --- /dev/null +++ b/src/engine/internal/codegen/instruction.h @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "instructionbase.h" + +namespace libscratchcpp +{ + +template +struct Instruction : public InstructionBase +{ + using Type = InstructionBase::Type; + using InstructionBase::InstructionBase; // inherit constructor + + inline TInstruction *getPrevious() const { return static_cast(previous); } + + inline TInstruction *getNext() const { return static_cast(next); } + + inline std::vector> getArgs() const + { + // TODO: Replace this with getArg(index) and argCount() methods + std::vector> out; + out.reserve(args.size()); + + for (auto &a : args) + out.emplace_back(a.first, dynamic_cast(a.second)); + + return out; + } + + inline TRegister *getFunctionReturnReg() const { return dynamic_cast(functionReturnReg); } + inline void setFunctionReturnReg(TRegister *reg) { functionReturnReg = reg; } +}; + +} // namespace libscratchcpp diff --git a/src/engine/internal/llvm/llvminstruction.h b/src/engine/internal/codegen/instructionbase.h similarity index 83% rename from src/engine/internal/llvm/llvminstruction.h rename to src/engine/internal/codegen/instructionbase.h index 521f25afd..f83bcfe30 100644 --- a/src/engine/internal/llvm/llvminstruction.h +++ b/src/engine/internal/codegen/instructionbase.h @@ -4,15 +4,12 @@ #include -#include "llvmregister.h" - namespace libscratchcpp { -class LLVMLocalVariableInfo; class BlockPrototype; -struct LLVMInstruction +struct InstructionBase { enum class Type { @@ -84,7 +81,7 @@ struct LLVMInstruction Print }; - LLVMInstruction(Type type, bool loopCondition) : + InstructionBase(Type type, bool loopCondition) : type(type), loopCondition(loopCondition) { @@ -92,11 +89,8 @@ struct LLVMInstruction Type type; std::string functionName; - std::vector> args; // target type, register - LLVMRegister *functionReturnReg = nullptr; bool functionTargetArg = false; // whether to add target ptr to function parameters bool functionCtxArg = false; // whether to add execution context ptr to function parameters - LLVMLocalVariableInfo *localVarInfo = nullptr; // for local variables Variable *targetVariable = nullptr; // for variables List *targetList = nullptr; // for lists Compiler::StaticType targetType = Compiler::StaticType::Unknown; // variable or list type (before read/write operation) @@ -104,10 +98,13 @@ struct LLVMInstruction size_t procedureArgIndex = 0; bool loopCondition = false; // whether the instruction is part of a loop condition + std::vector> args; // target type, register + CompilerValue *functionReturnReg = nullptr; + // Linked list - LLVMInstruction *previous = nullptr; - LLVMInstruction *next = nullptr; - std::shared_ptr _next; // ensure items are not deleted + InstructionBase *previous = nullptr; + InstructionBase *next = nullptr; + std::shared_ptr _next; // ensure items are not deleted }; } // namespace libscratchcpp diff --git a/src/engine/internal/codegen/instructionlist.h b/src/engine/internal/codegen/instructionlist.h new file mode 100644 index 000000000..d52beb37f --- /dev/null +++ b/src/engine/internal/codegen/instructionlist.h @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "instructionlistbase.h" + +namespace libscratchcpp +{ + +template +class InstructionList : public InstructionListBase +{ + public: + inline TInstruction *first() const { return static_cast(InstructionListBase::first()); } + + inline TInstruction *last() const { return static_cast(InstructionListBase::last()); } + + bool containsInstruction(InstructionBase *ins) const { return InstructionListBase::containsInstruction(ins); } + + bool containsInstruction(std::function func) const + { + return InstructionListBase::containsInstruction([&](const InstructionBase *i) { return func(static_cast(i)); }); + } + + void addInstruction(std::shared_ptr ins) { return InstructionListBase::addInstruction(std::move(ins)); } +}; + +} // namespace libscratchcpp diff --git a/src/engine/internal/llvm/llvminstructionlist.cpp b/src/engine/internal/codegen/instructionlistbase.cpp similarity index 54% rename from src/engine/internal/llvm/llvminstructionlist.cpp rename to src/engine/internal/codegen/instructionlistbase.cpp index 61b8d6c38..304b78a55 100644 --- a/src/engine/internal/llvm/llvminstructionlist.cpp +++ b/src/engine/internal/codegen/instructionlistbase.cpp @@ -1,25 +1,12 @@ // SPDX-License-Identifier: Apache-2.0 -#include - -#include "llvminstructionlist.h" -#include "llvminstruction.h" +#include "instructionlistbase.h" using namespace libscratchcpp; -LLVMInstruction *LLVMInstructionList::first() const -{ - return m_first.get(); -} - -LLVMInstruction *LLVMInstructionList::last() const -{ - return m_last.get(); -} - -bool LLVMInstructionList::containsInstruction(LLVMInstruction *ins) const +bool InstructionListBase::containsInstruction(InstructionBase *ins) const { - LLVMInstruction *ptr = m_first.get(); + InstructionBase *ptr = m_first.get(); while (ptr) { if (ptr == ins) @@ -31,9 +18,9 @@ bool LLVMInstructionList::containsInstruction(LLVMInstruction *ins) const return false; } -bool LLVMInstructionList::containsInstruction(std::function func) const +bool InstructionListBase::containsInstruction(std::function func) const { - LLVMInstruction *ptr = m_first.get(); + InstructionBase *ptr = m_first.get(); while (ptr) { if (func(ptr)) @@ -45,7 +32,7 @@ bool LLVMInstructionList::containsInstruction(std::function ins) +void InstructionListBase::addInstruction(std::shared_ptr ins) { if (!(m_first && m_last)) { assert(!m_first && !m_last); diff --git a/src/engine/internal/codegen/instructionlistbase.h b/src/engine/internal/codegen/instructionlistbase.h new file mode 100644 index 000000000..069582e67 --- /dev/null +++ b/src/engine/internal/codegen/instructionlistbase.h @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include +#include + +#include "test_export.h" +#include "instructionbase.h" + +namespace libscratchcpp +{ + +class LIBSCRATCHCPP_TEST_EXPORT InstructionListBase +{ + public: + InstructionListBase() = default; + InstructionListBase(const InstructionListBase &) = delete; + + inline InstructionBase *first() const { return m_first.get(); } + + inline InstructionBase *last() const { return m_last.get(); } + + inline bool empty() const { return !first(); } + + bool containsInstruction(InstructionBase *ins) const; + + bool containsInstruction(std::function func) const; + + void addInstruction(std::shared_ptr ins); + + private: + std::shared_ptr m_first; + std::shared_ptr m_last; +}; + +} // namespace libscratchcpp diff --git a/src/engine/internal/llvm/CMakeLists.txt b/src/engine/internal/codegen/llvm/CMakeLists.txt similarity index 89% rename from src/engine/internal/llvm/CMakeLists.txt rename to src/engine/internal/codegen/llvm/CMakeLists.txt index 818286c21..706fb1eae 100644 --- a/src/engine/internal/llvm/CMakeLists.txt +++ b/src/engine/internal/codegen/llvm/CMakeLists.txt @@ -2,14 +2,11 @@ target_sources(scratchcpp PRIVATE llvmbuildutils.cpp llvmbuildutils.h - llvmcodeanalyzer.cpp - llvmcodeanalyzer.h llvmcodebuilder.cpp llvmcodebuilder.h llvmregister.h llvmconstantregister.h llvminstruction.h - llvminstructionlist.cpp llvminstructionlist.h llvmifstatement.h llvmloop.h diff --git a/src/engine/internal/llvm/instructions/CMakeLists.txt b/src/engine/internal/codegen/llvm/instructions/CMakeLists.txt similarity index 100% rename from src/engine/internal/llvm/instructions/CMakeLists.txt rename to src/engine/internal/codegen/llvm/instructions/CMakeLists.txt diff --git a/src/engine/internal/llvm/instructions/comparison.cpp b/src/engine/internal/codegen/llvm/instructions/comparison.cpp similarity index 54% rename from src/engine/internal/llvm/instructions/comparison.cpp rename to src/engine/internal/codegen/llvm/instructions/comparison.cpp index 9a76ce24c..c504ba7f6 100644 --- a/src/engine/internal/llvm/instructions/comparison.cpp +++ b/src/engine/internal/codegen/llvm/instructions/comparison.cpp @@ -43,49 +43,54 @@ ProcessResult Comparison::process(LLVMInstruction *ins) LLVMInstruction *Comparison::buildCmpEQ(LLVMInstruction *ins) { assert(ins->args.size() == 2); - const auto &arg1 = ins->args[0].second; - const auto &arg2 = ins->args[1].second; - ins->functionReturnReg->value = m_utils.createComparison(arg1, arg2, LLVMBuildUtils::Comparison::EQ); + const auto &args = ins->getArgs(); + const auto &arg1 = args[0].second; + const auto &arg2 = args[1].second; + ins->getFunctionReturnReg()->value = m_utils.createComparison(arg1, arg2, LLVMBuildUtils::Comparison::EQ); - return ins->next; + return ins->getNext(); } LLVMInstruction *Comparison::buildCmpGT(LLVMInstruction *ins) { assert(ins->args.size() == 2); - const auto &arg1 = ins->args[0].second; - const auto &arg2 = ins->args[1].second; - ins->functionReturnReg->value = m_utils.createComparison(arg1, arg2, LLVMBuildUtils::Comparison::GT); + const auto &args = ins->getArgs(); + const auto &arg1 = args[0].second; + const auto &arg2 = args[1].second; + ins->getFunctionReturnReg()->value = m_utils.createComparison(arg1, arg2, LLVMBuildUtils::Comparison::GT); - return ins->next; + return ins->getNext(); } LLVMInstruction *Comparison::buildCmpLT(LLVMInstruction *ins) { assert(ins->args.size() == 2); - const auto &arg1 = ins->args[0].second; - const auto &arg2 = ins->args[1].second; - ins->functionReturnReg->value = m_utils.createComparison(arg1, arg2, LLVMBuildUtils::Comparison::LT); + const auto &args = ins->getArgs(); + const auto &arg1 = args[0].second; + const auto &arg2 = args[1].second; + ins->getFunctionReturnReg()->value = m_utils.createComparison(arg1, arg2, LLVMBuildUtils::Comparison::LT); - return ins->next; + return ins->getNext(); } LLVMInstruction *Comparison::buildStrCmpEQCS(LLVMInstruction *ins) { assert(ins->args.size() == 2); - const auto &arg1 = ins->args[0].second; - const auto &arg2 = ins->args[1].second; - ins->functionReturnReg->value = m_utils.createStringComparison(arg1, arg2, true); + const auto &args = ins->getArgs(); + const auto &arg1 = args[0].second; + const auto &arg2 = args[1].second; + ins->getFunctionReturnReg()->value = m_utils.createStringComparison(arg1, arg2, true); - return ins->next; + return ins->getNext(); } LLVMInstruction *Comparison::buildStrCmpEQCI(LLVMInstruction *ins) { assert(ins->args.size() == 2); - const auto &arg1 = ins->args[0].second; - const auto &arg2 = ins->args[1].second; - ins->functionReturnReg->value = m_utils.createStringComparison(arg1, arg2, false); + const auto &args = ins->getArgs(); + const auto &arg1 = args[0].second; + const auto &arg2 = args[1].second; + ins->getFunctionReturnReg()->value = m_utils.createStringComparison(arg1, arg2, false); - return ins->next; + return ins->getNext(); } diff --git a/src/engine/internal/llvm/instructions/comparison.h b/src/engine/internal/codegen/llvm/instructions/comparison.h similarity index 100% rename from src/engine/internal/llvm/instructions/comparison.h rename to src/engine/internal/codegen/llvm/instructions/comparison.h diff --git a/src/engine/internal/llvm/instructions/control.cpp b/src/engine/internal/codegen/llvm/instructions/control.cpp similarity index 88% rename from src/engine/internal/llvm/instructions/control.cpp rename to src/engine/internal/codegen/llvm/instructions/control.cpp index 3c9207a55..100f1875e 100644 --- a/src/engine/internal/llvm/instructions/control.cpp +++ b/src/engine/internal/codegen/llvm/instructions/control.cpp @@ -83,9 +83,10 @@ ProcessResult Control::process(LLVMInstruction *ins) LLVMInstruction *Control::buildSelect(LLVMInstruction *ins) { assert(ins->args.size() == 3); - const auto &arg1 = ins->args[0]; - const auto &arg2 = ins->args[1]; - const auto &arg3 = ins->args[2]; + const auto &args = ins->getArgs(); + const auto &arg1 = args[0]; + const auto &arg2 = args[1]; + const auto &arg3 = args[2]; auto type = arg2.first; llvm::Value *cond = m_utils.castValue(arg1.second, arg1.first); llvm::Value *trueValue; @@ -99,16 +100,16 @@ LLVMInstruction *Control::buildSelect(LLVMInstruction *ins) falseValue = m_utils.createValue(arg3.second); } - ins->functionReturnReg->value = m_builder.CreateSelect(cond, trueValue, falseValue); - ins->functionReturnReg->isInt = m_builder.CreateSelect(cond, arg2.second->isInt, arg3.second->isInt); - ins->functionReturnReg->intValue = m_builder.CreateSelect(cond, arg2.second->intValue, arg3.second->intValue); - return ins->next; + ins->getFunctionReturnReg()->value = m_builder.CreateSelect(cond, trueValue, falseValue); + ins->getFunctionReturnReg()->isInt = m_builder.CreateSelect(cond, arg2.second->isInt, arg3.second->isInt); + ins->getFunctionReturnReg()->intValue = m_builder.CreateSelect(cond, arg2.second->intValue, arg3.second->intValue); + return ins->getNext(); } LLVMInstruction *Control::buildYield(LLVMInstruction *ins) { m_utils.createSuspend(); - return ins->next; + return ins->getNext(); } LLVMInstruction *Control::buildBeginIf(LLVMInstruction *ins) @@ -119,7 +120,8 @@ LLVMInstruction *Control::buildBeginIf(LLVMInstruction *ins) // Use last reg assert(ins->args.size() == 1); - const auto ® = ins->args[0]; + const auto &args = ins->getArgs(); + const auto ® = args[0]; assert(reg.first == Compiler::StaticType::Bool); statement.condition = m_utils.castValue(reg.second, reg.first); @@ -128,7 +130,7 @@ LLVMInstruction *Control::buildBeginIf(LLVMInstruction *ins) m_utils.ifStatements().push_back(statement); - return ins->next; + return ins->getNext(); } LLVMInstruction *Control::buildBeginElse(LLVMInstruction *ins) @@ -155,7 +157,7 @@ LLVMInstruction *Control::buildBeginElse(LLVMInstruction *ins) // Switch to the else branch m_builder.SetInsertPoint(statement.elseBranch); - return ins->next; + return ins->getNext(); } LLVMInstruction *Control::buildEndIf(LLVMInstruction *ins) @@ -182,7 +184,7 @@ LLVMInstruction *Control::buildEndIf(LLVMInstruction *ins) ifStatements.pop_back(); - return ins->next; + return ins->getNext(); } LLVMInstruction *Control::buildBeginRepeatLoop(LLVMInstruction *ins) @@ -205,7 +207,8 @@ LLVMInstruction *Control::buildBeginRepeatLoop(LLVMInstruction *ins) // Use last reg for count assert(ins->args.size() == 1); - const auto ® = ins->args[0]; + const auto &args = ins->getArgs(); + const auto ® = args[0]; assert(reg.first == Compiler::StaticType::Number); llvm::Value *count = m_utils.castValue(reg.second, reg.first); llvm::Value *isInf = m_builder.CreateFCmpOEQ(count, llvm::ConstantFP::getInfinity(m_builder.getDoubleTy(), false)); @@ -241,7 +244,7 @@ LLVMInstruction *Control::buildBeginRepeatLoop(LLVMInstruction *ins) m_utils.loops().push_back(loop); - return ins->next; + return ins->getNext(); } LLVMInstruction *Control::buildLoopIndex(LLVMInstruction *ins) @@ -249,11 +252,11 @@ LLVMInstruction *Control::buildLoopIndex(LLVMInstruction *ins) assert(!m_utils.loops().empty()); LLVMLoop &loop = m_utils.loops().back(); llvm::Value *index = m_builder.CreateLoad(m_builder.getInt64Ty(), loop.index); - ins->functionReturnReg->value = m_builder.CreateUIToFP(index, m_builder.getDoubleTy()); - ins->functionReturnReg->intValue = index; - ins->functionReturnReg->isInt = m_builder.getInt1(true); + ins->getFunctionReturnReg()->value = m_builder.CreateUIToFP(index, m_builder.getDoubleTy()); + ins->getFunctionReturnReg()->intValue = index; + ins->getFunctionReturnReg()->isInt = m_builder.getInt1(true); - return ins->next; + return ins->getNext(); } LLVMInstruction *Control::buildBeginWhileLoop(LLVMInstruction *ins) @@ -270,7 +273,8 @@ LLVMInstruction *Control::buildBeginWhileLoop(LLVMInstruction *ins) // Use last reg assert(ins->args.size() == 1); - const auto ® = ins->args[0]; + const auto &args = ins->getArgs(); + const auto ® = args[0]; assert(reg.first == Compiler::StaticType::Bool); llvm::Value *condition = m_utils.castValue(reg.second, reg.first); m_builder.CreateCondBr(condition, body, loop.afterLoop); @@ -278,7 +282,7 @@ LLVMInstruction *Control::buildBeginWhileLoop(LLVMInstruction *ins) // Switch to body branch m_builder.SetInsertPoint(body); - return ins->next; + return ins->getNext(); } LLVMInstruction *Control::buildBeginRepeatUntilLoop(LLVMInstruction *ins) @@ -295,7 +299,8 @@ LLVMInstruction *Control::buildBeginRepeatUntilLoop(LLVMInstruction *ins) // Use last reg assert(ins->args.size() == 1); - const auto ® = ins->args[0]; + const auto &args = ins->getArgs(); + const auto ® = args[0]; assert(reg.first == Compiler::StaticType::Bool); llvm::Value *condition = m_utils.castValue(reg.second, reg.first); m_builder.CreateCondBr(condition, loop.afterLoop, body); @@ -303,7 +308,7 @@ LLVMInstruction *Control::buildBeginRepeatUntilLoop(LLVMInstruction *ins) // Switch to body branch m_builder.SetInsertPoint(body); - return ins->next; + return ins->getNext(); } LLVMInstruction *Control::buildBeginLoopCondition(LLVMInstruction *ins) @@ -315,7 +320,7 @@ LLVMInstruction *Control::buildBeginLoopCondition(LLVMInstruction *ins) m_builder.SetInsertPoint(loop.conditionBranch); m_utils.loops().push_back(loop); - return ins->next; + return ins->getNext(); } LLVMInstruction *Control::buildEndLoop(LLVMInstruction *ins) @@ -339,7 +344,7 @@ LLVMInstruction *Control::buildEndLoop(LLVMInstruction *ins) loops.pop_back(); - return ins->next; + return ins->getNext(); } LLVMInstruction *Control::buildStop(LLVMInstruction *ins) @@ -350,7 +355,7 @@ LLVMInstruction *Control::buildStop(LLVMInstruction *ins) llvm::BasicBlock *nextBranch = llvm::BasicBlock::Create(m_utils.llvmCtx(), "", m_utils.function()); m_builder.SetInsertPoint(nextBranch); - return ins->next; + return ins->getNext(); } LLVMInstruction *Control::buildThreadStop(LLVMInstruction *ins) @@ -361,22 +366,23 @@ LLVMInstruction *Control::buildThreadStop(LLVMInstruction *ins) llvm::BasicBlock *nextBranch = llvm::BasicBlock::Create(m_utils.llvmCtx(), "", m_utils.function()); m_builder.SetInsertPoint(nextBranch); - return ins->next; + return ins->getNext(); } LLVMInstruction *Control::buildInvalidateTarget(LLVMInstruction *ins) { m_utils.invalidateTarget(); - return ins->next; + return ins->getNext(); } LLVMInstruction *Control::buildPrint(LLVMInstruction *ins) { assert(ins->args.size() == 1); - const auto &arg = ins->args[0]; + const auto &args = ins->getArgs(); + const auto &arg = args[0]; llvm::Value *str = m_utils.castValue(arg.second, arg.first); m_builder.CreateCall(m_utils.functions().resolve_llvm_print(), str); - return ins->next; + return ins->getNext(); } diff --git a/src/engine/internal/llvm/instructions/control.h b/src/engine/internal/codegen/llvm/instructions/control.h similarity index 100% rename from src/engine/internal/llvm/instructions/control.h rename to src/engine/internal/codegen/llvm/instructions/control.h diff --git a/src/engine/internal/llvm/instructions/functions.cpp b/src/engine/internal/codegen/llvm/instructions/functions.cpp similarity index 68% rename from src/engine/internal/llvm/instructions/functions.cpp rename to src/engine/internal/codegen/llvm/instructions/functions.cpp index 50a37550f..b05623430 100644 --- a/src/engine/internal/llvm/instructions/functions.cpp +++ b/src/engine/internal/codegen/llvm/instructions/functions.cpp @@ -35,7 +35,7 @@ LLVMInstruction *Functions::buildFunctionCall(LLVMInstruction *ins) // Strings are returned through an output parameter llvm::Value *stringRet = nullptr; - if (ins->functionReturnReg && ins->functionReturnReg->type() == Compiler::StaticType::String) { + if (ins->getFunctionReturnReg() && ins->getFunctionReturnReg()->type() == Compiler::StaticType::String) { stringRet = m_utils.addStringAlloca(); types.push_back(m_utils.getType(Compiler::StaticType::String, false)); args.push_back(stringRet); @@ -54,24 +54,26 @@ LLVMInstruction *Functions::buildFunctionCall(LLVMInstruction *ins) } // Args - for (auto &arg : ins->args) { + const auto &insArgs = ins->getArgs(); + + for (auto &arg : insArgs) { types.push_back(m_utils.getType(arg.first, false)); args.push_back(m_utils.castValue(arg.second, arg.first)); } - llvm::Type *retType = m_utils.getType(ins->functionReturnReg ? ins->functionReturnReg->type() : Compiler::StaticType::Void, true); + llvm::Type *retType = m_utils.getType(ins->getFunctionReturnReg() ? ins->getFunctionReturnReg()->type() : Compiler::StaticType::Void, true); llvm::Value *ret = m_builder.CreateCall(m_utils.functions().resolveFunction(ins->functionName, llvm::FunctionType::get(retType, types, false)), args); - if (ins->functionReturnReg) { - if (ins->functionReturnReg->type() == Compiler::StaticType::String) - ins->functionReturnReg->value = stringRet; - else if (m_utils.isSingleType(ins->functionReturnReg->type())) - ins->functionReturnReg->value = ret; + if (ins->getFunctionReturnReg()) { + if (ins->getFunctionReturnReg()->type() == Compiler::StaticType::String) + ins->getFunctionReturnReg()->value = stringRet; + else if (m_utils.isSingleType(ins->getFunctionReturnReg()->type())) + ins->getFunctionReturnReg()->value = ret; else { - ins->functionReturnReg->value = m_utils.addAlloca(retType); - m_builder.CreateStore(ret, ins->functionReturnReg->value); + ins->getFunctionReturnReg()->value = m_utils.addAlloca(retType); + m_builder.CreateStore(ret, ins->getFunctionReturnReg()->value); } } - return ins->next; + return ins->getNext(); } diff --git a/src/engine/internal/llvm/instructions/functions.h b/src/engine/internal/codegen/llvm/instructions/functions.h similarity index 100% rename from src/engine/internal/llvm/instructions/functions.h rename to src/engine/internal/codegen/llvm/instructions/functions.h diff --git a/src/engine/internal/llvm/instructions/instructionbuilder.cpp b/src/engine/internal/codegen/llvm/instructions/instructionbuilder.cpp similarity index 91% rename from src/engine/internal/llvm/instructions/instructionbuilder.cpp rename to src/engine/internal/codegen/llvm/instructions/instructionbuilder.cpp index 87977e327..402118860 100644 --- a/src/engine/internal/llvm/instructions/instructionbuilder.cpp +++ b/src/engine/internal/codegen/llvm/instructions/instructionbuilder.cpp @@ -39,8 +39,8 @@ LLVMInstruction *InstructionBuilder::process(LLVMInstruction *ins) if (result.match) { #ifndef LLVM_INTEGER_SUPPORT - if (ins->functionReturnReg) - ins->functionReturnReg->isInt = m_utils.builder().getInt1(false); + if (ins->getFunctionReturnReg()) + ins->getFunctionReturnReg()->isInt = m_utils.builder().getInt1(false); #endif return result.next; diff --git a/src/engine/internal/llvm/instructions/instructionbuilder.h b/src/engine/internal/codegen/llvm/instructions/instructionbuilder.h similarity index 100% rename from src/engine/internal/llvm/instructions/instructionbuilder.h rename to src/engine/internal/codegen/llvm/instructions/instructionbuilder.h diff --git a/src/engine/internal/llvm/instructions/instructiongroup.cpp b/src/engine/internal/codegen/llvm/instructions/instructiongroup.cpp similarity index 100% rename from src/engine/internal/llvm/instructions/instructiongroup.cpp rename to src/engine/internal/codegen/llvm/instructions/instructiongroup.cpp diff --git a/src/engine/internal/llvm/instructions/instructiongroup.h b/src/engine/internal/codegen/llvm/instructions/instructiongroup.h similarity index 100% rename from src/engine/internal/llvm/instructions/instructiongroup.h rename to src/engine/internal/codegen/llvm/instructions/instructiongroup.h diff --git a/src/engine/internal/llvm/instructions/lists.cpp b/src/engine/internal/codegen/llvm/instructions/lists.cpp similarity index 91% rename from src/engine/internal/llvm/instructions/lists.cpp rename to src/engine/internal/codegen/llvm/instructions/lists.cpp index 77efbee55..e7ad0cf92 100644 --- a/src/engine/internal/llvm/instructions/lists.cpp +++ b/src/engine/internal/codegen/llvm/instructions/lists.cpp @@ -5,6 +5,7 @@ #include "../llvmbuildutils.h" #include "../llvmconstantregister.h" #include "../llvmcompilercontext.h" +#include "../../optimizationutils.h" using namespace libscratchcpp; using namespace libscratchcpp::llvmins; @@ -82,20 +83,21 @@ LLVMInstruction *Lists::buildClearList(LLVMInstruction *ins) } } - return ins->next; + return ins->getNext(); } LLVMInstruction *Lists::buildRemoveListItem(LLVMInstruction *ins) { // No-op in empty lists if (ins->targetType == Compiler::StaticType::Void) - return ins->next; + return ins->getNext(); llvm::LLVMContext &llvmCtx = m_utils.llvmCtx(); llvm::Function *function = m_utils.function(); assert(ins->args.size() == 1); - const auto &arg = ins->args[0]; + const auto &args = ins->getArgs(); + const auto &arg = args[0]; LLVMListPtr &listPtr = m_utils.listPtr(ins->targetList); @@ -121,7 +123,7 @@ LLVMInstruction *Lists::buildRemoveListItem(LLVMInstruction *ins) m_builder.CreateBr(nextBlock); m_builder.SetInsertPoint(nextBlock); - return ins->next; + return ins->getNext(); } LLVMInstruction *Lists::buildAppendToList(LLVMInstruction *ins) @@ -130,8 +132,9 @@ LLVMInstruction *Lists::buildAppendToList(LLVMInstruction *ins) llvm::Function *function = m_utils.function(); assert(ins->args.size() == 1); - const auto &arg = ins->args[0]; - Compiler::StaticType type = m_utils.optimizeRegisterType(arg.second); + const auto &args = ins->getArgs(); + const auto &arg = args[0]; + Compiler::StaticType type = OptimizationUtils::optimizeValueType(m_utils.compilerCtx(), arg.second); LLVMListPtr &listPtr = m_utils.listPtr(ins->targetList); // Check if enough space is allocated @@ -170,7 +173,7 @@ LLVMInstruction *Lists::buildAppendToList(LLVMInstruction *ins) } createListTypeUpdate(listPtr, arg.second, type); - return ins->next; + return ins->getNext(); } LLVMInstruction *Lists::buildInsertToList(LLVMInstruction *ins) @@ -179,9 +182,10 @@ LLVMInstruction *Lists::buildInsertToList(LLVMInstruction *ins) llvm::Function *function = m_utils.function(); assert(ins->args.size() == 2); - const auto &indexArg = ins->args[0]; - const auto &valueArg = ins->args[1]; - Compiler::StaticType type = m_utils.optimizeRegisterType(valueArg.second); + const auto &args = ins->getArgs(); + const auto &indexArg = args[0]; + const auto &valueArg = args[1]; + Compiler::StaticType type = OptimizationUtils::optimizeValueType(m_utils.compilerCtx(), valueArg.second); LLVMListPtr &listPtr = m_utils.listPtr(ins->targetList); // Range check @@ -212,22 +216,23 @@ LLVMInstruction *Lists::buildInsertToList(LLVMInstruction *ins) m_builder.CreateBr(nextBlock); m_builder.SetInsertPoint(nextBlock); - return ins->next; + return ins->getNext(); } LLVMInstruction *Lists::buildListReplace(LLVMInstruction *ins) { // No-op in empty lists if (ins->targetType == Compiler::StaticType::Void) - return ins->next; + return ins->getNext(); llvm::LLVMContext &llvmCtx = m_utils.llvmCtx(); llvm::Function *function = m_utils.function(); assert(ins->args.size() == 2); - const auto &indexArg = ins->args[0]; - const auto &valueArg = ins->args[1]; - Compiler::StaticType type = m_utils.optimizeRegisterType(valueArg.second); + const auto &args = ins->getArgs(); + const auto &indexArg = args[0]; + const auto &valueArg = args[1]; + Compiler::StaticType type = OptimizationUtils::optimizeValueType(m_utils.compilerCtx(), valueArg.second); LLVMListPtr &listPtr = m_utils.listPtr(ins->targetList); Compiler::StaticType listType = ins->targetType; @@ -263,7 +268,7 @@ LLVMInstruction *Lists::buildListReplace(LLVMInstruction *ins) m_builder.CreateBr(nextBlock); m_builder.SetInsertPoint(nextBlock); - return ins->next; + return ins->getNext(); } LLVMInstruction *Lists::buildGetListContents(LLVMInstruction *ins) @@ -272,9 +277,9 @@ LLVMInstruction *Lists::buildGetListContents(LLVMInstruction *ins) const LLVMListPtr &listPtr = m_utils.listPtr(ins->targetList); llvm::Value *ptr = m_utils.addStringAlloca(); m_builder.CreateCall(m_utils.functions().resolve_list_to_string(), { listPtr.ptr, ptr }); - ins->functionReturnReg->value = ptr; + ins->getFunctionReturnReg()->value = ptr; - return ins->next; + return ins->getNext(); } LLVMInstruction *Lists::buildGetListItem(LLVMInstruction *ins) @@ -285,12 +290,13 @@ LLVMInstruction *Lists::buildGetListItem(LLVMInstruction *ins) // Return empty string for empty lists if (ins->targetType == Compiler::StaticType::Void) { LLVMConstantRegister nullReg(Compiler::StaticType::String, ""); - ins->functionReturnReg->value = m_utils.createValue(static_cast(&nullReg)); - return ins->next; + ins->getFunctionReturnReg()->value = m_utils.createValue(static_cast(&nullReg)); + return ins->getNext(); } assert(ins->args.size() == 1); - const auto &arg = ins->args[0]; + const auto &args = ins->getArgs(); + const auto &arg = args[0]; LLVMListPtr &listPtr = m_utils.listPtr(ins->targetList); // Range check @@ -327,11 +333,11 @@ LLVMInstruction *Lists::buildGetListItem(LLVMInstruction *ins) itemTypeResult->addIncoming(stringType, outOfRangeBlock); llvm::Value *typeVar = createListTypeVar(listPtr, itemTypeResult); - ins->functionReturnReg->value = result; - ins->functionReturnReg->typeVar = typeVar; + ins->getFunctionReturnReg()->value = result; + ins->getFunctionReturnReg()->typeVar = typeVar; createListTypeAssumption(listPtr, typeVar, ins->targetType, inRange); - return ins->next; + return ins->getNext(); } LLVMInstruction *Lists::buildGetListSize(LLVMInstruction *ins) @@ -339,37 +345,39 @@ LLVMInstruction *Lists::buildGetListSize(LLVMInstruction *ins) assert(ins->args.size() == 0); const LLVMListPtr &listPtr = m_utils.listPtr(ins->targetList); llvm::Value *size = m_utils.getListSize(listPtr); - ins->functionReturnReg->value = m_builder.CreateUIToFP(size, m_builder.getDoubleTy()); - ins->functionReturnReg->isInt = m_builder.getInt1(true); - ins->functionReturnReg->intValue = size; + ins->getFunctionReturnReg()->value = m_builder.CreateUIToFP(size, m_builder.getDoubleTy()); + ins->getFunctionReturnReg()->isInt = m_builder.getInt1(true); + ins->getFunctionReturnReg()->intValue = size; - return ins->next; + return ins->getNext(); } LLVMInstruction *Lists::buildGetListItemIndex(LLVMInstruction *ins) { assert(ins->args.size() == 1); - const auto &arg = ins->args[0]; + const auto &args = ins->getArgs(); + const auto &arg = args[0]; LLVMListPtr &listPtr = m_utils.listPtr(ins->targetList); Compiler::StaticType listType = ins->targetType; - ins->functionReturnReg->value = m_builder.CreateSIToFP(m_utils.getListItemIndex(listPtr, listType, arg.second), m_builder.getDoubleTy()); - return ins->next; + ins->getFunctionReturnReg()->value = m_builder.CreateSIToFP(m_utils.getListItemIndex(listPtr, listType, arg.second), m_builder.getDoubleTy()); + return ins->getNext(); } LLVMInstruction *Lists::buildListContainsItem(LLVMInstruction *ins) { assert(ins->args.size() == 1); - const auto &arg = ins->args[0]; + const auto &args = ins->getArgs(); + const auto &arg = args[0]; LLVMListPtr &listPtr = m_utils.listPtr(ins->targetList); Compiler::StaticType listType = ins->targetType; llvm::Value *index = m_utils.getListItemIndex(listPtr, listType, arg.second); - ins->functionReturnReg->value = m_builder.CreateICmpSGT(index, llvm::ConstantInt::get(m_builder.getInt64Ty(), -1, true)); + ins->getFunctionReturnReg()->value = m_builder.CreateICmpSGT(index, llvm::ConstantInt::get(m_builder.getInt64Ty(), -1, true)); - return ins->next; + return ins->getNext(); } llvm::Value *Lists::createIndexRangeCheck(const LLVMListPtr &listPtr, llvm::Value *index, const std::string &name, bool includeSize) diff --git a/src/engine/internal/llvm/instructions/lists.h b/src/engine/internal/codegen/llvm/instructions/lists.h similarity index 100% rename from src/engine/internal/llvm/instructions/lists.h rename to src/engine/internal/codegen/llvm/instructions/lists.h diff --git a/src/engine/internal/llvm/instructions/logic.cpp b/src/engine/internal/codegen/llvm/instructions/logic.cpp similarity index 70% rename from src/engine/internal/llvm/instructions/logic.cpp rename to src/engine/internal/codegen/llvm/instructions/logic.cpp index e4410286b..920ac043d 100644 --- a/src/engine/internal/llvm/instructions/logic.cpp +++ b/src/engine/internal/codegen/llvm/instructions/logic.cpp @@ -36,33 +36,36 @@ ProcessResult Logic::process(LLVMInstruction *ins) LLVMInstruction *Logic::buildAnd(LLVMInstruction *ins) { assert(ins->args.size() == 2); - const auto &arg1 = ins->args[0]; - const auto &arg2 = ins->args[1]; + const auto &args = ins->getArgs(); + const auto &arg1 = args[0]; + const auto &arg2 = args[1]; llvm::Value *bool1 = m_utils.castValue(arg1.second, arg1.first); llvm::Value *bool2 = m_utils.castValue(arg2.second, arg2.first); - ins->functionReturnReg->value = m_builder.CreateAnd(bool1, bool2); + ins->getFunctionReturnReg()->value = m_builder.CreateAnd(bool1, bool2); - return ins->next; + return ins->getNext(); } LLVMInstruction *Logic::buildOr(LLVMInstruction *ins) { assert(ins->args.size() == 2); - const auto &arg1 = ins->args[0]; - const auto &arg2 = ins->args[1]; + const auto &args = ins->getArgs(); + const auto &arg1 = args[0]; + const auto &arg2 = args[1]; llvm::Value *bool1 = m_utils.castValue(arg1.second, arg1.first); llvm::Value *bool2 = m_utils.castValue(arg2.second, arg2.first); - ins->functionReturnReg->value = m_builder.CreateOr(bool1, bool2); + ins->getFunctionReturnReg()->value = m_builder.CreateOr(bool1, bool2); - return ins->next; + return ins->getNext(); } LLVMInstruction *Logic::buildNot(LLVMInstruction *ins) { assert(ins->args.size() == 1); - const auto &arg = ins->args[0]; + const auto &args = ins->getArgs(); + const auto &arg = args[0]; llvm::Value *value = m_utils.castValue(arg.second, arg.first); - ins->functionReturnReg->value = m_builder.CreateNot(value); + ins->getFunctionReturnReg()->value = m_builder.CreateNot(value); - return ins->next; + return ins->getNext(); } diff --git a/src/engine/internal/llvm/instructions/logic.h b/src/engine/internal/codegen/llvm/instructions/logic.h similarity index 100% rename from src/engine/internal/llvm/instructions/logic.h rename to src/engine/internal/codegen/llvm/instructions/logic.h diff --git a/src/engine/internal/llvm/instructions/math.cpp b/src/engine/internal/codegen/llvm/instructions/math.cpp similarity index 76% rename from src/engine/internal/llvm/instructions/math.cpp rename to src/engine/internal/codegen/llvm/instructions/math.cpp index aeb95cc3f..eb605abaa 100644 --- a/src/engine/internal/llvm/instructions/math.cpp +++ b/src/engine/internal/codegen/llvm/instructions/math.cpp @@ -111,81 +111,86 @@ ProcessResult Math::process(LLVMInstruction *ins) LLVMInstruction *Math::buildAdd(LLVMInstruction *ins) { assert(ins->args.size() == 2); - const auto &arg1 = ins->args[0]; - const auto &arg2 = ins->args[1]; + const auto &args = ins->getArgs(); + const auto &arg1 = args[0]; + const auto &arg2 = args[1]; llvm::Value *double1 = m_utils.removeNaN(m_utils.castValue(arg1.second, arg1.first, LLVMBuildUtils::NumberType::Double)); llvm::Value *double2 = m_utils.removeNaN(m_utils.castValue(arg2.second, arg2.first, LLVMBuildUtils::NumberType::Double)); - ins->functionReturnReg->value = m_builder.CreateFAdd(double1, double2); + ins->getFunctionReturnReg()->value = m_builder.CreateFAdd(double1, double2); llvm::Value *int1 = m_utils.castValue(arg1.second, arg1.first, LLVMBuildUtils::NumberType::Int); llvm::Value *int2 = m_utils.castValue(arg2.second, arg2.first, LLVMBuildUtils::NumberType::Int); - ins->functionReturnReg->isInt = m_builder.CreateAnd(arg1.second->isInt, arg2.second->isInt); - ins->functionReturnReg->intValue = m_builder.CreateAdd(int1, int2); + ins->getFunctionReturnReg()->isInt = m_builder.CreateAnd(arg1.second->isInt, arg2.second->isInt); + ins->getFunctionReturnReg()->intValue = m_builder.CreateAdd(int1, int2); - return ins->next; + return ins->getNext(); } LLVMInstruction *Math::buildSub(LLVMInstruction *ins) { assert(ins->args.size() == 2); - const auto &arg1 = ins->args[0]; - const auto &arg2 = ins->args[1]; + const auto &args = ins->getArgs(); + const auto &arg1 = args[0]; + const auto &arg2 = args[1]; llvm::Value *double1 = m_utils.removeNaN(m_utils.castValue(arg1.second, arg1.first, LLVMBuildUtils::NumberType::Double)); llvm::Value *double2 = m_utils.removeNaN(m_utils.castValue(arg2.second, arg2.first, LLVMBuildUtils::NumberType::Double)); - ins->functionReturnReg->value = m_builder.CreateFSub(double1, double2); + ins->getFunctionReturnReg()->value = m_builder.CreateFSub(double1, double2); llvm::Value *int1 = m_utils.castValue(arg1.second, arg1.first, LLVMBuildUtils::NumberType::Int); llvm::Value *int2 = m_utils.castValue(arg2.second, arg2.first, LLVMBuildUtils::NumberType::Int); - ins->functionReturnReg->isInt = m_builder.CreateAnd(arg1.second->isInt, arg2.second->isInt); - ins->functionReturnReg->intValue = m_builder.CreateSub(int1, int2); + ins->getFunctionReturnReg()->isInt = m_builder.CreateAnd(arg1.second->isInt, arg2.second->isInt); + ins->getFunctionReturnReg()->intValue = m_builder.CreateSub(int1, int2); - return ins->next; + return ins->getNext(); } LLVMInstruction *Math::buildMul(LLVMInstruction *ins) { assert(ins->args.size() == 2); - const auto &arg1 = ins->args[0]; - const auto &arg2 = ins->args[1]; + const auto &args = ins->getArgs(); + const auto &arg1 = args[0]; + const auto &arg2 = args[1]; llvm::Value *double1 = m_utils.removeNaN(m_utils.castValue(arg1.second, arg1.first, LLVMBuildUtils::NumberType::Double)); llvm::Value *double2 = m_utils.removeNaN(m_utils.castValue(arg2.second, arg2.first, LLVMBuildUtils::NumberType::Double)); - ins->functionReturnReg->value = m_builder.CreateFMul(double1, double2); + ins->getFunctionReturnReg()->value = m_builder.CreateFMul(double1, double2); llvm::Value *int1 = m_utils.castValue(arg1.second, arg1.first, LLVMBuildUtils::NumberType::Int); llvm::Value *int2 = m_utils.castValue(arg2.second, arg2.first, LLVMBuildUtils::NumberType::Int); - ins->functionReturnReg->isInt = m_builder.CreateAnd(arg1.second->isInt, arg2.second->isInt); - ins->functionReturnReg->intValue = m_builder.CreateMul(int1, int2); + ins->getFunctionReturnReg()->isInt = m_builder.CreateAnd(arg1.second->isInt, arg2.second->isInt); + ins->getFunctionReturnReg()->intValue = m_builder.CreateMul(int1, int2); - return ins->next; + return ins->getNext(); } LLVMInstruction *Math::buildDiv(LLVMInstruction *ins) { assert(ins->args.size() == 2); - const auto &arg1 = ins->args[0]; - const auto &arg2 = ins->args[1]; + const auto &args = ins->getArgs(); + const auto &arg1 = args[0]; + const auto &arg2 = args[1]; llvm::Value *num1 = m_utils.removeNaN(m_utils.castValue(arg1.second, arg1.first)); llvm::Value *num2 = m_utils.removeNaN(m_utils.castValue(arg2.second, arg2.first)); - ins->functionReturnReg->value = m_builder.CreateFDiv(num1, num2); + ins->getFunctionReturnReg()->value = m_builder.CreateFDiv(num1, num2); - return ins->next; + return ins->getNext(); } LLVMInstruction *Math::buildRandom(LLVMInstruction *ins) { assert(ins->args.size() == 2); - const auto &arg1 = ins->args[0]; - const auto &arg2 = ins->args[1]; + const auto &args = ins->getArgs(); + const auto &arg1 = args[0]; + const auto &arg2 = args[1]; LLVMRegister *reg1 = arg1.second; LLVMRegister *reg2 = arg2.second; if (reg1->type() == Compiler::StaticType::Bool && reg2->type() == Compiler::StaticType::Bool) { llvm::Value *bool1 = m_utils.castValue(arg1.second, Compiler::StaticType::Bool); llvm::Value *bool2 = m_utils.castValue(arg2.second, Compiler::StaticType::Bool); - ins->functionReturnReg->value = m_builder.CreateCall(m_utils.functions().resolve_llvm_random_bool(), { m_utils.executionContextPtr(), bool1, bool2 }); + ins->getFunctionReturnReg()->value = m_builder.CreateCall(m_utils.functions().resolve_llvm_random_bool(), { m_utils.executionContextPtr(), bool1, bool2 }); } else { llvm::Constant *inf = llvm::ConstantFP::getInfinity(m_builder.getDoubleTy(), false); llvm::Value *num1 = m_utils.removeNaN(m_utils.castValue(arg1.second, Compiler::StaticType::Number)); @@ -196,38 +201,41 @@ LLVMInstruction *Math::buildRandom(LLVMInstruction *ins) // NOTE: The random function will be called even in edge cases where it isn't needed, but they're rare, so it shouldn't be an issue if (reg1->type() == Compiler::StaticType::Number && reg2->type() == Compiler::StaticType::Number) - ins->functionReturnReg->value = + ins->getFunctionReturnReg()->value = m_builder.CreateSelect(isInfOrNaN, sum, m_builder.CreateCall(m_utils.functions().resolve_llvm_random_double(), { m_utils.executionContextPtr(), num1, num2 })); else { llvm::Value *value1 = m_utils.createValue(reg1); llvm::Value *value2 = m_utils.createValue(reg2); - ins->functionReturnReg->value = m_builder.CreateSelect(isInfOrNaN, sum, m_builder.CreateCall(m_utils.functions().resolve_llvm_random(), { m_utils.executionContextPtr(), value1, value2 })); + ins->getFunctionReturnReg()->value = + m_builder.CreateSelect(isInfOrNaN, sum, m_builder.CreateCall(m_utils.functions().resolve_llvm_random(), { m_utils.executionContextPtr(), value1, value2 })); } } - return ins->next; + return ins->getNext(); } LLVMInstruction *Math::buildRandomInt(LLVMInstruction *ins) { assert(ins->args.size() == 2); - const auto &arg1 = ins->args[0]; - const auto &arg2 = ins->args[1]; + const auto &args = ins->getArgs(); + const auto &arg1 = args[0]; + const auto &arg2 = args[1]; llvm::Value *from = m_builder.CreateFPToSI(m_utils.castValue(arg1.second, arg1.first), m_builder.getInt64Ty()); llvm::Value *to = m_builder.CreateFPToSI(m_utils.castValue(arg2.second, arg2.first), m_builder.getInt64Ty()); llvm::Value *intValue = m_builder.CreateCall(m_utils.functions().resolve_llvm_random_int64(), { m_utils.executionContextPtr(), from, to }); - ins->functionReturnReg->value = m_builder.CreateSIToFP(intValue, m_builder.getDoubleTy()); - ins->functionReturnReg->intValue = intValue; - ins->functionReturnReg->isInt = m_builder.getInt1(true); + ins->getFunctionReturnReg()->value = m_builder.CreateSIToFP(intValue, m_builder.getDoubleTy()); + ins->getFunctionReturnReg()->intValue = intValue; + ins->getFunctionReturnReg()->isInt = m_builder.getInt1(true); - return ins->next; + return ins->getNext(); } LLVMInstruction *Math::buildMod(LLVMInstruction *ins) { assert(ins->args.size() == 2); - const auto &arg1 = ins->args[0]; - const auto &arg2 = ins->args[1]; + const auto &args = ins->getArgs(); + const auto &arg1 = args[0]; + const auto &arg2 = args[1]; // double: rem(a, b) / b < 0.0 ? rem(a, b) + b : rem(a, b) llvm::Constant *doubleZero = llvm::ConstantFP::get(m_utils.llvmCtx(), llvm::APFloat(0.0)); @@ -235,7 +243,7 @@ LLVMInstruction *Math::buildMod(LLVMInstruction *ins) llvm::Value *double2 = m_utils.removeNaN(m_utils.castValue(arg2.second, arg2.first)); llvm::Value *doubleRem = m_builder.CreateFRem(double1, double2); // rem(a, b) llvm::Value *doubleCond = m_builder.CreateFCmpOLT(m_builder.CreateFDiv(doubleRem, double2), doubleZero); // rem(a, b) / b < 0.0 - ins->functionReturnReg->value = m_builder.CreateSelect(doubleCond, m_builder.CreateFAdd(doubleRem, double2), doubleRem); + ins->getFunctionReturnReg()->value = m_builder.CreateSelect(doubleCond, m_builder.CreateFAdd(doubleRem, double2), doubleRem); // int: b == 0 ? 0 (double fallback) : ((rem(a, b) < 0) != (b < 0) ? rem(a, b) + b : rem(a, b)) llvm::Constant *intZero = llvm::ConstantInt::get(m_builder.getInt64Ty(), 0, true); @@ -269,10 +277,10 @@ LLVMInstruction *Math::buildMod(LLVMInstruction *ins) isIntPhi->addIncoming(noInt, nanBlock); isIntPhi->addIncoming(isInt, intBlock); - ins->functionReturnReg->intValue = resultPhi; - ins->functionReturnReg->isInt = isIntPhi; + ins->getFunctionReturnReg()->intValue = resultPhi; + ins->getFunctionReturnReg()->isInt = isIntPhi; - return ins->next; + return ins->getNext(); } LLVMInstruction *Math::buildRound(LLVMInstruction *ins) @@ -281,7 +289,8 @@ LLVMInstruction *Math::buildRound(LLVMInstruction *ins) llvm::Module *module = m_utils.module(); assert(ins->args.size() == 1); - const auto &arg = ins->args[0]; + const auto &args = ins->getArgs(); + const auto &arg = args[0]; // double: x >= 0.0 ? round(x) : (x >= -0.5 ? -0.0 : floor(x + 0.5)) llvm::Constant *zero = llvm::ConstantFP::get(llvmCtx, llvm::APFloat(0.0)); @@ -293,7 +302,7 @@ LLVMInstruction *Math::buildRound(LLVMInstruction *ins) llvm::Value *roundNum = m_builder.CreateCall(roundFunc, doubleValue); // round(num) llvm::Value *negativeCond = m_builder.CreateFCmpOGE(doubleValue, llvm::ConstantFP::get(llvmCtx, llvm::APFloat(-0.5))); // num >= -0.5 llvm::Value *negativeRound = m_builder.CreateCall(floorFunc, m_builder.CreateFAdd(doubleValue, llvm::ConstantFP::get(llvmCtx, llvm::APFloat(0.5)))); // floor(x + 0.5) - ins->functionReturnReg->value = m_builder.CreateSelect(notNegative, roundNum, m_builder.CreateSelect(negativeCond, negativeZero, negativeRound)); + ins->getFunctionReturnReg()->value = m_builder.CreateSelect(notNegative, roundNum, m_builder.CreateSelect(negativeCond, negativeZero, negativeRound)); // int: doubleX == inf || doubleX == -inf ? doubleX : intX llvm::Constant *posInf = llvm::ConstantFP::getInfinity(m_builder.getDoubleTy(), false); @@ -301,39 +310,41 @@ LLVMInstruction *Math::buildRound(LLVMInstruction *ins) llvm::Value *isInt = arg.second->isInt; llvm::Value *intValue = arg.second->intValue; llvm::Value *isNotInf = m_builder.CreateAnd(m_builder.CreateFCmpONE(doubleValue, posInf), m_builder.CreateFCmpONE(doubleValue, negInf)); - llvm::Value *cast = m_builder.CreateFPToSI(ins->functionReturnReg->value, m_builder.getInt64Ty()); - ins->functionReturnReg->isInt = isNotInf; - ins->functionReturnReg->intValue = m_builder.CreateSelect(isInt, intValue, cast); + llvm::Value *cast = m_builder.CreateFPToSI(ins->getFunctionReturnReg()->value, m_builder.getInt64Ty()); + ins->getFunctionReturnReg()->isInt = isNotInf; + ins->getFunctionReturnReg()->intValue = m_builder.CreateSelect(isInt, intValue, cast); - return ins->next; + return ins->getNext(); } LLVMInstruction *Math::buildAbs(LLVMInstruction *ins) { assert(ins->args.size() == 1); - const auto &arg = ins->args[0]; + const auto &args = ins->getArgs(); + const auto &arg = args[0]; llvm::Function *fabsFunc = llvm::Intrinsic::getDeclaration(m_utils.module(), llvm::Intrinsic::fabs, m_builder.getDoubleTy()); llvm::Value *doubleValue = m_utils.removeNaN(m_utils.castValue(arg.second, arg.first)); - ins->functionReturnReg->value = m_builder.CreateCall(fabsFunc, doubleValue); + ins->getFunctionReturnReg()->value = m_builder.CreateCall(fabsFunc, doubleValue); llvm::Function *absFunc = llvm::Intrinsic::getDeclaration(m_utils.module(), llvm::Intrinsic::abs, m_builder.getInt64Ty()); llvm::Value *intValue = arg.second->intValue; - ins->functionReturnReg->isInt = arg.second->isInt; - ins->functionReturnReg->intValue = m_builder.CreateCall(absFunc, { intValue, m_builder.getInt1(false) }); + ins->getFunctionReturnReg()->isInt = arg.second->isInt; + ins->getFunctionReturnReg()->intValue = m_builder.CreateCall(absFunc, { intValue, m_builder.getInt1(false) }); - return ins->next; + return ins->getNext(); } LLVMInstruction *Math::buildFloor(LLVMInstruction *ins) { assert(ins->args.size() == 1); - const auto &arg = ins->args[0]; + const auto &args = ins->getArgs(); + const auto &arg = args[0]; // double: floor(doubleX) llvm::Function *floorFunc = llvm::Intrinsic::getDeclaration(m_utils.module(), llvm::Intrinsic::floor, m_builder.getDoubleTy()); llvm::Value *doubleValue = m_utils.removeNaN(m_utils.castValue(arg.second, arg.first)); - ins->functionReturnReg->value = m_builder.CreateCall(floorFunc, doubleValue); + ins->getFunctionReturnReg()->value = m_builder.CreateCall(floorFunc, doubleValue); // int: doubleX == inf || doubleX == -inf ? doubleX : intX llvm::Constant *posInf = llvm::ConstantFP::getInfinity(m_builder.getDoubleTy(), false); @@ -341,22 +352,23 @@ LLVMInstruction *Math::buildFloor(LLVMInstruction *ins) llvm::Value *isInt = arg.second->isInt; llvm::Value *intValue = arg.second->intValue; llvm::Value *isNotInf = m_builder.CreateAnd(m_builder.CreateFCmpONE(doubleValue, posInf), m_builder.CreateFCmpONE(doubleValue, negInf)); - llvm::Value *cast = m_builder.CreateFPToSI(ins->functionReturnReg->value, m_builder.getInt64Ty()); - ins->functionReturnReg->isInt = isNotInf; - ins->functionReturnReg->intValue = m_builder.CreateSelect(isInt, intValue, cast); + llvm::Value *cast = m_builder.CreateFPToSI(ins->getFunctionReturnReg()->value, m_builder.getInt64Ty()); + ins->getFunctionReturnReg()->isInt = isNotInf; + ins->getFunctionReturnReg()->intValue = m_builder.CreateSelect(isInt, intValue, cast); - return ins->next; + return ins->getNext(); } LLVMInstruction *Math::buildCeil(LLVMInstruction *ins) { assert(ins->args.size() == 1); - const auto &arg = ins->args[0]; + const auto &args = ins->getArgs(); + const auto &arg = args[0]; // double: ceil(doubleX) llvm::Function *ceilFunc = llvm::Intrinsic::getDeclaration(m_utils.module(), llvm::Intrinsic::ceil, m_builder.getDoubleTy()); llvm::Value *doubleValue = m_utils.removeNaN(m_utils.castValue(arg.second, arg.first)); - ins->functionReturnReg->value = m_builder.CreateCall(ceilFunc, doubleValue); + ins->getFunctionReturnReg()->value = m_builder.CreateCall(ceilFunc, doubleValue); // int: doubleX == inf || doubleX == -inf ? doubleX : intX llvm::Constant *posInf = llvm::ConstantFP::getInfinity(m_builder.getDoubleTy(), false); @@ -364,25 +376,27 @@ LLVMInstruction *Math::buildCeil(LLVMInstruction *ins) llvm::Value *isInt = arg.second->isInt; llvm::Value *intValue = arg.second->intValue; llvm::Value *isNotInf = m_builder.CreateAnd(m_builder.CreateFCmpONE(doubleValue, posInf), m_builder.CreateFCmpONE(doubleValue, negInf)); - llvm::Value *cast = m_builder.CreateFPToSI(ins->functionReturnReg->value, m_builder.getInt64Ty()); - ins->functionReturnReg->isInt = isNotInf; - ins->functionReturnReg->intValue = m_builder.CreateSelect(isInt, intValue, cast); + llvm::Value *cast = m_builder.CreateFPToSI(ins->getFunctionReturnReg()->value, m_builder.getInt64Ty()); + ins->getFunctionReturnReg()->isInt = isNotInf; + ins->getFunctionReturnReg()->intValue = m_builder.CreateSelect(isInt, intValue, cast); - return ins->next; + return ins->getNext(); } LLVMInstruction *Math::buildSqrt(LLVMInstruction *ins) { assert(ins->args.size() == 1); - const auto &arg = ins->args[0]; + const auto &args = ins->getArgs(); + const auto &arg = args[0]; + // sqrt(x) + 0.0 // This avoids negative zero llvm::Constant *zero = llvm::ConstantFP::get(m_utils.llvmCtx(), llvm::APFloat(0.0)); llvm::Function *sqrtFunc = llvm::Intrinsic::getDeclaration(m_utils.module(), llvm::Intrinsic::sqrt, m_builder.getDoubleTy()); llvm::Value *num = m_utils.removeNaN(m_utils.castValue(arg.second, arg.first)); - ins->functionReturnReg->value = m_builder.CreateFAdd(m_builder.CreateCall(sqrtFunc, num), zero); + ins->getFunctionReturnReg()->value = m_builder.CreateFAdd(m_builder.CreateCall(sqrtFunc, num), zero); - return ins->next; + return ins->getNext(); } LLVMInstruction *Math::buildSin(LLVMInstruction *ins) @@ -391,7 +405,9 @@ LLVMInstruction *Math::buildSin(LLVMInstruction *ins) llvm::Module *module = m_utils.module(); assert(ins->args.size() == 1); - const auto &arg = ins->args[0]; + const auto &args = ins->getArgs(); + const auto &arg = args[0]; + // round(sin(x * pi / 180.0) * 1e10) / 1e10 + 0.0 // +0.0 to avoid -0.0 llvm::Constant *zero = llvm::ConstantFP::get(llvmCtx, llvm::APFloat(0.0)); @@ -403,9 +419,9 @@ LLVMInstruction *Math::buildSin(LLVMInstruction *ins) llvm::Value *num = m_utils.removeNaN(m_utils.castValue(arg.second, arg.first)); llvm::Value *sinResult = m_builder.CreateCall(sinFunc, m_builder.CreateFDiv(m_builder.CreateFMul(num, pi), piDeg)); // sin(x * pi / 180) llvm::Value *rounded = m_builder.CreateCall(roundFunc, m_builder.CreateFMul(sinResult, factor)); // round(sin(x * 180) * 1e10) - ins->functionReturnReg->value = m_builder.CreateFAdd(m_builder.CreateFDiv(rounded, factor), zero); + ins->getFunctionReturnReg()->value = m_builder.CreateFAdd(m_builder.CreateFDiv(rounded, factor), zero); - return ins->next; + return ins->getNext(); } LLVMInstruction *Math::buildCos(LLVMInstruction *ins) @@ -414,7 +430,9 @@ LLVMInstruction *Math::buildCos(LLVMInstruction *ins) llvm::Module *module = m_utils.module(); assert(ins->args.size() == 1); - const auto &arg = ins->args[0]; + const auto &args = ins->getArgs(); + const auto &arg = args[0]; + // round(cos(x * pi / 180.0) * 1e10) / 1e10 llvm::Constant *pi = llvm::ConstantFP::get(llvmCtx, llvm::APFloat(std::acos(-1.0))); llvm::Constant *piDeg = llvm::ConstantFP::get(llvmCtx, llvm::APFloat(180.0)); @@ -424,9 +442,9 @@ LLVMInstruction *Math::buildCos(LLVMInstruction *ins) llvm::Value *num = m_utils.removeNaN(m_utils.castValue(arg.second, arg.first)); llvm::Value *cosResult = m_builder.CreateCall(cosFunc, m_builder.CreateFDiv(m_builder.CreateFMul(num, pi), piDeg)); // cos(x * pi / 180) llvm::Value *rounded = m_builder.CreateCall(roundFunc, m_builder.CreateFMul(cosResult, factor)); // round(cos(x * 180) * 1e10) - ins->functionReturnReg->value = m_builder.CreateFDiv(rounded, factor); + ins->getFunctionReturnReg()->value = m_builder.CreateFDiv(rounded, factor); - return ins->next; + return ins->getNext(); } LLVMInstruction *Math::buildTan(LLVMInstruction *ins) @@ -435,7 +453,9 @@ LLVMInstruction *Math::buildTan(LLVMInstruction *ins) llvm::Module *module = m_utils.module(); assert(ins->args.size() == 1); - const auto &arg = ins->args[0]; + const auto &args = ins->getArgs(); + const auto &arg = args[0]; + // ((mod = rem(x, 360.0)) == -270.0 || mod == 90.0) ? inf : ((mod == -90.0 || mod == 270.0) ? -inf : round(tan(x * pi / 180.0) * 1e10) / 1e10 + 0.0) // +0.0 to avoid -0.0 llvm::Constant *zero = llvm::ConstantFP::get(llvmCtx, llvm::APFloat(0.0)); @@ -461,9 +481,9 @@ LLVMInstruction *Math::buildTan(LLVMInstruction *ins) llvm::Value *rounded = m_builder.CreateCall(roundFunc, m_builder.CreateFMul(tanResult, factor)); // round(tan(x * 180) * 1e10) llvm::Value *result = m_builder.CreateFAdd(m_builder.CreateFDiv(rounded, factor), zero); // round(tan(x * pi / 180.0) * 1e10) / 1e10 + 0.0 llvm::Value *inner = m_builder.CreateSelect(m_builder.CreateOr(isUndefined3, isUndefined4), negInf, result); - ins->functionReturnReg->value = m_builder.CreateSelect(m_builder.CreateOr(isUndefined1, isUndefined2), posInf, inner); + ins->getFunctionReturnReg()->value = m_builder.CreateSelect(m_builder.CreateOr(isUndefined1, isUndefined2), posInf, inner); - return ins->next; + return ins->getNext(); } LLVMInstruction *Math::buildAsin(LLVMInstruction *ins) @@ -471,7 +491,9 @@ LLVMInstruction *Math::buildAsin(LLVMInstruction *ins) llvm::LLVMContext &llvmCtx = m_utils.llvmCtx(); assert(ins->args.size() == 1); - const auto &arg = ins->args[0]; + const auto &args = ins->getArgs(); + const auto &arg = args[0]; + // asin(x) * 180.0 / pi + 0.0 // +0.0 to avoid -0.0 llvm::Constant *zero = llvm::ConstantFP::get(llvmCtx, llvm::APFloat(0.0)); @@ -479,9 +501,9 @@ LLVMInstruction *Math::buildAsin(LLVMInstruction *ins) llvm::Constant *piDeg = llvm::ConstantFP::get(llvmCtx, llvm::APFloat(180.0)); llvm::Function *asinFunc = llvm::Intrinsic::getDeclaration(m_utils.module(), llvm::Intrinsic::asin, m_builder.getDoubleTy()); llvm::Value *num = m_utils.removeNaN(m_utils.castValue(arg.second, arg.first)); - ins->functionReturnReg->value = m_builder.CreateFAdd(m_builder.CreateFDiv(m_builder.CreateFMul(m_builder.CreateCall(asinFunc, num), piDeg), pi), zero); + ins->getFunctionReturnReg()->value = m_builder.CreateFAdd(m_builder.CreateFDiv(m_builder.CreateFMul(m_builder.CreateCall(asinFunc, num), piDeg), pi), zero); - return ins->next; + return ins->getNext(); } LLVMInstruction *Math::buildAcos(LLVMInstruction *ins) @@ -489,15 +511,17 @@ LLVMInstruction *Math::buildAcos(LLVMInstruction *ins) llvm::LLVMContext &llvmCtx = m_utils.llvmCtx(); assert(ins->args.size() == 1); - const auto &arg = ins->args[0]; + const auto &args = ins->getArgs(); + const auto &arg = args[0]; + // acos(x) * 180.0 / pi llvm::Constant *pi = llvm::ConstantFP::get(llvmCtx, llvm::APFloat(std::acos(-1.0))); llvm::Constant *piDeg = llvm::ConstantFP::get(llvmCtx, llvm::APFloat(180.0)); llvm::Function *acosFunc = llvm::Intrinsic::getDeclaration(m_utils.module(), llvm::Intrinsic::acos, m_builder.getDoubleTy()); llvm::Value *num = m_utils.removeNaN(m_utils.castValue(arg.second, arg.first)); - ins->functionReturnReg->value = m_builder.CreateFDiv(m_builder.CreateFMul(m_builder.CreateCall(acosFunc, num), piDeg), pi); + ins->getFunctionReturnReg()->value = m_builder.CreateFDiv(m_builder.CreateFMul(m_builder.CreateCall(acosFunc, num), piDeg), pi); - return ins->next; + return ins->getNext(); } LLVMInstruction *Math::buildAtan(LLVMInstruction *ins) @@ -505,61 +529,71 @@ LLVMInstruction *Math::buildAtan(LLVMInstruction *ins) llvm::LLVMContext &llvmCtx = m_utils.llvmCtx(); assert(ins->args.size() == 1); - const auto &arg = ins->args[0]; + const auto &args = ins->getArgs(); + const auto &arg = args[0]; + // atan(x) * 180.0 / pi llvm::Constant *pi = llvm::ConstantFP::get(llvmCtx, llvm::APFloat(std::acos(-1.0))); llvm::Constant *piDeg = llvm::ConstantFP::get(llvmCtx, llvm::APFloat(180.0)); llvm::Function *atanFunc = llvm::Intrinsic::getDeclaration(m_utils.module(), llvm::Intrinsic::atan, m_builder.getDoubleTy()); llvm::Value *num = m_utils.removeNaN(m_utils.castValue(arg.second, arg.first)); - ins->functionReturnReg->value = m_builder.CreateFDiv(m_builder.CreateFMul(m_builder.CreateCall(atanFunc, num), piDeg), pi); + ins->getFunctionReturnReg()->value = m_builder.CreateFDiv(m_builder.CreateFMul(m_builder.CreateCall(atanFunc, num), piDeg), pi); - return ins->next; + return ins->getNext(); } LLVMInstruction *Math::buildLn(LLVMInstruction *ins) { assert(ins->args.size() == 1); - const auto &arg = ins->args[0]; + const auto &args = ins->getArgs(); + const auto &arg = args[0]; + // log(x) llvm::Function *logFunc = llvm::Intrinsic::getDeclaration(m_utils.module(), llvm::Intrinsic::log, m_builder.getDoubleTy()); llvm::Value *num = m_utils.removeNaN(m_utils.castValue(arg.second, arg.first)); - ins->functionReturnReg->value = m_builder.CreateCall(logFunc, num); + ins->getFunctionReturnReg()->value = m_builder.CreateCall(logFunc, num); - return ins->next; + return ins->getNext(); } LLVMInstruction *Math::buildLog10(LLVMInstruction *ins) { assert(ins->args.size() == 1); - const auto &arg = ins->args[0]; + const auto &args = ins->getArgs(); + const auto &arg = args[0]; + // log10(x) llvm::Function *log10Func = llvm::Intrinsic::getDeclaration(m_utils.module(), llvm::Intrinsic::log10, m_builder.getDoubleTy()); llvm::Value *num = m_utils.removeNaN(m_utils.castValue(arg.second, arg.first)); - ins->functionReturnReg->value = m_builder.CreateCall(log10Func, num); + ins->getFunctionReturnReg()->value = m_builder.CreateCall(log10Func, num); - return ins->next; + return ins->getNext(); } LLVMInstruction *Math::buildExp(LLVMInstruction *ins) { assert(ins->args.size() == 1); - const auto &arg = ins->args[0]; + const auto &args = ins->getArgs(); + const auto &arg = args[0]; + // exp(x) llvm::Function *expFunc = llvm::Intrinsic::getDeclaration(m_utils.module(), llvm::Intrinsic::exp, m_builder.getDoubleTy()); llvm::Value *num = m_utils.removeNaN(m_utils.castValue(arg.second, arg.first)); - ins->functionReturnReg->value = m_builder.CreateCall(expFunc, num); + ins->getFunctionReturnReg()->value = m_builder.CreateCall(expFunc, num); - return ins->next; + return ins->getNext(); } LLVMInstruction *Math::buildExp10(LLVMInstruction *ins) { assert(ins->args.size() == 1); - const auto &arg = ins->args[0]; + const auto &args = ins->getArgs(); + const auto &arg = args[0]; + // exp10(x) llvm::Function *expFunc = llvm::Intrinsic::getDeclaration(m_utils.module(), llvm::Intrinsic::exp10, m_builder.getDoubleTy()); llvm::Value *num = m_utils.removeNaN(m_utils.castValue(arg.second, arg.first)); - ins->functionReturnReg->value = m_builder.CreateCall(expFunc, num); + ins->getFunctionReturnReg()->value = m_builder.CreateCall(expFunc, num); - return ins->next; + return ins->getNext(); } diff --git a/src/engine/internal/llvm/instructions/math.h b/src/engine/internal/codegen/llvm/instructions/math.h similarity index 100% rename from src/engine/internal/llvm/instructions/math.h rename to src/engine/internal/codegen/llvm/instructions/math.h diff --git a/src/engine/internal/llvm/instructions/procedures.cpp b/src/engine/internal/codegen/llvm/instructions/procedures.cpp similarity index 95% rename from src/engine/internal/llvm/instructions/procedures.cpp rename to src/engine/internal/codegen/llvm/instructions/procedures.cpp index cac59650a..d6d522834 100644 --- a/src/engine/internal/llvm/instructions/procedures.cpp +++ b/src/engine/internal/codegen/llvm/instructions/procedures.cpp @@ -59,7 +59,9 @@ LLVMInstruction *Procedures::buildCallProcedure(LLVMInstruction *ins) args.push_back(m_utils.procedurePrototype() ? m_utils.warpArg() : m_builder.getInt1(false)); // Add procedure args - for (const auto &arg : ins->args) { + const auto &insArgs = ins->getArgs(); + + for (const auto &arg : insArgs) { if (m_utils.isSingleType(arg.first)) args.push_back(m_utils.castValue(arg.second, arg.first)); else @@ -98,7 +100,7 @@ LLVMInstruction *Procedures::buildCallProcedure(LLVMInstruction *ins) m_utils.reloadVariables(); m_utils.reloadLists(); - return ins->next; + return ins->getNext(); } LLVMInstruction *Procedures::buildProcedureArg(LLVMInstruction *ins) @@ -107,7 +109,7 @@ LLVMInstruction *Procedures::buildProcedureArg(LLVMInstruction *ins) llvm::FunctionType *funcType = m_utils.scriptFunctionType(nullptr); int passArgCount = funcType->getNumParams(); llvm::Value *arg = m_utils.function()->getArg(passArgCount + 1 + ins->procedureArgIndex); // omit warp arg - ins->functionReturnReg->value = arg; + ins->getFunctionReturnReg()->value = arg; - return ins->next; + return ins->getNext(); } diff --git a/src/engine/internal/llvm/instructions/procedures.h b/src/engine/internal/codegen/llvm/instructions/procedures.h similarity index 100% rename from src/engine/internal/llvm/instructions/procedures.h rename to src/engine/internal/codegen/llvm/instructions/procedures.h diff --git a/src/engine/internal/llvm/instructions/processresult.h b/src/engine/internal/codegen/llvm/instructions/processresult.h similarity index 100% rename from src/engine/internal/llvm/instructions/processresult.h rename to src/engine/internal/codegen/llvm/instructions/processresult.h diff --git a/src/engine/internal/llvm/instructions/string.cpp b/src/engine/internal/codegen/llvm/instructions/string.cpp similarity index 89% rename from src/engine/internal/llvm/instructions/string.cpp rename to src/engine/internal/codegen/llvm/instructions/string.cpp index 77120976a..277b6468f 100644 --- a/src/engine/internal/llvm/instructions/string.cpp +++ b/src/engine/internal/codegen/llvm/instructions/string.cpp @@ -36,8 +36,9 @@ ProcessResult String::process(LLVMInstruction *ins) LLVMInstruction *String::buildStringConcat(LLVMInstruction *ins) { assert(ins->args.size() == 2); - const auto &arg1 = ins->args[0]; - const auto &arg2 = ins->args[1]; + const auto &args = ins->getArgs(); + const auto &arg1 = args[0]; + const auto &arg2 = args[1]; llvm::Value *str1 = m_utils.castValue(arg1.second, arg1.first); llvm::Value *str2 = m_utils.castValue(arg2.second, arg2.first); llvm::PointerType *charPointerType = m_builder.getInt16Ty()->getPointerTo(); @@ -73,16 +74,17 @@ LLVMInstruction *String::buildStringConcat(LLVMInstruction *ins) writePtr = m_builder.CreateGEP(m_builder.getInt16Ty(), writePtr, size1); m_builder.CreateCall(memcpyFunc, { writePtr, data2, m_builder.CreateMul(m_builder.CreateAdd(size2, m_builder.getInt64(1)), m_builder.getInt64(2)), m_builder.getInt1(false) }); - ins->functionReturnReg->value = result; + ins->getFunctionReturnReg()->value = result; - return ins->next; + return ins->getNext(); } LLVMInstruction *String::buildStringChar(LLVMInstruction *ins) { assert(ins->args.size() == 2); - const auto &arg1 = ins->args[0]; - const auto &arg2 = ins->args[1]; + const auto &args = ins->getArgs(); + const auto &arg1 = args[0]; + const auto &arg2 = args[1]; llvm::Value *str = m_utils.castValue(arg1.second, arg1.first); llvm::Value *index = m_utils.castValue(arg2.second, arg2.first, LLVMBuildUtils::NumberType::Int); llvm::PointerType *charPointerType = m_builder.getInt16Ty()->getPointerTo(); @@ -114,9 +116,9 @@ LLVMInstruction *String::buildStringChar(LLVMInstruction *ins) m_builder.CreateStore(m_builder.getInt16(0), char2); m_builder.CreateStore(m_builder.CreateSelect(inRange, m_builder.getInt64(1), m_builder.getInt64(0)), sizeField); - ins->functionReturnReg->value = result; + ins->getFunctionReturnReg()->value = result; - return ins->next; + return ins->getNext(); } LLVMInstruction *String::buildStringLength(LLVMInstruction *ins) @@ -124,13 +126,14 @@ LLVMInstruction *String::buildStringLength(LLVMInstruction *ins) llvm::StructType *stringPtrType = m_utils.compilerCtx()->stringPtrType(); assert(ins->args.size() == 1); - const auto &arg = ins->args[0]; + const auto &args = ins->getArgs(); + const auto &arg = args[0]; llvm::Value *str = m_utils.castValue(arg.second, arg.first); llvm::Value *sizeField = m_builder.CreateStructGEP(stringPtrType, str, 1); llvm::Value *size = m_builder.CreateLoad(m_builder.getInt64Ty(), sizeField); - ins->functionReturnReg->value = m_builder.CreateSIToFP(size, m_builder.getDoubleTy()); - ins->functionReturnReg->intValue = size; - ins->functionReturnReg->isInt = m_builder.getInt1(true); + ins->getFunctionReturnReg()->value = m_builder.CreateSIToFP(size, m_builder.getDoubleTy()); + ins->getFunctionReturnReg()->intValue = size; + ins->getFunctionReturnReg()->isInt = m_builder.getInt1(true); - return ins->next; + return ins->getNext(); } diff --git a/src/engine/internal/llvm/instructions/string.h b/src/engine/internal/codegen/llvm/instructions/string.h similarity index 100% rename from src/engine/internal/llvm/instructions/string.h rename to src/engine/internal/codegen/llvm/instructions/string.h diff --git a/src/engine/internal/llvm/instructions/variables.cpp b/src/engine/internal/codegen/llvm/instructions/variables.cpp similarity index 68% rename from src/engine/internal/llvm/instructions/variables.cpp rename to src/engine/internal/codegen/llvm/instructions/variables.cpp index 6eec5cf7c..04382ac3b 100644 --- a/src/engine/internal/llvm/instructions/variables.cpp +++ b/src/engine/internal/codegen/llvm/instructions/variables.cpp @@ -4,6 +4,7 @@ #include "../llvminstruction.h" #include "../llvmbuildutils.h" #include "../llvmconstantregister.h" +#include "../../optimizationutils.h" using namespace libscratchcpp; using namespace libscratchcpp::llvmins; @@ -51,45 +52,48 @@ LLVMInstruction *Variables::buildCreateLocalVariable(LLVMInstruction *ins) m_builder.CreateStore(m_builder.getInt1(false), info->isInt); m_builder.CreateStore(llvm::ConstantInt::get(m_builder.getInt64Ty(), 0, true), info->isInt); - LLVMConstantRegister null(ins->functionReturnReg->type(), Value()); - ins->functionReturnReg->value = m_utils.createValue(&null); - return ins->next; + LLVMConstantRegister null(ins->getFunctionReturnReg()->type(), Value()); + ins->getFunctionReturnReg()->value = m_utils.createValue(&null); + return ins->getNext(); } LLVMInstruction *Variables::buildWriteLocalVariable(LLVMInstruction *ins) { assert(ins->args.size() == 2); - const auto &arg1 = ins->args[0]; - const auto &arg2 = ins->args[1]; + const auto &args = ins->getArgs(); + const auto &arg1 = args[0]; + const auto &arg2 = args[1]; LLVMLocalVariableInfo *info = ins->localVarInfo; llvm::Value *typeVar = m_utils.addAlloca(m_builder.getInt32Ty()); m_builder.CreateStore(m_builder.getInt32(static_cast(m_utils.mapType(arg2.first))), typeVar); m_utils.createValueStore(arg1.second->value, typeVar, info->isInt, info->intValue, arg2.second, arg2.first, arg2.first); - return ins->next; + return ins->getNext(); } LLVMInstruction *Variables::buildReadLocalVariable(LLVMInstruction *ins) { assert(ins->args.size() == 1); - const auto &arg = ins->args[0]; + const auto &args = ins->getArgs(); + const auto &arg = args[0]; LLVMLocalVariableInfo *info = ins->localVarInfo; - ins->functionReturnReg->value = m_utils.castValue(arg.second, ins->functionReturnReg->type()); - ins->functionReturnReg->isInt = m_builder.CreateLoad(m_builder.getInt1Ty(), info->isInt); - ins->functionReturnReg->intValue = m_builder.CreateLoad(m_builder.getInt64Ty(), info->intValue); - return ins->next; + ins->getFunctionReturnReg()->value = m_utils.castValue(arg.second, ins->getFunctionReturnReg()->type()); + ins->getFunctionReturnReg()->isInt = m_builder.CreateLoad(m_builder.getInt1Ty(), info->isInt); + ins->getFunctionReturnReg()->intValue = m_builder.CreateLoad(m_builder.getInt64Ty(), info->intValue); + return ins->getNext(); } LLVMInstruction *Variables::buildWriteVariable(LLVMInstruction *ins) { assert(ins->args.size() == 1); - const auto &arg = ins->args[0]; - Compiler::StaticType argType = m_utils.optimizeRegisterType(arg.second); + const auto &args = ins->getArgs(); + const auto &arg = args[0]; + Compiler::StaticType argType = OptimizationUtils::optimizeValueType(m_utils.compilerCtx(), arg.second); LLVMVariablePtr &varPtr = m_utils.variablePtr(ins->targetVariable); m_utils.createValueStore(varPtr.stackPtr, m_utils.getValueTypePtr(varPtr.stackPtr), varPtr.isInt, varPtr.intValue, arg.second, ins->targetType, argType); m_builder.CreateStore(m_builder.getInt1(true), varPtr.changed); - return ins->next; + return ins->getNext(); } LLVMInstruction *Variables::buildReadVariable(LLVMInstruction *ins) @@ -97,8 +101,8 @@ LLVMInstruction *Variables::buildReadVariable(LLVMInstruction *ins) assert(ins->args.size() == 0); LLVMVariablePtr &varPtr = m_utils.variablePtr(ins->targetVariable); - ins->functionReturnReg->value = varPtr.stackPtr; - ins->functionReturnReg->isInt = m_builder.CreateLoad(m_builder.getInt1Ty(), varPtr.isInt); - ins->functionReturnReg->intValue = m_builder.CreateLoad(m_builder.getInt64Ty(), varPtr.intValue); - return ins->next; + ins->getFunctionReturnReg()->value = varPtr.stackPtr; + ins->getFunctionReturnReg()->isInt = m_builder.CreateLoad(m_builder.getInt1Ty(), varPtr.isInt); + ins->getFunctionReturnReg()->intValue = m_builder.CreateLoad(m_builder.getInt64Ty(), varPtr.intValue); + return ins->getNext(); } diff --git a/src/engine/internal/llvm/instructions/variables.h b/src/engine/internal/codegen/llvm/instructions/variables.h similarity index 100% rename from src/engine/internal/llvm/instructions/variables.h rename to src/engine/internal/codegen/llvm/instructions/variables.h diff --git a/src/engine/internal/llvm/llvmbuildutils.cpp b/src/engine/internal/codegen/llvm/llvmbuildutils.cpp similarity index 97% rename from src/engine/internal/llvm/llvmbuildutils.cpp rename to src/engine/internal/codegen/llvm/llvmbuildutils.cpp index 8dc05aaff..64a63b734 100644 --- a/src/engine/internal/llvm/llvmbuildutils.cpp +++ b/src/engine/internal/codegen/llvm/llvmbuildutils.cpp @@ -13,6 +13,7 @@ #include "llvmcompilercontext.h" #include "llvmregister.h" #include "llvminstruction.h" +#include "../optimizationutils.h" using namespace libscratchcpp; @@ -42,35 +43,6 @@ LLVMBuildUtils::LLVMBuildUtils(LLVMCompilerContext *ctx, llvm::IRBuilder<> &buil initTypes(); createVariableMap(); createListMap(); - - // Find unsafe numeric string constants in costume and sound names - if (m_target) { - IEngine *engine = m_target->engine(); - - if (engine) { - auto checkConstant = [this](const std::string &str) { - Value stringValue(str); - Value numberValue(stringValue.toDouble()); - - if (stringValue.isValidNumber() && str == numberValue.toString()) - m_unsafeConstants.insert(str); - }; - - const auto &targets = engine->targets(); - bool found = false; - - for (const auto &target : targets) { - const auto &costumes = target->costumes(); - const auto &sounds = target->sounds(); - - for (const auto &costume : costumes) - checkConstant(costume->name()); - - for (const auto &sound : sounds) - checkConstant(sound->name()); - } - } - } } void LLVMBuildUtils::init(llvm::Function *function, BlockPrototype *procedurePrototype, bool warp, const std::vector> ®s) @@ -97,7 +69,8 @@ void LLVMBuildUtils::init(llvm::Function *function, BlockPrototype *procedurePro // Init registers for (auto reg : regs) { #ifdef LLVM_INTEGER_SUPPORT - bool isIntConst = (reg->isConst() && optimizeRegisterType(reg.get()) == Compiler::StaticType::Number && reg->constValue().toDouble() == std::floor(reg->constValue().toDouble())); + bool isIntConst = + (reg->isConst() && OptimizationUtils::optimizeValueType(m_ctx, reg.get()) == Compiler::StaticType::Number && reg->constValue().toDouble() == std::floor(reg->constValue().toDouble())); reg->isInt = m_builder.getInt1(isIntConst); #else reg->isInt = m_builder.getInt1(false); @@ -205,7 +178,7 @@ void LLVMBuildUtils::end(LLVMInstruction *lastInstruction, LLVMRegister *lastCon case Compiler::CodeType::Reporter: { // Use last instruction return value (or last constant) and create a ValueData instance assert(lastInstruction || lastConstant); - LLVMRegister *ret = lastInstruction ? lastInstruction->functionReturnReg : lastConstant; + LLVMRegister *ret = lastInstruction ? lastInstruction->getFunctionReturnReg() : lastConstant; llvm::Value *copy = createNewValue(ret); m_builder.CreateRet(m_builder.CreateLoad(m_valueDataType, copy)); break; @@ -216,7 +189,7 @@ void LLVMBuildUtils::end(LLVMInstruction *lastInstruction, LLVMRegister *lastCon assert(lastInstruction || lastConstant); if (lastInstruction) - m_builder.CreateRet(castValue(lastInstruction->functionReturnReg, Compiler::StaticType::Bool)); + m_builder.CreateRet(castValue(lastInstruction->getFunctionReturnReg(), Compiler::StaticType::Bool)); else m_builder.CreateRet(castValue(lastConstant, Compiler::StaticType::Bool)); break; @@ -511,24 +484,6 @@ std::vector &LLVMBuildUtils::loops() return m_loops; } -Compiler::StaticType LLVMBuildUtils::optimizeRegisterType(const LLVMRegister *reg) const -{ - Compiler::StaticType ret = reg->type(); - - // Optimize string constants that represent numbers - if (reg->isConst() && reg->type() == Compiler::StaticType::String) { - const Value &value = reg->constValue(); - Value numberValue(value.toDouble()); - std::string str = value.toString(); - - // Apply this optimization only if the number matches the string and the constant is safe - if (value.isValidNumber() && numberValue.toString() == str && m_unsafeConstants.find(str) == m_unsafeConstants.cend()) - ret = Compiler::StaticType::Number; - } - - return ret; -} - Compiler::StaticType LLVMBuildUtils::mapType(ValueType type) { assert(TYPE_MAP.find(type) != TYPE_MAP.cend()); diff --git a/src/engine/internal/llvm/llvmbuildutils.h b/src/engine/internal/codegen/llvm/llvmbuildutils.h similarity index 98% rename from src/engine/internal/llvm/llvmbuildutils.h rename to src/engine/internal/codegen/llvm/llvmbuildutils.h index 2a7938081..2b2546cee 100644 --- a/src/engine/internal/llvm/llvmbuildutils.h +++ b/src/engine/internal/codegen/llvm/llvmbuildutils.h @@ -87,8 +87,6 @@ class LIBSCRATCHCPP_TEST_EXPORT LLVMBuildUtils std::vector &ifStatements(); std::vector &loops(); - Compiler::StaticType optimizeRegisterType(const LLVMRegister *reg) const; - static Compiler::StaticType mapType(ValueType type); static ValueType mapType(Compiler::StaticType type); static bool isSingleType(Compiler::StaticType type); @@ -180,8 +178,6 @@ class LIBSCRATCHCPP_TEST_EXPORT LLVMBuildUtils bool m_warp = false; Compiler::CodeType m_codeType = Compiler::CodeType::Script; - std::unordered_set m_unsafeConstants; - llvm::Value *m_executionContextPtr = nullptr; llvm::Value *m_targetPtr = nullptr; llvm::Value *m_targetVariables = nullptr; diff --git a/src/engine/internal/llvm/llvmcodebuilder.cpp b/src/engine/internal/codegen/llvm/llvmcodebuilder.cpp similarity index 99% rename from src/engine/internal/llvm/llvmcodebuilder.cpp rename to src/engine/internal/codegen/llvm/llvmcodebuilder.cpp index 7621e1e0d..3570020e6 100644 --- a/src/engine/internal/llvm/llvmcodebuilder.cpp +++ b/src/engine/internal/codegen/llvm/llvmcodebuilder.cpp @@ -28,7 +28,7 @@ LLVMCodeBuilder::LLVMCodeBuilder(LLVMCompilerContext *ctx, BlockPrototype *proce m_module(ctx->module()), m_builder(m_llvmCtx), m_utils(ctx, m_builder, codeType), - m_codeAnalyzer(m_utils), + m_codeAnalyzer(ctx), m_procedurePrototype(procedurePrototype), m_defaultWarp(procedurePrototype ? procedurePrototype->warp() : false), m_warp(m_defaultWarp), @@ -114,7 +114,7 @@ CompilerValue *LLVMCodeBuilder::addFunctionCall(const std::string &functionName, if (m_utils.isSingleType(returnType)) reg->isRawValue = true; - ins->functionReturnReg = reg.get(); + ins->setFunctionReturnReg(reg.get()); return addReg(reg, ins); } @@ -167,7 +167,7 @@ CompilerValue *LLVMCodeBuilder::addLocalVariableValue(CompilerLocalVariable *var auto ret = std::make_shared(variable->type()); ret->isRawValue = false; - ins->functionReturnReg = ret.get(); + ins->setFunctionReturnReg(ret.get()); m_instructions.addInstruction(ins); return addReg(ret, ins); @@ -181,7 +181,7 @@ CompilerValue *LLVMCodeBuilder::addVariableValue(Variable *variable) auto ret = std::make_shared(Compiler::StaticType::Unknown); ret->isRawValue = false; - ins->functionReturnReg = ret.get(); + ins->setFunctionReturnReg(ret.get()); m_instructions.addInstruction(ins); return addReg(ret, ins); @@ -206,7 +206,7 @@ CompilerValue *LLVMCodeBuilder::addListItem(List *list, CompilerValue *index) auto ret = std::make_shared(Compiler::StaticType::Unknown); ret->isRawValue = false; - ins->functionReturnReg = ret.get(); + ins->setFunctionReturnReg(ret.get()); m_instructions.addInstruction(ins); return addReg(ret, ins); @@ -259,7 +259,7 @@ CompilerValue *LLVMCodeBuilder::addProcedureArgument(const std::string &name) auto ins = std::make_shared(LLVMInstruction::Type::ProcedureArg, m_loopCondition); auto ret = std::make_shared(type); ret->isRawValue = (type != Compiler::StaticType::Unknown); - ins->functionReturnReg = ret.get(); + ins->setFunctionReturnReg(ret.get()); ins->procedureArgIndex = index; m_instructions.addInstruction(ins); diff --git a/src/engine/internal/llvm/llvmcodebuilder.h b/src/engine/internal/codegen/llvm/llvmcodebuilder.h similarity index 99% rename from src/engine/internal/llvm/llvmcodebuilder.h rename to src/engine/internal/codegen/llvm/llvmcodebuilder.h index 188fc3dc5..123eefd6f 100644 --- a/src/engine/internal/llvm/llvmcodebuilder.h +++ b/src/engine/internal/codegen/llvm/llvmcodebuilder.h @@ -12,7 +12,6 @@ #include "../icodebuilder.h" #include "test_export.h" #include "llvmbuildutils.h" -#include "llvmcodeanalyzer.h" #include "llvminstruction.h" #include "llvminstructionlist.h" #include "llvmcoroutine.h" @@ -20,6 +19,7 @@ #include "llvmlistptr.h" #include "llvmfunctions.h" #include "instructions/instructionbuilder.h" +#include "../codeanalyzer.h" namespace libscratchcpp { @@ -144,7 +144,7 @@ class LIBSCRATCHCPP_TEST_EXPORT LLVMCodeBuilder : public ICodeBuilder llvm::IRBuilder<> m_builder; llvm::Function *m_function = nullptr; LLVMBuildUtils m_utils; - LLVMCodeAnalyzer m_codeAnalyzer; + CodeAnalyzer m_codeAnalyzer; llvm::StructType *m_valueDataType = nullptr; llvm::StructType *m_stringPtrType = nullptr; diff --git a/src/engine/internal/llvm/llvmcompilercontext.cpp b/src/engine/internal/codegen/llvm/llvmcompilercontext.cpp similarity index 98% rename from src/engine/internal/llvm/llvmcompilercontext.cpp rename to src/engine/internal/codegen/llvm/llvmcompilercontext.cpp index 3e65c9d39..ea319c7aa 100644 --- a/src/engine/internal/llvm/llvmcompilercontext.cpp +++ b/src/engine/internal/codegen/llvm/llvmcompilercontext.cpp @@ -17,8 +17,8 @@ using namespace libscratchcpp; -LLVMCompilerContext::LLVMCompilerContext(IEngine *engine, Target *target) : - CompilerContext(engine, target), +LLVMCompilerContext::LLVMCompilerContext(IEngine *engine, Target *target, const std::unordered_set &unsafeConstants) : + CompilerContext(engine, target, unsafeConstants), m_llvmCtx(std::make_unique()), m_module(std::make_unique(target ? target->name() : "", *m_llvmCtx)), m_llvmCtxPtr(m_llvmCtx.get()), diff --git a/src/engine/internal/llvm/llvmcompilercontext.h b/src/engine/internal/codegen/llvm/llvmcompilercontext.h similarity index 96% rename from src/engine/internal/llvm/llvmcompilercontext.h rename to src/engine/internal/codegen/llvm/llvmcompilercontext.h index 9cc8462b7..19de9fbf9 100644 --- a/src/engine/internal/llvm/llvmcompilercontext.h +++ b/src/engine/internal/codegen/llvm/llvmcompilercontext.h @@ -28,7 +28,7 @@ using function_id_t = unsigned int; class LIBSCRATCHCPP_TEST_EXPORT LLVMCompilerContext : public CompilerContext { public: - LLVMCompilerContext(IEngine *engine, Target *target); + LLVMCompilerContext(IEngine *engine, Target *target, const std::unordered_set &unsafeConstants = {}); void preoptimize() override; diff --git a/src/engine/internal/llvm/llvmconstantregister.h b/src/engine/internal/codegen/llvm/llvmconstantregister.h similarity index 100% rename from src/engine/internal/llvm/llvmconstantregister.h rename to src/engine/internal/codegen/llvm/llvmconstantregister.h diff --git a/src/engine/internal/llvm/llvmcoroutine.cpp b/src/engine/internal/codegen/llvm/llvmcoroutine.cpp similarity index 100% rename from src/engine/internal/llvm/llvmcoroutine.cpp rename to src/engine/internal/codegen/llvm/llvmcoroutine.cpp diff --git a/src/engine/internal/llvm/llvmcoroutine.h b/src/engine/internal/codegen/llvm/llvmcoroutine.h similarity index 100% rename from src/engine/internal/llvm/llvmcoroutine.h rename to src/engine/internal/codegen/llvm/llvmcoroutine.h diff --git a/src/engine/internal/llvm/llvmexecutablecode.cpp b/src/engine/internal/codegen/llvm/llvmexecutablecode.cpp similarity index 100% rename from src/engine/internal/llvm/llvmexecutablecode.cpp rename to src/engine/internal/codegen/llvm/llvmexecutablecode.cpp diff --git a/src/engine/internal/llvm/llvmexecutablecode.h b/src/engine/internal/codegen/llvm/llvmexecutablecode.h similarity index 100% rename from src/engine/internal/llvm/llvmexecutablecode.h rename to src/engine/internal/codegen/llvm/llvmexecutablecode.h diff --git a/src/engine/internal/llvm/llvmexecutioncontext.cpp b/src/engine/internal/codegen/llvm/llvmexecutioncontext.cpp similarity index 100% rename from src/engine/internal/llvm/llvmexecutioncontext.cpp rename to src/engine/internal/codegen/llvm/llvmexecutioncontext.cpp diff --git a/src/engine/internal/llvm/llvmexecutioncontext.h b/src/engine/internal/codegen/llvm/llvmexecutioncontext.h similarity index 100% rename from src/engine/internal/llvm/llvmexecutioncontext.h rename to src/engine/internal/codegen/llvm/llvmexecutioncontext.h diff --git a/src/engine/internal/llvm/llvmfunctions.cpp b/src/engine/internal/codegen/llvm/llvmfunctions.cpp similarity index 100% rename from src/engine/internal/llvm/llvmfunctions.cpp rename to src/engine/internal/codegen/llvm/llvmfunctions.cpp diff --git a/src/engine/internal/llvm/llvmfunctions.h b/src/engine/internal/codegen/llvm/llvmfunctions.h similarity index 100% rename from src/engine/internal/llvm/llvmfunctions.h rename to src/engine/internal/codegen/llvm/llvmfunctions.h diff --git a/src/engine/internal/llvm/llvmifstatement.h b/src/engine/internal/codegen/llvm/llvmifstatement.h similarity index 100% rename from src/engine/internal/llvm/llvmifstatement.h rename to src/engine/internal/codegen/llvm/llvmifstatement.h diff --git a/src/engine/internal/codegen/llvm/llvminstruction.h b/src/engine/internal/codegen/llvm/llvminstruction.h new file mode 100644 index 000000000..3a0bb65bc --- /dev/null +++ b/src/engine/internal/codegen/llvm/llvminstruction.h @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include + +#include "../instruction.h" +#include "llvmregister.h" + +namespace libscratchcpp +{ + +class LLVMLocalVariableInfo; +class BlockPrototype; + +struct LLVMInstruction : Instruction +{ + using Type = Instruction::Type; + using Instruction::Instruction; // inherit constructor + + LLVMLocalVariableInfo *localVarInfo = nullptr; // for local variables +}; + +} // namespace libscratchcpp diff --git a/src/engine/internal/codegen/llvm/llvminstructionlist.h b/src/engine/internal/codegen/llvm/llvminstructionlist.h new file mode 100644 index 000000000..cf988aaf9 --- /dev/null +++ b/src/engine/internal/codegen/llvm/llvminstructionlist.h @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "../instructionlist.h" + +namespace libscratchcpp +{ + +struct LLVMInstruction; + +using LLVMInstructionList = InstructionList; + +} // namespace libscratchcpp diff --git a/src/engine/internal/llvm/llvmlistptr.h b/src/engine/internal/codegen/llvm/llvmlistptr.h similarity index 100% rename from src/engine/internal/llvm/llvmlistptr.h rename to src/engine/internal/codegen/llvm/llvmlistptr.h diff --git a/src/engine/internal/llvm/llvmlocalvariableinfo.h b/src/engine/internal/codegen/llvm/llvmlocalvariableinfo.h similarity index 100% rename from src/engine/internal/llvm/llvmlocalvariableinfo.h rename to src/engine/internal/codegen/llvm/llvmlocalvariableinfo.h diff --git a/src/engine/internal/llvm/llvmloop.h b/src/engine/internal/codegen/llvm/llvmloop.h similarity index 100% rename from src/engine/internal/llvm/llvmloop.h rename to src/engine/internal/codegen/llvm/llvmloop.h diff --git a/src/engine/internal/llvm/llvmregister.h b/src/engine/internal/codegen/llvm/llvmregister.h similarity index 100% rename from src/engine/internal/llvm/llvmregister.h rename to src/engine/internal/codegen/llvm/llvmregister.h diff --git a/src/engine/internal/llvm/llvmtypes.cpp b/src/engine/internal/codegen/llvm/llvmtypes.cpp similarity index 100% rename from src/engine/internal/llvm/llvmtypes.cpp rename to src/engine/internal/codegen/llvm/llvmtypes.cpp diff --git a/src/engine/internal/llvm/llvmtypes.h b/src/engine/internal/codegen/llvm/llvmtypes.h similarity index 100% rename from src/engine/internal/llvm/llvmtypes.h rename to src/engine/internal/codegen/llvm/llvmtypes.h diff --git a/src/engine/internal/llvm/llvmvariableptr.h b/src/engine/internal/codegen/llvm/llvmvariableptr.h similarity index 100% rename from src/engine/internal/llvm/llvmvariableptr.h rename to src/engine/internal/codegen/llvm/llvmvariableptr.h diff --git a/src/engine/internal/codegen/optimizationutils.cpp b/src/engine/internal/codegen/optimizationutils.cpp new file mode 100644 index 000000000..64ccf7077 --- /dev/null +++ b/src/engine/internal/codegen/optimizationutils.cpp @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: Apache-2.0 + +#include +#include +#include + +#include "optimizationutils.h" + +using namespace libscratchcpp; + +Compiler::StaticType OptimizationUtils::optimizeValueType(CompilerContext *ctx, const CompilerValue *value) +{ + Compiler::StaticType ret = value->type(); + + // Optimize string constants that represent numbers + if (value->isConst() && value->type() == Compiler::StaticType::String) { + const Value &constValue = dynamic_cast(value)->value(); + Value numberValue(constValue.toDouble()); + std::string str = constValue.toString(); + + // Apply this optimization only if the number matches the string and the constant is safe + const auto &unsafeConstants = ctx->unsafeConstants(); + + if (constValue.isValidNumber() && numberValue.toString() == str && unsafeConstants.find(str) == unsafeConstants.cend()) + ret = Compiler::StaticType::Number; + } + + return ret; +} diff --git a/src/engine/internal/codegen/optimizationutils.h b/src/engine/internal/codegen/optimizationutils.h new file mode 100644 index 000000000..ec0755033 --- /dev/null +++ b/src/engine/internal/codegen/optimizationutils.h @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include + +namespace libscratchcpp +{ + +class OptimizationUtils +{ + public: + static Compiler::StaticType optimizeValueType(CompilerContext *ctx, const CompilerValue *value); +}; + +} // namespace libscratchcpp diff --git a/src/engine/internal/engine.cpp b/src/engine/internal/engine.cpp index 170013de1..9ec5a0ddb 100644 --- a/src/engine/internal/engine.cpp +++ b/src/engine/internal/engine.cpp @@ -85,6 +85,7 @@ void Engine::clear() m_broadcastMap.clear(); m_broadcastSenders.clear(); m_sortedDrawables.clear(); + m_unsafeConstants.clear(); m_threads.clear(); m_threadsToStop.clear(); m_scripts.clear(); @@ -245,10 +246,34 @@ void Engine::compile() // Resolve entities by ID resolveIds(); + // Find unsafe constants (string numerals) in costume and sound names + m_unsafeConstants.clear(); + + auto checkConstant = [this](const std::string &str) { + Value stringValue(str); + Value numberValue(stringValue.toDouble()); + + if (stringValue.isValidNumber() && str == numberValue.toString()) + m_unsafeConstants.insert(str); + }; + + bool found = false; + + for (const auto &target : m_targets) { + const auto &costumes = target->costumes(); + const auto &sounds = target->sounds(); + + for (const auto &costume : costumes) + checkConstant(costume->name()); + + for (const auto &sound : sounds) + checkConstant(sound->name()); + } + // Compile scripts for (auto target : m_targets) { std::cout << "Compiling scripts in target " << target->name() << "..." << std::endl; - auto ctx = Compiler::createContext(this, target.get()); + auto ctx = Compiler::createContext(this, target.get(), m_unsafeConstants); m_compilerContexts[target.get()] = ctx; Compiler compiler(ctx.get()); const auto &blocks = target->blocks(); @@ -1789,7 +1814,7 @@ void Engine::compileMonitor(std::shared_ptr monitor) auto ext = blockExtension(block->opcode()); if (ext) { - auto ctx = Compiler::createContext(this, target); + auto ctx = Compiler::createContext(this, target, m_unsafeConstants); Compiler compiler(ctx.get()); MonitorNameFunc nameFunc = resolveMonitorNameFunc(ext, block->opcode()); diff --git a/src/engine/internal/engine.h b/src/engine/internal/engine.h index 2508a4677..8d10ba7a4 100644 --- a/src/engine/internal/engine.h +++ b/src/engine/internal/engine.h @@ -241,7 +241,8 @@ class LIBSCRATCHCPP_TEST_EXPORT Engine : public IEngine std::unordered_map m_broadcastSenders; // used for resolving broadcast promises std::vector> m_monitors; std::vector m_extensions; - std::vector m_sortedDrawables; // sorted by layer (reverse order of execution) + std::vector m_sortedDrawables; // sorted by layer (reverse order of execution) + std::unordered_set m_unsafeConstants; // string numerals which shouldn't be optimized std::vector> m_threads; std::vector> m_threadsToStop; std::shared_ptr m_activeThread; diff --git a/src/engine/internal/llvm/llvmcodeanalyzer.h b/src/engine/internal/llvm/llvmcodeanalyzer.h deleted file mode 100644 index 87668c3b8..000000000 --- a/src/engine/internal/llvm/llvmcodeanalyzer.h +++ /dev/null @@ -1,64 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -#pragma once - -#include -#include - -#include "test_export.h" - -namespace libscratchcpp -{ - -class LLVMBuildUtils; -class LLVMInstructionList; -class LLVMInstruction; - -class LIBSCRATCHCPP_TEST_EXPORT LLVMCodeAnalyzer -{ - public: - LLVMCodeAnalyzer(const LLVMBuildUtils &utils); - LLVMCodeAnalyzer(const LLVMCodeAnalyzer &) = delete; - - void analyzeScript(const LLVMInstructionList &script) const; - - private: - struct Branch - { - LLVMInstruction *start = nullptr; - bool typeChanges = false; - std::unordered_map variableTypes; - std::unordered_map listTypes; - - std::unique_ptr elseBranch; - bool isAtElse = false; // whether we're currently processing the else branch - }; - - void updateVariableType(Branch *branch, LLVMInstruction *ins, std::unordered_set &typeAssignedInstructions, bool isWrite) const; - void updateListType(Branch *branch, LLVMInstruction *ins, std::unordered_set &typeAssignedInstructions, bool isWrite) const; - - void mergeVariableTypes(Branch *branch, Branch *previousBranch) const; - void overrideVariableTypes(Branch *branch, Branch *previousBranch) const; - void mergeListTypes(Branch *branch, Branch *previousBranch, bool firstUnknown) const; - - bool isLoopStart(const LLVMInstruction *ins) const; - bool isLoopEnd(const LLVMInstruction *ins) const; - bool isIfStart(const LLVMInstruction *ins) const; - bool isElse(const LLVMInstruction *ins) const; - bool isIfEnd(const LLVMInstruction *ins) const; - - bool isVariableRead(const LLVMInstruction *ins) const; - bool isVariableWrite(const LLVMInstruction *ins) const; - - bool isListRead(const LLVMInstruction *ins) const; - bool isListWrite(const LLVMInstruction *ins) const; - bool isListClear(const LLVMInstruction *ins) const; - - bool isProcedureCall(const LLVMInstruction *ins) const; - - Compiler::StaticType writeType(LLVMInstruction *ins) const; - - const LLVMBuildUtils &m_utils; -}; - -} // namespace libscratchcpp diff --git a/src/engine/internal/llvm/llvminstructionlist.h b/src/engine/internal/llvm/llvminstructionlist.h deleted file mode 100644 index 916ba81f5..000000000 --- a/src/engine/internal/llvm/llvminstructionlist.h +++ /dev/null @@ -1,36 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -#pragma once - -#include -#include - -#include "test_export.h" - -namespace libscratchcpp -{ - -struct LLVMInstruction; - -class LIBSCRATCHCPP_TEST_EXPORT LLVMInstructionList -{ - public: - LLVMInstructionList() = default; - LLVMInstructionList(const LLVMInstructionList &) = delete; - - LLVMInstruction *first() const; - LLVMInstruction *last() const; - - bool empty() const { return !first(); } - - bool containsInstruction(LLVMInstruction *ins) const; - bool containsInstruction(std::function func) const; - - void addInstruction(std::shared_ptr ins); - - private: - std::shared_ptr m_first; - std::shared_ptr m_last; -}; - -} // namespace libscratchcpp diff --git a/test/codegen/CMakeLists.txt b/test/codegen/CMakeLists.txt index 0037124b2..cd67920ff 100644 --- a/test/codegen/CMakeLists.txt +++ b/test/codegen/CMakeLists.txt @@ -19,6 +19,7 @@ add_executable( codebuilder_test.cpp codegentestutils.cpp codegentestutils.h + instructionlist_test.cpp operators/equal_comparison_test.cpp operators/greater_than_test.cpp operators/less_than_test.cpp diff --git a/test/codegen/codegentestutils.cpp b/test/codegen/codegentestutils.cpp index 6311f5fb1..b5f47c8ce 100644 --- a/test/codegen/codegentestutils.cpp +++ b/test/codegen/codegentestutils.cpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include "codegentestutils.h" #include "testfunctions.h" diff --git a/test/codegen/codegentestutils.h b/test/codegen/codegentestutils.h index 9fb9598a8..158f93f92 100644 --- a/test/codegen/codegentestutils.h +++ b/test/codegen/codegentestutils.h @@ -5,7 +5,7 @@ #include #include -#include +#include #define ASSERT_NUM_OP1(utils, type, rawArg, v) \ Value result = utils.getOpResult(type, rawArg, v); \ diff --git a/test/codegen/instructionlist_test.cpp b/test/codegen/instructionlist_test.cpp new file mode 100644 index 000000000..837bfcd3e --- /dev/null +++ b/test/codegen/instructionlist_test.cpp @@ -0,0 +1,372 @@ +#include +#include +#include +#include + +using namespace libscratchcpp; + +struct TInstruction : public Instruction +{ + using Type = Instruction::Type; + using Instruction::Instruction; // inherit constructor +}; + +TEST(InstructionListTest, EmptyList_First) +{ + InstructionList list; + ASSERT_EQ(list.first(), nullptr); +} + +TEST(InstructionListTest, EmptyList_Last) +{ + InstructionList list; + ASSERT_EQ(list.last(), nullptr); +} + +TEST(InstructionListTest, EmptyList_Empty) +{ + InstructionList list; + ASSERT_TRUE(list.empty()); +} + +TEST(InstructionListTest, EmptyList_ContainsNull) +{ + InstructionList list; + ASSERT_FALSE(list.containsInstruction(nullptr)); +} + +TEST(InstructionListTest, EmptyList_ContainsFunc) +{ + InstructionList list; + auto ins = std::make_shared(TInstruction::Type::FunctionCall, false); + ASSERT_FALSE(list.containsInstruction([](const TInstruction *) { return true; })); +} + +TEST(InstructionListTest, EmptyList_Contains) +{ + InstructionList list; + auto ins = std::make_shared(TInstruction::Type::FunctionCall, false); + ASSERT_FALSE(list.containsInstruction(ins.get())); +} + +TEST(InstructionListTest, SingleInstructionList_ContainsExistent) +{ + InstructionList list; + + auto ins1 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins1); + + ASSERT_TRUE(list.containsInstruction(ins1.get())); +} + +TEST(InstructionListTest, SingleInstructionList_ContainsNonExistent) +{ + InstructionList list; + + auto ins1 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins1); + + auto ins = std::make_shared(TInstruction::Type::FunctionCall, false); + ASSERT_FALSE(list.containsInstruction(ins.get())); +} + +TEST(InstructionListTest, SingleInstructionList_ContainsNull) +{ + InstructionList list; + + auto ins1 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins1); + + ASSERT_FALSE(list.containsInstruction(nullptr)); +} + +TEST(InstructionListTest, SingleInstructionList_ContainsFuncTrue) +{ + InstructionList list; + + auto ins1 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins1); + + ASSERT_TRUE(list.containsInstruction([](const TInstruction *) { return true; })); +} + +TEST(InstructionListTest, SingleInstructionList_ContainsFuncFalse) +{ + InstructionList list; + + auto ins1 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins1); + + auto ins = std::make_shared(TInstruction::Type::FunctionCall, false); + ASSERT_FALSE(list.containsInstruction([](const TInstruction *) { return false; })); +} + +TEST(InstructionListTest, MultipleInstructionList_ContainsExistent) +{ + InstructionList list; + + auto ins1 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins1); + + auto ins2 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins2); + + auto ins3 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins3); + + ASSERT_TRUE(list.containsInstruction(ins2.get())); +} + +TEST(InstructionListTest, MultipleInstructionList_ContainsNonExistent) +{ + InstructionList list; + + auto ins1 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins1); + + auto ins2 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins2); + + auto ins3 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins3); + + auto ins = std::make_shared(TInstruction::Type::FunctionCall, false); + ASSERT_FALSE(list.containsInstruction(ins.get())); +} + +TEST(InstructionListTest, MultipleInstructionList_ContainsNull) +{ + InstructionList list; + + auto ins1 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins1); + + auto ins2 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins2); + + auto ins3 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins3); + + ASSERT_FALSE(list.containsInstruction(nullptr)); +} + +TEST(InstructionListTest, MultipleInstructionList_ContainsExistentFunc) +{ + InstructionList list; + + auto ins1 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins1); + + auto ins2 = std::make_shared(TInstruction::Type::Stop, false); + list.addInstruction(ins2); + + auto ins3 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins3); + + ASSERT_TRUE(list.containsInstruction([](const TInstruction *ins) { return ins->type == TInstruction::Type::Stop; })); +} + +TEST(InstructionListTest, MultipleInstructionList_ContainsNonExistentFunc) +{ + InstructionList list; + + auto ins1 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins1); + + auto ins2 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins2); + + auto ins3 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins3); + + ASSERT_FALSE(list.containsInstruction([](const TInstruction *ins) { return ins->type == TInstruction::Type::Stop; })); +} + +TEST(InstructionListTest, AddSingleInstruction_First) +{ + InstructionList list; + auto ins = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins); + + ASSERT_EQ(list.first(), ins.get()); +} + +TEST(InstructionListTest, AddSingleInstruction_Last) +{ + InstructionList list; + auto ins = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins); + + ASSERT_EQ(list.last(), ins.get()); +} + +TEST(InstructionListTest, AddSingleInstruction_Empty) +{ + InstructionList list; + auto ins = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins); + + ASSERT_FALSE(list.empty()); +} + +TEST(InstructionListTest, AddSingleInstruction_Previous) +{ + InstructionList list; + auto ins = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins); + + ASSERT_EQ(ins->previous, nullptr); +} + +TEST(InstructionListTest, AddSingleInstruction_Next) +{ + InstructionList list; + auto ins = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins); + + ASSERT_EQ(ins->next, nullptr); +} + +TEST(InstructionListTest, AddMultipleInstructions_First) +{ + InstructionList list; + + auto ins1 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins1); + + auto ins2 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins2); + + auto ins3 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins3); + + ASSERT_EQ(list.first(), ins1.get()); +} + +TEST(InstructionListTest, AddMultipleInstructions_Last) +{ + InstructionList list; + + auto ins1 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins1); + + auto ins2 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins2); + + auto ins3 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins3); + + ASSERT_EQ(list.last(), ins3.get()); +} + +TEST(InstructionListTest, AddMultipleInstructions_Empty) +{ + InstructionList list; + + auto ins1 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins1); + + auto ins2 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins2); + + auto ins3 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins3); + + ASSERT_FALSE(list.empty()); +} + +TEST(InstructionListTest, AddMultipleInstructions_PreviousOfFirst) +{ + InstructionList list; + + auto ins1 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins1); + + auto ins2 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins2); + + auto ins3 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins3); + + ASSERT_EQ(ins1->previous, nullptr); +} + +TEST(InstructionListTest, AddMultipleInstructions_NextOfFirst) +{ + InstructionList list; + + auto ins1 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins1); + + auto ins2 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins2); + + auto ins3 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins3); + + ASSERT_EQ(ins1->next, ins2.get()); +} + +TEST(InstructionListTest, AddMultipleInstructions_PreviousOfMiddle) +{ + InstructionList list; + + auto ins1 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins1); + + auto ins2 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins2); + + auto ins3 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins3); + + ASSERT_EQ(ins2->previous, ins1.get()); +} + +TEST(InstructionListTest, AddMultipleInstructions_NextOfMiddle) +{ + InstructionList list; + + auto ins1 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins1); + + auto ins2 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins2); + + auto ins3 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins3); + + ASSERT_EQ(ins2->next, ins3.get()); +} + +TEST(InstructionListTest, AddMultipleInstructions_PreviousOfLast) +{ + InstructionList list; + + auto ins1 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins1); + + auto ins2 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins2); + + auto ins3 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins3); + + ASSERT_EQ(ins3->previous, ins2.get()); +} + +TEST(InstructionListTest, AddMultipleInstructions_NextOfLast) +{ + InstructionList list; + + auto ins1 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins1); + + auto ins2 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins2); + + auto ins3 = std::make_shared(TInstruction::Type::FunctionCall, false); + list.addInstruction(ins3); + + ASSERT_EQ(ins3->next, nullptr); +} diff --git a/test/compiler/compiler_test.cpp b/test/compiler/compiler_test.cpp index 0869357db..c3cfa4b8a 100644 --- a/test/compiler/compiler_test.cpp +++ b/test/compiler/compiler_test.cpp @@ -31,7 +31,7 @@ class CompilerTest : public testing::Test m_ctx = std::make_shared(&m_engine, &m_target); CompilerPrivate::setBuilderFactory(&m_builderFactory); - EXPECT_CALL(m_builderFactory, createCtx(&m_engine, &m_target)).WillOnce(Return(m_ctx)); + EXPECT_CALL(m_builderFactory, createCtx(&m_engine, &m_target, _)).WillOnce(Return(m_ctx)); m_compiler = std::make_unique(&m_engine, &m_target); m_builder = std::make_shared(); @@ -1873,7 +1873,7 @@ TEST_F(CompilerTest, Preoptimize) { auto ctx = std::make_shared(&m_engine, &m_target); - EXPECT_CALL(m_builderFactory, createCtx(&m_engine, &m_target)).WillOnce(Return(ctx)); + EXPECT_CALL(m_builderFactory, createCtx(&m_engine, &m_target, _)).WillOnce(Return(ctx)); Compiler compiler(&m_engine, &m_target); EXPECT_CALL(*ctx, preoptimize()); diff --git a/test/llvm/CMakeLists.txt b/test/llvm/CMakeLists.txt index 96e8c6897..b435c092b 100644 --- a/test/llvm/CMakeLists.txt +++ b/test/llvm/CMakeLists.txt @@ -18,7 +18,6 @@ add_executable( main.cpp llvmexecutioncontext_test.cpp llvmexecutablecode_test.cpp - llvminstructionlist_test.cpp code_analyzer/variable_type_analysis.cpp code_analyzer/list_type_analysis.cpp code_analyzer/mixed_type_analysis.cpp diff --git a/test/llvm/code_analyzer/list_type_analysis.cpp b/test/llvm/code_analyzer/list_type_analysis.cpp index 45d479fb3..55c9e0651 100644 --- a/test/llvm/code_analyzer/list_type_analysis.cpp +++ b/test/llvm/code_analyzer/list_type_analysis.cpp @@ -1,44 +1,32 @@ #include -#include -#include -#include +#include #include #include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include using namespace libscratchcpp; -class LLVMCodeAnalyzer_ListTypeAnalysis : public testing::Test +class CodeAnalyzer_ListTypeAnalysis : public testing::Test { public: void SetUp() override { auto engine = m_project.engine(); m_target = std::make_shared(); - m_spriteWithUnsafeConstants = std::make_shared(); - auto costume = std::make_shared(m_unsafeCostumeNumConstant, "", ""); - m_spriteWithUnsafeConstants->addCostume(costume); + engine->setTargets({ m_target }); - auto sound = std::make_shared(m_unsafeSoundNumConstant, "", ""); - m_spriteWithUnsafeConstants->addSound(sound); - - engine->setTargets({ m_target, m_spriteWithUnsafeConstants }); - - m_ctx = std::make_unique(engine.get(), m_target.get()); - m_builder = std::make_unique>(*m_ctx->llvmCtx()); - m_utils = std::make_unique(m_ctx.get(), *m_builder, Compiler::CodeType::Script); - m_analyzer = std::make_unique(*m_utils); + const std::unordered_set unsafeConstants = { m_unsafeCostumeNumConstant, m_unsafeSoundNumConstant }; + m_ctx = std::make_unique(engine.get(), m_target.get(), unsafeConstants); + m_analyzer = std::make_unique(m_ctx.get()); } - std::unique_ptr m_analyzer; + std::unique_ptr m_analyzer; const std::string m_safeNumConstant = "3.14"; const std::string m_unsafeCostumeNumConstant = "12"; @@ -48,18 +36,16 @@ class LLVMCodeAnalyzer_ListTypeAnalysis : public testing::Test Project m_project; std::shared_ptr m_target; std::shared_ptr m_spriteWithUnsafeConstants; - std::unique_ptr m_ctx; - std::unique_ptr> m_builder; - std::unique_ptr m_utils; + std::unique_ptr m_ctx; }; -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, FirstListWrite) +TEST_F(CodeAnalyzer_ListTypeAnalysis, FirstListWrite) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto appendList = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value(Compiler::StaticType::Number, 42.0); + auto appendList = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value(Compiler::StaticType::Number, 42.0); appendList->targetList = &targetList; appendList->args.push_back({ Compiler::StaticType::Unknown, &value }); list.addInstruction(appendList); @@ -70,19 +56,19 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, FirstListWrite) ASSERT_EQ(appendList->targetType, Compiler::StaticType::Unknown); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, SecondListWrite) +TEST_F(CodeAnalyzer_ListTypeAnalysis, SecondListWrite) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 42); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::Number, 42); appendList1->targetList = &targetList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList1); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "hello"); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value2(Compiler::StaticType::String, "hello"); appendList2->targetList = &targetList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(appendList2); @@ -96,29 +82,29 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, SecondListWrite) ASSERT_EQ(appendList2->targetType, Compiler::StaticType::Unknown); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, MultipleWritesSameType_AfterClear) +TEST_F(CodeAnalyzer_ListTypeAnalysis, MultipleWritesSameType_AfterClear) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::String, "first"); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::String, "first"); appendList1->targetList = &targetList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList1); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "second"); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value2(Compiler::StaticType::String, "second"); appendList2->targetList = &targetList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(appendList2); - auto appendList3 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value3(Compiler::StaticType::String, "third"); + auto appendList3 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value3(Compiler::StaticType::String, "third"); appendList3->targetList = &targetList; appendList3->args.push_back({ Compiler::StaticType::Unknown, &value3 }); list.addInstruction(appendList3); @@ -130,29 +116,29 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, MultipleWritesSameType_AfterClear) ASSERT_EQ(appendList3->targetType, Compiler::StaticType::String); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, MultipleWritesDifferentTypes_AfterClear) +TEST_F(CodeAnalyzer_ListTypeAnalysis, MultipleWritesDifferentTypes_AfterClear) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 42); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::Number, 42); appendList1->targetList = &targetList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList1); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "hello"); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value2(Compiler::StaticType::String, "hello"); appendList2->targetList = &targetList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(appendList2); - auto appendList3 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value3(Compiler::StaticType::Bool, true); + auto appendList3 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value3(Compiler::StaticType::Bool, true); appendList3->targetList = &targetList; appendList3->args.push_back({ Compiler::StaticType::Unknown, &value3 }); list.addInstruction(appendList3); @@ -164,23 +150,23 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, MultipleWritesDifferentTypes_AfterClea ASSERT_EQ(appendList3->targetType, Compiler::StaticType::Number | Compiler::StaticType::String); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, StringOptimization_AfterClear) +TEST_F(CodeAnalyzer_ListTypeAnalysis, StringOptimization_AfterClear) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::String, m_safeNumConstant); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::String, m_safeNumConstant); appendList1->targetList = &targetList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList1); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value2(Compiler::StaticType::Bool, true); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value2(Compiler::StaticType::Bool, true); appendList2->targetList = &targetList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(appendList2); @@ -193,23 +179,23 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, StringOptimization_AfterClear) ASSERT_EQ(appendList2->targetType, Compiler::StaticType::Number); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, StringOptimization_AfterClear_DifferentString) +TEST_F(CodeAnalyzer_ListTypeAnalysis, StringOptimization_AfterClear_DifferentString) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::String, "1.0"); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::String, "1.0"); appendList1->targetList = &targetList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList1); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value2(Compiler::StaticType::Bool, true); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value2(Compiler::StaticType::Bool, true); appendList2->targetList = &targetList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(appendList2); @@ -222,23 +208,23 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, StringOptimization_AfterClear_Differen ASSERT_EQ(appendList2->targetType, Compiler::StaticType::String); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, StringOptimization_AfterClear_UnsafeCostumeConstant) +TEST_F(CodeAnalyzer_ListTypeAnalysis, StringOptimization_AfterClear_UnsafeCostumeConstant) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::String, m_unsafeCostumeNumConstant); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::String, m_unsafeCostumeNumConstant); appendList1->targetList = &targetList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList1); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value2(Compiler::StaticType::Bool, true); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value2(Compiler::StaticType::Bool, true); appendList2->targetList = &targetList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(appendList2); @@ -251,23 +237,23 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, StringOptimization_AfterClear_UnsafeCo ASSERT_EQ(appendList2->targetType, Compiler::StaticType::String); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, StringOptimization_AfterClear_UnsafeSoundConstant) +TEST_F(CodeAnalyzer_ListTypeAnalysis, StringOptimization_AfterClear_UnsafeSoundConstant) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::String, m_unsafeSoundNumConstant); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::String, m_unsafeSoundNumConstant); appendList1->targetList = &targetList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList1); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value2(Compiler::StaticType::Bool, true); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value2(Compiler::StaticType::Bool, true); appendList2->targetList = &targetList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(appendList2); @@ -280,23 +266,23 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, StringOptimization_AfterClear_UnsafeSo ASSERT_EQ(appendList2->targetType, Compiler::StaticType::String); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, ClearListOperation) +TEST_F(CodeAnalyzer_ListTypeAnalysis, ClearListOperation) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 42); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::Number, 42); appendList1->targetList = &targetList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList1); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "hello"); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value2(Compiler::StaticType::String, "hello"); appendList2->targetList = &targetList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(appendList2); @@ -309,26 +295,26 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, ClearListOperation) ASSERT_EQ(appendList2->targetType, Compiler::StaticType::Void); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, ProcedureCall) +TEST_F(CodeAnalyzer_ListTypeAnalysis, ProcedureCall) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::String, "hello"); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::String, "hello"); appendList1->targetList = &targetList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList1); - auto procCall = std::make_shared(LLVMInstruction::Type::CallProcedure, false); + auto procCall = std::make_shared(InstructionBase::Type::CallProcedure, false); list.addInstruction(procCall); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value2(Compiler::StaticType::Number, 5.2); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value2(Compiler::StaticType::Number, 5.2); appendList2->targetList = &targetList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(appendList2); @@ -341,32 +327,32 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, ProcedureCall) ASSERT_EQ(appendList2->targetType, Compiler::StaticType::Unknown); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, MixedWriteOperationsSameType_AfterClear) +TEST_F(CodeAnalyzer_ListTypeAnalysis, MixedWriteOperationsSameType_AfterClear) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); - auto appendList = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::String, "hello"); + auto appendList = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::String, "hello"); appendList->targetList = &targetList; appendList->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList); - auto insertList = std::make_shared(LLVMInstruction::Type::InsertToList, false); - LLVMConstantRegister index(Compiler::StaticType::Number, 0); - LLVMConstantRegister value2(Compiler::StaticType::String, "world"); + auto insertList = std::make_shared(InstructionBase::Type::InsertToList, false); + CompilerConstant index(Compiler::StaticType::Number, 0); + CompilerConstant value2(Compiler::StaticType::String, "world"); insertList->targetList = &targetList; insertList->args.push_back({ Compiler::StaticType::Number, &index }); insertList->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(insertList); - auto replaceList = std::make_shared(LLVMInstruction::Type::ListReplace, false); - LLVMConstantRegister index2(Compiler::StaticType::Number, 1); - LLVMConstantRegister value3(Compiler::StaticType::String, "test"); + auto replaceList = std::make_shared(InstructionBase::Type::ListReplace, false); + CompilerConstant index2(Compiler::StaticType::Number, 1); + CompilerConstant value3(Compiler::StaticType::String, "test"); replaceList->targetList = &targetList; replaceList->args.push_back({ Compiler::StaticType::Number, &index2 }); replaceList->args.push_back({ Compiler::StaticType::Unknown, &value3 }); @@ -379,39 +365,39 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, MixedWriteOperationsSameType_AfterClea ASSERT_EQ(replaceList->targetType, Compiler::StaticType::String); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, MixedWriteOperationsDifferentTypes_AfterClear) +TEST_F(CodeAnalyzer_ListTypeAnalysis, MixedWriteOperationsDifferentTypes_AfterClear) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); - auto appendList = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::String, "hello"); + auto appendList = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::String, "hello"); appendList->targetList = &targetList; appendList->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList); - auto insertList = std::make_shared(LLVMInstruction::Type::InsertToList, false); - LLVMConstantRegister index(Compiler::StaticType::Number, 0); - LLVMConstantRegister value2(Compiler::StaticType::Bool, false); + auto insertList = std::make_shared(InstructionBase::Type::InsertToList, false); + CompilerConstant index(Compiler::StaticType::Number, 0); + CompilerConstant value2(Compiler::StaticType::Bool, false); insertList->targetList = &targetList; insertList->args.push_back({ Compiler::StaticType::Number, &index }); insertList->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(insertList); - auto replaceList = std::make_shared(LLVMInstruction::Type::ListReplace, false); - LLVMConstantRegister index2(Compiler::StaticType::Number, 1); - LLVMConstantRegister value3(Compiler::StaticType::Number, 5.2); + auto replaceList = std::make_shared(InstructionBase::Type::ListReplace, false); + CompilerConstant index2(Compiler::StaticType::Number, 1); + CompilerConstant value3(Compiler::StaticType::Number, 5.2); replaceList->targetList = &targetList; replaceList->args.push_back({ Compiler::StaticType::Number, &index2 }); replaceList->args.push_back({ Compiler::StaticType::Unknown, &value3 }); list.addInstruction(replaceList); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value4(Compiler::StaticType::String, "world"); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value4(Compiler::StaticType::String, "world"); appendList2->targetList = &targetList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value4 }); list.addInstruction(appendList2); @@ -424,21 +410,21 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, MixedWriteOperationsDifferentTypes_Aft ASSERT_EQ(appendList2->targetType, Compiler::StaticType::Unknown); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, LoopSingleWrite) +TEST_F(CodeAnalyzer_ListTypeAnalysis, LoopSingleWrite) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto loopStart = std::make_shared(LLVMInstruction::Type::BeginRepeatLoop, false); + auto loopStart = std::make_shared(InstructionBase::Type::BeginRepeatLoop, false); list.addInstruction(loopStart); - auto appendList = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value(Compiler::StaticType::Number, 1.25); + auto appendList = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value(Compiler::StaticType::Number, 1.25); appendList->targetList = &targetList; appendList->args.push_back({ Compiler::StaticType::Unknown, &value }); list.addInstruction(appendList); - auto loopEnd = std::make_shared(LLVMInstruction::Type::EndLoop, false); + auto loopEnd = std::make_shared(InstructionBase::Type::EndLoop, false); list.addInstruction(loopEnd); m_analyzer->analyzeScript(list); @@ -447,25 +433,25 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, LoopSingleWrite) ASSERT_EQ(appendList->targetType, Compiler::StaticType::Unknown); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, LoopSingleWrite_AfterClear) +TEST_F(CodeAnalyzer_ListTypeAnalysis, LoopSingleWrite_AfterClear) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); - auto loopStart = std::make_shared(LLVMInstruction::Type::BeginRepeatLoop, false); + auto loopStart = std::make_shared(InstructionBase::Type::BeginRepeatLoop, false); list.addInstruction(loopStart); - auto appendList = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value(Compiler::StaticType::Number, 1.25); + auto appendList = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value(Compiler::StaticType::Number, 1.25); appendList->targetList = &targetList; appendList->args.push_back({ Compiler::StaticType::Unknown, &value }); list.addInstruction(appendList); - auto loopEnd = std::make_shared(LLVMInstruction::Type::EndLoop, false); + auto loopEnd = std::make_shared(InstructionBase::Type::EndLoop, false); list.addInstruction(loopEnd); m_analyzer->analyzeScript(list); @@ -473,29 +459,29 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, LoopSingleWrite_AfterClear) ASSERT_EQ(appendList->targetType, Compiler::StaticType::Number); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, ClearAndWriteInIfStatement_IfBranch) +TEST_F(CodeAnalyzer_ListTypeAnalysis, ClearAndWriteInIfStatement_IfBranch) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto ifStart = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto ifStart = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(ifStart); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 1.25); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::Number, 1.25); appendList1->targetList = &targetList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList1); - auto ifEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto ifEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(ifEnd); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value2(Compiler::StaticType::Bool, true); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value2(Compiler::StaticType::Bool, true); appendList2->targetList = &targetList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(appendList2); @@ -508,32 +494,32 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, ClearAndWriteInIfStatement_IfBranch) ASSERT_EQ(appendList2->targetType, Compiler::StaticType::Unknown); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, ClearAndWriteInIfStatement_ElseBranch) +TEST_F(CodeAnalyzer_ListTypeAnalysis, ClearAndWriteInIfStatement_ElseBranch) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto ifStart = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto ifStart = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(ifStart); - auto elseStart = std::make_shared(LLVMInstruction::Type::BeginElse, false); + auto elseStart = std::make_shared(InstructionBase::Type::BeginElse, false); list.addInstruction(elseStart); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 1.25); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::Number, 1.25); appendList1->targetList = &targetList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList1); - auto ifEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto ifEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(ifEnd); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value2(Compiler::StaticType::Bool, true); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value2(Compiler::StaticType::Bool, true); appendList2->targetList = &targetList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(appendList2); @@ -546,42 +532,42 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, ClearAndWriteInIfStatement_ElseBranch) ASSERT_EQ(appendList2->targetType, Compiler::StaticType::Unknown); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, ClearAndWriteInIfElse) +TEST_F(CodeAnalyzer_ListTypeAnalysis, ClearAndWriteInIfElse) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto ifStart = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto ifStart = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(ifStart); - auto clearList1 = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList1 = std::make_shared(InstructionBase::Type::ClearList, false); clearList1->targetList = &targetList; list.addInstruction(clearList1); - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 1.25); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::Number, 1.25); appendList1->targetList = &targetList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList1); - auto elseStart = std::make_shared(LLVMInstruction::Type::BeginElse, false); + auto elseStart = std::make_shared(InstructionBase::Type::BeginElse, false); list.addInstruction(elseStart); - auto clearList2 = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList2 = std::make_shared(InstructionBase::Type::ClearList, false); clearList2->targetList = &targetList; list.addInstruction(clearList2); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "hello"); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value2(Compiler::StaticType::String, "hello"); appendList2->targetList = &targetList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(appendList2); - auto ifEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto ifEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(ifEnd); - auto appendList3 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value3(Compiler::StaticType::Bool, true); + auto appendList3 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value3(Compiler::StaticType::Bool, true); appendList3->targetList = &targetList; appendList3->args.push_back({ Compiler::StaticType::Unknown, &value3 }); list.addInstruction(appendList3); @@ -595,29 +581,29 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, ClearAndWriteInIfElse) ASSERT_EQ(appendList3->targetType, Compiler::StaticType::Number | Compiler::StaticType::String); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, ClearAndWriteInLoop) +TEST_F(CodeAnalyzer_ListTypeAnalysis, ClearAndWriteInLoop) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto loopStart = std::make_shared(LLVMInstruction::Type::BeginRepeatLoop, false); + auto loopStart = std::make_shared(InstructionBase::Type::BeginRepeatLoop, false); list.addInstruction(loopStart); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 1.25); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::Number, 1.25); appendList1->targetList = &targetList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList1); - auto loopEnd = std::make_shared(LLVMInstruction::Type::EndLoop, false); + auto loopEnd = std::make_shared(InstructionBase::Type::EndLoop, false); list.addInstruction(loopEnd); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value2(Compiler::StaticType::Bool, true); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value2(Compiler::StaticType::Bool, true); appendList2->targetList = &targetList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(appendList2); @@ -630,36 +616,36 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, ClearAndWriteInLoop) ASSERT_EQ(appendList2->targetType, Compiler::StaticType::Unknown); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, ClearAfterWriteInLoop) +TEST_F(CodeAnalyzer_ListTypeAnalysis, ClearAfterWriteInLoop) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); // Establish bool type before loop - auto clearList1 = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList1 = std::make_shared(InstructionBase::Type::ClearList, false); clearList1->targetList = &targetList; list.addInstruction(clearList1); - auto appendList = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value(Compiler::StaticType::Bool, true); + auto appendList = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value(Compiler::StaticType::Bool, true); appendList->targetList = &targetList; appendList->args.push_back({ Compiler::StaticType::Unknown, &value }); list.addInstruction(appendList); - auto loopStart = std::make_shared(LLVMInstruction::Type::BeginRepeatLoop, false); + auto loopStart = std::make_shared(InstructionBase::Type::BeginRepeatLoop, false); list.addInstruction(loopStart); - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 5); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::Number, 5); appendList1->targetList = &targetList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList1); - auto clearList2 = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList2 = std::make_shared(InstructionBase::Type::ClearList, false); clearList2->targetList = &targetList; list.addInstruction(clearList2); - auto loopEnd = std::make_shared(LLVMInstruction::Type::EndLoop, false); + auto loopEnd = std::make_shared(InstructionBase::Type::EndLoop, false); list.addInstruction(loopEnd); m_analyzer->analyzeScript(list); @@ -667,48 +653,46 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, ClearAfterWriteInLoop) ASSERT_EQ(appendList1->targetType, Compiler::StaticType::Bool); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, WhileLoop) +TEST_F(CodeAnalyzer_ListTypeAnalysis, WhileLoop) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); // Establish bool type before loop - auto clearList1 = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList1 = std::make_shared(InstructionBase::Type::ClearList, false); clearList1->targetList = &targetList; list.addInstruction(clearList1); - auto appendList = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value(Compiler::StaticType::Bool, true); + auto appendList = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value(Compiler::StaticType::Bool, true); appendList->targetList = &targetList; appendList->args.push_back({ Compiler::StaticType::Unknown, &value }); list.addInstruction(appendList); - auto loopCond = std::make_shared(LLVMInstruction::Type::BeginLoopCondition, false); + auto loopCond = std::make_shared(InstructionBase::Type::BeginLoopCondition, false); list.addInstruction(loopCond); // Read an item in loop condition - auto getItem = std::make_shared(LLVMInstruction::Type::GetListItem, false); - LLVMConstantRegister index(Compiler::StaticType::Number, 0); + auto getItem = std::make_shared(InstructionBase::Type::GetListItem, false); + CompilerConstant index(Compiler::StaticType::Number, 0); getItem->targetList = &targetList; getItem->args.push_back({ Compiler::StaticType::Number, &index }); list.addInstruction(getItem); - LLVMRegister sourceValue(Compiler::StaticType::Unknown); - sourceValue.isRawValue = false; - sourceValue.instruction = getItem; + CompilerValue sourceValue(Compiler::StaticType::Unknown); getItem->functionReturnReg = &sourceValue; - auto loopStart = std::make_shared(LLVMInstruction::Type::BeginWhileLoop, false); + auto loopStart = std::make_shared(InstructionBase::Type::BeginWhileLoop, false); list.addInstruction(loopStart); // Change the type in the loop - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 5); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::Number, 5); appendList1->targetList = &targetList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList1); - auto loopEnd = std::make_shared(LLVMInstruction::Type::EndLoop, false); + auto loopEnd = std::make_shared(InstructionBase::Type::EndLoop, false); list.addInstruction(loopEnd); m_analyzer->analyzeScript(list); @@ -717,48 +701,46 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, WhileLoop) ASSERT_EQ(appendList1->targetType, Compiler::StaticType::Number | Compiler::StaticType::Bool); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, RepeatUntilLoop) +TEST_F(CodeAnalyzer_ListTypeAnalysis, RepeatUntilLoop) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); // Establish bool type before loop - auto clearList1 = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList1 = std::make_shared(InstructionBase::Type::ClearList, false); clearList1->targetList = &targetList; list.addInstruction(clearList1); - auto appendList = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value(Compiler::StaticType::Bool, true); + auto appendList = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value(Compiler::StaticType::Bool, true); appendList->targetList = &targetList; appendList->args.push_back({ Compiler::StaticType::Unknown, &value }); list.addInstruction(appendList); - auto loopCond = std::make_shared(LLVMInstruction::Type::BeginLoopCondition, false); + auto loopCond = std::make_shared(InstructionBase::Type::BeginLoopCondition, false); list.addInstruction(loopCond); // Read an item in loop condition - auto getItem = std::make_shared(LLVMInstruction::Type::GetListItem, false); - LLVMConstantRegister index(Compiler::StaticType::Number, 0); + auto getItem = std::make_shared(InstructionBase::Type::GetListItem, false); + CompilerConstant index(Compiler::StaticType::Number, 0); getItem->targetList = &targetList; getItem->args.push_back({ Compiler::StaticType::Number, &index }); list.addInstruction(getItem); - LLVMRegister sourceValue(Compiler::StaticType::Unknown); - sourceValue.isRawValue = false; - sourceValue.instruction = getItem; + CompilerValue sourceValue(Compiler::StaticType::Unknown); getItem->functionReturnReg = &sourceValue; - auto loopStart = std::make_shared(LLVMInstruction::Type::BeginRepeatUntilLoop, false); + auto loopStart = std::make_shared(InstructionBase::Type::BeginRepeatUntilLoop, false); list.addInstruction(loopStart); // Change the type in the loop - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 5); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::Number, 5); appendList1->targetList = &targetList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList1); - auto loopEnd = std::make_shared(LLVMInstruction::Type::EndLoop, false); + auto loopEnd = std::make_shared(InstructionBase::Type::EndLoop, false); list.addInstruction(loopEnd); m_analyzer->analyzeScript(list); @@ -767,27 +749,27 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, RepeatUntilLoop) ASSERT_EQ(appendList1->targetType, Compiler::StaticType::Number | Compiler::StaticType::Bool); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, LoopMultipleWrites_UnknownType) +TEST_F(CodeAnalyzer_ListTypeAnalysis, LoopMultipleWrites_UnknownType) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto loopStart = std::make_shared(LLVMInstruction::Type::BeginRepeatLoop, false); + auto loopStart = std::make_shared(InstructionBase::Type::BeginRepeatLoop, false); list.addInstruction(loopStart); - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 5); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::Number, 5); appendList1->targetList = &targetList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList1); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "test"); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value2(Compiler::StaticType::String, "test"); appendList2->targetList = &targetList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(appendList2); - auto loopEnd = std::make_shared(LLVMInstruction::Type::EndLoop, false); + auto loopEnd = std::make_shared(InstructionBase::Type::EndLoop, false); list.addInstruction(loopEnd); m_analyzer->analyzeScript(list); @@ -799,32 +781,32 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, LoopMultipleWrites_UnknownType) ASSERT_EQ(appendList2->targetType, Compiler::StaticType::Unknown); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, LoopMultipleWrites_AfterClear) +TEST_F(CodeAnalyzer_ListTypeAnalysis, LoopMultipleWrites_AfterClear) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); // Establish bool type before loop - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); - auto loopStart = std::make_shared(LLVMInstruction::Type::BeginRepeatLoop, false); + auto loopStart = std::make_shared(InstructionBase::Type::BeginRepeatLoop, false); list.addInstruction(loopStart); - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 5); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::Number, 5); appendList1->targetList = &targetList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList1); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "test"); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value2(Compiler::StaticType::String, "test"); appendList2->targetList = &targetList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(appendList2); - auto loopEnd = std::make_shared(LLVMInstruction::Type::EndLoop, false); + auto loopEnd = std::make_shared(InstructionBase::Type::EndLoop, false); list.addInstruction(loopEnd); m_analyzer->analyzeScript(list); @@ -834,34 +816,34 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, LoopMultipleWrites_AfterClear) ASSERT_EQ(appendList2->targetType, Compiler::StaticType::Number | Compiler::StaticType::String); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, IfElseStatementDifferentTypes) +TEST_F(CodeAnalyzer_ListTypeAnalysis, IfElseStatementDifferentTypes) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto ifStart = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto ifStart = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(ifStart); - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 42); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::Number, 42); appendList1->targetList = &targetList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList1); - auto elseStart = std::make_shared(LLVMInstruction::Type::BeginElse, false); + auto elseStart = std::make_shared(InstructionBase::Type::BeginElse, false); list.addInstruction(elseStart); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "test"); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value2(Compiler::StaticType::String, "test"); appendList2->targetList = &targetList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(appendList2); - auto ifEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto ifEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(ifEnd); - auto appendList3 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value3(Compiler::StaticType::Bool, true); + auto appendList3 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value3(Compiler::StaticType::Bool, true); appendList3->targetList = &targetList; appendList3->args.push_back({ Compiler::StaticType::Unknown, &value3 }); list.addInstruction(appendList3); @@ -876,38 +858,38 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, IfElseStatementDifferentTypes) ASSERT_EQ(appendList3->targetType, Compiler::StaticType::Unknown); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, IfElseStatementDifferentTypes_AfterClear) +TEST_F(CodeAnalyzer_ListTypeAnalysis, IfElseStatementDifferentTypes_AfterClear) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); - auto ifStart = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto ifStart = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(ifStart); - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 42); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::Number, 42); appendList1->targetList = &targetList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList1); - auto elseStart = std::make_shared(LLVMInstruction::Type::BeginElse, false); + auto elseStart = std::make_shared(InstructionBase::Type::BeginElse, false); list.addInstruction(elseStart); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "test"); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value2(Compiler::StaticType::String, "test"); appendList2->targetList = &targetList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(appendList2); - auto ifEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto ifEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(ifEnd); - auto appendList3 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value3(Compiler::StaticType::Bool, true); + auto appendList3 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value3(Compiler::StaticType::Bool, true); appendList3->targetList = &targetList; appendList3->args.push_back({ Compiler::StaticType::Unknown, &value3 }); list.addInstruction(appendList3); @@ -922,35 +904,35 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, IfElseStatementDifferentTypes_AfterCle ASSERT_EQ(appendList3->targetType, Compiler::StaticType::Number | Compiler::StaticType::String); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, IfElseStatementDifferentType_IfBranch_AfterClear) +TEST_F(CodeAnalyzer_ListTypeAnalysis, IfElseStatementDifferentType_IfBranch_AfterClear) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 42); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::Number, 42); appendList1->targetList = &targetList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList1); - auto ifStart = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto ifStart = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(ifStart); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "test"); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value2(Compiler::StaticType::String, "test"); appendList2->targetList = &targetList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(appendList2); - auto ifEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto ifEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(ifEnd); - auto appendList3 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value3(Compiler::StaticType::Bool, true); + auto appendList3 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value3(Compiler::StaticType::Bool, true); appendList3->targetList = &targetList; appendList3->args.push_back({ Compiler::StaticType::Unknown, &value3 }); list.addInstruction(appendList3); @@ -964,38 +946,38 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, IfElseStatementDifferentType_IfBranch_ ASSERT_EQ(appendList3->targetType, Compiler::StaticType::Number | Compiler::StaticType::String); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, IfElseStatementDifferentType_ElseBranch_AfterClear) +TEST_F(CodeAnalyzer_ListTypeAnalysis, IfElseStatementDifferentType_ElseBranch_AfterClear) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 42); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::Number, 42); appendList1->targetList = &targetList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList1); - auto ifStart = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto ifStart = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(ifStart); - auto elseStart = std::make_shared(LLVMInstruction::Type::BeginElse, false); + auto elseStart = std::make_shared(InstructionBase::Type::BeginElse, false); list.addInstruction(elseStart); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "test"); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value2(Compiler::StaticType::String, "test"); appendList2->targetList = &targetList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(appendList2); - auto ifEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto ifEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(ifEnd); - auto appendList3 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value3(Compiler::StaticType::Bool, true); + auto appendList3 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value3(Compiler::StaticType::Bool, true); appendList3->targetList = &targetList; appendList3->args.push_back({ Compiler::StaticType::Unknown, &value3 }); list.addInstruction(appendList3); @@ -1009,35 +991,35 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, IfElseStatementDifferentType_ElseBranc ASSERT_EQ(appendList3->targetType, Compiler::StaticType::Number | Compiler::StaticType::String); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, WriteBeforeLoop_AfterClear) +TEST_F(CodeAnalyzer_ListTypeAnalysis, WriteBeforeLoop_AfterClear) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); - auto appendListBefore = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister valueBefore(Compiler::StaticType::String, "initial"); + auto appendListBefore = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant valueBefore(Compiler::StaticType::String, "initial"); appendListBefore->targetList = &targetList; appendListBefore->args.push_back({ Compiler::StaticType::Unknown, &valueBefore }); list.addInstruction(appendListBefore); - auto loopStart = std::make_shared(LLVMInstruction::Type::BeginRepeatLoop, false); + auto loopStart = std::make_shared(InstructionBase::Type::BeginRepeatLoop, false); list.addInstruction(loopStart); - auto appendListInLoop = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister valueInLoop(Compiler::StaticType::Number, 42); + auto appendListInLoop = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant valueInLoop(Compiler::StaticType::Number, 42); appendListInLoop->targetList = &targetList; appendListInLoop->args.push_back({ Compiler::StaticType::Unknown, &valueInLoop }); list.addInstruction(appendListInLoop); - auto loopEnd = std::make_shared(LLVMInstruction::Type::EndLoop, false); + auto loopEnd = std::make_shared(InstructionBase::Type::EndLoop, false); list.addInstruction(loopEnd); - auto appendList = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value(Compiler::StaticType::Bool, false); + auto appendList = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value(Compiler::StaticType::Bool, false); appendList->targetList = &targetList; appendList->args.push_back({ Compiler::StaticType::Unknown, &value }); list.addInstruction(appendList); @@ -1047,35 +1029,35 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, WriteBeforeLoop_AfterClear) ASSERT_EQ(appendList->targetType, Compiler::StaticType::String | Compiler::StaticType::Number); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, WriteBeforeIfStatement_IfBranch_AfterClear) +TEST_F(CodeAnalyzer_ListTypeAnalysis, WriteBeforeIfStatement_IfBranch_AfterClear) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); - auto appendListBefore = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister valueBefore(Compiler::StaticType::String, "initial"); + auto appendListBefore = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant valueBefore(Compiler::StaticType::String, "initial"); appendListBefore->targetList = &targetList; appendListBefore->args.push_back({ Compiler::StaticType::Unknown, &valueBefore }); list.addInstruction(appendListBefore); - auto ifStart = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto ifStart = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(ifStart); - auto appendListInIfStatement = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister valueInIfStatement(Compiler::StaticType::Number, 42); + auto appendListInIfStatement = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant valueInIfStatement(Compiler::StaticType::Number, 42); appendListInIfStatement->targetList = &targetList; appendListInIfStatement->args.push_back({ Compiler::StaticType::Unknown, &valueInIfStatement }); list.addInstruction(appendListInIfStatement); - auto ifEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto ifEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(ifEnd); - auto appendList = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value(Compiler::StaticType::Bool, false); + auto appendList = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value(Compiler::StaticType::Bool, false); appendList->targetList = &targetList; appendList->args.push_back({ Compiler::StaticType::Unknown, &value }); list.addInstruction(appendList); @@ -1085,38 +1067,38 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, WriteBeforeIfStatement_IfBranch_AfterC ASSERT_EQ(appendList->targetType, Compiler::StaticType::String | Compiler::StaticType::Number); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, WriteBeforeIfStatement_ElseBranch_AfterClear) +TEST_F(CodeAnalyzer_ListTypeAnalysis, WriteBeforeIfStatement_ElseBranch_AfterClear) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); - auto appendListBefore = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister valueBefore(Compiler::StaticType::String, "initial"); + auto appendListBefore = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant valueBefore(Compiler::StaticType::String, "initial"); appendListBefore->targetList = &targetList; appendListBefore->args.push_back({ Compiler::StaticType::Unknown, &valueBefore }); list.addInstruction(appendListBefore); - auto ifStart = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto ifStart = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(ifStart); - auto elseStart = std::make_shared(LLVMInstruction::Type::BeginElse, false); + auto elseStart = std::make_shared(InstructionBase::Type::BeginElse, false); list.addInstruction(elseStart); - auto appendListInIfStatement = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister valueInIfStatement(Compiler::StaticType::Number, 42); + auto appendListInIfStatement = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant valueInIfStatement(Compiler::StaticType::Number, 42); appendListInIfStatement->targetList = &targetList; appendListInIfStatement->args.push_back({ Compiler::StaticType::Unknown, &valueInIfStatement }); list.addInstruction(appendListInIfStatement); - auto ifEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto ifEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(ifEnd); - auto appendList = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value(Compiler::StaticType::Bool, false); + auto appendList = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value(Compiler::StaticType::Bool, false); appendList->targetList = &targetList; appendList->args.push_back({ Compiler::StaticType::Unknown, &value }); list.addInstruction(appendList); @@ -1126,44 +1108,44 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, WriteBeforeIfStatement_ElseBranch_Afte ASSERT_EQ(appendList->targetType, Compiler::StaticType::String | Compiler::StaticType::Number); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, WriteBeforeIfElse_AfterClear) +TEST_F(CodeAnalyzer_ListTypeAnalysis, WriteBeforeIfElse_AfterClear) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); - auto appendListBefore = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister valueBefore(Compiler::StaticType::Bool, true); + auto appendListBefore = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant valueBefore(Compiler::StaticType::Bool, true); appendListBefore->targetList = &targetList; appendListBefore->args.push_back({ Compiler::StaticType::Unknown, &valueBefore }); list.addInstruction(appendListBefore); - auto ifStart = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto ifStart = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(ifStart); - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 42); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::Number, 42); appendList1->targetList = &targetList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList1); - auto elseStart = std::make_shared(LLVMInstruction::Type::BeginElse, false); + auto elseStart = std::make_shared(InstructionBase::Type::BeginElse, false); list.addInstruction(elseStart); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "test"); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value2(Compiler::StaticType::String, "test"); appendList2->targetList = &targetList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(appendList2); - auto ifEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto ifEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(ifEnd); - auto appendList3 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value3(Compiler::StaticType::Bool, true); + auto appendList3 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value3(Compiler::StaticType::Bool, true); appendList3->targetList = &targetList; appendList3->args.push_back({ Compiler::StaticType::Unknown, &value3 }); list.addInstruction(appendList3); @@ -1181,56 +1163,56 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, WriteBeforeIfElse_AfterClear) ASSERT_EQ(appendList3->targetType, Compiler::StaticType::Unknown); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, ComplexNestedControlFlow_AfterClear) +TEST_F(CodeAnalyzer_ListTypeAnalysis, ComplexNestedControlFlow_AfterClear) { - LLVMInstructionList list; + InstructionListBase list; List targetList("", ""); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); - auto outerLoop = std::make_shared(LLVMInstruction::Type::BeginRepeatLoop, false); + auto outerLoop = std::make_shared(InstructionBase::Type::BeginRepeatLoop, false); list.addInstruction(outerLoop); - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 1); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::Number, 1); appendList1->targetList = &targetList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList1); - auto ifStart = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto ifStart = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(ifStart); - auto innerLoop = std::make_shared(LLVMInstruction::Type::BeginRepeatLoop, false); + auto innerLoop = std::make_shared(InstructionBase::Type::BeginRepeatLoop, false); list.addInstruction(innerLoop); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "nested"); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value2(Compiler::StaticType::String, "nested"); appendList2->targetList = &targetList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(appendList2); - auto innerLoopEnd = std::make_shared(LLVMInstruction::Type::EndLoop, false); + auto innerLoopEnd = std::make_shared(InstructionBase::Type::EndLoop, false); list.addInstruction(innerLoopEnd); - auto elseStart = std::make_shared(LLVMInstruction::Type::BeginElse, false); + auto elseStart = std::make_shared(InstructionBase::Type::BeginElse, false); list.addInstruction(elseStart); - auto appendList3 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value3(Compiler::StaticType::Bool, true); + auto appendList3 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value3(Compiler::StaticType::Bool, true); appendList3->targetList = &targetList; appendList3->args.push_back({ Compiler::StaticType::Unknown, &value3 }); list.addInstruction(appendList3); - auto ifEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto ifEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(ifEnd); - auto outerLoopEnd = std::make_shared(LLVMInstruction::Type::EndLoop, false); + auto outerLoopEnd = std::make_shared(InstructionBase::Type::EndLoop, false); list.addInstruction(outerLoopEnd); - auto appendList4 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value4(Compiler::StaticType::Bool, true); + auto appendList4 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value4(Compiler::StaticType::Bool, true); appendList4->targetList = &targetList; appendList4->args.push_back({ Compiler::StaticType::Unknown, &value4 }); list.addInstruction(appendList4); @@ -1244,23 +1226,23 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, ComplexNestedControlFlow_AfterClear) ASSERT_EQ(appendList4->targetType, Compiler::StaticType::Unknown); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, MultipleListsSeparateAnalysis) +TEST_F(CodeAnalyzer_ListTypeAnalysis, MultipleListsSeparateAnalysis) { - LLVMInstructionList list; + InstructionListBase list; List targetList1("", ""), targetList2("", ""); - auto clearList1 = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList1 = std::make_shared(InstructionBase::Type::ClearList, false); clearList1->targetList = &targetList1; list.addInstruction(clearList1); - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 42); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::Number, 42); appendList1->targetList = &targetList1; appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList1); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "test"); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value2(Compiler::StaticType::String, "test"); appendList2->targetList = &targetList2; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(appendList2); @@ -1272,43 +1254,41 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, MultipleListsSeparateAnalysis) ASSERT_EQ(appendList2->targetType, Compiler::StaticType::Unknown); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, CrossListDependency_SingleType) +TEST_F(CodeAnalyzer_ListTypeAnalysis, CrossListDependency_SingleType) { - LLVMInstructionList list; + InstructionListBase list; List sourceList("", ""), targetList("", ""); - auto clearSource = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearSource = std::make_shared(InstructionBase::Type::ClearList, false); clearSource->targetList = &sourceList; list.addInstruction(clearSource); - auto clearTarget = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearTarget = std::make_shared(InstructionBase::Type::ClearList, false); clearTarget->targetList = &targetList; list.addInstruction(clearTarget); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 3.14); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::Number, 3.14); appendList2->targetList = &sourceList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList2); - auto getItem = std::make_shared(LLVMInstruction::Type::GetListItem, false); - LLVMConstantRegister index(Compiler::StaticType::Number, 0); + auto getItem = std::make_shared(InstructionBase::Type::GetListItem, false); + CompilerConstant index(Compiler::StaticType::Number, 0); getItem->targetList = &sourceList; getItem->args.push_back({ Compiler::StaticType::Number, &index }); list.addInstruction(getItem); - LLVMRegister sourceValue(Compiler::StaticType::Unknown); - sourceValue.isRawValue = false; - sourceValue.instruction = getItem; + CompilerValue sourceValue(Compiler::StaticType::Unknown); getItem->functionReturnReg = &sourceValue; - auto appendList1_1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); + auto appendList1_1 = std::make_shared(InstructionBase::Type::AppendToList, false); appendList1_1->targetList = &targetList; appendList1_1->args.push_back({ Compiler::StaticType::Unknown, &sourceValue }); list.addInstruction(appendList1_1); - auto appendList1_2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "test"); + auto appendList1_2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value2(Compiler::StaticType::String, "test"); appendList1_2->targetList = &targetList; appendList1_2->args.push_back({ Compiler::StaticType::Unknown, &sourceValue }); list.addInstruction(appendList1_2); @@ -1328,50 +1308,48 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, CrossListDependency_SingleType) ASSERT_EQ(appendList1_2->targetType, Compiler::StaticType::Number | Compiler::StaticType::String); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, CrossListDependency_MultipleTypes) +TEST_F(CodeAnalyzer_ListTypeAnalysis, CrossListDependency_MultipleTypes) { - LLVMInstructionList list; + InstructionListBase list; List sourceList("", ""), targetList("", ""); - auto clearSource = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearSource = std::make_shared(InstructionBase::Type::ClearList, false); clearSource->targetList = &sourceList; list.addInstruction(clearSource); - auto clearTarget = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearTarget = std::make_shared(InstructionBase::Type::ClearList, false); clearTarget->targetList = &targetList; list.addInstruction(clearTarget); // Establish Number | Bool types - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 42); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::Number, 42); appendList1->targetList = &sourceList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList1); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value2(Compiler::StaticType::Bool, true); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value2(Compiler::StaticType::Bool, true); appendList2->targetList = &sourceList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(appendList2); - auto getItem = std::make_shared(LLVMInstruction::Type::GetListItem, false); - LLVMConstantRegister index(Compiler::StaticType::Number, 0); + auto getItem = std::make_shared(InstructionBase::Type::GetListItem, false); + CompilerConstant index(Compiler::StaticType::Number, 0); getItem->targetList = &sourceList; getItem->args.push_back({ Compiler::StaticType::Number, &index }); list.addInstruction(getItem); - LLVMRegister sourceValue(Compiler::StaticType::Unknown); - sourceValue.isRawValue = false; - sourceValue.instruction = getItem; + CompilerValue sourceValue(Compiler::StaticType::Unknown); getItem->functionReturnReg = &sourceValue; - auto appendList3 = std::make_shared(LLVMInstruction::Type::AppendToList, false); + auto appendList3 = std::make_shared(InstructionBase::Type::AppendToList, false); appendList3->targetList = &targetList; appendList3->args.push_back({ Compiler::StaticType::Unknown, &sourceValue }); list.addInstruction(appendList3); - auto appendList4 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value4(Compiler::StaticType::String, "test"); + auto appendList4 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value4(Compiler::StaticType::String, "test"); appendList4->targetList = &targetList; appendList4->args.push_back({ Compiler::StaticType::Unknown, &sourceValue }); list.addInstruction(appendList4); @@ -1388,126 +1366,120 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, CrossListDependency_MultipleTypes) ASSERT_EQ(appendList4->targetType, Compiler::StaticType::Unknown); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, ChainedAssignmentsInLoop) +TEST_F(CodeAnalyzer_ListTypeAnalysis, ChainedAssignmentsInLoop) { - LLVMInstructionList list; + InstructionListBase list; List listA("a", ""), listB("b", ""), listC("c", ""); - auto clearA = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearA = std::make_shared(InstructionBase::Type::ClearList, false); clearA->targetList = &listA; list.addInstruction(clearA); - auto clearB = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearB = std::make_shared(InstructionBase::Type::ClearList, false); clearB->targetList = &listB; list.addInstruction(clearB); - auto clearC = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearC = std::make_shared(InstructionBase::Type::ClearList, false); clearC->targetList = &listC; list.addInstruction(clearC); // a += 5 - auto appendA1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister valueA1(Compiler::StaticType::Number, 5); + auto appendA1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant valueA1(Compiler::StaticType::Number, 5); appendA1->targetList = &listA; appendA1->args.push_back({ Compiler::StaticType::Unknown, &valueA1 }); list.addInstruction(appendA1); // b += 3 - auto appendB1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister valueB1(Compiler::StaticType::Number, 3); + auto appendB1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant valueB1(Compiler::StaticType::Number, 3); appendB1->targetList = &listB; appendB1->args.push_back({ Compiler::StaticType::Unknown, &valueB1 }); list.addInstruction(appendB1); // c += 8 - auto appendC1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister valueC1(Compiler::StaticType::Number, 8); + auto appendC1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant valueC1(Compiler::StaticType::Number, 8); appendC1->targetList = &listC; appendC1->args.push_back({ Compiler::StaticType::Unknown, &valueC1 }); list.addInstruction(appendC1); // loop start - auto loopStart = std::make_shared(LLVMInstruction::Type::BeginRepeatLoop, false); + auto loopStart = std::make_shared(InstructionBase::Type::BeginRepeatLoop, false); list.addInstruction(loopStart); // clear a - clearA = std::make_shared(LLVMInstruction::Type::ClearList, false); + clearA = std::make_shared(InstructionBase::Type::ClearList, false); clearA->targetList = &listA; list.addInstruction(clearA); // a += b[0] - auto getB = std::make_shared(LLVMInstruction::Type::GetListItem, false); - LLVMConstantRegister indexB(Compiler::StaticType::Number, 0); + auto getB = std::make_shared(InstructionBase::Type::GetListItem, false); + CompilerConstant indexB(Compiler::StaticType::Number, 0); getB->targetList = &listB; getB->args.push_back({ Compiler::StaticType::Number, &indexB }); list.addInstruction(getB); - LLVMRegister bValue(Compiler::StaticType::Unknown); - bValue.isRawValue = false; - bValue.instruction = getB; + CompilerValue bValue(Compiler::StaticType::Unknown); getB->functionReturnReg = &bValue; - auto appendA2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); + auto appendA2 = std::make_shared(InstructionBase::Type::AppendToList, false); appendA2->targetList = &listA; appendA2->args.push_back({ Compiler::StaticType::Unknown, &bValue }); list.addInstruction(appendA2); // clear b - clearB = std::make_shared(LLVMInstruction::Type::ClearList, false); + clearB = std::make_shared(InstructionBase::Type::ClearList, false); clearB->targetList = &listB; list.addInstruction(clearB); // b += c[0] - auto getC1 = std::make_shared(LLVMInstruction::Type::GetListItem, false); - LLVMConstantRegister indexC1(Compiler::StaticType::Number, 0); + auto getC1 = std::make_shared(InstructionBase::Type::GetListItem, false); + CompilerConstant indexC1(Compiler::StaticType::Number, 0); getC1->targetList = &listC; getC1->args.push_back({ Compiler::StaticType::Number, &indexC1 }); list.addInstruction(getC1); - LLVMRegister cValue1(Compiler::StaticType::Unknown); - cValue1.isRawValue = false; - cValue1.instruction = getC1; + CompilerValue cValue1(Compiler::StaticType::Unknown); getC1->functionReturnReg = &cValue1; - auto appendB2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); + auto appendB2 = std::make_shared(InstructionBase::Type::AppendToList, false); appendB2->targetList = &listB; appendB2->args.push_back({ Compiler::StaticType::Unknown, &cValue1 }); list.addInstruction(appendB2); // clear c - clearC = std::make_shared(LLVMInstruction::Type::ClearList, false); + clearC = std::make_shared(InstructionBase::Type::ClearList, false); clearC->targetList = &listC; list.addInstruction(clearC); // c += "test" - auto appendC2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister valueC2(Compiler::StaticType::String, "test"); + auto appendC2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant valueC2(Compiler::StaticType::String, "test"); appendC2->targetList = &listC; appendC2->args.push_back({ Compiler::StaticType::Unknown, &valueC2 }); list.addInstruction(appendC2); // loop end - auto loopEnd = std::make_shared(LLVMInstruction::Type::EndLoop, false); + auto loopEnd = std::make_shared(InstructionBase::Type::EndLoop, false); list.addInstruction(loopEnd); // clear a (final assignment we want to test) - auto clearAFinal = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearAFinal = std::make_shared(InstructionBase::Type::ClearList, false); clearAFinal->targetList = &listA; list.addInstruction(clearAFinal); // a += c[0] - auto getC2 = std::make_shared(LLVMInstruction::Type::GetListItem, false); - LLVMConstantRegister indexC2(Compiler::StaticType::Number, 0); + auto getC2 = std::make_shared(InstructionBase::Type::GetListItem, false); + CompilerConstant indexC2(Compiler::StaticType::Number, 0); getC2->targetList = &listC; getC2->args.push_back({ Compiler::StaticType::Number, &indexC2 }); list.addInstruction(getC2); - LLVMRegister cValue2(Compiler::StaticType::Unknown); - cValue2.isRawValue = false; - cValue2.instruction = getC2; + CompilerValue cValue2(Compiler::StaticType::Unknown); getC2->functionReturnReg = &cValue2; - auto appendA3 = std::make_shared(LLVMInstruction::Type::AppendToList, false); + auto appendA3 = std::make_shared(InstructionBase::Type::AppendToList, false); appendA3->targetList = &listA; appendA3->args.push_back({ Compiler::StaticType::Unknown, &cValue2 }); list.addInstruction(appendA3); @@ -1525,37 +1497,35 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, ChainedAssignmentsInLoop) ASSERT_EQ(getC2->targetType, expectedCType); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, ListReadReturnRegType) +TEST_F(CodeAnalyzer_ListTypeAnalysis, ListReadReturnRegType) { - LLVMInstructionList list; + InstructionListBase list; List sourceList("", ""), targetList("", ""); - auto clearSource = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearSource = std::make_shared(InstructionBase::Type::ClearList, false); clearSource->targetList = &sourceList; list.addInstruction(clearSource); - auto clearTarget = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearTarget = std::make_shared(InstructionBase::Type::ClearList, false); clearTarget->targetList = &targetList; list.addInstruction(clearTarget); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 3.14); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::Number, 3.14); appendList2->targetList = &sourceList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList2); - auto getItem = std::make_shared(LLVMInstruction::Type::GetListItem, false); - LLVMConstantRegister index(Compiler::StaticType::Number, 0); + auto getItem = std::make_shared(InstructionBase::Type::GetListItem, false); + CompilerConstant index(Compiler::StaticType::Number, 0); getItem->targetList = &sourceList; getItem->args.push_back({ Compiler::StaticType::Number, &index }); list.addInstruction(getItem); - LLVMRegister sourceValue(Compiler::StaticType::Unknown); - sourceValue.isRawValue = false; - sourceValue.instruction = getItem; + CompilerValue sourceValue(Compiler::StaticType::Unknown); getItem->functionReturnReg = &sourceValue; - auto appendList1_1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); + auto appendList1_1 = std::make_shared(InstructionBase::Type::AppendToList, false); appendList1_1->targetList = &targetList; appendList1_1->args.push_back({ Compiler::StaticType::Unknown, &sourceValue }); list.addInstruction(appendList1_1); @@ -1566,37 +1536,35 @@ TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, ListReadReturnRegType) ASSERT_EQ(sourceValue.type(), Compiler::StaticType::Number | Compiler::StaticType::String); } -TEST_F(LLVMCodeAnalyzer_ListTypeAnalysis, CrossListWriteArgType) +TEST_F(CodeAnalyzer_ListTypeAnalysis, CrossListWriteArgType) { - LLVMInstructionList list; + InstructionListBase list; List sourceList("", ""), targetList("", ""); - auto clearSource = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearSource = std::make_shared(InstructionBase::Type::ClearList, false); clearSource->targetList = &sourceList; list.addInstruction(clearSource); - auto clearTarget = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearTarget = std::make_shared(InstructionBase::Type::ClearList, false); clearTarget->targetList = &targetList; list.addInstruction(clearTarget); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 3.14); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant value1(Compiler::StaticType::Number, 3.14); appendList2->targetList = &sourceList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(appendList2); - auto getItem = std::make_shared(LLVMInstruction::Type::GetListItem, false); - LLVMConstantRegister index(Compiler::StaticType::Number, 0); + auto getItem = std::make_shared(InstructionBase::Type::GetListItem, false); + CompilerConstant index(Compiler::StaticType::Number, 0); getItem->targetList = &sourceList; getItem->args.push_back({ Compiler::StaticType::Number, &index }); list.addInstruction(getItem); - LLVMRegister sourceValue(Compiler::StaticType::Unknown); - sourceValue.isRawValue = false; - sourceValue.instruction = getItem; + CompilerValue sourceValue(Compiler::StaticType::Unknown); getItem->functionReturnReg = &sourceValue; - auto appendList1_1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); + auto appendList1_1 = std::make_shared(InstructionBase::Type::AppendToList, false); appendList1_1->targetList = &targetList; appendList1_1->args.push_back({ Compiler::StaticType::Unknown, &sourceValue }); list.addInstruction(appendList1_1); diff --git a/test/llvm/code_analyzer/mixed_type_analysis.cpp b/test/llvm/code_analyzer/mixed_type_analysis.cpp index a082ee921..d5bc4c28a 100644 --- a/test/llvm/code_analyzer/mixed_type_analysis.cpp +++ b/test/llvm/code_analyzer/mixed_type_analysis.cpp @@ -2,84 +2,77 @@ #include #include #include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include using namespace libscratchcpp; -class LLVMCodeAnalyzer_MixedTypeAnalysis : public testing::Test +class CodeAnalyzer_MixedTypeAnalysis : public testing::Test { public: void SetUp() override { auto engine = m_project.engine(); - m_ctx = std::make_unique(engine.get(), &m_target); - m_builder = std::make_unique>(*m_ctx->llvmCtx()); - m_utils = std::make_unique(m_ctx.get(), *m_builder, Compiler::CodeType::Script); - m_analyzer = std::make_unique(*m_utils); + const std::unordered_set unsafeConstants = {}; + m_ctx = std::make_unique(engine.get(), &m_target, unsafeConstants); + m_analyzer = std::make_unique(m_ctx.get()); } - std::unique_ptr m_analyzer; + std::unique_ptr m_analyzer; private: Project m_project; Target m_target; - std::unique_ptr m_ctx; - std::unique_ptr> m_builder; - std::unique_ptr m_utils; + std::unique_ptr m_ctx; }; -TEST_F(LLVMCodeAnalyzer_MixedTypeAnalysis, EmptyScript) +TEST_F(CodeAnalyzer_MixedTypeAnalysis, EmptyScript) { - LLVMInstructionList list; + InstructionListBase list; m_analyzer->analyzeScript(list); } -TEST_F(LLVMCodeAnalyzer_MixedTypeAnalysis, NoOperations) +TEST_F(CodeAnalyzer_MixedTypeAnalysis, NoOperations) { - LLVMInstructionList list; + InstructionListBase list; - auto funcCall = std::make_shared(LLVMInstruction::Type::FunctionCall, false); + auto funcCall = std::make_shared(InstructionBase::Type::FunctionCall, false); list.addInstruction(funcCall); m_analyzer->analyzeScript(list); } -TEST_F(LLVMCodeAnalyzer_MixedTypeAnalysis, VariableToList_SimpleTransfer) +TEST_F(CodeAnalyzer_MixedTypeAnalysis, VariableToList_SimpleTransfer) { - LLVMInstructionList list; + InstructionListBase list; Variable sourceVar("sourceVar", ""); List targetList("targetList", ""); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); // Write to variable - auto writeVar = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister varValue(Compiler::StaticType::String, "hello"); + auto writeVar = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant varValue(Compiler::StaticType::String, "hello"); writeVar->targetVariable = &sourceVar; writeVar->args.push_back({ Compiler::StaticType::Unknown, &varValue }); list.addInstruction(writeVar); // Read from variable and write to list - auto readVar = std::make_shared(LLVMInstruction::Type::ReadVariable, false); + auto readVar = std::make_shared(InstructionBase::Type::ReadVariable, false); readVar->targetVariable = &sourceVar; list.addInstruction(readVar); - LLVMRegister readVarReg(Compiler::StaticType::Unknown); - readVarReg.isRawValue = false; - readVarReg.instruction = readVar; + CompilerValue readVarReg(Compiler::StaticType::Unknown); readVar->functionReturnReg = &readVarReg; - auto appendList = std::make_shared(LLVMInstruction::Type::AppendToList, false); + auto appendList = std::make_shared(InstructionBase::Type::AppendToList, false); appendList->targetList = &targetList; appendList->args.push_back({ Compiler::StaticType::Unknown, &readVarReg }); list.addInstruction(appendList); @@ -99,36 +92,34 @@ TEST_F(LLVMCodeAnalyzer_MixedTypeAnalysis, VariableToList_SimpleTransfer) ASSERT_EQ(readVarReg.type(), Compiler::StaticType::String); } -TEST_F(LLVMCodeAnalyzer_MixedTypeAnalysis, ListToVariable_SimpleTransfer) +TEST_F(CodeAnalyzer_MixedTypeAnalysis, ListToVariable_SimpleTransfer) { - LLVMInstructionList list; + InstructionListBase list; List sourceList("sourceList", ""); Variable targetVar("targetVar", ""); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &sourceList; list.addInstruction(clearList); // Write to list - auto appendList = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister listValue(Compiler::StaticType::Bool, true); + auto appendList = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant listValue(Compiler::StaticType::Bool, true); appendList->targetList = &sourceList; appendList->args.push_back({ Compiler::StaticType::Unknown, &listValue }); list.addInstruction(appendList); // Read from list and write to variable - auto getListItem = std::make_shared(LLVMInstruction::Type::GetListItem, false); - LLVMConstantRegister index(Compiler::StaticType::Number, 0); + auto getListItem = std::make_shared(InstructionBase::Type::GetListItem, false); + CompilerConstant index(Compiler::StaticType::Number, 0); getListItem->targetList = &sourceList; getListItem->args.push_back({ Compiler::StaticType::Number, &index }); list.addInstruction(getListItem); - LLVMRegister readListReg(Compiler::StaticType::Unknown); - readListReg.isRawValue = false; - readListReg.instruction = getListItem; + CompilerValue readListReg(Compiler::StaticType::Unknown); getListItem->functionReturnReg = &readListReg; - auto writeVar = std::make_shared(LLVMInstruction::Type::WriteVariable, false); + auto writeVar = std::make_shared(InstructionBase::Type::WriteVariable, false); writeVar->targetVariable = &targetVar; writeVar->args.push_back({ Compiler::StaticType::Unknown, &readListReg }); list.addInstruction(writeVar); @@ -148,52 +139,48 @@ TEST_F(LLVMCodeAnalyzer_MixedTypeAnalysis, ListToVariable_SimpleTransfer) ASSERT_EQ(readListReg.type(), Compiler::StaticType::Bool | Compiler::StaticType::String); } -TEST_F(LLVMCodeAnalyzer_MixedTypeAnalysis, CircularVarListDependency) +TEST_F(CodeAnalyzer_MixedTypeAnalysis, CircularVarListDependency) { - LLVMInstructionList list; + InstructionListBase list; Variable var("var", ""); List targetList("list", ""); // Initialize variable with number - auto writeVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 42); + auto writeVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::Number, 42); writeVar1->targetVariable = &var; writeVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(writeVar1); // Clear list - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); // Read from variable and write to list - auto readVar = std::make_shared(LLVMInstruction::Type::ReadVariable, false); + auto readVar = std::make_shared(InstructionBase::Type::ReadVariable, false); readVar->targetVariable = &var; list.addInstruction(readVar); - LLVMRegister readVarReg(Compiler::StaticType::Unknown); - readVarReg.isRawValue = false; - readVarReg.instruction = readVar; + CompilerValue readVarReg(Compiler::StaticType::Unknown); readVar->functionReturnReg = &readVarReg; - auto appendList = std::make_shared(LLVMInstruction::Type::AppendToList, false); + auto appendList = std::make_shared(InstructionBase::Type::AppendToList, false); appendList->targetList = &targetList; appendList->args.push_back({ Compiler::StaticType::Unknown, &readVarReg }); list.addInstruction(appendList); // Read from list and write back to variable (circular dependency) - auto getListItem = std::make_shared(LLVMInstruction::Type::GetListItem, false); - LLVMConstantRegister index(Compiler::StaticType::Number, 0); + auto getListItem = std::make_shared(InstructionBase::Type::GetListItem, false); + CompilerConstant index(Compiler::StaticType::Number, 0); getListItem->targetList = &targetList; getListItem->args.push_back({ Compiler::StaticType::Number, &index }); list.addInstruction(getListItem); - LLVMRegister readListReg(Compiler::StaticType::Unknown); - readListReg.isRawValue = false; - readListReg.instruction = getListItem; + CompilerValue readListReg(Compiler::StaticType::Unknown); getListItem->functionReturnReg = &readListReg; - auto writeVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); + auto writeVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); writeVar2->targetVariable = &var; writeVar2->args.push_back({ Compiler::StaticType::Unknown, &readListReg }); list.addInstruction(writeVar2); @@ -208,49 +195,47 @@ TEST_F(LLVMCodeAnalyzer_MixedTypeAnalysis, CircularVarListDependency) ASSERT_EQ(writeVar2->targetType, Compiler::StaticType::Number); } -TEST_F(LLVMCodeAnalyzer_MixedTypeAnalysis, LoopWithVarListInteraction_TypeConflict) +TEST_F(CodeAnalyzer_MixedTypeAnalysis, LoopWithVarListInteraction_TypeConflict) { - LLVMInstructionList list; + InstructionListBase list; Variable var("var", ""); List targetList("list", ""); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); // First iteration: write string to variable - auto writeVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::String, "text"); + auto writeVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::String, "text"); writeVar1->targetVariable = &var; writeVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(writeVar1); - auto loopStart = std::make_shared(LLVMInstruction::Type::BeginRepeatLoop, false); + auto loopStart = std::make_shared(InstructionBase::Type::BeginRepeatLoop, false); list.addInstruction(loopStart); // Read from variable and write to list - auto readVar = std::make_shared(LLVMInstruction::Type::ReadVariable, false); + auto readVar = std::make_shared(InstructionBase::Type::ReadVariable, false); readVar->targetVariable = &var; list.addInstruction(readVar); - LLVMRegister readVarReg(Compiler::StaticType::Unknown); - readVarReg.isRawValue = false; - readVarReg.instruction = readVar; + CompilerValue readVarReg(Compiler::StaticType::Unknown); readVar->functionReturnReg = &readVarReg; - auto appendList = std::make_shared(LLVMInstruction::Type::AppendToList, false); + auto appendList = std::make_shared(InstructionBase::Type::AppendToList, false); appendList->targetList = &targetList; appendList->args.push_back({ Compiler::StaticType::Unknown, &readVarReg }); list.addInstruction(appendList); // Second iteration: write number to variable (creates type conflict) - auto writeVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value2(Compiler::StaticType::Number, 42); + auto writeVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value2(Compiler::StaticType::Number, 42); writeVar2->targetVariable = &var; writeVar2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(writeVar2); - auto loopEnd = std::make_shared(LLVMInstruction::Type::EndLoop, false); + auto loopEnd = std::make_shared(InstructionBase::Type::EndLoop, false); list.addInstruction(loopEnd); m_analyzer->analyzeScript(list); @@ -262,65 +247,61 @@ TEST_F(LLVMCodeAnalyzer_MixedTypeAnalysis, LoopWithVarListInteraction_TypeConfli ASSERT_EQ(writeVar2->targetType, Compiler::StaticType::String | Compiler::StaticType::Number); } -TEST_F(LLVMCodeAnalyzer_MixedTypeAnalysis, ConditionalVarListTransfer_TypeConflict) +TEST_F(CodeAnalyzer_MixedTypeAnalysis, ConditionalVarListTransfer_TypeConflict) { - LLVMInstructionList list; + InstructionListBase list; Variable var("var", ""); List targetList("list", ""); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); - auto ifStart = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto ifStart = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(ifStart); // If branch: write number to variable, transfer to list - auto writeVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 123); + auto writeVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::Number, 123); writeVar1->targetVariable = &var; writeVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(writeVar1); - auto readVar1 = std::make_shared(LLVMInstruction::Type::ReadVariable, false); + auto readVar1 = std::make_shared(InstructionBase::Type::ReadVariable, false); readVar1->targetVariable = &var; list.addInstruction(readVar1); - LLVMRegister readVar1Reg(Compiler::StaticType::Unknown); - readVar1Reg.isRawValue = false; - readVar1Reg.instruction = readVar1; + CompilerValue readVar1Reg(Compiler::StaticType::Unknown); readVar1->functionReturnReg = &readVar1Reg; - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); appendList1->targetList = &targetList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &readVar1Reg }); list.addInstruction(appendList1); - auto elseStart = std::make_shared(LLVMInstruction::Type::BeginElse, false); + auto elseStart = std::make_shared(InstructionBase::Type::BeginElse, false); list.addInstruction(elseStart); // Else branch: write string to variable, transfer to list - auto writeVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "hello"); + auto writeVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value2(Compiler::StaticType::String, "hello"); writeVar2->targetVariable = &var; writeVar2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(writeVar2); - auto readVar2 = std::make_shared(LLVMInstruction::Type::ReadVariable, false); + auto readVar2 = std::make_shared(InstructionBase::Type::ReadVariable, false); readVar2->targetVariable = &var; list.addInstruction(readVar2); - LLVMRegister readVar2Reg(Compiler::StaticType::Unknown); - readVar2Reg.isRawValue = false; - readVar2Reg.instruction = readVar2; + CompilerValue readVar2Reg(Compiler::StaticType::Unknown); readVar2->functionReturnReg = &readVar2Reg; - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); appendList2->targetList = &targetList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &readVar2Reg }); list.addInstruction(appendList2); - auto ifEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto ifEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(ifEnd); m_analyzer->analyzeScript(list); @@ -332,57 +313,53 @@ TEST_F(LLVMCodeAnalyzer_MixedTypeAnalysis, ConditionalVarListTransfer_TypeConfli ASSERT_EQ(appendList2->targetType, Compiler::StaticType::Void); } -TEST_F(LLVMCodeAnalyzer_MixedTypeAnalysis, MultipleVarsToSingleList_TypePropagation) +TEST_F(CodeAnalyzer_MixedTypeAnalysis, MultipleVarsToSingleList_TypePropagation) { - LLVMInstructionList list; + InstructionListBase list; Variable var1("var1", ""); Variable var2("var2", ""); List targetList("list", ""); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &targetList; list.addInstruction(clearList); // Initialize first variable with boolean - auto writeVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::Bool, true); + auto writeVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::Bool, true); writeVar1->targetVariable = &var1; writeVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(writeVar1); // Initialize second variable with same type - auto writeVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value2(Compiler::StaticType::Bool, false); + auto writeVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value2(Compiler::StaticType::Bool, false); writeVar2->targetVariable = &var2; writeVar2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(writeVar2); // Read from first variable and write to list - auto readVar1 = std::make_shared(LLVMInstruction::Type::ReadVariable, false); + auto readVar1 = std::make_shared(InstructionBase::Type::ReadVariable, false); readVar1->targetVariable = &var1; list.addInstruction(readVar1); - LLVMRegister readVar1Reg(Compiler::StaticType::Unknown); - readVar1Reg.isRawValue = false; - readVar1Reg.instruction = readVar1; + CompilerValue readVar1Reg(Compiler::StaticType::Unknown); readVar1->functionReturnReg = &readVar1Reg; - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); appendList1->targetList = &targetList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &readVar1Reg }); list.addInstruction(appendList1); // Read from second variable and write to list - auto readVar2 = std::make_shared(LLVMInstruction::Type::ReadVariable, false); + auto readVar2 = std::make_shared(InstructionBase::Type::ReadVariable, false); readVar2->targetVariable = &var2; list.addInstruction(readVar2); - LLVMRegister readVar2Reg(Compiler::StaticType::Unknown); - readVar2Reg.isRawValue = false; - readVar2Reg.instruction = readVar2; + CompilerValue readVar2Reg(Compiler::StaticType::Unknown); readVar2->functionReturnReg = &readVar2Reg; - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); appendList2->targetList = &targetList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &readVar2Reg }); list.addInstruction(appendList2); @@ -394,60 +371,56 @@ TEST_F(LLVMCodeAnalyzer_MixedTypeAnalysis, MultipleVarsToSingleList_TypePropagat ASSERT_EQ(appendList2->targetType, Compiler::StaticType::Bool); } -TEST_F(LLVMCodeAnalyzer_MixedTypeAnalysis, SingleListToMultipleVars_TypePropagation) +TEST_F(CodeAnalyzer_MixedTypeAnalysis, SingleListToMultipleVars_TypePropagation) { - LLVMInstructionList list; + InstructionListBase list; List sourceList("sourceList", ""); Variable var1("var1", ""); Variable var2("var2", ""); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &sourceList; list.addInstruction(clearList); // Initialize list with string values - auto appendList1 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister listValue1(Compiler::StaticType::String, "first"); + auto appendList1 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant listValue1(Compiler::StaticType::String, "first"); appendList1->targetList = &sourceList; appendList1->args.push_back({ Compiler::StaticType::Unknown, &listValue1 }); list.addInstruction(appendList1); - auto appendList2 = std::make_shared(LLVMInstruction::Type::AppendToList, false); - LLVMConstantRegister listValue2(Compiler::StaticType::String, "second"); + auto appendList2 = std::make_shared(InstructionBase::Type::AppendToList, false); + CompilerConstant listValue2(Compiler::StaticType::String, "second"); appendList2->targetList = &sourceList; appendList2->args.push_back({ Compiler::StaticType::Unknown, &listValue2 }); list.addInstruction(appendList2); // Read first item and write to first variable - auto readList1 = std::make_shared(LLVMInstruction::Type::GetListItem, false); - LLVMConstantRegister index1(Compiler::StaticType::Number, 0); + auto readList1 = std::make_shared(InstructionBase::Type::GetListItem, false); + CompilerConstant index1(Compiler::StaticType::Number, 0); readList1->targetList = &sourceList; readList1->args.push_back({ Compiler::StaticType::Number, &index1 }); list.addInstruction(readList1); - LLVMRegister readList1Reg(Compiler::StaticType::Unknown); - readList1Reg.isRawValue = false; - readList1Reg.instruction = readList1; + CompilerValue readList1Reg(Compiler::StaticType::Unknown); readList1->functionReturnReg = &readList1Reg; - auto writeVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); + auto writeVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); writeVar1->targetVariable = &var1; writeVar1->args.push_back({ Compiler::StaticType::Unknown, &readList1Reg }); list.addInstruction(writeVar1); // Read second item and write to second variable - auto readList2 = std::make_shared(LLVMInstruction::Type::GetListItem, false); - LLVMConstantRegister index2(Compiler::StaticType::Number, 1); + auto readList2 = std::make_shared(InstructionBase::Type::GetListItem, false); + CompilerConstant index2(Compiler::StaticType::Number, 1); readList2->targetList = &sourceList; readList2->args.push_back({ Compiler::StaticType::Number, &index2 }); list.addInstruction(readList2); - LLVMRegister readList2Reg(Compiler::StaticType::Unknown); - readList2Reg.isRawValue = false; - readList2Reg.instruction = readList2; + CompilerValue readList2Reg(Compiler::StaticType::Unknown); readList2->functionReturnReg = &readList2Reg; - auto writeVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); + auto writeVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); writeVar2->targetVariable = &var2; writeVar2->args.push_back({ Compiler::StaticType::Unknown, &readList2Reg }); list.addInstruction(writeVar2); @@ -461,52 +434,48 @@ TEST_F(LLVMCodeAnalyzer_MixedTypeAnalysis, SingleListToMultipleVars_TypePropagat ASSERT_EQ(readList2Reg.type(), Compiler::StaticType::String); } -TEST_F(LLVMCodeAnalyzer_MixedTypeAnalysis, ComplexChain_VarToListToVar_TypePropagation) +TEST_F(CodeAnalyzer_MixedTypeAnalysis, ComplexChain_VarToListToVar_TypePropagation) { - LLVMInstructionList list; + InstructionListBase list; Variable sourceVar("sourceVar", ""); List intermediateList("intermediateList", ""); Variable targetVar("targetVar", ""); - auto clearList = std::make_shared(LLVMInstruction::Type::ClearList, false); + auto clearList = std::make_shared(InstructionBase::Type::ClearList, false); clearList->targetList = &intermediateList; list.addInstruction(clearList); // Initialize source variable - auto writeSourceVar = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister sourceValue(Compiler::StaticType::Number, 3.14159); + auto writeSourceVar = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant sourceValue(Compiler::StaticType::Number, 3.14159); writeSourceVar->targetVariable = &sourceVar; writeSourceVar->args.push_back({ Compiler::StaticType::Unknown, &sourceValue }); list.addInstruction(writeSourceVar); // Read from source variable and write to intermediate list - auto readSourceVar = std::make_shared(LLVMInstruction::Type::ReadVariable, false); + auto readSourceVar = std::make_shared(InstructionBase::Type::ReadVariable, false); readSourceVar->targetVariable = &sourceVar; list.addInstruction(readSourceVar); - LLVMRegister readSourceVarReg(Compiler::StaticType::Unknown); - readSourceVarReg.isRawValue = false; - readSourceVarReg.instruction = readSourceVar; + CompilerValue readSourceVarReg(Compiler::StaticType::Unknown); readSourceVar->functionReturnReg = &readSourceVarReg; - auto appendList = std::make_shared(LLVMInstruction::Type::AppendToList, false); + auto appendList = std::make_shared(InstructionBase::Type::AppendToList, false); appendList->targetList = &intermediateList; appendList->args.push_back({ Compiler::StaticType::Unknown, &readSourceVarReg }); list.addInstruction(appendList); // Read from intermediate list and write to target variable - auto readList = std::make_shared(LLVMInstruction::Type::GetListItem, false); - LLVMConstantRegister index(Compiler::StaticType::Number, 0); + auto readList = std::make_shared(InstructionBase::Type::GetListItem, false); + CompilerConstant index(Compiler::StaticType::Number, 0); readList->targetList = &intermediateList; readList->args.push_back({ Compiler::StaticType::Number, &index }); list.addInstruction(readList); - LLVMRegister readListReg(Compiler::StaticType::Unknown); - readListReg.isRawValue = false; - readListReg.instruction = readList; + CompilerValue readListReg(Compiler::StaticType::Unknown); readList->functionReturnReg = &readListReg; - auto writeTargetVar = std::make_shared(LLVMInstruction::Type::WriteVariable, false); + auto writeTargetVar = std::make_shared(InstructionBase::Type::WriteVariable, false); writeTargetVar->targetVariable = &targetVar; writeTargetVar->args.push_back({ Compiler::StaticType::Unknown, &readListReg }); list.addInstruction(writeTargetVar); diff --git a/test/llvm/code_analyzer/variable_type_analysis.cpp b/test/llvm/code_analyzer/variable_type_analysis.cpp index b910b154a..ce46efd9d 100644 --- a/test/llvm/code_analyzer/variable_type_analysis.cpp +++ b/test/llvm/code_analyzer/variable_type_analysis.cpp @@ -1,44 +1,32 @@ #include -#include -#include -#include +#include #include #include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include using namespace libscratchcpp; -class LLVMCodeAnalyzer_VariableTypeAnalysis : public testing::Test +class CodeAnalyzer_VariableTypeAnalysis : public testing::Test { public: void SetUp() override { auto engine = m_project.engine(); m_target = std::make_shared(); - m_spriteWithUnsafeConstants = std::make_shared(); - auto costume = std::make_shared(m_unsafeCostumeNumConstant, "", ""); - m_spriteWithUnsafeConstants->addCostume(costume); + engine->setTargets({ m_target }); - auto sound = std::make_shared(m_unsafeSoundNumConstant, "", ""); - m_spriteWithUnsafeConstants->addSound(sound); - - engine->setTargets({ m_target, m_spriteWithUnsafeConstants }); - - m_ctx = std::make_unique(engine.get(), m_target.get()); - m_builder = std::make_unique>(*m_ctx->llvmCtx()); - m_utils = std::make_unique(m_ctx.get(), *m_builder, Compiler::CodeType::Script); - m_analyzer = std::make_unique(*m_utils); + const std::unordered_set unsafeConstants = { m_unsafeCostumeNumConstant, m_unsafeSoundNumConstant }; + m_ctx = std::make_unique(engine.get(), m_target.get(), unsafeConstants); + m_analyzer = std::make_unique(m_ctx.get()); } - std::unique_ptr m_analyzer; + std::unique_ptr m_analyzer; const std::string m_safeNumConstant = "3.14"; const std::string m_unsafeCostumeNumConstant = "12"; @@ -47,19 +35,16 @@ class LLVMCodeAnalyzer_VariableTypeAnalysis : public testing::Test private: Project m_project; std::shared_ptr m_target; - std::shared_ptr m_spriteWithUnsafeConstants; - std::unique_ptr m_ctx; - std::unique_ptr> m_builder; - std::unique_ptr m_utils; + std::unique_ptr m_ctx; }; -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, FirstVariableWrite) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, FirstVariableWrite) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto setVar = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value(Compiler::StaticType::Number, 42.0); + auto setVar = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value(Compiler::StaticType::Number, 42.0); setVar->targetVariable = &var; setVar->args.push_back({ Compiler::StaticType::Unknown, &value }); list.addInstruction(setVar); @@ -70,13 +55,13 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, FirstVariableWrite) ASSERT_EQ(setVar->targetType, Compiler::StaticType::Unknown); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, FirstVariableWrite_ArgType) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, FirstVariableWrite_ArgType) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto setVar = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value(Compiler::StaticType::String, "test"); + auto setVar = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value(Compiler::StaticType::String, "test"); setVar->targetVariable = &var; setVar->args.push_back({ Compiler::StaticType::Unknown, &value }); list.addInstruction(setVar); @@ -87,19 +72,19 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, FirstVariableWrite_ArgType) ASSERT_EQ(setVar->args.back().first, Compiler::StaticType::String); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, SecondVariableWrite) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, SecondVariableWrite) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto setVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 42); + auto setVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::Number, 42); setVar1->targetVariable = &var; setVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar1); - auto setVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "hello"); + auto setVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value2(Compiler::StaticType::String, "hello"); setVar2->targetVariable = &var; setVar2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(setVar2); @@ -112,26 +97,24 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, SecondVariableWrite) ASSERT_EQ(setVar2->targetType, Compiler::StaticType::Number); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, InstructionReturnTypePropagation) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, InstructionReturnTypePropagation) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto ins = std::make_shared(LLVMInstruction::Type::Add, true); + auto ins = std::make_shared(InstructionBase::Type::Add, true); list.addInstruction(ins); - LLVMRegister ret(Compiler::StaticType::Number); - ret.isRawValue = false; - ret.instruction = ins; + CompilerValue ret(Compiler::StaticType::Number); ins->functionReturnReg = &ret; - auto setVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); + auto setVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); setVar1->targetVariable = &var; setVar1->args.push_back({ Compiler::StaticType::Unknown, &ret }); list.addInstruction(setVar1); - auto setVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "hello"); + auto setVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value2(Compiler::StaticType::String, "hello"); setVar2->targetVariable = &var; setVar2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(setVar2); @@ -144,20 +127,18 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, InstructionReturnTypePropagation) ASSERT_EQ(setVar2->targetType, Compiler::StaticType::Number); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, InstructionReturnTypePropagation_ArgType) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, InstructionReturnTypePropagation_ArgType) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto ins = std::make_shared(LLVMInstruction::Type::Add, true); + auto ins = std::make_shared(InstructionBase::Type::Add, true); list.addInstruction(ins); - LLVMRegister ret(Compiler::StaticType::Number); - ret.isRawValue = false; - ret.instruction = ins; + CompilerValue ret(Compiler::StaticType::Number); ins->functionReturnReg = &ret; - auto setVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); + auto setVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); setVar1->targetVariable = &var; setVar1->args.push_back({ Compiler::StaticType::Unknown, &ret }); list.addInstruction(setVar1); @@ -168,25 +149,25 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, InstructionReturnTypePropagation_A ASSERT_EQ(setVar1->args.back().first, Compiler::StaticType::Number); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, MultipleWritesSameType) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, MultipleWritesSameType) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto setVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::String, "first"); + auto setVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::String, "first"); setVar1->targetVariable = &var; setVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar1); - auto setVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "second"); + auto setVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value2(Compiler::StaticType::String, "second"); setVar2->targetVariable = &var; setVar2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(setVar2); - auto setVar3 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value3(Compiler::StaticType::String, "third"); + auto setVar3 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value3(Compiler::StaticType::String, "third"); setVar3->targetVariable = &var; setVar3->args.push_back({ Compiler::StaticType::Unknown, &value3 }); list.addInstruction(setVar3); @@ -198,19 +179,19 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, MultipleWritesSameType) ASSERT_EQ(setVar3->targetType, Compiler::StaticType::String); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, StringOptimization) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, StringOptimization) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto setVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::String, m_safeNumConstant); + auto setVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::String, m_safeNumConstant); setVar1->targetVariable = &var; setVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar1); - auto setVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value2(Compiler::StaticType::Bool, true); + auto setVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value2(Compiler::StaticType::Bool, true); setVar2->targetVariable = &var; setVar2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(setVar2); @@ -222,19 +203,19 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, StringOptimization) ASSERT_EQ(setVar2->targetType, Compiler::StaticType::Number); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, StringOptimization_DifferentString) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, StringOptimization_DifferentString) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto setVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::String, "1.0"); + auto setVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::String, "1.0"); setVar1->targetVariable = &var; setVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar1); - auto setVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value2(Compiler::StaticType::Bool, true); + auto setVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value2(Compiler::StaticType::Bool, true); setVar2->targetVariable = &var; setVar2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(setVar2); @@ -246,19 +227,19 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, StringOptimization_DifferentString ASSERT_EQ(setVar2->targetType, Compiler::StaticType::String); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, StringOptimization_UnsafeCostumeConstant) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, StringOptimization_UnsafeCostumeConstant) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto setVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::String, m_unsafeCostumeNumConstant); + auto setVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::String, m_unsafeCostumeNumConstant); setVar1->targetVariable = &var; setVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar1); - auto setVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value2(Compiler::StaticType::Bool, true); + auto setVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value2(Compiler::StaticType::Bool, true); setVar2->targetVariable = &var; setVar2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(setVar2); @@ -270,19 +251,19 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, StringOptimization_UnsafeCostumeCo ASSERT_EQ(setVar2->targetType, Compiler::StaticType::String); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, StringOptimization_UnsafeSoundConstant) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, StringOptimization_UnsafeSoundConstant) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto setVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::String, m_unsafeSoundNumConstant); + auto setVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::String, m_unsafeSoundNumConstant); setVar1->targetVariable = &var; setVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar1); - auto setVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value2(Compiler::StaticType::Bool, true); + auto setVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value2(Compiler::StaticType::Bool, true); setVar2->targetVariable = &var; setVar2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(setVar2); @@ -294,13 +275,13 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, StringOptimization_UnsafeSoundCons ASSERT_EQ(setVar2->targetType, Compiler::StaticType::String); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, ArgTypeStringOptimization) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, ArgTypeStringOptimization) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto setVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::String, m_safeNumConstant); + auto setVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::String, m_safeNumConstant); setVar1->targetVariable = &var; setVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar1); @@ -310,21 +291,21 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, ArgTypeStringOptimization) ASSERT_EQ(setVar1->args.back().first, Compiler::StaticType::Number); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, LoopSingleWrite) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, LoopSingleWrite) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto loopStart = std::make_shared(LLVMInstruction::Type::BeginRepeatLoop, false); + auto loopStart = std::make_shared(InstructionBase::Type::BeginRepeatLoop, false); list.addInstruction(loopStart); - auto setVar = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value(Compiler::StaticType::Number, 1.25); + auto setVar = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value(Compiler::StaticType::Number, 1.25); setVar->targetVariable = &var; setVar->args.push_back({ Compiler::StaticType::Unknown, &value }); list.addInstruction(setVar); - auto loopEnd = std::make_shared(LLVMInstruction::Type::EndLoop, false); + auto loopEnd = std::make_shared(InstructionBase::Type::EndLoop, false); list.addInstruction(loopEnd); m_analyzer->analyzeScript(list); @@ -333,42 +314,40 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, LoopSingleWrite) ASSERT_EQ(setVar->targetType, Compiler::StaticType::Unknown); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, WhileLoop) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, WhileLoop) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); // Establish bool type before loop - auto setVar = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value(Compiler::StaticType::Bool, true); + auto setVar = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value(Compiler::StaticType::Bool, true); setVar->targetVariable = &var; setVar->args.push_back({ Compiler::StaticType::Unknown, &value }); list.addInstruction(setVar); - auto loopCond = std::make_shared(LLVMInstruction::Type::BeginLoopCondition, false); + auto loopCond = std::make_shared(InstructionBase::Type::BeginLoopCondition, false); list.addInstruction(loopCond); // Read the variable in loop condition - auto readVar = std::make_shared(LLVMInstruction::Type::ReadVariable, true); + auto readVar = std::make_shared(InstructionBase::Type::ReadVariable, true); readVar->targetVariable = &var; list.addInstruction(readVar); - LLVMRegister varValue(Compiler::StaticType::Unknown); - varValue.isRawValue = false; - varValue.instruction = readVar; + CompilerValue varValue(Compiler::StaticType::Unknown); readVar->functionReturnReg = &varValue; - auto loopStart = std::make_shared(LLVMInstruction::Type::BeginWhileLoop, false); + auto loopStart = std::make_shared(InstructionBase::Type::BeginWhileLoop, false); list.addInstruction(loopStart); // Change the type in the loop - auto setVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 5); + auto setVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::Number, 5); setVar1->targetVariable = &var; setVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar1); - auto loopEnd = std::make_shared(LLVMInstruction::Type::EndLoop, false); + auto loopEnd = std::make_shared(InstructionBase::Type::EndLoop, false); list.addInstruction(loopEnd); m_analyzer->analyzeScript(list); @@ -377,42 +356,40 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, WhileLoop) ASSERT_EQ(setVar1->targetType, Compiler::StaticType::Number | Compiler::StaticType::Bool); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, RepeatUntilLoop) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, RepeatUntilLoop) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); // Establish bool type before loop - auto setVar = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value(Compiler::StaticType::Bool, true); + auto setVar = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value(Compiler::StaticType::Bool, true); setVar->targetVariable = &var; setVar->args.push_back({ Compiler::StaticType::Unknown, &value }); list.addInstruction(setVar); - auto loopCond = std::make_shared(LLVMInstruction::Type::BeginLoopCondition, false); + auto loopCond = std::make_shared(InstructionBase::Type::BeginLoopCondition, false); list.addInstruction(loopCond); // Read the variable in loop condition - auto readVar = std::make_shared(LLVMInstruction::Type::ReadVariable, true); + auto readVar = std::make_shared(InstructionBase::Type::ReadVariable, true); readVar->targetVariable = &var; list.addInstruction(readVar); - LLVMRegister varValue(Compiler::StaticType::Unknown); - varValue.isRawValue = false; - varValue.instruction = readVar; + CompilerValue varValue(Compiler::StaticType::Unknown); readVar->functionReturnReg = &varValue; - auto loopStart = std::make_shared(LLVMInstruction::Type::BeginRepeatUntilLoop, false); + auto loopStart = std::make_shared(InstructionBase::Type::BeginRepeatUntilLoop, false); list.addInstruction(loopStart); // Change the type in the loop - auto setVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 5); + auto setVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::Number, 5); setVar1->targetVariable = &var; setVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar1); - auto loopEnd = std::make_shared(LLVMInstruction::Type::EndLoop, false); + auto loopEnd = std::make_shared(InstructionBase::Type::EndLoop, false); list.addInstruction(loopEnd); m_analyzer->analyzeScript(list); @@ -421,30 +398,30 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, RepeatUntilLoop) ASSERT_EQ(setVar1->targetType, Compiler::StaticType::Number | Compiler::StaticType::Bool); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, ProcedureCallInLoop) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, ProcedureCallInLoop) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto setVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 1.25); + auto setVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::Number, 1.25); setVar1->targetVariable = &var; setVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar1); - auto loopStart = std::make_shared(LLVMInstruction::Type::BeginRepeatLoop, false); + auto loopStart = std::make_shared(InstructionBase::Type::BeginRepeatLoop, false); list.addInstruction(loopStart); - auto setVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value2(Compiler::StaticType::Number, 5); + auto setVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value2(Compiler::StaticType::Number, 5); setVar2->targetVariable = &var; setVar2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(setVar2); - auto procCall = std::make_shared(LLVMInstruction::Type::CallProcedure, false); + auto procCall = std::make_shared(InstructionBase::Type::CallProcedure, false); list.addInstruction(procCall); - auto loopEnd = std::make_shared(LLVMInstruction::Type::EndLoop, false); + auto loopEnd = std::make_shared(InstructionBase::Type::EndLoop, false); list.addInstruction(loopEnd); m_analyzer->analyzeScript(list); @@ -455,27 +432,27 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, ProcedureCallInLoop) ASSERT_EQ(setVar2->targetType, Compiler::StaticType::Unknown); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, LoopMultipleWrites_UnknownType) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, LoopMultipleWrites_UnknownType) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto loopStart = std::make_shared(LLVMInstruction::Type::BeginRepeatLoop, false); + auto loopStart = std::make_shared(InstructionBase::Type::BeginRepeatLoop, false); list.addInstruction(loopStart); - auto setVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 5); + auto setVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::Number, 5); setVar1->targetVariable = &var; setVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar1); - auto setVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "test"); + auto setVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value2(Compiler::StaticType::String, "test"); setVar2->targetVariable = &var; setVar2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(setVar2); - auto loopEnd = std::make_shared(LLVMInstruction::Type::EndLoop, false); + auto loopEnd = std::make_shared(InstructionBase::Type::EndLoop, false); list.addInstruction(loopEnd); m_analyzer->analyzeScript(list); @@ -487,34 +464,34 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, LoopMultipleWrites_UnknownType) ASSERT_EQ(setVar2->targetType, Compiler::StaticType::Number); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, LoopMultipleWrites_KnownType) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, LoopMultipleWrites_KnownType) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); // Establish bool type before loop - auto setVar = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value(Compiler::StaticType::Bool, true); + auto setVar = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value(Compiler::StaticType::Bool, true); setVar->targetVariable = &var; setVar->args.push_back({ Compiler::StaticType::Unknown, &value }); list.addInstruction(setVar); - auto loopStart = std::make_shared(LLVMInstruction::Type::BeginRepeatLoop, false); + auto loopStart = std::make_shared(InstructionBase::Type::BeginRepeatLoop, false); list.addInstruction(loopStart); - auto setVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 5); + auto setVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::Number, 5); setVar1->targetVariable = &var; setVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar1); - auto setVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "test"); + auto setVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value2(Compiler::StaticType::String, "test"); setVar2->targetVariable = &var; setVar2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(setVar2); - auto loopEnd = std::make_shared(LLVMInstruction::Type::EndLoop, false); + auto loopEnd = std::make_shared(InstructionBase::Type::EndLoop, false); list.addInstruction(loopEnd); m_analyzer->analyzeScript(list); @@ -526,50 +503,50 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, LoopMultipleWrites_KnownType) ASSERT_EQ(setVar2->targetType, Compiler::StaticType::Number); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, LoopWithIfElseWrites) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, LoopWithIfElseWrites) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); // var = "string" - auto setVarBefore = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister valueBefore(Compiler::StaticType::String, "string"); + auto setVarBefore = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant valueBefore(Compiler::StaticType::String, "string"); setVarBefore->targetVariable = &var; setVarBefore->args.push_back({ Compiler::StaticType::Unknown, &valueBefore }); list.addInstruction(setVarBefore); // repeat - auto loopStart = std::make_shared(LLVMInstruction::Type::BeginRepeatLoop, false); + auto loopStart = std::make_shared(InstructionBase::Type::BeginRepeatLoop, false); list.addInstruction(loopStart); // if - auto ifStart = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto ifStart = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(ifStart); // var = 1 - auto setVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 1); + auto setVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::Number, 1); setVar1->targetVariable = &var; setVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar1); // else - auto elseStart = std::make_shared(LLVMInstruction::Type::BeginElse, false); + auto elseStart = std::make_shared(InstructionBase::Type::BeginElse, false); list.addInstruction(elseStart); // var = 2 - auto setVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value2(Compiler::StaticType::Number, 2); + auto setVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value2(Compiler::StaticType::Number, 2); setVar2->targetVariable = &var; setVar2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(setVar2); // end if - auto ifEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto ifEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(ifEnd); // end loop - auto loopEnd = std::make_shared(LLVMInstruction::Type::EndLoop, false); + auto loopEnd = std::make_shared(InstructionBase::Type::EndLoop, false); list.addInstruction(loopEnd); m_analyzer->analyzeScript(list); @@ -581,34 +558,34 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, LoopWithIfElseWrites) ASSERT_EQ(setVar2->targetType, Compiler::StaticType::String | Compiler::StaticType::Number); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, IfElseStatementSameTypes) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, IfElseStatementSameTypes) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto ifStart = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto ifStart = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(ifStart); - auto setVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 42); + auto setVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::Number, 42); setVar1->targetVariable = &var; setVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar1); - auto elseStart = std::make_shared(LLVMInstruction::Type::BeginElse, false); + auto elseStart = std::make_shared(InstructionBase::Type::BeginElse, false); list.addInstruction(elseStart); - auto setVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value2(Compiler::StaticType::Number, 1.25); + auto setVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value2(Compiler::StaticType::Number, 1.25); setVar2->targetVariable = &var; setVar2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(setVar2); - auto ifEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto ifEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(ifEnd); - auto setVar3 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value3(Compiler::StaticType::String, "test"); + auto setVar3 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value3(Compiler::StaticType::String, "test"); setVar3->targetVariable = &var; setVar3->args.push_back({ Compiler::StaticType::Unknown, &value3 }); list.addInstruction(setVar3); @@ -623,34 +600,34 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, IfElseStatementSameTypes) ASSERT_EQ(setVar3->targetType, Compiler::StaticType::Number); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, IfElseStatementDifferentTypes) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, IfElseStatementDifferentTypes) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto ifStart = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto ifStart = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(ifStart); - auto setVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 42); + auto setVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::Number, 42); setVar1->targetVariable = &var; setVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar1); - auto elseStart = std::make_shared(LLVMInstruction::Type::BeginElse, false); + auto elseStart = std::make_shared(InstructionBase::Type::BeginElse, false); list.addInstruction(elseStart); - auto setVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "test"); + auto setVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value2(Compiler::StaticType::String, "test"); setVar2->targetVariable = &var; setVar2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(setVar2); - auto ifEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto ifEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(ifEnd); - auto setVar3 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value3(Compiler::StaticType::Bool, true); + auto setVar3 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value3(Compiler::StaticType::Bool, true); setVar3->targetVariable = &var; setVar3->args.push_back({ Compiler::StaticType::Unknown, &value3 }); list.addInstruction(setVar3); @@ -665,31 +642,31 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, IfElseStatementDifferentTypes) ASSERT_EQ(setVar3->targetType, Compiler::StaticType::Number | Compiler::StaticType::String); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, IfElseStatementDifferentType_IfBranch) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, IfElseStatementDifferentType_IfBranch) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto setVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 42); + auto setVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::Number, 42); setVar1->targetVariable = &var; setVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar1); - auto ifStart = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto ifStart = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(ifStart); - auto setVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "test"); + auto setVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value2(Compiler::StaticType::String, "test"); setVar2->targetVariable = &var; setVar2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(setVar2); - auto ifEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto ifEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(ifEnd); - auto setVar3 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value3(Compiler::StaticType::Bool, true); + auto setVar3 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value3(Compiler::StaticType::Bool, true); setVar3->targetVariable = &var; setVar3->args.push_back({ Compiler::StaticType::Unknown, &value3 }); list.addInstruction(setVar3); @@ -703,34 +680,34 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, IfElseStatementDifferentType_IfBra ASSERT_EQ(setVar3->targetType, Compiler::StaticType::Number | Compiler::StaticType::String); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, IfElseStatementDifferentType_ElseBranch) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, IfElseStatementDifferentType_ElseBranch) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto setVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 42); + auto setVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::Number, 42); setVar1->targetVariable = &var; setVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar1); - auto ifStart = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto ifStart = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(ifStart); - auto elseStart = std::make_shared(LLVMInstruction::Type::BeginElse, false); + auto elseStart = std::make_shared(InstructionBase::Type::BeginElse, false); list.addInstruction(elseStart); - auto setVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "test"); + auto setVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value2(Compiler::StaticType::String, "test"); setVar2->targetVariable = &var; setVar2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(setVar2); - auto ifEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto ifEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(ifEnd); - auto setVar3 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value3(Compiler::StaticType::Bool, true); + auto setVar3 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value3(Compiler::StaticType::Bool, true); setVar3->targetVariable = &var; setVar3->args.push_back({ Compiler::StaticType::Unknown, &value3 }); list.addInstruction(setVar3); @@ -744,31 +721,31 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, IfElseStatementDifferentType_ElseB ASSERT_EQ(setVar3->targetType, Compiler::StaticType::Number | Compiler::StaticType::String); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, WriteBeforeLoop) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, WriteBeforeLoop) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto setVarBefore = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister valueBefore(Compiler::StaticType::String, "initial"); + auto setVarBefore = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant valueBefore(Compiler::StaticType::String, "initial"); setVarBefore->targetVariable = &var; setVarBefore->args.push_back({ Compiler::StaticType::Unknown, &valueBefore }); list.addInstruction(setVarBefore); - auto loopStart = std::make_shared(LLVMInstruction::Type::BeginRepeatLoop, false); + auto loopStart = std::make_shared(InstructionBase::Type::BeginRepeatLoop, false); list.addInstruction(loopStart); - auto setVarInLoop = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister valueInLoop(Compiler::StaticType::Number, 42); + auto setVarInLoop = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant valueInLoop(Compiler::StaticType::Number, 42); setVarInLoop->targetVariable = &var; setVarInLoop->args.push_back({ Compiler::StaticType::Unknown, &valueInLoop }); list.addInstruction(setVarInLoop); - auto loopEnd = std::make_shared(LLVMInstruction::Type::EndLoop, false); + auto loopEnd = std::make_shared(InstructionBase::Type::EndLoop, false); list.addInstruction(loopEnd); - auto setVar = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value(Compiler::StaticType::Bool, false); + auto setVar = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value(Compiler::StaticType::Bool, false); setVar->targetVariable = &var; setVar->args.push_back({ Compiler::StaticType::Unknown, &value }); list.addInstruction(setVar); @@ -779,31 +756,31 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, WriteBeforeLoop) ASSERT_EQ(setVar->targetType, Compiler::StaticType::String | Compiler::StaticType::Number); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, WriteBeforeIfStatement_IfBranch) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, WriteBeforeIfStatement_IfBranch) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto setVarBefore = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister valueBefore(Compiler::StaticType::String, "initial"); + auto setVarBefore = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant valueBefore(Compiler::StaticType::String, "initial"); setVarBefore->targetVariable = &var; setVarBefore->args.push_back({ Compiler::StaticType::Unknown, &valueBefore }); list.addInstruction(setVarBefore); - auto ifStart = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto ifStart = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(ifStart); - auto setVarInIfStatement = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister valueInIfStatement(Compiler::StaticType::Number, 42); + auto setVarInIfStatement = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant valueInIfStatement(Compiler::StaticType::Number, 42); setVarInIfStatement->targetVariable = &var; setVarInIfStatement->args.push_back({ Compiler::StaticType::Unknown, &valueInIfStatement }); list.addInstruction(setVarInIfStatement); - auto ifEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto ifEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(ifEnd); - auto setVar = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value(Compiler::StaticType::Bool, false); + auto setVar = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value(Compiler::StaticType::Bool, false); setVar->targetVariable = &var; setVar->args.push_back({ Compiler::StaticType::Unknown, &value }); list.addInstruction(setVar); @@ -814,34 +791,34 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, WriteBeforeIfStatement_IfBranch) ASSERT_EQ(setVar->targetType, Compiler::StaticType::String | Compiler::StaticType::Number); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, WriteBeforeIfStatement_ElseBranch) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, WriteBeforeIfStatement_ElseBranch) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto setVarBefore = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister valueBefore(Compiler::StaticType::String, "initial"); + auto setVarBefore = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant valueBefore(Compiler::StaticType::String, "initial"); setVarBefore->targetVariable = &var; setVarBefore->args.push_back({ Compiler::StaticType::Unknown, &valueBefore }); list.addInstruction(setVarBefore); - auto ifStart = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto ifStart = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(ifStart); - auto elseStart = std::make_shared(LLVMInstruction::Type::BeginElse, false); + auto elseStart = std::make_shared(InstructionBase::Type::BeginElse, false); list.addInstruction(elseStart); - auto setVarInIfStatement = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister valueInIfStatement(Compiler::StaticType::Number, 42); + auto setVarInIfStatement = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant valueInIfStatement(Compiler::StaticType::Number, 42); setVarInIfStatement->targetVariable = &var; setVarInIfStatement->args.push_back({ Compiler::StaticType::Unknown, &valueInIfStatement }); list.addInstruction(setVarInIfStatement); - auto ifEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto ifEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(ifEnd); - auto setVar = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value(Compiler::StaticType::Bool, false); + auto setVar = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value(Compiler::StaticType::Bool, false); setVar->targetVariable = &var; setVar->args.push_back({ Compiler::StaticType::Unknown, &value }); list.addInstruction(setVar); @@ -852,40 +829,40 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, WriteBeforeIfStatement_ElseBranch) ASSERT_EQ(setVar->targetType, Compiler::StaticType::String | Compiler::StaticType::Number); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, WriteBeforeIfElse) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, WriteBeforeIfElse) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto setVarBefore = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister valueBefore(Compiler::StaticType::Bool, true); + auto setVarBefore = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant valueBefore(Compiler::StaticType::Bool, true); setVarBefore->targetVariable = &var; setVarBefore->args.push_back({ Compiler::StaticType::Unknown, &valueBefore }); list.addInstruction(setVarBefore); - auto ifStart = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto ifStart = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(ifStart); - auto setVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 42); + auto setVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::Number, 42); setVar1->targetVariable = &var; setVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar1); - auto elseStart = std::make_shared(LLVMInstruction::Type::BeginElse, false); + auto elseStart = std::make_shared(InstructionBase::Type::BeginElse, false); list.addInstruction(elseStart); - auto setVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "test"); + auto setVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value2(Compiler::StaticType::String, "test"); setVar2->targetVariable = &var; setVar2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(setVar2); - auto ifEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto ifEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(ifEnd); - auto setVar3 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value3(Compiler::StaticType::Bool, true); + auto setVar3 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value3(Compiler::StaticType::Bool, true); setVar3->targetVariable = &var; setVar3->args.push_back({ Compiler::StaticType::Unknown, &value3 }); list.addInstruction(setVar3); @@ -903,25 +880,25 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, WriteBeforeIfElse) ASSERT_EQ(setVar3->targetType, Compiler::StaticType::Number | Compiler::StaticType::String); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, WriteInIfStatement_IfBranch) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, WriteInIfStatement_IfBranch) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto ifStart = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto ifStart = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(ifStart); - auto setVarInIfStatement = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister valueInIfStatement(Compiler::StaticType::Number, 42); + auto setVarInIfStatement = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant valueInIfStatement(Compiler::StaticType::Number, 42); setVarInIfStatement->targetVariable = &var; setVarInIfStatement->args.push_back({ Compiler::StaticType::Unknown, &valueInIfStatement }); list.addInstruction(setVarInIfStatement); - auto ifEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto ifEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(ifEnd); - auto setVar = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value(Compiler::StaticType::Bool, false); + auto setVar = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value(Compiler::StaticType::Bool, false); setVar->targetVariable = &var; setVar->args.push_back({ Compiler::StaticType::Unknown, &value }); list.addInstruction(setVar); @@ -934,28 +911,28 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, WriteInIfStatement_IfBranch) ASSERT_EQ(setVar->targetType, Compiler::StaticType::Unknown); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, WriteInIfStatement_ElseBranch) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, WriteInIfStatement_ElseBranch) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto ifStart = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto ifStart = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(ifStart); - auto elseStart = std::make_shared(LLVMInstruction::Type::BeginElse, false); + auto elseStart = std::make_shared(InstructionBase::Type::BeginElse, false); list.addInstruction(elseStart); - auto setVarInIfStatement = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister valueInIfStatement(Compiler::StaticType::Number, 42); + auto setVarInIfStatement = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant valueInIfStatement(Compiler::StaticType::Number, 42); setVarInIfStatement->targetVariable = &var; setVarInIfStatement->args.push_back({ Compiler::StaticType::Unknown, &valueInIfStatement }); list.addInstruction(setVarInIfStatement); - auto ifEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto ifEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(ifEnd); - auto setVar = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value(Compiler::StaticType::Bool, false); + auto setVar = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value(Compiler::StaticType::Bool, false); setVar->targetVariable = &var; setVar->args.push_back({ Compiler::StaticType::Unknown, &value }); list.addInstruction(setVar); @@ -968,34 +945,34 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, WriteInIfStatement_ElseBranch) ASSERT_EQ(setVar->targetType, Compiler::StaticType::Unknown); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, WriteInIfElse) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, WriteInIfElse) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto ifStart = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto ifStart = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(ifStart); - auto setVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 42); + auto setVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::Number, 42); setVar1->targetVariable = &var; setVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar1); - auto elseStart = std::make_shared(LLVMInstruction::Type::BeginElse, false); + auto elseStart = std::make_shared(InstructionBase::Type::BeginElse, false); list.addInstruction(elseStart); - auto setVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "test"); + auto setVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value2(Compiler::StaticType::String, "test"); setVar2->targetVariable = &var; setVar2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(setVar2); - auto ifEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto ifEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(ifEnd); - auto setVar3 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value3(Compiler::StaticType::Bool, true); + auto setVar3 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value3(Compiler::StaticType::Bool, true); setVar3->targetVariable = &var; setVar3->args.push_back({ Compiler::StaticType::Unknown, &value3 }); list.addInstruction(setVar3); @@ -1009,25 +986,25 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, WriteInIfElse) ASSERT_EQ(setVar3->targetType, Compiler::StaticType::Number | Compiler::StaticType::String); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, WriteInLoop) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, WriteInLoop) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto loopStart = std::make_shared(LLVMInstruction::Type::BeginRepeatLoop, false); + auto loopStart = std::make_shared(InstructionBase::Type::BeginRepeatLoop, false); list.addInstruction(loopStart); - auto setVarInLoop = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister valueInLoop(Compiler::StaticType::Number, 42); + auto setVarInLoop = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant valueInLoop(Compiler::StaticType::Number, 42); setVarInLoop->targetVariable = &var; setVarInLoop->args.push_back({ Compiler::StaticType::Unknown, &valueInLoop }); list.addInstruction(setVarInLoop); - auto loopEnd = std::make_shared(LLVMInstruction::Type::EndLoop, false); + auto loopEnd = std::make_shared(InstructionBase::Type::EndLoop, false); list.addInstruction(loopEnd); - auto setVar = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value(Compiler::StaticType::Bool, false); + auto setVar = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value(Compiler::StaticType::Bool, false); setVar->targetVariable = &var; setVar->args.push_back({ Compiler::StaticType::Unknown, &value }); list.addInstruction(setVar); @@ -1040,39 +1017,39 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, WriteInLoop) ASSERT_EQ(setVar->targetType, Compiler::StaticType::Unknown); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, NestedIfStatements) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, NestedIfStatements) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto outerIf = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto outerIf = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(outerIf); - auto setVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 1); + auto setVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::Number, 1); setVar1->targetVariable = &var; setVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar1); - auto outerElse = std::make_shared(LLVMInstruction::Type::BeginElse, false); + auto outerElse = std::make_shared(InstructionBase::Type::BeginElse, false); list.addInstruction(outerElse); - auto innerIf = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto innerIf = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(innerIf); - auto innerElse = std::make_shared(LLVMInstruction::Type::BeginElse, false); + auto innerElse = std::make_shared(InstructionBase::Type::BeginElse, false); list.addInstruction(innerElse); - auto innerIfEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto innerIfEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(innerIfEnd); - auto setVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "test"); + auto setVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value2(Compiler::StaticType::String, "test"); setVar2->targetVariable = &var; setVar2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(setVar2); - auto outerIfEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto outerIfEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(outerIfEnd); m_analyzer->analyzeScript(list); @@ -1081,52 +1058,52 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, NestedIfStatements) ASSERT_EQ(setVar2->targetType, Compiler::StaticType::Unknown); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, ComplexNestedControlFlow) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, ComplexNestedControlFlow) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto outerLoop = std::make_shared(LLVMInstruction::Type::BeginRepeatLoop, false); + auto outerLoop = std::make_shared(InstructionBase::Type::BeginRepeatLoop, false); list.addInstruction(outerLoop); - auto setVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 1); + auto setVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::Number, 1); setVar1->targetVariable = &var; setVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar1); - auto ifStart = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto ifStart = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(ifStart); - auto innerLoop = std::make_shared(LLVMInstruction::Type::BeginRepeatLoop, false); + auto innerLoop = std::make_shared(InstructionBase::Type::BeginRepeatLoop, false); list.addInstruction(innerLoop); - auto setVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "nested"); + auto setVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value2(Compiler::StaticType::String, "nested"); setVar2->targetVariable = &var; setVar2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(setVar2); - auto innerLoopEnd = std::make_shared(LLVMInstruction::Type::EndLoop, false); + auto innerLoopEnd = std::make_shared(InstructionBase::Type::EndLoop, false); list.addInstruction(innerLoopEnd); - auto elseStart = std::make_shared(LLVMInstruction::Type::BeginElse, false); + auto elseStart = std::make_shared(InstructionBase::Type::BeginElse, false); list.addInstruction(elseStart); - auto setVar3 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value3(Compiler::StaticType::Bool, true); + auto setVar3 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value3(Compiler::StaticType::Bool, true); setVar3->targetVariable = &var; setVar3->args.push_back({ Compiler::StaticType::Unknown, &value3 }); list.addInstruction(setVar3); - auto ifEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto ifEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(ifEnd); - auto outerLoopEnd = std::make_shared(LLVMInstruction::Type::EndLoop, false); + auto outerLoopEnd = std::make_shared(InstructionBase::Type::EndLoop, false); list.addInstruction(outerLoopEnd); - auto setVar4 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value4(Compiler::StaticType::Bool, true); + auto setVar4 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value4(Compiler::StaticType::Bool, true); setVar4->targetVariable = &var; setVar4->args.push_back({ Compiler::StaticType::Unknown, &value4 }); list.addInstruction(setVar4); @@ -1140,19 +1117,19 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, ComplexNestedControlFlow) ASSERT_EQ(setVar4->targetType, Compiler::StaticType::Unknown); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, MultipleVariablesSeparateAnalysis) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, MultipleVariablesSeparateAnalysis) { - LLVMInstructionList list; + InstructionListBase list; Variable var1("", ""), var2("", ""); - auto setVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 42); + auto setVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::Number, 42); setVar1->targetVariable = &var1; setVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar1); - auto setVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "test"); + auto setVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value2(Compiler::StaticType::String, "test"); setVar2->targetVariable = &var2; setVar2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(setVar2); @@ -1164,33 +1141,31 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, MultipleVariablesSeparateAnalysis) ASSERT_EQ(setVar2->targetType, Compiler::StaticType::Unknown); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, CrossVariableDependency_SingleType) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, CrossVariableDependency_SingleType) { - LLVMInstructionList list; + InstructionListBase list; Variable var1("", ""), var2("", ""); - auto setVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 3.14); + auto setVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::Number, 3.14); setVar2->targetVariable = &var2; setVar2->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar2); - auto readVar2 = std::make_shared(LLVMInstruction::Type::ReadVariable, false); + auto readVar2 = std::make_shared(InstructionBase::Type::ReadVariable, false); readVar2->targetVariable = &var2; list.addInstruction(readVar2); - LLVMRegister var2Value(Compiler::StaticType::Unknown); - var2Value.isRawValue = false; - var2Value.instruction = readVar2; + CompilerValue var2Value(Compiler::StaticType::Unknown); readVar2->functionReturnReg = &var2Value; - auto setVar1_1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); + auto setVar1_1 = std::make_shared(InstructionBase::Type::WriteVariable, false); setVar1_1->targetVariable = &var1; setVar1_1->args.push_back({ Compiler::StaticType::Unknown, &var2Value }); list.addInstruction(setVar1_1); - auto setVar1_2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value2(Compiler::StaticType::String, "test"); + auto setVar1_2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value2(Compiler::StaticType::String, "test"); setVar1_2->targetVariable = &var1; setVar1_2->args.push_back({ Compiler::StaticType::Unknown, &var2Value }); list.addInstruction(setVar1_2); @@ -1210,49 +1185,47 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, CrossVariableDependency_SingleType ASSERT_EQ(setVar1_2->targetType, Compiler::StaticType::Number); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, CrossVariableDependency_MultipleTypes) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, CrossVariableDependency_MultipleTypes) { - LLVMInstructionList list; + InstructionListBase list; Variable var1("", ""), var2("", ""); // Establish Number | Bool types - auto ifStart = std::make_shared(LLVMInstruction::Type::BeginIf, false); + auto ifStart = std::make_shared(InstructionBase::Type::BeginIf, false); list.addInstruction(ifStart); - auto setVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 42); + auto setVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::Number, 42); setVar1->targetVariable = &var2; setVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar1); - auto elseStart = std::make_shared(LLVMInstruction::Type::BeginElse, false); + auto elseStart = std::make_shared(InstructionBase::Type::BeginElse, false); list.addInstruction(elseStart); - auto setVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value2(Compiler::StaticType::Bool, true); + auto setVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value2(Compiler::StaticType::Bool, true); setVar2->targetVariable = &var2; setVar2->args.push_back({ Compiler::StaticType::Unknown, &value2 }); list.addInstruction(setVar2); - auto ifEnd = std::make_shared(LLVMInstruction::Type::EndIf, false); + auto ifEnd = std::make_shared(InstructionBase::Type::EndIf, false); list.addInstruction(ifEnd); - auto readVar2 = std::make_shared(LLVMInstruction::Type::ReadVariable, false); + auto readVar2 = std::make_shared(InstructionBase::Type::ReadVariable, false); readVar2->targetVariable = &var2; list.addInstruction(readVar2); - LLVMRegister var2Value(Compiler::StaticType::Unknown); - var2Value.isRawValue = false; - var2Value.instruction = readVar2; + CompilerValue var2Value(Compiler::StaticType::Unknown); readVar2->functionReturnReg = &var2Value; - auto setVar3 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); + auto setVar3 = std::make_shared(InstructionBase::Type::WriteVariable, false); setVar3->targetVariable = &var1; setVar3->args.push_back({ Compiler::StaticType::Unknown, &var2Value }); list.addInstruction(setVar3); - auto setVar4 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value4(Compiler::StaticType::String, "test"); + auto setVar4 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value4(Compiler::StaticType::String, "test"); setVar4->targetVariable = &var1; setVar4->args.push_back({ Compiler::StaticType::Unknown, &var2Value }); list.addInstruction(setVar4); @@ -1269,88 +1242,82 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, CrossVariableDependency_MultipleTy ASSERT_EQ(setVar4->targetType, Compiler::StaticType::Number | Compiler::StaticType::Bool); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, ChainedAssignmentsInLoop) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, ChainedAssignmentsInLoop) { - LLVMInstructionList list; + InstructionListBase list; Variable varA("a", ""), varB("b", ""), varC("c", ""); // a = 5 - auto setA1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister valueA1(Compiler::StaticType::Number, 5); + auto setA1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant valueA1(Compiler::StaticType::Number, 5); setA1->targetVariable = &varA; setA1->args.push_back({ Compiler::StaticType::Unknown, &valueA1 }); list.addInstruction(setA1); // b = 3 - auto setB1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister valueB1(Compiler::StaticType::Number, 3); + auto setB1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant valueB1(Compiler::StaticType::Number, 3); setB1->targetVariable = &varB; setB1->args.push_back({ Compiler::StaticType::Unknown, &valueB1 }); list.addInstruction(setB1); // c = 8 - auto setC1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister valueC1(Compiler::StaticType::Number, 8); + auto setC1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant valueC1(Compiler::StaticType::Number, 8); setC1->targetVariable = &varC; setC1->args.push_back({ Compiler::StaticType::Unknown, &valueC1 }); list.addInstruction(setC1); // loop start - auto loopStart = std::make_shared(LLVMInstruction::Type::BeginRepeatLoop, false); + auto loopStart = std::make_shared(InstructionBase::Type::BeginRepeatLoop, false); list.addInstruction(loopStart); // a = b - auto readB = std::make_shared(LLVMInstruction::Type::ReadVariable, false); + auto readB = std::make_shared(InstructionBase::Type::ReadVariable, false); readB->targetVariable = &varB; list.addInstruction(readB); - LLVMRegister bValue(Compiler::StaticType::Unknown); - bValue.isRawValue = false; - bValue.instruction = readB; + CompilerValue bValue(Compiler::StaticType::Unknown); readB->functionReturnReg = &bValue; - auto setA2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); + auto setA2 = std::make_shared(InstructionBase::Type::WriteVariable, false); setA2->targetVariable = &varA; setA2->args.push_back({ Compiler::StaticType::Unknown, &bValue }); list.addInstruction(setA2); // b = c - auto readC1 = std::make_shared(LLVMInstruction::Type::ReadVariable, false); + auto readC1 = std::make_shared(InstructionBase::Type::ReadVariable, false); readC1->targetVariable = &varC; list.addInstruction(readC1); - LLVMRegister cValue1(Compiler::StaticType::Unknown); - cValue1.isRawValue = false; - cValue1.instruction = readC1; + CompilerValue cValue1(Compiler::StaticType::Unknown); readC1->functionReturnReg = &cValue1; - auto setB2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); + auto setB2 = std::make_shared(InstructionBase::Type::WriteVariable, false); setB2->targetVariable = &varB; setB2->args.push_back({ Compiler::StaticType::Unknown, &cValue1 }); list.addInstruction(setB2); // c = "test" - auto setC2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister valueC2(Compiler::StaticType::String, "test"); + auto setC2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant valueC2(Compiler::StaticType::String, "test"); setC2->targetVariable = &varC; setC2->args.push_back({ Compiler::StaticType::Unknown, &valueC2 }); list.addInstruction(setC2); // loop end - auto loopEnd = std::make_shared(LLVMInstruction::Type::EndLoop, false); + auto loopEnd = std::make_shared(InstructionBase::Type::EndLoop, false); list.addInstruction(loopEnd); // a = c (final assignment we want to test) - auto readC2 = std::make_shared(LLVMInstruction::Type::ReadVariable, false); + auto readC2 = std::make_shared(InstructionBase::Type::ReadVariable, false); readC2->targetVariable = &varC; list.addInstruction(readC2); - LLVMRegister cValue2(Compiler::StaticType::Unknown); - cValue2.isRawValue = false; - cValue2.instruction = readC2; + CompilerValue cValue2(Compiler::StaticType::Unknown); readC2->functionReturnReg = &cValue2; - auto setA3 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); + auto setA3 = std::make_shared(InstructionBase::Type::WriteVariable, false); setA3->targetVariable = &varA; setA3->args.push_back({ Compiler::StaticType::Unknown, &cValue2 }); list.addInstruction(setA3); @@ -1370,36 +1337,34 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, ChainedAssignmentsInLoop) ASSERT_EQ(readC2->targetType, expectedCType); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, SelfAssignment) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, SelfAssignment) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); // First establish a type for the variable - auto setVar1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 42); + auto setVar1 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::Number, 42); setVar1->targetVariable = &var; setVar1->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar1); // Self-assignment: var = var - auto readVar = std::make_shared(LLVMInstruction::Type::ReadVariable, false); + auto readVar = std::make_shared(InstructionBase::Type::ReadVariable, false); readVar->targetVariable = &var; list.addInstruction(readVar); - LLVMRegister varValue(Compiler::StaticType::Unknown); - varValue.isRawValue = false; - varValue.instruction = readVar; + CompilerValue varValue(Compiler::StaticType::Unknown); readVar->functionReturnReg = &varValue; - auto setVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); + auto setVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); setVar2->targetVariable = &var; setVar2->args.push_back({ Compiler::StaticType::Unknown, &varValue }); list.addInstruction(setVar2); // Should still be Number - auto setVar3 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value3(Compiler::StaticType::String, "test"); + auto setVar3 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value3(Compiler::StaticType::String, "test"); setVar3->targetVariable = &var; setVar3->args.push_back({ Compiler::StaticType::Unknown, &value3 }); list.addInstruction(setVar3); @@ -1416,30 +1381,28 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, SelfAssignment) ASSERT_EQ(setVar3->targetType, Compiler::StaticType::Number); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, SelfAssignmentInLoop) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, SelfAssignmentInLoop) { - LLVMInstructionList list; + InstructionListBase list; Variable var("", ""); - auto loopStart = std::make_shared(LLVMInstruction::Type::BeginRepeatLoop, false); + auto loopStart = std::make_shared(InstructionBase::Type::BeginRepeatLoop, false); list.addInstruction(loopStart); // Self-assignment inside loop: var = var - auto readVar = std::make_shared(LLVMInstruction::Type::ReadVariable, false); + auto readVar = std::make_shared(InstructionBase::Type::ReadVariable, false); readVar->targetVariable = &var; list.addInstruction(readVar); - LLVMRegister varValue(Compiler::StaticType::Unknown); - varValue.isRawValue = false; - varValue.instruction = readVar; + CompilerValue varValue(Compiler::StaticType::Unknown); readVar->functionReturnReg = &varValue; - auto setVar = std::make_shared(LLVMInstruction::Type::WriteVariable, false); + auto setVar = std::make_shared(InstructionBase::Type::WriteVariable, false); setVar->targetVariable = &var; setVar->args.push_back({ Compiler::StaticType::Unknown, &varValue }); list.addInstruction(setVar); - auto loopEnd = std::make_shared(LLVMInstruction::Type::EndLoop, false); + auto loopEnd = std::make_shared(InstructionBase::Type::EndLoop, false); list.addInstruction(loopEnd); m_analyzer->analyzeScript(list); @@ -1449,27 +1412,25 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, SelfAssignmentInLoop) ASSERT_EQ(setVar->targetType, Compiler::StaticType::Unknown); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, VariableReadReturnRegType) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, VariableReadReturnRegType) { - LLVMInstructionList list; + InstructionListBase list; Variable var1("", ""), var2("", ""); - auto setVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 3.14); + auto setVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::Number, 3.14); setVar2->targetVariable = &var2; setVar2->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar2); - auto readVar2 = std::make_shared(LLVMInstruction::Type::ReadVariable, false); + auto readVar2 = std::make_shared(InstructionBase::Type::ReadVariable, false); readVar2->targetVariable = &var2; list.addInstruction(readVar2); - LLVMRegister var2Value(Compiler::StaticType::Unknown); - var2Value.isRawValue = false; - var2Value.instruction = readVar2; + CompilerValue var2Value(Compiler::StaticType::Unknown); readVar2->functionReturnReg = &var2Value; - auto setVar1_1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); + auto setVar1_1 = std::make_shared(InstructionBase::Type::WriteVariable, false); setVar1_1->targetVariable = &var1; setVar1_1->args.push_back({ Compiler::StaticType::Unknown, &var2Value }); list.addInstruction(setVar1_1); @@ -1480,27 +1441,25 @@ TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, VariableReadReturnRegType) ASSERT_EQ(var2Value.type(), Compiler::StaticType::Number); } -TEST_F(LLVMCodeAnalyzer_VariableTypeAnalysis, CrossVariableWriteArgType) +TEST_F(CodeAnalyzer_VariableTypeAnalysis, CrossVariableWriteArgType) { - LLVMInstructionList list; + InstructionListBase list; Variable var1("", ""), var2("", ""); - auto setVar2 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); - LLVMConstantRegister value1(Compiler::StaticType::Number, 3.14); + auto setVar2 = std::make_shared(InstructionBase::Type::WriteVariable, false); + CompilerConstant value1(Compiler::StaticType::Number, 3.14); setVar2->targetVariable = &var2; setVar2->args.push_back({ Compiler::StaticType::Unknown, &value1 }); list.addInstruction(setVar2); - auto readVar2 = std::make_shared(LLVMInstruction::Type::ReadVariable, false); + auto readVar2 = std::make_shared(InstructionBase::Type::ReadVariable, false); readVar2->targetVariable = &var2; list.addInstruction(readVar2); - LLVMRegister var2Value(Compiler::StaticType::Unknown); - var2Value.isRawValue = false; - var2Value.instruction = readVar2; + CompilerValue var2Value(Compiler::StaticType::Unknown); readVar2->functionReturnReg = &var2Value; - auto setVar1_1 = std::make_shared(LLVMInstruction::Type::WriteVariable, false); + auto setVar1_1 = std::make_shared(InstructionBase::Type::WriteVariable, false); setVar1_1->targetVariable = &var1; setVar1_1->args.push_back({ Compiler::StaticType::Unknown, &var2Value }); list.addInstruction(setVar1_1); diff --git a/test/llvm/llvmexecutablecode_test.cpp b/test/llvm/llvmexecutablecode_test.cpp index 1bb2fae01..6f14485b8 100644 --- a/test/llvm/llvmexecutablecode_test.cpp +++ b/test/llvm/llvmexecutablecode_test.cpp @@ -5,10 +5,10 @@ #include #include #include -#include -#include -#include -#include +#include +#include +#include +#include #include #include #include diff --git a/test/llvm/llvmexecutioncontext_test.cpp b/test/llvm/llvmexecutioncontext_test.cpp index a26e88299..37939d036 100644 --- a/test/llvm/llvmexecutioncontext_test.cpp +++ b/test/llvm/llvmexecutioncontext_test.cpp @@ -1,6 +1,6 @@ #include -#include -#include +#include +#include #include using namespace libscratchcpp; diff --git a/test/llvm/llvminstructionlist_test.cpp b/test/llvm/llvminstructionlist_test.cpp deleted file mode 100644 index 02535da28..000000000 --- a/test/llvm/llvminstructionlist_test.cpp +++ /dev/null @@ -1,366 +0,0 @@ -#include -#include -#include -#include - -using namespace libscratchcpp; - -TEST(LLVMInstructionListTest, EmptyList_First) -{ - LLVMInstructionList list; - ASSERT_EQ(list.first(), nullptr); -} - -TEST(LLVMInstructionListTest, EmptyList_Last) -{ - LLVMInstructionList list; - ASSERT_EQ(list.last(), nullptr); -} - -TEST(LLVMInstructionListTest, EmptyList_Empty) -{ - LLVMInstructionList list; - ASSERT_TRUE(list.empty()); -} - -TEST(LLVMInstructionListTest, EmptyList_ContainsNull) -{ - LLVMInstructionList list; - ASSERT_FALSE(list.containsInstruction(nullptr)); -} - -TEST(LLVMInstructionListTest, EmptyList_ContainsFunc) -{ - LLVMInstructionList list; - auto ins = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - ASSERT_FALSE(list.containsInstruction([](const LLVMInstruction *) { return true; })); -} - -TEST(LLVMInstructionListTest, EmptyList_Contains) -{ - LLVMInstructionList list; - auto ins = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - ASSERT_FALSE(list.containsInstruction(ins.get())); -} - -TEST(LLVMInstructionListTest, SingleInstructionList_ContainsExistent) -{ - LLVMInstructionList list; - - auto ins1 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins1); - - ASSERT_TRUE(list.containsInstruction(ins1.get())); -} - -TEST(LLVMInstructionListTest, SingleInstructionList_ContainsNonExistent) -{ - LLVMInstructionList list; - - auto ins1 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins1); - - auto ins = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - ASSERT_FALSE(list.containsInstruction(ins.get())); -} - -TEST(LLVMInstructionListTest, SingleInstructionList_ContainsNull) -{ - LLVMInstructionList list; - - auto ins1 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins1); - - ASSERT_FALSE(list.containsInstruction(nullptr)); -} - -TEST(LLVMInstructionListTest, SingleInstructionList_ContainsFuncTrue) -{ - LLVMInstructionList list; - - auto ins1 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins1); - - ASSERT_TRUE(list.containsInstruction([](const LLVMInstruction *) { return true; })); -} - -TEST(LLVMInstructionListTest, SingleInstructionList_ContainsFuncFalse) -{ - LLVMInstructionList list; - - auto ins1 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins1); - - auto ins = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - ASSERT_FALSE(list.containsInstruction([](const LLVMInstruction *) { return false; })); -} - -TEST(LLVMInstructionListTest, MultipleInstructionList_ContainsExistent) -{ - LLVMInstructionList list; - - auto ins1 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins1); - - auto ins2 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins2); - - auto ins3 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins3); - - ASSERT_TRUE(list.containsInstruction(ins2.get())); -} - -TEST(LLVMInstructionListTest, MultipleInstructionList_ContainsNonExistent) -{ - LLVMInstructionList list; - - auto ins1 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins1); - - auto ins2 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins2); - - auto ins3 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins3); - - auto ins = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - ASSERT_FALSE(list.containsInstruction(ins.get())); -} - -TEST(LLVMInstructionListTest, MultipleInstructionList_ContainsNull) -{ - LLVMInstructionList list; - - auto ins1 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins1); - - auto ins2 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins2); - - auto ins3 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins3); - - ASSERT_FALSE(list.containsInstruction(nullptr)); -} - -TEST(LLVMInstructionListTest, MultipleInstructionList_ContainsExistentFunc) -{ - LLVMInstructionList list; - - auto ins1 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins1); - - auto ins2 = std::make_shared(LLVMInstruction::Type::Stop, false); - list.addInstruction(ins2); - - auto ins3 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins3); - - ASSERT_TRUE(list.containsInstruction([](const LLVMInstruction *ins) { return ins->type == LLVMInstruction::Type::Stop; })); -} - -TEST(LLVMInstructionListTest, MultipleInstructionList_ContainsNonExistentFunc) -{ - LLVMInstructionList list; - - auto ins1 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins1); - - auto ins2 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins2); - - auto ins3 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins3); - - ASSERT_FALSE(list.containsInstruction([](const LLVMInstruction *ins) { return ins->type == LLVMInstruction::Type::Stop; })); -} - -TEST(LLVMInstructionListTest, AddSingleInstruction_First) -{ - LLVMInstructionList list; - auto ins = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins); - - ASSERT_EQ(list.first(), ins.get()); -} - -TEST(LLVMInstructionListTest, AddSingleInstruction_Last) -{ - LLVMInstructionList list; - auto ins = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins); - - ASSERT_EQ(list.last(), ins.get()); -} - -TEST(LLVMInstructionListTest, AddSingleInstruction_Empty) -{ - LLVMInstructionList list; - auto ins = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins); - - ASSERT_FALSE(list.empty()); -} - -TEST(LLVMInstructionListTest, AddSingleInstruction_Previous) -{ - LLVMInstructionList list; - auto ins = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins); - - ASSERT_EQ(ins->previous, nullptr); -} - -TEST(LLVMInstructionListTest, AddSingleInstruction_Next) -{ - LLVMInstructionList list; - auto ins = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins); - - ASSERT_EQ(ins->next, nullptr); -} - -TEST(LLVMInstructionListTest, AddMultipleInstructions_First) -{ - LLVMInstructionList list; - - auto ins1 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins1); - - auto ins2 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins2); - - auto ins3 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins3); - - ASSERT_EQ(list.first(), ins1.get()); -} - -TEST(LLVMInstructionListTest, AddMultipleInstructions_Last) -{ - LLVMInstructionList list; - - auto ins1 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins1); - - auto ins2 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins2); - - auto ins3 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins3); - - ASSERT_EQ(list.last(), ins3.get()); -} - -TEST(LLVMInstructionListTest, AddMultipleInstructions_Empty) -{ - LLVMInstructionList list; - - auto ins1 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins1); - - auto ins2 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins2); - - auto ins3 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins3); - - ASSERT_FALSE(list.empty()); -} - -TEST(LLVMInstructionListTest, AddMultipleInstructions_PreviousOfFirst) -{ - LLVMInstructionList list; - - auto ins1 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins1); - - auto ins2 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins2); - - auto ins3 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins3); - - ASSERT_EQ(ins1->previous, nullptr); -} - -TEST(LLVMInstructionListTest, AddMultipleInstructions_NextOfFirst) -{ - LLVMInstructionList list; - - auto ins1 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins1); - - auto ins2 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins2); - - auto ins3 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins3); - - ASSERT_EQ(ins1->next, ins2.get()); -} - -TEST(LLVMInstructionListTest, AddMultipleInstructions_PreviousOfMiddle) -{ - LLVMInstructionList list; - - auto ins1 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins1); - - auto ins2 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins2); - - auto ins3 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins3); - - ASSERT_EQ(ins2->previous, ins1.get()); -} - -TEST(LLVMInstructionListTest, AddMultipleInstructions_NextOfMiddle) -{ - LLVMInstructionList list; - - auto ins1 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins1); - - auto ins2 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins2); - - auto ins3 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins3); - - ASSERT_EQ(ins2->next, ins3.get()); -} - -TEST(LLVMInstructionListTest, AddMultipleInstructions_PreviousOfLast) -{ - LLVMInstructionList list; - - auto ins1 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins1); - - auto ins2 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins2); - - auto ins3 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins3); - - ASSERT_EQ(ins3->previous, ins2.get()); -} - -TEST(LLVMInstructionListTest, AddMultipleInstructions_NextOfLast) -{ - LLVMInstructionList list; - - auto ins1 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins1); - - auto ins2 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins2); - - auto ins3 = std::make_shared(LLVMInstruction::Type::FunctionCall, false); - list.addInstruction(ins3); - - ASSERT_EQ(ins3->next, nullptr); -} diff --git a/test/mocks/codebuilderfactorymock.h b/test/mocks/codebuilderfactorymock.h index d1d1d080c..4913525f0 100644 --- a/test/mocks/codebuilderfactorymock.h +++ b/test/mocks/codebuilderfactorymock.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include using namespace libscratchcpp; @@ -9,5 +9,5 @@ class CodeBuilderFactoryMock : public ICodeBuilderFactory { public: MOCK_METHOD(std::shared_ptr, create, (CompilerContext *, BlockPrototype *, Compiler::CodeType), (const, override)); - MOCK_METHOD(std::shared_ptr, createCtx, (IEngine *, Target *), (const, override)); + MOCK_METHOD(std::shared_ptr, createCtx, (IEngine *, Target *, const std::unordered_set &), (const, override)); }; diff --git a/test/mocks/codebuildermock.h b/test/mocks/codebuildermock.h index 2eb3e0d87..9a41bd5e9 100644 --- a/test/mocks/codebuildermock.h +++ b/test/mocks/codebuildermock.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include using namespace libscratchcpp;