diff --git a/abstract-machine/.gitignore b/abstract-machine/.gitignore index bcba0ab..0990b71 100644 --- a/abstract-machine/.gitignore +++ b/abstract-machine/.gitignore @@ -2,5 +2,7 @@ **/build/ **/.envrc **/.cache +out/ .vscode compile_commands.json +cmakeUserh diff --git a/abstract-machine/CMakeLists.txt b/abstract-machine/CMakeLists.txt index 508bc68..6eb89d7 100644 --- a/abstract-machine/CMakeLists.txt +++ b/abstract-machine/CMakeLists.txt @@ -8,6 +8,7 @@ set(CMAKE_CXX_STANDARD 11) include(CMakeDependentOption) include(CMakePackageConfigHelpers) # Used to find libcheck include(CTest) +include(GNUInstallDirs) # -- General options set(ISA CACHE STRING "Target ISA") @@ -17,12 +18,15 @@ string(TOUPPER ${ISA} ISA_UPPER) cmake_dependent_option( __PLATFORM_NEMU__ "Run on NEMU" ON "ISA MATCHES \"(riscv | x86)\"" OFF) +cmake_dependent_option( + __PLATFORM_NPC__ "Run on NPC" + ON "ISA MATCHES riscv" OFF) cmake_dependent_option( __PLATFORM_NATIVE__ "Run on native" ON "ISA MATCHES native" OFF) # -- Set PLATFORM according to options -set(MATCH_PLATFORM_PATTERN "^__PLATFORM_([A-Z]*)__") +set(MATCH_PLATFORM_PATTERN "^__PLATFORM_([A-Z]*)__$") get_cmake_property(CACHE_VARS CACHE_VARIABLES) message(STATUS "ISA: ${ISA}") @@ -32,16 +36,27 @@ foreach(VAR IN LISTS CACHE_VARS) get_property(VAR_VALUE CACHE ${VAR} PROPERTY VALUE) set(PLATFORM_UPPER ${CMAKE_MATCH_1}) string(TOLOWER ${PLATFORM_UPPER} PLATFORM) + list(APPEND PLATFORMS ${PLATFORM}) message(STATUS "Variable: ${VAR}=${VAR_VALUE}, Platform: ${PLATFORM}") endif() endforeach() -if(${PLATFORM} MATCHES "native") -set(ARCH "native") -else() -set(ARCH ${ISA}-${PLATFORM}) +if((NOT PLATFORM) AND (NOT ISA MATCHES native)) + message(FATAL_ERROR "Platform not given!") endif() -string(TOUPPER ${ARCH} ARCH_UPPER) + +set(SUPPORTED_ARCH "riscv-nemu" "riscv-npc" "native") +foreach(PLATFORM IN LISTS PLATFORMS) + if(${ISA} MATCHES "native") + set(ARCH "native") + else() + set(ARCH ${ISA}-${PLATFORM}) + endif() + + if(NOT ARCH IN_LIST SUPPORTED_ARCH) + message(FATAL_ERROR "Given ISA-PLATFORM (${ISA}-${PLATFORM}) does not match one of the following: ${SUPPORTED_ARCH}") + endif() +endforeach() # -- Target specific options cmake_dependent_option( @@ -49,39 +64,34 @@ cmake_dependent_option( ON "NOT __ISA_NATIVE__" OFF) # -- Add compile definitions based on options -add_compile_definitions( - $ - __ISA_${ISA_UPPER}__ - __PLATFORM_${PLATFORM_UPPER}__ -) - -add_compile_definitions( - $<$:__NATIVE_USE_KLIB__> -) - -# -- Required compiler flags -add_compile_options( - # -Werror - -Wno-main - -fno-asynchronous-unwind-tables - -fno-builtin - -fno-stack-protector - -U_FORTIFY_SOURCE - $<$:-fno-exceptions> - $<$:-ffreestanding> - $<$:-fno-rtti>) - -add_link_options( - -znoexecstack -) - -# -- Include linker script here. Use this linker script at link time if INCLUDE_LINKER_SCRIPT is set to true -set(LINKER_SCRIPT linker.ld) list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake") -add_compile_options(-march=rv32if -mabi=ilp32) -add_link_options(-march=rv32if -mabi=ilp32) +# NOTE: klib and am include header files in each other, +# so we need to create interface libraries for correct dependency +add_library(am_interface INTERFACE) +target_include_directories(am_interface INTERFACE + $ + $ + $) +target_compile_definitions(am_interface INTERFACE ARCH_H=) + +add_library(klib_interface INTERFACE) +target_include_directories(klib_interface + INTERFACE + $ + $) + +install(TARGETS am_interface klib_interface + EXPORT interfaceTargets + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/abstract-machine) + +install(EXPORT interfaceTargets + FILE interfaceTargets.cmake + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake) add_subdirectory(klib) add_subdirectory(am) + +# -- Test depends on klib and am should be added last. +add_subdirectory(klib/tests) diff --git a/abstract-machine/CMakePresets.json b/abstract-machine/CMakePresets.json index d14c0b6..73dab56 100644 --- a/abstract-machine/CMakePresets.json +++ b/abstract-machine/CMakePresets.json @@ -14,14 +14,14 @@ } }, { - "name": "riscv-nemu", - "displayName": "Riscv32 NEMU", + "name": "riscv", + "displayName": "RV32 all platform", "generator": "Unix Makefiles", "binaryDir": "${sourceDir}/out/build/${presetName}", - "installDir": "/home/xin/repo/ysyx-workbench/abstract-machine/out/install", "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug", "ISA": "riscv", + "__PLATFORM_NPC__": true, "__PLATFORM_NEMU__": true } } diff --git a/abstract-machine/CMakeUserPresets.json b/abstract-machine/CMakeUserPresets.json new file mode 100644 index 0000000..3f222ce --- /dev/null +++ b/abstract-machine/CMakeUserPresets.json @@ -0,0 +1,15 @@ +{ + "version": 6, + "configurePresets": [ + { + "name": "Native install", + "inherits": "native", + "installDir": "/home/xin/repo/ysyx-workbench/abstract-machine/out/install" + }, + { + "name": "RISCV install", + "inherits": "riscv", + "installDir": "/home/xin/repo/ysyx-workbench/abstract-machine/out/install" + } + ] +} \ No newline at end of file diff --git a/abstract-machine/am/CMakeLists.txt b/abstract-machine/am/CMakeLists.txt index b0462e4..25895b7 100644 --- a/abstract-machine/am/CMakeLists.txt +++ b/abstract-machine/am/CMakeLists.txt @@ -1,10 +1,26 @@ -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) - -add_library(am_interface INTERFACE) -target_include_directories(am_interface INTERFACE - $ - $) - add_subdirectory(src) -install(DIRECTORY include/ DESTINATION include/abstract-machine) +install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/abstract-machine) + +foreach(PLATFORM IN LISTS PLATFORMS) + if(ISA MATCHES "native") + set(ARCH "native") + else() + set(ARCH ${ISA}-${PLATFORM}) + endif() + install(TARGETS am-${ARCH} + EXPORT amTargets-${ARCH} + LIBRARY DESTINATION lib) + + install(EXPORT amTargets-${ARCH} + FILE amTargets.cmake + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/am-${ARCH}) + + configure_package_config_file( + ${CMAKE_SOURCE_DIR}/cmake/am-config.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/am-${ARCH}-config.cmake + INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/am-${ARCH}) + + install(FILES ${CMAKE_CURRENT_BINARY_DIR}/am-${ARCH}-config.cmake + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/am-${ARCH}) +endforeach() diff --git a/abstract-machine/am/src/CMakeLists.txt b/abstract-machine/am/src/CMakeLists.txt index 533dd3b..4988276 100644 --- a/abstract-machine/am/src/CMakeLists.txt +++ b/abstract-machine/am/src/CMakeLists.txt @@ -1,53 +1 @@ -if(ISA MATCHES "native") -set(SOURCEDIR "./${PLATFORM}") -else() -set(SOURCEDIR "./${ISA}/${PLATFORM}") -endif() - -add_subdirectory(${SOURCEDIR}) - -target_include_directories(am-${ARCH} - PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} - PUBLIC - $ - $) -target_link_libraries(am-${ARCH} - PUBLIC klib_interface - INTERFACE m) - -# TODO: Check -target_link_options(am-${ARCH} INTERFACE - $ - $) - -# Interface compile flags -target_link_options(am-${ARCH} INTERFACE - -znoexecstack) - -target_compile_options(am-${ARCH} INTERFACE - -fno-asynchronous-unwind-tables - -fno-builtin - -fno-stack-protector - -U_FORTIFY_SOURCE - $<$:-fno-exceptions> - $<$:-ffreestanding> - $<$:-fno-rtti>) - -install(TARGETS am-${ARCH} klib_interface am_interface - EXPORT amTargets - LIBRARY DESTINATION lib) - -install(EXPORT amTargets - FILE amTargets.cmake - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/am-${ARCH}) - -configure_package_config_file(${CMAKE_SOURCE_DIR}/cmake/am-config.cmake.in - ${CMAKE_CURRENT_BINARY_DIR}/am-${ARCH}-config.cmake - INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/am-${ARCH}) - -install(FILES ${CMAKE_CURRENT_BINARY_DIR}/am-${ARCH}-config.cmake - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/am-${ARCH}) - -# TODO: check -install(FILES ${CMAKE_SOURCE_DIR}/scripts/${LINKER_SCRIPT} - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/am-${ARCH}) +add_subdirectory(${ISA}) diff --git a/abstract-machine/am/src/native/CMakeLists.txt b/abstract-machine/am/src/native/CMakeLists.txt index e3c9303..706aa80 100644 --- a/abstract-machine/am/src/native/CMakeLists.txt +++ b/abstract-machine/am/src/native/CMakeLists.txt @@ -1,6 +1,3 @@ -include(CheckPIESupported) -check_pie_supported() - set(SOURCES trap.S cte.c @@ -23,4 +20,4 @@ set_target_properties(am-native PROPERTIES INTERFACE_POSITION_INDEPENDENT_CODE TRUE) find_package(SDL2 REQUIRED) -target_link_libraries(am-${ARCH} PUBLIC SDL2::SDL2) +target_link_libraries(am-native PUBLIC SDL2::SDL2 PRIVATE klib_interface am_interface) diff --git a/abstract-machine/am/src/riscv/CMakeLists.txt b/abstract-machine/am/src/riscv/CMakeLists.txt new file mode 100644 index 0000000..ced867e --- /dev/null +++ b/abstract-machine/am/src/riscv/CMakeLists.txt @@ -0,0 +1,12 @@ +foreach(PLATFORM IN LISTS PLATFORMS) + string(TOUPPER ${ARCH} ARCH_UPPER) + set(AM_COMMON_COMPILE_DEF + # -- Arch related + $ + __ISA_${ISA_UPPER}__ + __PLATFORM_${PLATFORM_UPPER}__ + + $<$:__NATIVE_USE_KLIB__> + ) + add_subdirectory(${PLATFORM}) +endforeach() diff --git a/abstract-machine/am/src/riscv/nemu/CMakeLists.txt b/abstract-machine/am/src/riscv/nemu/CMakeLists.txt index a6992db..79db40f 100644 --- a/abstract-machine/am/src/riscv/nemu/CMakeLists.txt +++ b/abstract-machine/am/src/riscv/nemu/CMakeLists.txt @@ -1,7 +1,7 @@ include(nemu-settings) include(riscv-settings) -add_library(am-${ISA}-nemu +add_library(am-riscv-nemu cte.c start.S trap.S @@ -9,26 +9,44 @@ add_library(am-${ISA}-nemu ${NEMU_SOURCES} ) -target_compile_options(am-${ISA}-nemu PRIVATE +target_compile_options(am-riscv-nemu PRIVATE ${NEMU_COMPILE_OPTIONS} ${RISCV_COMPILE_OPTIONS}) -target_link_options(am-${ISA}-nemu PRIVATE + +target_link_options(am-riscv-nemu PRIVATE ${NEMU_LINK_OPITIONS} ${RISCV_LINK_OPTIONS}) -target_include_directories(am-${ISA}-nemu PRIVATE + +target_include_directories(am-riscv-nemu PRIVATE ${NEMU_INCLUDE_DIRECTORIES}) -target_link_options(am-${ISA}-nemu INTERFACE + +target_link_options(am-riscv-nemu INTERFACE LINKER:--defsym=_pmem_start=0x80000000 LINKER:--defsym=_entry_offset=0x0 LINKER:--gc-sections LINKER:-e _start -nostartfiles) -target_compile_definitions(am-${ISA}-nemu PUBLIC - ARCH_H="arch/riscv.h") -target_compile_definitions(am-${ISA}-nemu PRIVATE - ISA_H="riscv/riscv.h") +target_link_options(am-riscv-nemu INTERFACE + $ + $) -set_target_properties(am-${ISA}-nemu PROPERTIES +target_include_directories(am-riscv-nemu + PUBLIC + $ + $) + +target_link_libraries(am-riscv-nemu + PUBLIC am_interface klib_interface + INTERFACE m) + +target_compile_definitions(am-riscv-nemu PRIVATE + ISA_H=) + +set_target_properties(am-riscv-nemu PROPERTIES POSITION_INDEPENDENT_CODE OFF INTERFACE_POSITION_INDEPENDENT_CODE OFF) + +install(FILES ${CMAKE_SOURCE_DIR}/scripts/linker.ld + DESTINATION ${CMAKE_INSTALL_DATADIR}) + diff --git a/abstract-machine/am/src/riscv/npc/CMakeLists.txt b/abstract-machine/am/src/riscv/npc/CMakeLists.txt new file mode 100644 index 0000000..0859c11 --- /dev/null +++ b/abstract-machine/am/src/riscv/npc/CMakeLists.txt @@ -0,0 +1,39 @@ +include(riscv-settings) + +add_subdirectory(libgcc) + +add_library(am-riscv-npc + cte.c + input.c + ioe.c + mpe.c + start.S + timer.c + trap.S + trm.c + vme.c +) + +target_link_libraries(am-riscv-npc PRIVATE npcgcc PUBLIC am_interface klib_interface) + +target_link_options(am-riscv-npc INTERFACE + $ + $) + +target_link_options(am-riscv-npc INTERFACE + LINKER:--defsym=_pmem_start=0x80000000 + LINKER:--defsym=_entry_offset=0x0 + LINKER:--gc-sections + LINKER:-e _start + -nostartfiles) + +target_link_options(am-riscv-npc INTERFACE + $ + $) + +target_compile_definitions(am-riscv-npc + PUBLIC ${AM_COMMON_COMPILE_DEF} ARCH_H= +) + +install(FILES ${CMAKE_SOURCE_DIR}/scripts/linker.ld + DESTINATION ${CMAKE_INSTALL_DATADIR}) diff --git a/abstract-machine/am/src/riscv/npc/libgcc/CMakeLists.txt b/abstract-machine/am/src/riscv/npc/libgcc/CMakeLists.txt new file mode 100644 index 0000000..841adb6 --- /dev/null +++ b/abstract-machine/am/src/riscv/npc/libgcc/CMakeLists.txt @@ -0,0 +1,14 @@ +add_library(npcgcc + ashldi3.c + div.S + muldi3.S + multi3.c + unused.c +) + +target_link_libraries(npcgcc PRIVATE klib_interface am_interface) +target_link_options(npcgcc INTERFACE -nolibc -nostdlib) + +install(TARGETS npcgcc + EXPORT amTargets-riscv-npc + LIBRARY DESTINATION lib) diff --git a/abstract-machine/am/src/riscv/npc/trm.c b/abstract-machine/am/src/riscv/npc/trm.c index 0efe6e7..a5f4fac 100644 --- a/abstract-machine/am/src/riscv/npc/trm.c +++ b/abstract-machine/am/src/riscv/npc/trm.c @@ -6,7 +6,7 @@ int main(const char *args); extern char _pmem_start; #define PMEM_SIZE (128 * 1024 * 1024) -#define PMEM_END ((uintptr_t)&_pmem_start + PMEM_SIZE) +#define PMEM_END ((uintptr_t)&_pmem_start + PMEM_SIZE) Area heap = RANGE(&_heap_start, PMEM_END); #ifndef MAINARGS @@ -14,11 +14,12 @@ Area heap = RANGE(&_heap_start, PMEM_END); #endif static const char mainargs[] = MAINARGS; -void putch(char ch) { -} +void putch(char ch) {} void halt(int code) { - while (1); + asm volatile("mv a0, %0; ebreak" : : "r"(code)); + while (1) + ; } void _trm_init() { diff --git a/abstract-machine/cmake/am-config.cmake.in b/abstract-machine/cmake/am-config.cmake.in index f2fbb32..0445b66 100644 --- a/abstract-machine/cmake/am-config.cmake.in +++ b/abstract-machine/cmake/am-config.cmake.in @@ -6,4 +6,5 @@ find_dependency(SDL2 REQUIRED) endif() # Include the targets file +include("${CMAKE_CURRENT_LIST_DIR}/../interfaceTargets.cmake") include("${CMAKE_CURRENT_LIST_DIR}/amTargets.cmake") diff --git a/abstract-machine/default.nix b/abstract-machine/default.nix index 1f1f67d..2b614ac 100644 --- a/abstract-machine/default.nix +++ b/abstract-machine/default.nix @@ -1,9 +1,9 @@ -{ stdenv, +{ stdenv, lib, cmake, SDL2, isa ? "native", - platform ? "NEMU" + platform ? [ ] }: stdenv.mkDerivation { pname = "abstract-machine"; @@ -13,8 +13,7 @@ stdenv.mkDerivation { cmakeFlags = [ (lib.cmakeFeature "ISA" isa) - (lib.cmakeBool "__PLATFORM_${lib.strings.toUpper platform}__" true) - ]; + ] ++ map (p: (lib.cmakeBool "__PLATFORM_${lib.strings.toUpper p}__" true)) platform; nativeBuildInputs = [ cmake @@ -22,5 +21,7 @@ stdenv.mkDerivation { buildInputs = [ - ] ++ (if platform=="native" then [ SDL2 ] else [ ]); + ] ++ (if isa=="native" then [ SDL2 ] else [ ]); + + doCheck = true; } diff --git a/abstract-machine/klib/CMakeLists.txt b/abstract-machine/klib/CMakeLists.txt index 2cf4a78..febd4f0 100644 --- a/abstract-machine/klib/CMakeLists.txt +++ b/abstract-machine/klib/CMakeLists.txt @@ -1,12 +1 @@ -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) - -add_library(klib_interface INTERFACE) -target_include_directories(klib_interface - INTERFACE - $ - $) - add_subdirectory(src) -# add_subdirectory(tests) - -install(DIRECTORY include/ DESTINATION include/abstract-machine) diff --git a/abstract-machine/klib/src/CMakeLists.txt b/abstract-machine/klib/src/CMakeLists.txt index bf7e136..b3a59fd 100644 --- a/abstract-machine/klib/src/CMakeLists.txt +++ b/abstract-machine/klib/src/CMakeLists.txt @@ -13,8 +13,7 @@ set(SOURCES ) add_library(klib ${SOURCES}) -target_include_directories(klib PUBLIC $) -target_compile_definitions(klib PUBLIC $) +target_link_libraries(klib PRIVATE am_interface klib_interface) install(TARGETS klib EXPORT klibTargets diff --git a/abstract-machine/klib/tests/CMakeLists.txt b/abstract-machine/klib/tests/CMakeLists.txt index f72c555..6cb0e40 100644 --- a/abstract-machine/klib/tests/CMakeLists.txt +++ b/abstract-machine/klib/tests/CMakeLists.txt @@ -4,14 +4,11 @@ set(TEST_SOURCES ) foreach(TEST IN LISTS TEST_SOURCES) - add_executable(${TEST} ${TEST}.c) - target_link_libraries(${TEST} am-${ARCH} klib m) - target_include_directories(${TEST} - PRIVATE $ - PRIVATE $ - ) # TODO: Run tests in other configurations if(__PLATFORM_NATIVE__) + add_executable(${TEST} ${TEST}.c) + target_link_libraries(${TEST} PRIVATE am_interface klib_interface klib m) + target_link_libraries(${TEST} PRIVATE am-native) add_test(NAME ${TEST} COMMAND ${TEST}) endif() endforeach() diff --git a/abstract-machine/out/install/lib/libklib.a b/abstract-machine/out/install/lib/libklib.a deleted file mode 100644 index 5023a30..0000000 Binary files a/abstract-machine/out/install/lib/libklib.a and /dev/null differ diff --git a/flake.nix b/flake.nix index c824872..58652b3 100644 --- a/flake.nix +++ b/flake.nix @@ -42,6 +42,8 @@ src = ./.; hooks = { trim-trailing-whitespace.enable = true; + end-of-file-fixer.enable = true; + cmake-format.enable = true; clang-format = { enable = true; types_or = pkgs.lib.mkForce [ "c" "c++" ]; @@ -52,7 +54,8 @@ packages.nemu = pkgs.callPackage ./nemu { am-kernels = self.packages.${system}.am-kernels; }; packages.nemu-lib = pkgs.callPackage ./nemu { am-kernels = self.packages.${system}.am-kernels; defconfig = "riscv32-lib_defconfig"; }; - packages.abstract-machine = crossPkgs.callPackage ./abstract-machine { isa = "riscv"; platform = "nemu"; }; + packages.abstract-machine = crossPkgs.callPackage ./abstract-machine { isa = "riscv"; platform = [ "nemu" "npc" ]; }; + packages.abstract-machine-native = pkgs.callPackage ./abstract-machine { isa = "native"; }; packages.am-kernels = crossPkgs.stdenv.mkDerivation rec { pname = "am-kernels-cmake"; @@ -67,7 +70,6 @@ cmakeFlags = [ (pkgs.lib.cmakeFeature "ISA" "riscv") (pkgs.lib.cmakeFeature "PLATFORM" "nemu") - (pkgs.lib.cmakeFeature "CMAKE_INSTALL_DATADIR" "share") ]; buildInputs = [ @@ -87,13 +89,12 @@ IMAGES_PATH = "${self.packages.${system}.am-kernels}/share/binary"; }; - devShells.npc = with pkgs; mkShell { + devShells.npc = with pkgs; mkShell.override { stdenv = ccacheStdenv; } { inherit (self.checks.${system}.pre-commit-check) shellHook; CHISEL_FIRTOOL_PATH = "${nixpkgs-circt162.legacyPackages.${system}.circt}/bin"; packages = [ clang-tools cmake - ninja coursier espresso bloop