Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ option(LIBSCRATCHCPP_BUILD_UNIT_TESTS "Build unit tests" ON)
option(LIBSCRATCHCPP_NETWORK_SUPPORT "Support for downloading projects" ON)
option(LIBSCRATCHCPP_PRINT_LLVM_IR "Print LLVM IR of compiled Scratch scripts (for debugging)" OFF)

if (CMAKE_BUILD_TYPE MATCHES "Debug")
set(SANITIZER_FLAGS "-fsanitize=undefined -fsanitize=address -fno-omit-frame-pointer")
set(CMAKE_CFLAGS ${CMAKE_CFLAGS} ${SANITIZER_FLAGS})
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} ${SANITIZER_FLAGS})
endif()

add_library(scratchcpp SHARED)
add_subdirectory(src)
include_directories(src) # TODO: Remove this line
Expand Down
2 changes: 1 addition & 1 deletion include/scratchcpp/compilerconstant.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace libscratchcpp
class CompilerConstantPrivate;

/*! \brief The CompilerConstant class represents a constant value in compiled code. */
class LIBSCRATCHCPP_EXPORT CompilerConstant : public CompilerValue
class LIBSCRATCHCPP_EXPORT CompilerConstant : public virtual CompilerValue
{
public:
CompilerConstant(Compiler::StaticType type, const Value &value);
Expand Down
1 change: 0 additions & 1 deletion src/engine/internal/llvm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ target_sources(scratchcpp
PRIVATE
llvmcodebuilder.cpp
llvmcodebuilder.h
llvmregisterbase.h
llvmregister.h
llvmconstantregister.h
llvminstruction.h
Expand Down
20 changes: 10 additions & 10 deletions src/engine/internal/llvm/llvmcodebuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
llvm::Value *inRange = m_builder.CreateAnd(m_builder.CreateFCmpOGE(index, min), m_builder.CreateFCmpOLT(index, size));

LLVMConstantRegister nullReg(listPtr.type == Compiler::StaticType::Unknown ? Compiler::StaticType::Number : listPtr.type, Value());
llvm::Value *null = createValue(static_cast<LLVMRegister *>(static_cast<CompilerValue *>(&nullReg)));
llvm::Value *null = createValue(static_cast<LLVMRegister *>(&nullReg));

index = m_builder.CreateFPToUI(index, m_builder.getInt64Ty());
step.functionReturnReg->value = m_builder.CreateSelect(inRange, getListItem(listPtr, index), null);
Expand Down Expand Up @@ -1378,7 +1378,7 @@ CompilerValue *LLVMCodeBuilder::addFunctionCall(const std::string &functionName,
ins->functionName = functionName;

for (size_t i = 0; i < args.size(); i++)
ins->args.push_back({ argTypes[i], static_cast<LLVMRegister *>(args[i]) });
ins->args.push_back({ argTypes[i], dynamic_cast<LLVMRegister *>(args[i]) });

if (returnType != Compiler::StaticType::Void) {
auto reg = std::make_shared<LLVMRegister>(returnType);
Expand Down Expand Up @@ -1409,9 +1409,9 @@ CompilerValue *LLVMCodeBuilder::addFunctionCallWithCtx(const std::string &functi
CompilerConstant *LLVMCodeBuilder::addConstValue(const Value &value)
{
auto constReg = std::make_shared<LLVMConstantRegister>(TYPE_MAP[value.type()], value);
auto reg = std::reinterpret_pointer_cast<LLVMRegister>(constReg);
auto reg = std::static_pointer_cast<LLVMRegister>(constReg);
m_lastConstValue = reg.get();
return static_cast<CompilerConstant *>(static_cast<CompilerValue *>(addReg(reg, nullptr)));
return static_cast<CompilerConstant *>(static_cast<LLVMConstantRegister *>(addReg(reg, nullptr)));
}

CompilerValue *LLVMCodeBuilder::addStringChar(CompilerValue *string, CompilerValue *index)
Expand Down Expand Up @@ -1470,7 +1470,7 @@ CompilerValue *LLVMCodeBuilder::addListItem(List *list, CompilerValue *index)
if (m_listPtrs.find(list) == m_listPtrs.cend())
m_listPtrs[list] = LLVMListPtr();

ins->args.push_back({ Compiler::StaticType::Number, static_cast<LLVMRegister *>(index) });
ins->args.push_back({ Compiler::StaticType::Number, dynamic_cast<LLVMRegister *>(index) });

auto ret = std::make_shared<LLVMRegister>(Compiler::StaticType::Unknown);
ret->isRawValue = false;
Expand Down Expand Up @@ -1807,7 +1807,7 @@ void LLVMCodeBuilder::createListReplace(List *list, CompilerValue *index, Compil
void LLVMCodeBuilder::beginIfStatement(CompilerValue *cond)
{
auto ins = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::BeginIf, currentLoopScope(), m_loopCondition);
ins->args.push_back({ Compiler::StaticType::Bool, static_cast<LLVMRegister *>(cond) });
ins->args.push_back({ Compiler::StaticType::Bool, dynamic_cast<LLVMRegister *>(cond) });
m_instructions.push_back(ins);
}

Expand All @@ -1826,7 +1826,7 @@ void LLVMCodeBuilder::beginRepeatLoop(CompilerValue *count)
assert(!m_loopCondition);

auto ins = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::BeginRepeatLoop, currentLoopScope(), m_loopCondition);
ins->args.push_back({ Compiler::StaticType::Number, static_cast<LLVMRegister *>(count) });
ins->args.push_back({ Compiler::StaticType::Number, dynamic_cast<LLVMRegister *>(count) });
m_instructions.push_back(ins);
pushLoopScope(false);
}
Expand All @@ -1837,7 +1837,7 @@ void LLVMCodeBuilder::beginWhileLoop(CompilerValue *cond)
m_loopCondition = false;

auto ins = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::BeginWhileLoop, currentLoopScope(), m_loopCondition);
ins->args.push_back({ Compiler::StaticType::Bool, static_cast<LLVMRegister *>(cond) });
ins->args.push_back({ Compiler::StaticType::Bool, dynamic_cast<LLVMRegister *>(cond) });
m_instructions.push_back(ins);
pushLoopScope(false);
}
Expand All @@ -1848,7 +1848,7 @@ void LLVMCodeBuilder::beginRepeatUntilLoop(CompilerValue *cond)
m_loopCondition = false;

auto ins = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::BeginRepeatUntilLoop, currentLoopScope(), m_loopCondition);
ins->args.push_back({ Compiler::StaticType::Bool, static_cast<LLVMRegister *>(cond) });
ins->args.push_back({ Compiler::StaticType::Bool, dynamic_cast<LLVMRegister *>(cond) });
m_instructions.push_back(ins);
pushLoopScope(false);
}
Expand Down Expand Up @@ -2687,7 +2687,7 @@ LLVMRegister *LLVMCodeBuilder::createOp(const LLVMInstruction &ins, Compiler::St
m_instructions.push_back(createdIns);

for (size_t i = 0; i < args.size(); i++)
createdIns->args.push_back({ argTypes[i], static_cast<LLVMRegister *>(args[i]) });
createdIns->args.push_back({ argTypes[i], dynamic_cast<LLVMRegister *>(args[i]) });

if (retType != Compiler::StaticType::Void) {
auto ret = std::make_shared<LLVMRegister>(retType);
Expand Down
11 changes: 6 additions & 5 deletions src/engine/internal/llvm/llvmconstantregister.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ namespace libscratchcpp
{

struct LLVMConstantRegister
: public LLVMRegisterBase
, public CompilerConstant
: public CompilerConstant
, public LLVMRegister
{
LLVMConstantRegister(Compiler::StaticType type, const Value &value) :
LLVMRegisterBase(),
CompilerConstant(type, value)
CompilerValue(type),
CompilerConstant(type, value),
LLVMRegister(type)
{
}

const Value &constValue() const override { return CompilerConstant::value(); }
const Value &constValue() const override final { return CompilerConstant::value(); }
};

} // namespace libscratchcpp
24 changes: 12 additions & 12 deletions src/engine/internal/llvm/llvmregister.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

#pragma once

#include "llvmregisterbase.h"
#include <scratchcpp/compilervalue.h>
#include <scratchcpp/value.h>

namespace llvm
{
Expand All @@ -14,25 +15,24 @@ class Value;
namespace libscratchcpp
{

struct LLVMRegister
: public LLVMRegisterBase
, public CompilerValue
class LLVMInstruction;

struct LLVMRegister : public virtual CompilerValue
{
LLVMRegister(Compiler::StaticType type) :
LLVMRegisterBase(),
CompilerValue(type)
{
}

const Value &constValue() const override
virtual const Value &constValue() const
{
if (isConst())
return static_cast<const CompilerConstant *>(static_cast<const CompilerValue *>(this))->value();
else {
static const Value null = Value();
return null;
}
static const Value null = Value();
return null;
}

llvm::Value *value = nullptr;
bool isRawValue = false;
std::shared_ptr<LLVMInstruction> instruction;
};

} // namespace libscratchcpp
29 changes: 0 additions & 29 deletions src/engine/internal/llvm/llvmregisterbase.h

This file was deleted.

1 change: 1 addition & 0 deletions test/llvm/llvmcodebuilder_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <scratchcpp/variable.h>
#include <scratchcpp/list.h>
#include <scratchcpp/blockprototype.h>
#include <scratchcpp/compilerconstant.h>
#include <engine/internal/llvm/llvmcodebuilder.h>
#include <engine/internal/llvm/llvmcompilercontext.h>
#include <gmock/gmock.h>
Expand Down