Skip to content

Commit

Permalink
refactor: move uarch generated files to uarch source directory
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Feb 2, 2024
1 parent 7932e52 commit 388ebff
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 70 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ jobs:
path: |
uarch-ram.bin
uarch-pristine-ram.c
uarch-pristine-state-hash.cpp
uarch-pristine-hash.c
cartesi-machine-v${{ env.MACHINE_EMULATOR_VERSION }}_amd64.deb
cartesi-machine-v${{ env.MACHINE_EMULATOR_VERSION }}_arm64.deb
Expand Down Expand Up @@ -686,5 +686,5 @@ jobs:
artifacts/cartesi-machine-*.deb
artifacts/uarch-ram.bin
artifacts/uarch-pristine-ram.c
artifacts/uarch-pristine-state-hash.cpp
artifacts/uarch-pristine-hash.c
uarch-logs/uarch-riscv-tests-json-logs-*.tar.gz
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ build
pkg
third-party/downloads
third-party/mongoose-*
src/compute-uarch-pristine-hash
src/remote-cartesi-machine
src/jsonrpc-remote-cartesi-machine
src/merkle-tree-hash
Expand Down
39 changes: 25 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ copy:
ID=`docker create $(DOCKER_PLATFORM) $(DEBIAN_IMG)` && \
docker cp $$ID:/usr/src/emulator/$(DEB_FILENAME) . && \
docker cp $$ID:/usr/src/emulator/uarch/uarch-ram.bin . && \
docker cp $$ID:/usr/src/emulator/uarch/uarch-pristine-ram.c . && \
docker cp $$ID:/usr/src/emulator/uarch/uarch-pristine-hash.c . && \
docker rm $$ID

check-linux-env:
Expand Down Expand Up @@ -325,30 +327,39 @@ $(BIN_INSTALL_PATH) $(LIB_INSTALL_PATH) $(LUA_INSTALL_PATH) $(LUA_INSTALL_CPATH)
install-headers: $(INC_INSTALL_PATH)
$(INSTALL_FILE) $(EMU_TO_INC) $(INC_INSTALL_PATH)

install-emulator: $(BIN_INSTALL_PATH) $(LIB_INSTALL_PATH) $(LUA_INSTALL_CPATH)/cartesi $(LUA_INSTALL_PATH)/cartesi $(IMAGES_INSTALL_PATH)
$(INSTALL_EXEC) $(EMU_TO_BIN) $(BIN_INSTALL_PATH)
$(INSTALL_EXEC) $(EMU_TO_LIB) $(LIB_INSTALL_PATH)
install-static-libs: $(LIB_INSTALL_PATH)
$(INSTALL_FILE) $(EMU_TO_LIB_A) $(LIB_INSTALL_PATH)
$(STRIP_STATIC) $(subst src/,$(LIB_INSTALL_PATH)/,$(EMU_TO_LIB_A))

install-shared-libs: $(LIB_INSTALL_PATH)
$(INSTALL_EXEC) $(EMU_TO_LIB) $(LIB_INSTALL_PATH)
$(SYMLINK) $(LIBCARTESI_SO) $(LIB_INSTALL_PATH)/$(LIBCARTESI)
$(SYMLINK) $(LIBCARTESI_SO_GRPC) $(LIB_INSTALL_PATH)/$(LIBCARTESI_GRPC)
$(SYMLINK) $(LIBCARTESI_SO_JSONRPC) $(LIB_INSTALL_PATH)/$(LIBCARTESI_JSONRPC)
$(STRIP_SHARED) $(subst src/,$(LIB_INSTALL_PATH)/,$(EMU_TO_LIB))

install-lua-libs: $(LUA_INSTALL_PATH)/cartesi $(LUA_INSTALL_CPATH)/cartesi
$(INSTALL_FILE) $(EMU_LUA_TO_BIN) $(LUA_INSTALL_PATH)
$(INSTALL_FILE) $(EMU_TO_LUA_PATH) $(LUA_INSTALL_PATH)/cartesi
$(INSTALL_EXEC) $(EMU_TO_LUA_CPATH) $(LUA_INSTALL_CPATH)
$(INSTALL_EXEC) $(EMU_TO_LUA_CARTESI_CPATH) $(LUA_INSTALL_CPATH)/cartesi
$(INSTALL_FILE) $(EMU_TO_LUA_PATH) $(LUA_INSTALL_PATH)/cartesi
$(STRIP_SHARED) $(subst src/,$(LUA_INSTALL_CPATH)/,$(EMU_TO_LUA_CPATH))
$(STRIP_SHARED) $(subst src/,$(LUA_INSTALL_CPATH)/,$(EMU_TO_LUA_CARTESI_CPATH))

