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: 5 additions & 1 deletion .github/workflows/utests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
codegen_backend: ['llvm', 'cpp']

steps:
- uses: actions/checkout@v3
with:
Expand All @@ -24,7 +28,7 @@ jobs:
sudo apt-get install -y nlohmann-json3-dev libutfcpp-dev
shell: bash
- name: Configure CMake
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DLIBSCRATCHCPP_BUILD_UNIT_TESTS=ON -DLIBSCRATCHCPP_ENABLE_SANITIZER=ON
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DLIBSCRATCHCPP_BUILD_UNIT_TESTS=ON -DLIBSCRATCHCPP_ENABLE_SANITIZER=ON -DLIBSCRATCHCPP_CODEGEN_BACKEND=${{ matrix.codegen_backend }}

- name: Build
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} -j$(nproc --all)
Expand Down
31 changes: 21 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ option(LIBSCRATCHCPP_ENABLE_CODE_ANALYZER "Analyze Scratch scripts to enable var
option(LIBSCRATCHCPP_LLVM_INTEGER_SUPPORT "Use integers when possible to enable various optimizations" ON)
option(LIBSCRATCHCPP_ENABLE_SANITIZER "Enable sanitizer to detect memory issues" OFF)

set(LIBSCRATCHCPP_CODEGEN_BACKEND "llvm" CACHE STRING "The backend used for code generation")

if (LIBSCRATCHCPP_ENABLE_SANITIZER)
set(SANITIZER_FLAGS "-fsanitize=undefined -fsanitize=address -fno-omit-frame-pointer")
set(CMAKE_CFLAGS ${CMAKE_CFLAGS} ${SANITIZER_FLAGS})
Expand Down Expand Up @@ -130,20 +132,29 @@ if (LIBSCRATCHCPP_NETWORK_SUPPORT)
endif()

# LLVM
include(build/HunterPackages.cmake)
include(build/LLVM.cmake)
target_link_libraries(scratchcpp PRIVATE LLVM)
if (LIBSCRATCHCPP_CODEGEN_BACKEND STREQUAL "llvm")
include(build/HunterPackages.cmake)
include(build/LLVM.cmake)
target_link_libraries(scratchcpp PRIVATE LLVM)

if(LIBSCRATCHCPP_PRINT_LLVM_IR)
target_compile_definitions(scratchcpp PRIVATE PRINT_LLVM_IR)
endif()
target_compile_definitions(scratchcpp PRIVATE CODEGEN_BACKEND_LLVM)

if(LIBSCRATCHCPP_PRINT_LLVM_IR)
target_compile_definitions(scratchcpp PRIVATE PRINT_LLVM_IR)
endif()

if(LIBSCRATCHCPP_ENABLE_CODE_ANALYZER)
target_compile_definitions(scratchcpp PRIVATE ENABLE_CODE_ANALYZER)
if(LIBSCRATCHCPP_ENABLE_CODE_ANALYZER)
target_compile_definitions(scratchcpp PRIVATE ENABLE_CODE_ANALYZER)
endif()

if(LIBSCRATCHCPP_LLVM_INTEGER_SUPPORT)
target_compile_definitions(scratchcpp PRIVATE LLVM_INTEGER_SUPPORT)
endif()
endif()

if(LIBSCRATCHCPP_LLVM_INTEGER_SUPPORT)
target_compile_definitions(scratchcpp PRIVATE LLVM_INTEGER_SUPPORT)
# C++ transpiler
if (LIBSCRATCHCPP_CODEGEN_BACKEND STREQUAL "cpp")
target_compile_definitions(scratchcpp PRIVATE CODEGEN_BACKEND_CPP)
endif()

# Macros
Expand Down
8 changes: 7 additions & 1 deletion src/engine/internal/codegen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ target_sources(scratchcpp
codeanalyzer.h
optimizationutils.cpp
optimizationutils.h
typeutils.cpp
typeutils.h
)

add_subdirectory(llvm)
if (LIBSCRATCHCPP_CODEGEN_BACKEND STREQUAL "llvm")
add_subdirectory(llvm)
elseif (LIBSCRATCHCPP_CODEGEN_BACKEND STREQUAL "cpp")
add_subdirectory(cpp)
endif()
16 changes: 16 additions & 0 deletions src/engine/internal/codegen/codebuilderfactory.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
// SPDX-License-Identifier: Apache-2.0

#include "codebuilderfactory.h"

#ifdef CODEGEN_BACKEND_LLVM
#include "llvm/llvmcodebuilder.h"
#include "llvm/llvmcompilercontext.h"
#elif defined(CODEGEN_BACKEND_CPP)
#include "cpp/cppcodebuilder.h"
#include "cpp/cppcompilercontext.h"
#endif

using namespace libscratchcpp;

Expand All @@ -15,12 +21,22 @@ std::shared_ptr<CodeBuilderFactory> CodeBuilderFactory::instance()

std::shared_ptr<ICodeBuilder> CodeBuilderFactory::create(CompilerContext *ctx, BlockPrototype *procedurePrototype, Compiler::CodeType codeType) const
{
#ifdef CODEGEN_BACKEND_LLVM
assert(dynamic_cast<LLVMCompilerContext *>(ctx));
return std::make_shared<LLVMCodeBuilder>(static_cast<LLVMCompilerContext *>(ctx), procedurePrototype, codeType);
#elif defined(CODEGEN_BACKEND_CPP)
assert(dynamic_cast<CppCompilerContext *>(ctx));
return std::make_shared<CppCodeBuilder>(static_cast<CppCompilerContext *>(ctx), procedurePrototype, codeType);
#endif
}

std::shared_ptr<CompilerContext> CodeBuilderFactory::createCtx(IEngine *engine, Target *target, const std::unordered_set<std::string> &unsafeConstants) const
{
#ifdef CODEGEN_BACKEND_LLVM
auto ptr = std::make_shared<LLVMCompilerContext>(engine, target, unsafeConstants);
return ptr;
#elif defined(CODEGEN_BACKEND_CPP)
auto ptr = std::make_shared<CppCompilerContext>(engine, target, unsafeConstants);
return ptr;
#endif
}
15 changes: 15 additions & 0 deletions src/engine/internal/codegen/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
target_sources(scratchcpp
PRIVATE
cppcompilercontext.cpp
cppcompilercontext.h
cppexecutablecode.cpp
cppexecutablecode.h
cppexecutioncontext.cpp
cppexecutioncontext.h
cppinstruction.h
cppinstructionlist.h
cppvalue.h
cppconstant.h
cppcodebuilder.cpp
cppcodebuilder.h
)
Loading
Loading