From da70596895d8135e08f8caac6978117697b4c021 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sun, 18 Aug 2024 21:39:29 +0200 Subject: [REFACTOR] Improved project structure. Signed-off-by: Amlal El Mahrouss --- dev/CRT/ReadMe.md | 5 ++ dev/CRT/__ndk_alloca.hxx | 15 ++++++ dev/CRT/__ndk_defines.hxx | 121 +++++++++++++++++++++++++++++++++++++++++++ dev/CRT/__ndk_exception.hxx | 35 +++++++++++++ dev/CRT/__ndk_new_delete.hxx | 61 ++++++++++++++++++++++ dev/CRT/__ndk_runtime.cxx | 12 +++++ dev/CRT/build.json | 10 ++++ 7 files changed, 259 insertions(+) create mode 100644 dev/CRT/ReadMe.md create mode 100644 dev/CRT/__ndk_alloca.hxx create mode 100644 dev/CRT/__ndk_defines.hxx create mode 100644 dev/CRT/__ndk_exception.hxx create mode 100644 dev/CRT/__ndk_new_delete.hxx create mode 100644 dev/CRT/__ndk_runtime.cxx create mode 100644 dev/CRT/build.json (limited to 'dev/CRT') diff --git a/dev/CRT/ReadMe.md b/dev/CRT/ReadMe.md new file mode 100644 index 00000000..25dc2cb2 --- /dev/null +++ b/dev/CRT/ReadMe.md @@ -0,0 +1,5 @@ +# ZKA C++ RunTime + +This is the public interface of ZKA' NDK RunTime, please check ndk.dll instead. + +###### (c) ZKA Technologies, all rights reserved. \ No newline at end of file diff --git a/dev/CRT/__ndk_alloca.hxx b/dev/CRT/__ndk_alloca.hxx new file mode 100644 index 00000000..db572bcd --- /dev/null +++ b/dev/CRT/__ndk_alloca.hxx @@ -0,0 +1,15 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#pragma once + +typedef void* ptr_type; +typedef __SIZE_TYPE__ size_type; + +inline void* __ndk_alloca(size_type sz) +{ + return __builtin_alloca(sz); +} diff --git a/dev/CRT/__ndk_defines.hxx b/dev/CRT/__ndk_defines.hxx new file mode 100644 index 00000000..ebf530a5 --- /dev/null +++ b/dev/CRT/__ndk_defines.hxx @@ -0,0 +1,121 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#ifndef __NDK_DEFINES_HXX__ +#define __NDK_DEFINES_HXX__ + +#ifndef __GNUC__ + +typedef __SIZE_TYPE__ size_t; + +#ifdef __LP64__ +typedef long int ssize_t; +#else +typedef int ssize_t; +#endif // __LP64__ + +typedef size_t ptrdiff_t; +typedef size_t uintptr_t; +typedef void* voidptr_t; +typedef void* any_t; +typedef char* caddr_t; + +#ifndef NULL +#define NULL ((voidptr_t)0) +#endif // !null + +#ifdef __GNUC__ +#include +#define __ndk_alloca(sz) __ndk_alloca(sz) +#elif defined(__NDK__) + +#define __alloca(sz) __ndk_alloca(sz) +#endif + +#define __deref(ptr) (*(ptr)) + +#ifdef __cplusplus +#define __init_decl() \ + extern "C" \ + { +#define __fini_decl() \ + } \ + ; +#else +#define __init_decl() +#define __fini_decl() +#endif + +#if __has_builtin(__builtin_alloca) +#define alloca(sz) __builtin_alloca(sz) +#ifdef __alloca +#undef __alloca +#endif +#define __alloca alloca +#else +#warning alloca not detected +#endif + +typedef long long off_t; +typedef unsigned long long uoff_t; + +typedef union float_cast { + struct + { + unsigned int mantissa : 23; + unsigned int exponent : 8; + unsigned int sign : 1; + }; + + float f; +} __attribute__((packed)) float_cast_t; + +typedef union double_cast { + struct + { + unsigned long long int mantissa : 52; + unsigned int exponent : 11; + unsigned int sign : 1; + }; + + double f; +} __attribute__((packed)) double_cast_t; + +#endif // ifndef __GNUC__ + +/// Include these helpers as well. + +#ifdef __STD_CXX__ + +#include +#include + +#endif // ifdef __STD_CXX__ + +namespace std +{ + /// @brief Forward object. + /// @tparam Args the object type. + /// @param arg the object. + /// @return object's rvalue + template + inline Args&& forward(Args& arg) + { + return static_cast(arg); + } + + /// @brief Move object. + /// @tparam Args the object type. + /// @param arg the object. + /// @return object's rvalue + template + inline Args&& move(Args&& arg) + { + return static_cast(arg); + } +} // namespace std + +#endif /* __NDK_DEFINES_HXX__ */ diff --git a/dev/CRT/__ndk_exception.hxx b/dev/CRT/__ndk_exception.hxx new file mode 100644 index 00000000..93407595 --- /dev/null +++ b/dev/CRT/__ndk_exception.hxx @@ -0,0 +1,35 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#pragma once + +#include + +/// @brief CRT exit, with exit code (!!! exits all threads. !!!) +/// @param code +/// @return +extern "C" int __exit(int code); + +/// @brief Standard C++ namespace +namespace std +{ + inline void __throw_general(void) + { + __exit(33); + } + + inline void __throw_domain_error(const char* error) + { + __throw_general(); + __builtin_unreachable(); // prevent from continuing. + } + + inline void __throw_bad_array_new_length(void) + { + __throw_general(); + __builtin_unreachable(); // prevent from continuing. + } +} // namespace std diff --git a/dev/CRT/__ndk_new_delete.hxx b/dev/CRT/__ndk_new_delete.hxx new file mode 100644 index 00000000..464ea334 --- /dev/null +++ b/dev/CRT/__ndk_new_delete.hxx @@ -0,0 +1,61 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#pragma once + +#include +#include + +namespace stdx +{ + /// @brief allocate a new class. + /// @tparam KindClass the class type to allocate. + template + inline void* allocate(Args&&... args) + { + return new KindClass(forward(args)...); + } + + /// @brief free a class. + /// @tparam KindClass the class type to allocate. + template + inline void release(KindClass ptr) + { + delete ptr; + } +} // namespace stdx + +void* operator new(size_type len) +{ + if (!len) + ++len; + + return RtlCreateHeap(len, 0); +} + +void operator delete(void* ptr) +{ + if (!ptr) + return; + + RtlDestroyHeap(ptr); +} + +void* operator new[](size_type len) +{ + if (!len) + ++len; + + return RtlCreateHeap(len, 0); +} + +void operator delete[](void* ptr) +{ + if (!ptr) + return; + + RtlDestroyHeap(ptr); +} diff --git a/dev/CRT/__ndk_runtime.cxx b/dev/CRT/__ndk_runtime.cxx new file mode 100644 index 00000000..fc63b12d --- /dev/null +++ b/dev/CRT/__ndk_runtime.cxx @@ -0,0 +1,12 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#include +#include +#include +#include + +/// @note No sources needed for now. diff --git a/dev/CRT/build.json b/dev/CRT/build.json new file mode 100644 index 00000000..2d4c0c1e --- /dev/null +++ b/dev/CRT/build.json @@ -0,0 +1,10 @@ +{ + "compiler_path": "x86_64-w64-mingw32-g++", + "compiler_std": "c++20", + "headers_path": ["../", "./"], + "sources_path": ["*.cxx"], + "output_name": "ndkcrt.dll", + "compiler_flags": ["-ffreestanding", "-shared", "-fno-rtti", "-fno-exceptions", " -Wl,--subsystem=17"], + "cpp_macros": ["__CRT_AMD64__", "cCRTVersion=0x0100", "cEFSVersionHighest=0x0100", "cEFSVersionLowest=0x0100"] + } + \ No newline at end of file -- cgit v1.2.3