install-bins: $(BIN_INSTALL_PATH)
$(INSTALL_EXEC) $(EMU_TO_BIN) $(BIN_INSTALL_PATH)
$(STRIP_BINARY) $(subst src/,$(BIN_INSTALL_PATH)/,$(EMU_TO_BIN))

install-lua-bins: $(BIN_INSTALL_PATH)
cat tools/template/cartesi-machine.template | sed 's|ARG_LUA_PATH|$(LUA_RUNTIME_PATH)/?.lua|g;s|ARG_LUA_CPATH|$(LUA_RUNTIME_CPATH)/?.so|g;s|ARG_IMAGES_PATH|$(IMAGES_RUNTIME_PATH)|g;s|ARG_LUA_RUNTIME_PATH|$(LUA_RUNTIME_PATH)|g' > $(BIN_INSTALL_PATH)/cartesi-machine
cat tools/template/cartesi-machine-stored-hash.template | sed 's|ARG_LUA_PATH|$(LUA_RUNTIME_PATH)/?.lua|g;s|ARG_LUA_CPATH|$(LUA_RUNTIME_CPATH)/?.so|g;s|ARG_LUA_RUNTIME_PATH|$(LUA_RUNTIME_PATH)|g' > $(BIN_INSTALL_PATH)/cartesi-machine-stored-hash
cat tools/template/rollup-memory-range.template | sed 's|ARG_LUA_PATH|$(LUA_RUNTIME_PATH)/?.lua|g;s|ARG_LUA_CPATH|$(LUA_RUNTIME_CPATH)/?.so|g;s|ARG_LUA_RUNTIME_PATH|$(LUA_RUNTIME_PATH)|g' > $(BIN_INSTALL_PATH)/rollup-memory-range
$(CHMOD_EXEC) $(BIN_INSTALL_PATH)/cartesi-machine $(BIN_INSTALL_PATH)/cartesi-machine-stored-hash $(BIN_INSTALL_PATH)/rollup-memory-range
$(SYMLINK) $(LIBCARTESI_SO) $(LIB_INSTALL_PATH)/$(LIBCARTESI)
$(SYMLINK) $(LIBCARTESI_SO_GRPC) $(LIB_INSTALL_PATH)/$(LIBCARTESI_GRPC)
$(SYMLINK) $(LIBCARTESI_SO_JSONRPC) $(LIB_INSTALL_PATH)/$(LIBCARTESI_JSONRPC)
$(INSTALL_DIR) tools/gdb $(SHARE_INSTALL_PATH)/gdb

install-strip: install-emulator
$(STRIP_BINARY) $(subst src/,$(BIN_INSTALL_PATH)/,$(EMU_TO_BIN))
$(STRIP_SHARED) $(subst src/,$(LUA_INSTALL_CPATH)/,$(EMU_TO_LUA_CPATH))
$(STRIP_SHARED) $(subst src/,$(LIB_INSTALL_PATH)/,$(EMU_TO_LIB))
$(STRIP_STATIC) $(subst src/,$(LIB_INSTALL_PATH)/,$(EMU_TO_LIB_A))
install-shared-files: $(IMAGES_INSTALL_PATH)
$(INSTALL_DIR) tools/gdb $(SHARE_INSTALL_PATH)/gdb

install: install-strip install-headers
install: install-headers install-static-libs install-shared-libs install-lua-libs install-bins install-lua-bins install-shared-files

install-uarch: install $(UARCH_INSTALL_PATH)
$(INSTALL_FILE) uarch/$(UARCH_TO_SHARE) $(UARCH_INSTALL_PATH)
Expand Down
3 changes: 0 additions & 3 deletions src/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,3 @@ compile_flags.txt
coverage*
jsonrpc-discover.cpp
machine-c-version.h
uarch-pristine-state-hash.cpp
uarch-pristine-ram.c

