From d48cbe75ef29a9c67c9d176bf58e56ea6448fb9e Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Mon, 21 Oct 2024 20:23:36 +0200 Subject: IMP: Major refactor of header and source files extensions. Signed-off-by: Amlal El Mahrouss --- dev/crt/alloca.h | 19 +++++++ dev/crt/alloca.hxx | 19 ------- dev/crt/base_alloc.h | 49 +++++++++++++++++ dev/crt/base_alloc.hxx | 49 ----------------- dev/crt/base_exception.h | 36 +++++++++++++ dev/crt/base_exception.hxx | 36 ------------- dev/crt/crtx64.json | 2 +- dev/crt/defines.h | 106 +++++++++++++++++++++++++++++++++++++ dev/crt/defines.hxx | 106 ------------------------------------- dev/crt/exit.h | 18 +++++++ dev/crt/exit.hxx | 18 ------- dev/crt/math.h | 62 ++++++++++++++++++++++ dev/crt/math.hxx | 62 ---------------------- dev/crt/src/alloca.cc | 9 ++++ dev/crt/src/alloca.cxx | 9 ---- dev/crt/src/base_exception.cc | 9 ++++ dev/crt/src/base_exception.cxx | 9 ---- dev/crt/src/hal/x86/base_alloc.cc | 55 +++++++++++++++++++ dev/crt/src/hal/x86/base_alloc.cxx | 55 ------------------- dev/crt/src/hal/x86/exit.cc | 19 +++++++ dev/crt/src/hal/x86/exit.cxx | 19 ------- 21 files changed, 383 insertions(+), 383 deletions(-) create mode 100644 dev/crt/alloca.h delete mode 100644 dev/crt/alloca.hxx create mode 100644 dev/crt/base_alloc.h delete mode 100644 dev/crt/base_alloc.hxx create mode 100644 dev/crt/base_exception.h delete mode 100644 dev/crt/base_exception.hxx create mode 100644 dev/crt/defines.h delete mode 100644 dev/crt/defines.hxx create mode 100644 dev/crt/exit.h delete mode 100644 dev/crt/exit.hxx create mode 100644 dev/crt/math.h delete mode 100644 dev/crt/math.hxx create mode 100644 dev/crt/src/alloca.cc delete mode 100644 dev/crt/src/alloca.cxx create mode 100644 dev/crt/src/base_exception.cc delete mode 100644 dev/crt/src/base_exception.cxx create mode 100644 dev/crt/src/hal/x86/base_alloc.cc delete mode 100644 dev/crt/src/hal/x86/base_alloc.cxx create mode 100644 dev/crt/src/hal/x86/exit.cc delete mode 100644 dev/crt/src/hal/x86/exit.cxx (limited to 'dev/crt') diff --git a/dev/crt/alloca.h b/dev/crt/alloca.h new file mode 100644 index 00000000..857a0ade --- /dev/null +++ b/dev/crt/alloca.h @@ -0,0 +1,19 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + + File: alloca.h + Purpose: Stack allocation functions. + +------------------------------------------- */ + +#pragma once + +#include + +inline ptr_type __ndk_alloca(size_type sz) +{ + return __builtin_alloca(sz); +} + +#define alloca __ndk_alloca diff --git a/dev/crt/alloca.hxx b/dev/crt/alloca.hxx deleted file mode 100644 index ca4d2a99..00000000 --- a/dev/crt/alloca.hxx +++ /dev/null @@ -1,19 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - - File: alloca.hxx - Purpose: Stack allocation functions. - -------------------------------------------- */ - -#pragma once - -#include - -inline ptr_type __ndk_alloca(size_type sz) -{ - return __builtin_alloca(sz); -} - -#define alloca __ndk_alloca diff --git a/dev/crt/base_alloc.h b/dev/crt/base_alloc.h new file mode 100644 index 00000000..97826d80 --- /dev/null +++ b/dev/crt/base_alloc.h @@ -0,0 +1,49 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include + +namespace std::base_alloc +{ + /// @brief allocate a new class. + /// @tparam KindClass the class type to allocate. + template + inline KindClass* allocate(Args&&... args) + { + return new KindClass(forward(args)...); + } + + /// @brief allocate a new class. + /// @note aborts on error. + /// @tparam KindClass the class type to allocate. + template + inline KindClass* allocate_nothrow(Args&&... args) noexcept + { + return allocate(forward(args)...); + } + + /// @brief free a class. + /// @tparam KindClass the class type to allocate. + template + inline void release(KindClass ptr) + { + if (!ptr) + return; + + delete ptr; + } + + /// @brief destroy and free a class. + /// @note aborts on error. + /// @tparam KindClass the class type to allocate. + template + inline void release_nothrow(KindClass ptr) noexcept + { + release(ptr); + } +} // namespace std::base_alloc diff --git a/dev/crt/base_alloc.hxx b/dev/crt/base_alloc.hxx deleted file mode 100644 index 94f2b25c..00000000 --- a/dev/crt/base_alloc.hxx +++ /dev/null @@ -1,49 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#pragma once - -#include - -namespace std::base_alloc -{ - /// @brief allocate a new class. - /// @tparam KindClass the class type to allocate. - template - inline KindClass* allocate(Args&&... args) - { - return new KindClass(forward(args)...); - } - - /// @brief allocate a new class. - /// @note aborts on error. - /// @tparam KindClass the class type to allocate. - template - inline KindClass* allocate_nothrow(Args&&... args) noexcept - { - return allocate(forward(args)...); - } - - /// @brief free a class. - /// @tparam KindClass the class type to allocate. - template - inline void release(KindClass ptr) - { - if (!ptr) - return; - - delete ptr; - } - - /// @brief destroy and free a class. - /// @note aborts on error. - /// @tparam KindClass the class type to allocate. - template - inline void release_nothrow(KindClass ptr) noexcept - { - release(ptr); - } -} // namespace std::base_alloc diff --git a/dev/crt/base_exception.h b/dev/crt/base_exception.h new file mode 100644 index 00000000..3b95f400 --- /dev/null +++ b/dev/crt/base_exception.h @@ -0,0 +1,36 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include +#include + +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_alloc(void) + { + __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/base_exception.hxx b/dev/crt/base_exception.hxx deleted file mode 100644 index 4d815374..00000000 --- a/dev/crt/base_exception.hxx +++ /dev/null @@ -1,36 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#pragma once - -#include -#include - -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_alloc(void) - { - __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/crtx64.json b/dev/crt/crtx64.json index 3e321e5d..b8e99385 100644 --- a/dev/crt/crtx64.json +++ b/dev/crt/crtx64.json @@ -2,7 +2,7 @@ "compiler_path": "x86_64-w64-mingw32-g++", "compiler_std": "c++20", "headers_path": ["../", "./"], - "sources_path": ["src/*.cxx", "src/hal/x86/*.cxx"], + "sources_path": ["src/*.cc", "src/hal/x86/*.cc"], "output_name": "crtx64.dll", "compiler_flags": [ "-ffreestanding", diff --git a/dev/crt/defines.h b/dev/crt/defines.h new file mode 100644 index 00000000..38fe2b6c --- /dev/null +++ b/dev/crt/defines.h @@ -0,0 +1,106 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#ifndef __NDK_DEFINES_HXX__ +#define __NDK_DEFINES_HXX__ + +#include +#include + +#ifdef __GNUC__ + +typedef void* ptr_type; +typedef __SIZE_TYPE__ size_type; + +typedef void* voidptr_t; +typedef void* any_t; +typedef char* caddr_t; + +#ifndef NULL +#define NULL ((voidptr_t)0) +#endif // !null + +#ifdef __GNUC__ +#include +#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 + +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__ + +/// @brief Standard C++ namespace. +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/defines.hxx b/dev/crt/defines.hxx deleted file mode 100644 index f23bdaa3..00000000 --- a/dev/crt/defines.hxx +++ /dev/null @@ -1,106 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#ifndef __NDK_DEFINES_HXX__ -#define __NDK_DEFINES_HXX__ - -#include -#include - -#ifdef __GNUC__ - -typedef void* ptr_type; -typedef __SIZE_TYPE__ size_type; - -typedef void* voidptr_t; -typedef void* any_t; -typedef char* caddr_t; - -#ifndef NULL -#define NULL ((voidptr_t)0) -#endif // !null - -#ifdef __GNUC__ -#include -#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 - -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__ - -/// @brief Standard C++ namespace. -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/exit.h b/dev/crt/exit.h new file mode 100644 index 00000000..69b046c4 --- /dev/null +++ b/dev/crt/exit.h @@ -0,0 +1,18 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +extern "C" int exit(int code); + +/// @brief Standard C++ namespace +namespace std +{ + inline int exit(int code) + { + return exit(code); + } +} // namespace std diff --git a/dev/crt/exit.hxx b/dev/crt/exit.hxx deleted file mode 100644 index 69b046c4..00000000 --- a/dev/crt/exit.hxx +++ /dev/null @@ -1,18 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#pragma once - -extern "C" int exit(int code); - -/// @brief Standard C++ namespace -namespace std -{ - inline int exit(int code) - { - return exit(code); - } -} // namespace std diff --git a/dev/crt/math.h b/dev/crt/math.h new file mode 100644 index 00000000..fd948d63 --- /dev/null +++ b/dev/crt/math.h @@ -0,0 +1,62 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include + +/// @file Math.h +/// @brief Math functions. + +#ifdef __ZKA_USE_DOUBLE__ +typedef double real_type; +#else +typedef float real_type; +#endif + +namespace std::math +{ + /// @brief Power function, with Repeat argument. + template + inline real_type pow(real_type in) + { + if (Exponent == 0) + return 1; // Any number to the power of 0 is 1. + + if (Exponent == 1) + return in; // Any number to the power of 1 is itself. + + size_t cnt = Exponent; + + real_type result = 1; + + for (auto i = 0; i < cnt; ++i) + result *= in; + + return result; + } + + /// @brief Square of function, with Base template argument. + /// @param of Base argument to find sqquare of + template + inline real_type sqr(real_type in) + { + if (in == 0) + return 0; + + return pow<1 / Base>(in); + } + + /// @brief Linear interpolation equation solver. + /// @param from where? + /// @param to to? + /// @param Updated diff value according to difference. + inline real_type lerp(real_type to, real_type from, real_type stat) + { + real_type diff = (to - from); + return from + (diff * stat); + } +} // namespace std::math diff --git a/dev/crt/math.hxx b/dev/crt/math.hxx deleted file mode 100644 index 57ae16d8..00000000 --- a/dev/crt/math.hxx +++ /dev/null @@ -1,62 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#pragma once - -#include - -/// @file Math.hxx -/// @brief Math functions. - -#ifdef __ZKA_USE_DOUBLE__ -typedef double real_type; -#else -typedef float real_type; -#endif - -namespace std::math -{ - /// @brief Power function, with Repeat argument. - template - inline real_type pow(real_type in) - { - if (Exponent == 0) - return 1; // Any number to the power of 0 is 1. - - if (Exponent == 1) - return in; // Any number to the power of 1 is itself. - - size_t cnt = Exponent; - - real_type result = 1; - - for (auto i = 0; i < cnt; ++i) - result *= in; - - return result; - } - - /// @brief Square of function, with Base template argument. - /// @param of Base argument to find sqquare of - template - inline real_type sqr(real_type in) - { - if (in == 0) - return 0; - - return pow<1 / Base>(in); - } - - /// @brief Linear interpolation equation solver. - /// @param from where? - /// @param to to? - /// @param Updated diff value according to difference. - inline real_type lerp(real_type to, real_type from, real_type stat) - { - real_type diff = (to - from); - return from + (diff * stat); - } -} // namespace std::math diff --git a/dev/crt/src/alloca.cc b/dev/crt/src/alloca.cc new file mode 100644 index 00000000..ccee07a7 --- /dev/null +++ b/dev/crt/src/alloca.cc @@ -0,0 +1,9 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#include + +/// @note Just here for building. diff --git a/dev/crt/src/alloca.cxx b/dev/crt/src/alloca.cxx deleted file mode 100644 index 042eb769..00000000 --- a/dev/crt/src/alloca.cxx +++ /dev/null @@ -1,9 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#include - -/// @note Just here for building. diff --git a/dev/crt/src/base_exception.cc b/dev/crt/src/base_exception.cc new file mode 100644 index 00000000..75699f64 --- /dev/null +++ b/dev/crt/src/base_exception.cc @@ -0,0 +1,9 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#include + +/// @note Just here for building. diff --git a/dev/crt/src/base_exception.cxx b/dev/crt/src/base_exception.cxx deleted file mode 100644 index f82d610a..00000000 --- a/dev/crt/src/base_exception.cxx +++ /dev/null @@ -1,9 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#include - -/// @note Just here for building. diff --git a/dev/crt/src/hal/x86/base_alloc.cc b/dev/crt/src/hal/x86/base_alloc.cc new file mode 100644 index 00000000..10690d4b --- /dev/null +++ b/dev/crt/src/hal/x86/base_alloc.cc @@ -0,0 +1,55 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#include +#include + +#define kAllocSyscallId "mov $10, %%r9\n\t" +#define kFreeSyscallId "mov $11, %%r9\n\t" + +void* operator new(size_t length) +{ + void* ptr = nullptr; + + // do syscall according to PEF convention. + asm("mov %0, %%r8\n\t" kAllocSyscallId + "syscall" + : "=r"(ptr)); + + if (!ptr) + std::__throw_bad_alloc(); + + return ptr; +} + +void* operator new[](size_t length) +{ + ptr_type ptr = nullptr; + + // do syscall according to PEF convention. + asm("mov %0, %%r8\n\t" kAllocSyscallId + "syscall" + : "=r"(ptr)); + + if (!ptr) + std::__throw_bad_alloc(); + + return ptr; +} + +void operator delete(void* ptr) noexcept +{ + asm("mov %0, %%r8\n\t" kFreeSyscallId + "syscall" + : "=r"(ptr)); +} + +void operator delete[](void* ptr) noexcept +{ + asm("mov %0, %%r8\n\t" kFreeSyscallId + "syscall" + : "=r"(ptr)); +} diff --git a/dev/crt/src/hal/x86/base_alloc.cxx b/dev/crt/src/hal/x86/base_alloc.cxx deleted file mode 100644 index 55a53c9d..00000000 --- a/dev/crt/src/hal/x86/base_alloc.cxx +++ /dev/null @@ -1,55 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#include -#include - -#define kAllocSyscallId "mov $10, %%r9\n\t" -#define kFreeSyscallId "mov $11, %%r9\n\t" - -void* operator new(size_t length) -{ - void* ptr = nullptr; - - // do syscall according to PEF convention. - asm("mov %0, %%r8\n\t" kAllocSyscallId - "syscall" - : "=r"(ptr)); - - if (!ptr) - std::__throw_bad_alloc(); - - return ptr; -} - -void* operator new[](size_t length) -{ - ptr_type ptr = nullptr; - - // do syscall according to PEF convention. - asm("mov %0, %%r8\n\t" kAllocSyscallId - "syscall" - : "=r"(ptr)); - - if (!ptr) - std::__throw_bad_alloc(); - - return ptr; -} - -void operator delete(void* ptr) noexcept -{ - asm("mov %0, %%r8\n\t" kFreeSyscallId - "syscall" - : "=r"(ptr)); -} - -void operator delete[](void* ptr) noexcept -{ - asm("mov %0, %%r8\n\t" kFreeSyscallId - "syscall" - : "=r"(ptr)); -} diff --git a/dev/crt/src/hal/x86/exit.cc b/dev/crt/src/hal/x86/exit.cc new file mode 100644 index 00000000..8ee6744b --- /dev/null +++ b/dev/crt/src/hal/x86/exit.cc @@ -0,0 +1,19 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#include + +#define kAllocSyscallId "mov $12, %%r9\n\t" + +/// @note Just here for building. +extern "C" int exit(int code) +{ + asm("mov 0, %%r8\n\t" kAllocSyscallId + "syscall" + : "=r"(code)); + + return 1; +} diff --git a/dev/crt/src/hal/x86/exit.cxx b/dev/crt/src/hal/x86/exit.cxx deleted file mode 100644 index c44c07a4..00000000 --- a/dev/crt/src/hal/x86/exit.cxx +++ /dev/null @@ -1,19 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Web Services Co. - -------------------------------------------- */ - -#include - -#define kAllocSyscallId "mov $12, %%r9\n\t" - -/// @note Just here for building. -extern "C" int exit(int code) -{ - asm("mov 0, %%r8\n\t" kAllocSyscallId - "syscall" - : "=r"(code)); - - return 1; -} -- cgit v1.2.3