Skip to content

Commit

Permalink
Merge pull request #155 from fwsGonzo/embed_libtcc
Browse files Browse the repository at this point in the history
bintr: Embed libtcc properly and build libtcc1.a
  • Loading branch information
fwsGonzo committed Jun 14, 2024
2 parents f2e8c61 + 26a6d21 commit 2366f66
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/bintr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Install dependencies
run: |
sudo apt update
sudo apt install -y gcc-12-riscv64-linux-gnu g++-12-riscv64-linux-gnu gcc g++ libtcc-dev cmake
sudo apt install -y gcc-12-riscv64-linux-gnu g++-12-riscv64-linux-gnu gcc g++ cmake
- name: Configure emulator
env:
Expand Down
56 changes: 56 additions & 0 deletions .github/workflows/packaging_libtcc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Debian Packaging w/libtcc JIT

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

env:
BUILD_TYPE: RelWithDebInfo

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
compiler: [g++, clang++-17]
steps:
- uses: actions/checkout@v2

- name: Install dependencies
run: |
sudo apt update
sudo apt install -y g++ clang
- name: Install newer Clang
run: |
wget https://apt.llvm.org/llvm.sh
chmod +x ./llvm.sh
sudo ./llvm.sh 17
- name: CMake configuration
working-directory: ${{github.workspace}}/
env:
CXX: ${{ matrix.compiler }}

# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DRISCV_BINARY_TRANSLATION=ON -DRISCV_LIBTCC=ON

- name: Build and packaging
run: |
cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
pushd build
cpack -G DEB
popd
pushd packages
sudo dpkg -i *.deb
popd
- name: Build and run the package example
run: |
pushd examples/package
bash build_and_run.sh
popd
4 changes: 2 additions & 2 deletions .github/workflows/unittests_libtcc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Install dependencies
run: |
sudo apt update
sudo apt install -y gcc-12-riscv64-linux-gnu g++-12-riscv64-linux-gnu libtcc-dev tcc
sudo apt install -y gcc-12-riscv64-linux-gnu g++-12-riscv64-linux-gnu
git submodule update --init ${{github.workspace}}/tests/Catch2
git submodule update --init ${{github.workspace}}/tests/unit/ext/lodepng
Expand All @@ -31,4 +31,4 @@ jobs:

- name: Run tests
working-directory: ${{github.workspace}}/build
run: CFLAGS=-O0 ctest --verbose . -j4
run: ctest --verbose . -j4
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ riscv32-unknown-elf/
emulator/*rvmicro
emulator/*rvnewlib
emulator/*rvlinux
emulator/libtcc1.a
nodiverge
tests/autotest/emulator/emulator
binaries/crc32/clmul
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM ubuntu:latest

RUN apt update && apt install -y \
cmake \
cmake git \
clang-18 \
tcc libtcc-dev \
g++-13-riscv64-linux-gnu
Expand All @@ -16,7 +16,7 @@ COPY binaries/measure_mips/fib.c /app/emulator/fib.c

# Fast emulation (with TCC JIT compilation)
WORKDIR /app/emulator
RUN ./build.sh --tcc && cp .build/rvlinux /app/rvlinux
RUN ./build.sh --tcc && cp .build/rvlinux /app/rvlinux && cp .build/libtcc1.a /app/libtcc1.a

# Fastest emulator (with binary translation)
WORKDIR /app/emulator
Expand Down
3 changes: 3 additions & 0 deletions emulator/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,7 @@ fi
if test -f ".build/rvnewlib"; then
ln -fs .build/rvnewlib .
fi
if test -f ".build/libtcc1.a"; then
ln -fs .build/libtcc1.a .
fi
ln -fs .build/rvlinux .
49 changes: 38 additions & 11 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,11 @@ if (RISCV_BINARY_TRANSLATION)
endif()

configure_file(libriscv_settings.h.in ${CMAKE_CURRENT_BINARY_DIR}/libriscv_settings.h)
configure_file(libriscv.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libriscv.pc)
if (RISCV_LIBTCC)
configure_file(libriscv_libtcc.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libriscv.pc)
else()
configure_file(libriscv.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libriscv.pc)
endif()

add_library(riscv ${SOURCES})
target_compile_features(riscv PUBLIC cxx_std_20)
Expand All @@ -161,16 +165,29 @@ endif()


if (RISCV_LIBTCC)
#include(FetchContent)
#FetchContent_Declare(
# libtcc
# GIT_REPOSITORY https://github.com/fwsGonzo/libtcc-cmake
# GIT_TAG master
#)
#FetchContent_MakeAvailable(libtcc)
#target_compile_definitions(libtcc PUBLIC LIBTCC1_NAME="libtcc1.a")

target_link_libraries(riscv PUBLIC libtcc.a)
if(CMAKE_VERSION VERSION_LESS "3.14.0")
target_link_libraries(riscv PUBLIC libtcc.a dl)
target_compile_definitions(riscv PUBLIC LIBTCC_PACKAGE=1)
set(LIBTCC_FROM_PACKAGE TRUE)
else()
include(FetchContent)
FetchContent_Declare(
libtcc
GIT_REPOSITORY https://github.com/fwsGonzo/libtcc-cmake
GIT_TAG master
)
FetchContent_MakeAvailable(libtcc)
target_compile_definitions(libtcc PUBLIC LIBTCC1_NAME="libtcc1.a")
target_link_libraries(riscv PUBLIC libtcc libtcc1)
set(LIBTCC_FROM_PACKAGE FALSE)

# Installed version will need to find libtcc
if (${CMAKE_PROJECT_NAME} STREQUAL "libriscv")
target_compile_definitions(riscv PUBLIC
LIBTCC_LIBRARY_PATH="/usr/lib/libriscv"
)
endif()
endif()
endif()

if (${CMAKE_PROJECT_NAME} STREQUAL "libriscv")
Expand Down Expand Up @@ -235,4 +252,14 @@ if (${CMAKE_PROJECT_NAME} STREQUAL "libriscv")
FILES ${CMAKE_CURRENT_BINARY_DIR}/libriscv.pc
DESTINATION lib/pkgconfig
)
if (RISCV_LIBTCC)
# Install our own copy of libtcc, if we're not using the system package
if (NOT LIBTCC_FROM_PACKAGE)
install(FILES
${CMAKE_BINARY_DIR}/_deps/libtcc-build/libtcc.a
${CMAKE_BINARY_DIR}/libtcc1.a
DESTINATION lib/${PROJECT_NAME}
)
endif()
endif()
endif()
2 changes: 1 addition & 1 deletion lib/libriscv/machine_defaults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace riscv
return 0;
}

// Default: RDTIME produces monotonic time with *millisecond*-granularity
// Default: RDTIME produces monotonic time with *microsecond*-granularity
template <int W>
uint64_t Machine<W>::default_rdtime(const Machine<W>&) {
auto now = std::chrono::steady_clock::now();
Expand Down
13 changes: 12 additions & 1 deletion lib/libriscv/tr_tcc.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
#include "common.hpp"
#include <libtcc.h>

#ifdef LIBTCC_PACKAGE
# include <libtcc.h>
#else
# include <tcc/libtcc.h>
# ifndef LIBTCC_LIBRARY_PATH
# define LIBTCC_LIBRARY_PATH "."
# endif
#endif

namespace riscv
{
Expand All @@ -18,6 +26,9 @@ namespace riscv

if (!libtcc1.empty())
tcc_add_library_path(state, libtcc1.c_str());
#ifdef LIBTCC_LIBRARY_PATH
tcc_add_library_path(state, LIBTCC_LIBRARY_PATH);
#endif

if (tcc_compile_string(state, code.c_str()) < 0) {
tcc_delete(state);
Expand Down
28 changes: 28 additions & 0 deletions lib/libriscv/tr_translate.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#if __has_include(<bit>)
#include <bit>
#define RISCV_HAS_BITOPS
#endif
#include <cmath>
#include <chrono>
#include <mutex>
Expand All @@ -16,6 +19,7 @@ extern "C" int unlink(const char* path);
# endif
#else
# include <dlfcn.h>
# include <unistd.h>
#endif
#include "machine.hpp"
#include "decoder_cache.hpp"
Expand Down Expand Up @@ -736,22 +740,46 @@ bool CPU<W>::initialize_translated_segment(DecodedExecuteSegment<W>&, void* dyli
return std::sqrt(d);
},
.clz = [] (uint32_t x) -> int {
#ifdef RISCV_HAS_BITOPS
return std::countl_zero(x);
#else
return x ? __builtin_clz(x) : 32;
#endif
},
.clzl = [] (uint64_t x) -> int {
#ifdef RISCV_HAS_BITOPS
return std::countl_zero(x);
#else
return x ? __builtin_clzl(x) : 64;
#endif
},
.ctz = [] (uint32_t x) -> int {
#ifdef RISCV_HAS_BITOPS
return std::countr_zero(x);
#else
return x ? __builtin_ctz(x) : 0;
#endif
},
.ctzl = [] (uint64_t x) -> int {
#ifdef RISCV_HAS_BITOPS
return std::countr_zero(x);
#else
return x ? __builtin_ctzl(x) : 0;
#endif
},
.cpop = [] (uint32_t x) -> int {
#ifdef RISCV_HAS_BITOPS
return std::popcount(x);
#else
return __builtin_popcount(x);
#endif
},
.cpopl = [] (uint64_t x) -> int {
#ifdef RISCV_HAS_BITOPS
return std::popcount(x);
#else
return __builtin_popcountl(x);
#endif
},
});

Expand Down
6 changes: 6 additions & 0 deletions lib/libriscv_libtcc.pc.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Name: @CMAKE_PROJECT_NAME@
Description: @CMAKE_PROJECT_DESCRIPTION@
URL: https://github.com/fwsGonzo/libriscv
Version: @PROJECT_VERSION@
Cflags: -I/usr/include
Libs: -L/usr/lib -lriscv -l:libriscv/libtcc.a

0 comments on commit 2366f66

Please sign in to comment.