57 changes: 26 additions & 31 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ GRPC_CPP_PLUGIN=$(shell which grpc_cpp_plugin)
# Are we on an environment that has the riscv64-cartesi-linux-gnu-* toolchain?
DEV_ENV_HAS_TOOLCHAIN ?= no

# If instead of building the microarchitecture you wamt to use a pre-built image file, modify this variable to
# If instead of building the microarchitecture you want to use a pre-built image file, modify this variable to
# indicate the path of the desired file.
UARCH_RAM_IMAGE ?= ../uarch/uarch-ram.bin
UARCH_PRISTINE_RAM_C ?= ../uarch/uarch-pristine-ram.c
UARCH_PRISTINE_HASH_C ?= ../uarch/uarch-pristine-hash.c

# Code instrumentation
release?=no
Expand Down Expand Up @@ -243,9 +244,7 @@ PGO_WORKLOAD=\
dhrystone 5000000; \
whetstone 25000

# We ignore uarch-pristine-ram.c because it is generated by xxd.
# We ignore uarch-pristine-state-hash.cpp because it is generated by compute-uarch-pristine-hash.
LINTER_IGNORE_SOURCES=uarch-pristine-ram.c uarch-pristine-state-hash.cpp
LINTER_IGNORE_SOURCES=
LINTER_IGNORE_HEADERS=%.pb.h
LINTER_SOURCES=$(filter-out $(LINTER_IGNORE_SOURCES),$(strip $(wildcard *.cpp) $(wildcard *.c)))
LINTER_HEADERS=$(filter-out $(LINTER_IGNORE_HEADERS),$(strip $(wildcard *.hpp) $(wildcard *.h)))
Expand All @@ -257,7 +256,7 @@ CLANG_FORMAT=clang-format
CLANG_FORMAT_UARCH_FILES:=$(wildcard ../uarch/*.cpp)
CLANG_FORMAT_UARCH_FILES:=$(filter-out %uarch-printf%,$(strip $(CLANG_FORMAT_UARCH_FILES)))
CLANG_FORMAT_FILES:=$(wildcard *.cpp) $(wildcard *.c) $(wildcard *.h) $(wildcard *.hpp) $(CLANG_FORMAT_UARCH_FILES)
CLANG_FORMAT_IGNORE_FILES:=uarch-pristine-ram.c uarch-pristine-state-hash.cpp
CLANG_FORMAT_IGNORE_FILES:=
CLANG_FORMAT_FILES:=$(filter-out %.pb.h,$(strip $(CLANG_FORMAT_FILES)))
CLANG_FORMAT_FILES:=$(filter-out $(CLANG_FORMAT_IGNORE_FILES),$(strip $(CLANG_FORMAT_FILES)))

Expand Down Expand Up @@ -346,15 +345,17 @@ LIBCARTESI_OBJS:= \
uarch-interpret.o \
machine-c-api.o \
uarch-pristine-ram.o \
uarch-pristine-state-hash.o
uarch-pristine-state-hash.o \
uarch-pristine-hash.o

CARTESI_CLUA_OBJS:= \
clua.o \
clua-i-virtual-machine.o \
clua-htif.o \
clua-machine-util.o \
uarch-pristine-ram.o \
uarch-pristine-state-hash.o
uarch-pristine-state-hash.o \
uarch-pristine-hash.o

LUACARTESI_OBJS:= \
clua-cartesi.o \
Expand All @@ -381,10 +382,6 @@ LIBCARTESI_MERKLE_TREE_OBJS:= \
MERKLE_TREE_HASH_OBJS:= \
merkle-tree-hash.o

COMPUTE_UARCH_PRISTINE_HASH_OBJS := \
compute-uarch-pristine-hash.o \
uarch-pristine-ram.o

LIBCARTESI_GRPC_OBJS:= \
grpc-virtual-machine.o \
grpc-machine-c-api.o \
Expand All @@ -397,7 +394,8 @@ LIBCARTESI_JSONRPC_OBJS:= \
jsonrpc-machine-c-api.o \
mongoose.o \
uarch-pristine-ram.o \
uarch-pristine-state-hash.o
uarch-pristine-state-hash.o \
uarch-pristine-hash.o

LUACARTESI_GRPC_OBJS:= \
clua-cartesi-grpc.o \
Expand All @@ -421,15 +419,17 @@ REMOTE_CARTESI_MACHINE_OBJS:= \
remote-machine.o \
slog.o \
uarch-pristine-ram.o \
uarch-pristine-state-hash.o
uarch-pristine-state-hash.o \
uarch-pristine-hash.o

JSONRPC_REMOTE_CARTESI_MACHINE_OBJS:= \
jsonrpc-remote-machine.o \
jsonrpc-discover.o \
mongoose.o \
slog.o \
uarch-pristine-ram.o \
uarch-pristine-state-hash.o
uarch-pristine-state-hash.o \
uarch-pristine-hash.o

ifeq ($(gperf),yes)
DEFS+=-DGPERF
Expand Down Expand Up @@ -538,9 +538,6 @@ $(PROFILE_DATA):
llvm-profdata merge -output=default.profdata default*.profraw
endif

compute-uarch-pristine-hash: $(COMPUTE_UARCH_PRISTINE_HASH_OBJS) libcartesi_merkle_tree.a
$(CXX) -o $@ $^ $(HASH_LIBS) $(LDFLAGS) $(EXELDFLAGS)

merkle-tree-hash: $(MERKLE_TREE_HASH_OBJS) libcartesi_merkle_tree.a
$(CXX) -o $@ $^ $(HASH_LIBS) $(LDFLAGS) $(EXELDFLAGS)

Expand Down Expand Up @@ -579,15 +576,6 @@ jsonrpc-discover.cpp: jsonrpc-discover.json
echo ')json";' >> jsonrpc-discover.cpp
echo '} // namespace cartesi' >> jsonrpc-discover.cpp

uarch-pristine-state-hash.cpp: compute-uarch-pristine-hash
@echo '// This file is auto-generated and should not be modified' > $@
@echo '#include "uarch-pristine-state-hash.h"' >> $@
@echo 'namespace cartesi {' >> $@
@echo ' const machine_merkle_tree::hash_type uarch_pristine_state_hash{' >> $@
@./compute-uarch-pristine-hash >> $@
@echo ' };' >> $@
@echo '} // namespace cartesi' >> $@

%.clang-tidy: %.cpp $(PROTO_SOURCES) machine-c-version.h
@$(CLANG_TIDY) --header-filter='$(CLANG_TIDY_HEADER_FILTER)' $< -- $(CXXFLAGS) $(LUA_INC) $(GRPC_PROTOBUF_INC) 2>/dev/null
@$(CXX) $(CXXFLAGS) $(LUA_INC) $(GRPC_PROTOBUF_INC) $< -MM -MT $@ -MF $@.d > /dev/null 2>&1
Expand All @@ -604,6 +592,12 @@ mongoose.o: ../third-party/mongoose-7.12/mongoose.c
sha3.o: ../third-party/tiny_sha3/sha3.c
$(CC) $(CFLAGS) -c -o $@ $<

uarch-pristine-ram.o: $(UARCH_PRISTINE_RAM_C)
$(CC) $(CFLAGS) -c -o $@ $<

uarch-pristine-hash.o: $(UARCH_PRISTINE_HASH_C)
$(CC) $(CFLAGS) -c -o $@ $<

%.o: %.cpp machine-c-version.h
$(CXX) $(CXXFLAGS) -c -o $@ $<

Expand All @@ -613,20 +607,21 @@ sha3.o: ../third-party/tiny_sha3/sha3.c
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<

uarch-pristine-ram.c: $(UARCH_RAM_IMAGE)
@xxd -i -n uarch_pristine_ram $< > $@
../uarch/uarch-pristine-ram.c ../uarch/uarch-pristine-hash.c: generate-uarch-pristine

$(UARCH_RAM_IMAGE):
generate-uarch-pristine:
ifeq (,$(wildcard ../uarch/uarch-pristine-hash.c))
@if [ "$(DEV_ENV_HAS_TOOLCHAIN)" = "yes" ]; then \
$(MAKE) -C .. uarch; \
else \
$(MAKE) -C .. uarch-with-linux-env; \
fi
endif

clean: clean-auto-generated clean-coverage clean-profile clean-proto-sources clean-tidy clean-libcartesi clean-executables

clean-auto-generated:
@rm -f jsonrpc-discover.cpp machine-c-version.h uarch-pristine-ram.c uarch-pristine-state-hash.cpp
@rm -f jsonrpc-discover.cpp machine-c-version.h

clean-proto-sources:
@rm -f *.pb.cc *.pb.h
Expand Down
6 changes: 3 additions & 3 deletions src/clua-cartesi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "machine-c-api.h"
#include "riscv-constants.h"
#include "uarch-constants.h"
#include "uarch-pristine-state-hash.h"
#include "uarch-pristine.h"

/// \file
/// \brief Scripting interface for the Cartesi SDK.
Expand Down Expand Up @@ -130,8 +130,8 @@ CM_API int luaopen_cartesi(lua_State *L) {
clua_setintegerfield(L, UARCH_RAM_LENGTH, "UARCH_RAM_LENGTH", -1);
clua_setintegerfield(L, UARCH_RAM_START_ADDRESS, "UARCH_RAM_START_ADDRESS", -1);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
clua_setlstringfield(L, reinterpret_cast<const char *>(uarch_pristine_state_hash.data()),
uarch_pristine_state_hash.size(), "UARCH_PRISTINE_STATE_HASH", -1);
clua_setlstringfield(L, reinterpret_cast<const char *>(uarch_pristine_hash), uarch_pristine_hash_len,
"UARCH_PRISTINE_STATE_HASH", -1);
clua_setintegerfield(L, MVENDORID_INIT, "MVENDORID", -1);
clua_setintegerfield(L, MARCHID_INIT, "MARCHID", -1);
clua_setintegerfield(L, MIMPID_INIT, "MIMPID", -1);
Expand Down
36 changes: 36 additions & 0 deletions src/uarch-pristine-state-hash.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option) any
// later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along
// with this program (see COPYING). If not, see <https://www.gnu.org/licenses/>.
//

#include "uarch-pristine-state-hash.h"
#include "uarch-pristine.h"

#include <algorithm>

namespace cartesi {

static machine_merkle_tree::hash_type get_uarch_pristine_state_hash() noexcept {
machine_merkle_tree::hash_type h;
for (std::size_t i = 0; i < h.size(); ++i) {
h[i] = uarch_pristine_hash[i];
}
return h;
}

/// \brief Hash of the pristine uarch state.
/// \details This hash is computed at compile time by the program compute-uarch-pristine-hash.cpp
const machine_merkle_tree::hash_type uarch_pristine_state_hash = get_uarch_pristine_state_hash();

} // namespace cartesi
32 changes: 32 additions & 0 deletions src/uarch-pristine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option) any
// later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along
// with this program (see COPYING). If not, see <https://www.gnu.org/licenses/>.
//

#ifndef UARCH_PRISTINE_H
#define UARCH_PRISTINE_H

/// \brief Embedded pristine uarch ram image. This symbol is created by "xxd"
extern "C" const unsigned char uarch_pristine_ram[];

/// \brief Length of the embedded pristine uarch ram image. This symbol is created by "xxd"
extern "C" const unsigned int uarch_pristine_ram_len;

/// \brief Embedded pristine uarch ram image. This symbol is created by "compute-uarch-pristine-hash"
extern "C" const unsigned char uarch_pristine_hash[];

/// \brief Length of the embedded pristine uarch ram image. This symbol is created by "compute-uarch-pristine-hash"
extern "C" const unsigned int uarch_pristine_hash_len;

#endif // UARCH_PRISTINE_H
7 changes: 1 addition & 6 deletions src/uarch-state.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@

#include "pma.h"
#include "riscv-constants.h"

/// \brief Embedded pristine uarch ram image. This symbol is created by "xxd"
extern "C" const unsigned char uarch_pristine_ram[];

/// \brief Length of the embedded pristine uarch ram image. This symbol is created by "xxd"
extern "C" const uint32_t uarch_pristine_ram_len;
#include "uarch-pristine.h"

namespace cartesi {

Expand Down
3 changes: 3 additions & 0 deletions uarch/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
*.bin
.DS_Store
.vscode
uarch-pristine-hash.c
uarch-pristine-ram.c
compute-uarch-pristine-hash
Loading

0 comments on commit 388ebff

Please sign in to comment.