Skip to content

Commit 6b095a0

Browse files
authored
Merge pull request #699 from scratchcpp/cpp_architecture
Prepare initial C++ transpiler architecture
2 parents 34338c9 + b07f9de commit 6b095a0

41 files changed

Lines changed: 1255 additions & 80 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/utests.yml‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ jobs:
1313
build:
1414
runs-on: ubuntu-latest
1515

16+
strategy:
17+
matrix:
18+
codegen_backend: ['llvm', 'cpp']
19+
1620
steps:
1721
- uses: actions/checkout@v3
1822
with:
@@ -24,7 +28,7 @@ jobs:
2428
sudo apt-get install -y nlohmann-json3-dev libutfcpp-dev
2529
shell: bash
2630
- name: Configure CMake
27-
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DLIBSCRATCHCPP_BUILD_UNIT_TESTS=ON -DLIBSCRATCHCPP_ENABLE_SANITIZER=ON
31+
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 }}
2832

2933
- name: Build
3034
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} -j$(nproc --all)

‎CMakeLists.txt‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ option(LIBSCRATCHCPP_ENABLE_CODE_ANALYZER "Analyze Scratch scripts to enable var
1313
option(LIBSCRATCHCPP_LLVM_INTEGER_SUPPORT "Use integers when possible to enable various optimizations" ON)
1414
option(LIBSCRATCHCPP_ENABLE_SANITIZER "Enable sanitizer to detect memory issues" OFF)
1515

16+
set(LIBSCRATCHCPP_CODEGEN_BACKEND "llvm" CACHE STRING "The backend used for code generation")
17+
1618
if (LIBSCRATCHCPP_ENABLE_SANITIZER)
1719
set(SANITIZER_FLAGS "-fsanitize=undefined -fsanitize=address -fno-omit-frame-pointer")
1820
set(CMAKE_CFLAGS ${CMAKE_CFLAGS} ${SANITIZER_FLAGS})
@@ -130,20 +132,29 @@ if (LIBSCRATCHCPP_NETWORK_SUPPORT)
130132
endif()
131133

132134
# LLVM
133-
include(build/HunterPackages.cmake)
134-
include(build/LLVM.cmake)
135-
target_link_libraries(scratchcpp PRIVATE LLVM)
135+
if (LIBSCRATCHCPP_CODEGEN_BACKEND STREQUAL "llvm")
136+
include(build/HunterPackages.cmake)
137+
include(build/LLVM.cmake)
138+
target_link_libraries(scratchcpp PRIVATE LLVM)
136139

137-
if(LIBSCRATCHCPP_PRINT_LLVM_IR)
138-
target_compile_definitions(scratchcpp PRIVATE PRINT_LLVM_IR)
139-
endif()
140+
target_compile_definitions(scratchcpp PRIVATE CODEGEN_BACKEND_LLVM)
141+
142+
if(LIBSCRATCHCPP_PRINT_LLVM_IR)
143+
target_compile_definitions(scratchcpp PRIVATE PRINT_LLVM_IR)
144+
endif()
140145

141-
if(LIBSCRATCHCPP_ENABLE_CODE_ANALYZER)
142-
target_compile_definitions(scratchcpp PRIVATE ENABLE_CODE_ANALYZER)
146+
if(LIBSCRATCHCPP_ENABLE_CODE_ANALYZER)
147+
target_compile_definitions(scratchcpp PRIVATE ENABLE_CODE_ANALYZER)
148+
endif()
149+
150+
if(LIBSCRATCHCPP_LLVM_INTEGER_SUPPORT)
151+
target_compile_definitions(scratchcpp PRIVATE LLVM_INTEGER_SUPPORT)
152+
endif()
143153
endif()
144154

145-
if(LIBSCRATCHCPP_LLVM_INTEGER_SUPPORT)
146-
target_compile_definitions(scratchcpp PRIVATE LLVM_INTEGER_SUPPORT)
155+
# C++ transpiler
156+
if (LIBSCRATCHCPP_CODEGEN_BACKEND STREQUAL "cpp")
157+
target_compile_definitions(scratchcpp PRIVATE CODEGEN_BACKEND_CPP)
147158
endif()
148159

149160
# Macros

‎src/engine/internal/codegen/CMakeLists.txt‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ target_sources(scratchcpp
1313
codeanalyzer.h
1414
optimizationutils.cpp
1515
optimizationutils.h
16+
typeutils.cpp
17+
typeutils.h
1618
)
1719

18-
add_subdirectory(llvm)
20+
if (LIBSCRATCHCPP_CODEGEN_BACKEND STREQUAL "llvm")
21+
add_subdirectory(llvm)
22+
elseif (LIBSCRATCHCPP_CODEGEN_BACKEND STREQUAL "cpp")
23+
add_subdirectory(cpp)
24+
endif()

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
// SPDX-License-Identifier: Apache-2.0
22

33
#include "codebuilderfactory.h"
4+
5+
#ifdef CODEGEN_BACKEND_LLVM
46
#include "llvm/llvmcodebuilder.h"
57
#include "llvm/llvmcompilercontext.h"
8+
#elif defined(CODEGEN_BACKEND_CPP)
9+
#include "cpp/cppcodebuilder.h"
10+
#include "cpp/cppcompilercontext.h"
11+
#endif
612

713
using namespace libscratchcpp;
814

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

1622
std::shared_ptr<ICodeBuilder> CodeBuilderFactory::create(CompilerContext *ctx, BlockPrototype *procedurePrototype, Compiler::CodeType codeType) const
1723
{
24+
#ifdef CODEGEN_BACKEND_LLVM
1825
assert(dynamic_cast<LLVMCompilerContext *>(ctx));
1926
return std::make_shared<LLVMCodeBuilder>(static_cast<LLVMCompilerContext *>(ctx), procedurePrototype, codeType);
27+
#elif defined(CODEGEN_BACKEND_CPP)
28+
assert(dynamic_cast<CppCompilerContext *>(ctx));
29+
return std::make_shared<CppCodeBuilder>(static_cast<CppCompilerContext *>(ctx), procedurePrototype, codeType);
30+
#endif
2031
}
2132

2233
std::shared_ptr<CompilerContext> CodeBuilderFactory::createCtx(IEngine *engine, Target *target, const std::unordered_set<std::string> &unsafeConstants) const
2334
{
35+
#ifdef CODEGEN_BACKEND_LLVM
2436
auto ptr = std::make_shared<LLVMCompilerContext>(engine, target, unsafeConstants);
2537
return ptr;
38+
#elif defined(CODEGEN_BACKEND_CPP)
39+
auto ptr = std::make_shared<CppCompilerContext>(engine, target, unsafeConstants);
40+
return ptr;
41+
#endif
2642
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
target_sources(scratchcpp
2+
PRIVATE
3+
cppcompilercontext.cpp
4+
cppcompilercontext.h
5+
cppexecutablecode.cpp
6+
cppexecutablecode.h
7+
cppexecutioncontext.cpp
8+
cppexecutioncontext.h
9+
cppinstruction.h
10+
cppinstructionlist.h
11+
cppvalue.h
12+
cppconstant.h
13+
cppcodebuilder.cpp
14+
cppcodebuilder.h
15+
)

0 commit comments

Comments
 (0)