diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index e727efa..5164019 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -1,2 +1,3 @@ add_subdirectory(libc) +add_subdirectory(libcxx) add_subdirectory(mammoth) diff --git a/lib/libcxx/CMakeLists.txt b/lib/libcxx/CMakeLists.txt new file mode 100644 index 0000000..1ca1741 --- /dev/null +++ b/lib/libcxx/CMakeLists.txt @@ -0,0 +1,15 @@ +add_library(libcxx STATIC + src/new.cpp + ) + +target_include_directories(libcxx + PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) + +target_link_libraries(libcxx + libc + zion_lib + ) + +set_target_properties(libcxx PROPERTIES + COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -ffreestanding -mgeneral-regs-only") diff --git a/lib/libcxx/include/cstddef b/lib/libcxx/include/cstddef new file mode 100644 index 0000000..aa7d250 --- /dev/null +++ b/lib/libcxx/include/cstddef @@ -0,0 +1,34 @@ +/* vim: syntax=cpp */ + +#pragma once + +#include "cstdint" + +namespace std { + using ptrdiff_t = uint64_t; + using size_t = uint64_t; + // FIXME: I don't understand what this does. + using max_align_t = uint64_t; + using nullptr_t = decltype(nullptr); + + enum class byte : unsigned char {}; + + // byte type operations + template + constexpr byte& operator<<=(byte& b, IntType shift) noexcept; + template + constexpr byte operator<<(byte b, IntType shift) noexcept; + template + constexpr byte& operator>>=(byte& b, IntType shift) noexcept; + template + constexpr byte operator>>(byte b, IntType shift) noexcept; + constexpr byte& operator|=(byte& l, byte r) noexcept; + constexpr byte operator|(byte l, byte r) noexcept; + constexpr byte& operator&=(byte& l, byte r) noexcept; + constexpr byte operator&(byte l, byte r) noexcept; + constexpr byte& operator^=(byte& l, byte r) noexcept; + constexpr byte operator^(byte l, byte r) noexcept; + constexpr byte operator~(byte b) noexcept; + template + constexpr IntType to_integer(byte b) noexcept; +} diff --git a/lib/libcxx/include/cstdint b/lib/libcxx/include/cstdint new file mode 100644 index 0000000..9e30b50 --- /dev/null +++ b/lib/libcxx/include/cstdint @@ -0,0 +1,5 @@ +/* vim: syntax=cpp */ + +#pragma once + +#include diff --git a/lib/libcxx/include/new b/lib/libcxx/include/new new file mode 100644 index 0000000..8e943e0 --- /dev/null +++ b/lib/libcxx/include/new @@ -0,0 +1,69 @@ +/* vim: syntax=cpp */ + +#pragma once + +#include "cstddef" + +namespace std { + // storage allocation errors + class bad_alloc; + class bad_array_new_length; + + struct destroying_delete_t { + explicit destroying_delete_t() = default; + }; + inline constexpr destroying_delete_t destroying_delete{}; + + // global operator new control + enum class align_val_t : size_t {}; + + struct nothrow_t { explicit nothrow_t() = default; }; + extern const nothrow_t nothrow; + + using new_handler = void (*)(); + new_handler get_new_handler() noexcept; + new_handler set_new_handler(new_handler new_p) noexcept; + + // pointer optimization barrier + template [[nodiscard]] constexpr T* launder(T* p) noexcept; + + // hardware interference size + // inline constexpr size_t hardware_destructive_interference_size = + // /* implementation-defined */; + // inline constexpr size_t hardware_constructive_interference_size = + // /* implementation-defined */; +} + +// storage allocation and deallocation +[[nodiscard]] void* operator new(std::size_t size); +[[nodiscard]] void* operator new(std::size_t size, std::align_val_t alignment); +[[nodiscard]] void* operator new(std::size_t size, const std::nothrow_t&) noexcept; +[[nodiscard]] void* operator new(std::size_t size, std::align_val_t alignment, + const std::nothrow_t&) noexcept; + +void operator delete(void* ptr) noexcept; +void operator delete(void* ptr, std::size_t size) noexcept; +void operator delete(void* ptr, std::align_val_t alignment) noexcept; +void operator delete(void* ptr, std::size_t size, std::align_val_t alignment) noexcept; +void operator delete(void* ptr, const std::nothrow_t&) noexcept; +void operator delete(void* ptr, std::align_val_t alignment, + const std::nothrow_t&) noexcept; + +[[nodiscard]] void* operator new[](std::size_t size); +[[nodiscard]] void* operator new[](std::size_t size, std::align_val_t alignment); +[[nodiscard]] void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; +[[nodiscard]] void* operator new[](std::size_t size, std::align_val_t alignment, + const std::nothrow_t&) noexcept; + +void operator delete[](void* ptr) noexcept; +void operator delete[](void* ptr, std::size_t size) noexcept; +void operator delete[](void* ptr, std::align_val_t alignment) noexcept; +void operator delete[](void* ptr, std::size_t size, std::align_val_t alignment) noexcept; +void operator delete[](void* ptr, const std::nothrow_t&) noexcept; +void operator delete[](void* ptr, std::align_val_t alignment, + const std::nothrow_t&) noexcept; + +[[nodiscard]] void* operator new (std::size_t size, void* ptr) noexcept; +[[nodiscard]] void* operator new[](std::size_t size, void* ptr) noexcept; +void operator delete (void* ptr, void*) noexcept; +void operator delete[](void* ptr, void*) noexcept; diff --git a/lib/libcxx/src/new.cpp b/lib/libcxx/src/new.cpp new file mode 100644 index 0000000..dc36303 --- /dev/null +++ b/lib/libcxx/src/new.cpp @@ -0,0 +1,6 @@ +#include "include/new" + +#include "stdlib.h" + +[[nodiscard]] void* operator new(std::size_t size) { return malloc(size); } +[[nodiscard]] void* operator new[](std::size_t size) { return malloc(size); } diff --git a/sys/CMakeLists.txt b/sys/CMakeLists.txt index adb8d91..6bffd62 100644 --- a/sys/CMakeLists.txt +++ b/sys/CMakeLists.txt @@ -20,7 +20,7 @@ add_executable(test2 test2.cpp) target_link_libraries(test2 - libc + libcxx mammoth_lib) set_target_properties(test2 diff --git a/sys/test2.cpp b/sys/test2.cpp index d2c8398..b711ce3 100644 --- a/sys/test2.cpp +++ b/sys/test2.cpp @@ -19,7 +19,7 @@ int main(uint64_t bootstrap_cap) { Thread t2(thread_entry, b); uint64_t size = 10; - char* buff = (char*)malloc(size); + char* buff = new char[size]; Channel c1; c1.adopt_cap(bootstrap_cap); check(c1.ReadStr(buff, &size));