From ef7f9bead82e73ff3efd4577a20e5170b017a859 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Mon, 12 Jan 2026 20:54:09 +0100 Subject: chore: Nectar Generics Library improvements. Signed-off-by: Amlal El Mahrouss --- CLAUDE.md | 2 +- include/LibNectar/.gitignore | 1 - include/LibNectar/__abi.h | 24 --------- include/LibNectar/__power64.inc | 40 -------------- include/LibNectar/base_alloc.h | 43 --------------- include/LibNectar/base_exception.h | 35 ------------ include/LibNectar/base_math.h | 98 ---------------------------------- include/LibNectar/base_process.h | 44 --------------- include/LibNectar/defines.h | 80 --------------------------- include/LibNectar/filesystem.h | 18 ------- include/LibNectar/iostream.nhh | 24 +++++++++ include/LibNectar/iterator.nhh | 33 ++++++++++++ include/LibNectar/mkhdrs.sh | 15 ------ include/LibNectar/new.h | 65 ---------------------- include/LibNectar/utility.h | 27 ---------- include/LibNectarCore/.gitignore | 1 + include/LibNectarCore/__abi.h | 24 +++++++++ include/LibNectarCore/__power64.inc | 40 ++++++++++++++ include/LibNectarCore/base_alloc.h | 43 +++++++++++++++ include/LibNectarCore/base_exception.h | 35 ++++++++++++ include/LibNectarCore/base_math.h | 98 ++++++++++++++++++++++++++++++++++ include/LibNectarCore/base_process.h | 44 +++++++++++++++ include/LibNectarCore/defines.h | 80 +++++++++++++++++++++++++++ include/LibNectarCore/filesystem.h | 18 +++++++ include/LibNectarCore/mkhdrs.sh | 15 ++++++ include/LibNectarCore/new.h | 65 ++++++++++++++++++++++ include/LibNectarCore/utility.h | 27 ++++++++++ include/LibNectarStd/iostream.ncpp | 19 ------- include/LibNectarStd/iterator.ncpp | 13 ----- test/test_samples/iostream.ncpp | 2 +- test/test_samples/return_value.ncpp | 2 + 31 files changed, 551 insertions(+), 524 deletions(-) delete mode 100644 include/LibNectar/.gitignore delete mode 100644 include/LibNectar/__abi.h delete mode 100644 include/LibNectar/__power64.inc delete mode 100644 include/LibNectar/base_alloc.h delete mode 100644 include/LibNectar/base_exception.h delete mode 100644 include/LibNectar/base_math.h delete mode 100644 include/LibNectar/base_process.h delete mode 100644 include/LibNectar/defines.h delete mode 100644 include/LibNectar/filesystem.h create mode 100644 include/LibNectar/iostream.nhh create mode 100644 include/LibNectar/iterator.nhh delete mode 100755 include/LibNectar/mkhdrs.sh delete mode 100644 include/LibNectar/new.h delete mode 100644 include/LibNectar/utility.h create mode 100644 include/LibNectarCore/.gitignore create mode 100644 include/LibNectarCore/__abi.h create mode 100644 include/LibNectarCore/__power64.inc create mode 100644 include/LibNectarCore/base_alloc.h create mode 100644 include/LibNectarCore/base_exception.h create mode 100644 include/LibNectarCore/base_math.h create mode 100644 include/LibNectarCore/base_process.h create mode 100644 include/LibNectarCore/defines.h create mode 100644 include/LibNectarCore/filesystem.h create mode 100755 include/LibNectarCore/mkhdrs.sh create mode 100644 include/LibNectarCore/new.h create mode 100644 include/LibNectarCore/utility.h delete mode 100644 include/LibNectarStd/iostream.ncpp delete mode 100644 include/LibNectarStd/iterator.ncpp diff --git a/CLAUDE.md b/CLAUDE.md index c470fec..65404fd 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -76,7 +76,7 @@ src/ │ ├── dbg.cc # User-space debugger │ └── kdbg.cc # Kernel debugger │ -└── LibNectar/ # Nectar ABI runtime (header-only) +└── LibNectarCore/ # C++/Nectar ABI runtime (header-only) ``` ### Compilation Pipeline diff --git a/include/LibNectar/.gitignore b/include/LibNectar/.gitignore deleted file mode 100644 index e3f10ea..0000000 --- a/include/LibNectar/.gitignore +++ /dev/null @@ -1 +0,0 @@ -libc++/ diff --git a/include/LibNectar/__abi.h b/include/LibNectar/__abi.h deleted file mode 100644 index ce6424b..0000000 --- a/include/LibNectar/__abi.h +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (See accompanying -// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) -// Official repository: https://github.com/nekernel-org/nectar - -#ifndef NECTAR_LIBNECTAR_ABI_H -#define NECTAR_LIBNECTAR_ABI_H - -#include -#include - -__init_decl() - - static constexpr int32_t __unreachable_code = 34; - -inline void __compilerkit_unreachable(void) { - std::base_process::signal(__unreachable_code); - - while (1); -} - -__fini_decl() - -#endif // NECTAR_LIBNECTAR_ABI_H diff --git a/include/LibNectar/__power64.inc b/include/LibNectar/__power64.inc deleted file mode 100644 index 8b65ad7..0000000 --- a/include/LibNectar/__power64.inc +++ /dev/null @@ -1,40 +0,0 @@ -# Path: LibC++/__power64.inc -# Language: CompilerKit POWER Assembly support for GNU. -# Build Date: 2024-6-4 - -#ifdef __NECTAR__ - -#ifdef __ASSEMBLER__ - -#define lda li -#define sta stw -#define ldw li - -#define r0 0 -#define r1 1 -#define r2 2 -#define r3 3 -#define r4 4 -#define r5 5 -#define r6 6 -#define r7 7 -#define r8 8 -#define r9 9 -#define r10 10 -#define r11 11 -#define r12 12 -#define r13 13 -#define r14 14 -#define r15 15 -#define r16 16 -#define r17 17 -#define r18 18 -#define r19 19 -#define r20 20 - -#define nop mr 0, 0 - -#endif - -#endif - diff --git a/include/LibNectar/base_alloc.h b/include/LibNectar/base_alloc.h deleted file mode 100644 index 81a3299..0000000 --- a/include/LibNectar/base_alloc.h +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (See accompanying -// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) -// Official repository: https://github.com/nekernel-org/nectar - -#ifndef NECTAR_LIBNECTAR_BASE_ALLOC_H -#define NECTAR_LIBNECTAR_BASE_ALLOC_H - -#include - -/// @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); -} - -#endif // NECTAR_LIBNECTAR_BASE_ALLOC_H diff --git a/include/LibNectar/base_exception.h b/include/LibNectar/base_exception.h deleted file mode 100644 index b5c6a88..0000000 --- a/include/LibNectar/base_exception.h +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (See accompanying -// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) -// Official repository: https://github.com/nekernel-org/nectar - -#ifndef NECTAR_LIBCXX_BASE_EXCEPTION_H -#define NECTAR_LIBCXX_BASE_EXCEPTION_H - -#include -#include -#include - -/// @author Amlal El Mahrouss (amlal@nekernel.org) - -inline constexpr int __terminate_id = 33; - -/// @note This function is internal, don't call it. -extern void __unwind_object_list(); - -inline void __throw_general(const char* what) { - __unwind_object_list(); - exit(__terminate_id); -} - -inline void __throw_domain_error(const char* what) { - __throw_general(what); - __builtin_unreachable(); // prevent from continuing. -} - -inline void __throw_bad_array_new_length(const char* what) { - __throw_general(what); - __builtin_unreachable(); // prevent from continuing. -} - -#endif // NECTAR_LIBCXX_BASE_EXCEPTION_H diff --git a/include/LibNectar/base_math.h b/include/LibNectar/base_math.h deleted file mode 100644 index 5a1ce8b..0000000 --- a/include/LibNectar/base_math.h +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (See accompanying -// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) -// Official repository: https://github.com/nekernel-org/nectar - -#ifndef NECTAR_LIBCXX_BASE_MATH_H -#define NECTAR_LIBCXX_BASE_MATH_H - -#include - -#ifndef NAN -#define NAN (__builtin_nanf("")) -#endif // !NAN - -/// @file base_math.h -/// @brief Base Mathematic functions. - -#ifdef __LIBCXX_USE_DOUBLE__ -typedef double real_type; -#else -typedef float real_type; -#endif - -inline constexpr static auto not_a_number = NAN; - -/// =========================================================== /// -/// @brief Power of Exponent function. -/// =========================================================== /// -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. - - real_type result = 1; - - for (auto i = 0UL; i < Exponent; ++i) result *= in; - - return result; -} - -/// =========================================================== /// -/// @brief Square root function. -/// =========================================================== /// -inline real_type sqrt(real_type in) { - if (in == 0) return 0; - if (in == not_a_number) return not_a_number; - - auto constexpr const static Base = 2; - - auto x = in / Base; - - for (int i = 0; i < 10; ++i) { - x = (x + in / x) / Base; - } - - return x; -} - -/// =========================================================== /// -/// @brief Square of function, with Base template argument. -/// @param of Base argument to find the square of. -/// =========================================================== /// -template -inline real_type surd(real_type in) { - if (in == 0) return 0; - if (in == 1) return 1; - - if (Base == 1) return in; - if (Base == 2) return sqrt(in); - - return not_a_number; -} - -/// =========================================================== /// -/// @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); -} - -using real_domain = double; - -struct complex_domain final { - double Re; - double Im; -}; - -typename using callable_type = Result (*)(size_t n, ...); - -#ifdef __cpp_lib_base_math -#define __cpp_lib_base_math 1 -#endif - -#endif // NECTAR_LIBCXX_BASE_MATH_H diff --git a/include/LibNectar/base_process.h b/include/LibNectar/base_process.h deleted file mode 100644 index 80ef299..0000000 --- a/include/LibNectar/base_process.h +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (See accompanying -// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) -// Official repository: https://github.com/nekernel-org/nectar - -#ifndef NECTAR_LIBCXX_BASE_PROCESS_H -#define NECTAR_LIBCXX_BASE_PROCESS_H - -#include - -__init_decl() - - /// @brief CRT exit, with exit code (!!! exits all threads. !!!) - /// @param code the exit code. - /// @return the return > 0 for non successful. - extern int exit_(int code); - -/// @brief CRT signal handler. -/// @param code the signal code. -extern void signal_(int code); - -extern void (*__atexit_cdecl_ptr)(void); -extern void (**__atexit_lst_ptr)(void); -extern size_t __atexit_lst_cnt; - -__fini_decl() - - inline int signal(int code) { - signal_(code); - return -1; -} - -inline int32_t exit(const int32_t& code) { - for (auto idx = 0UL; idx < __atexit_lst_cnt; ++idx) { - __atexit_lst_ptr[idx](); - } - - if (__atexit_cdecl_ptr) __atexit_cdecl_ptr(); - - exit_(code); - return -1; -} - -#endif // NECTAR_LIBCXX_BASE_PROCESS_H diff --git a/include/LibNectar/defines.h b/include/LibNectar/defines.h deleted file mode 100644 index 061769c..0000000 --- a/include/LibNectar/defines.h +++ /dev/null @@ -1,80 +0,0 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (See accompanying -// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) -// Official repository: https://github.com/nekernel-org/nectar - -#ifndef __NECTAR_DEFINES_H__ -#define __NECTAR_DEFINES_H__ - -#define __ATTRIBUTE(X) __attribute__((X)) - -typedef __SIZE_TYPE__ size_t; -typedef __INT64_TYPE__ ssize_t; -typedef __INT32_TYPE__ int32_t; - -typedef void* ptr_type; -typedef __SIZE_TYPE__ size_type; - -typedef __INT64_TYPE__ 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 - -#define __alloca(sz) __ck_alloca(sz) - -#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; - -namespace std { -struct placement_t; -struct nothrow_t; -} // namespace std - -#endif /* __NECTAR_DEFINES_H__ */ diff --git a/include/LibNectar/filesystem.h b/include/LibNectar/filesystem.h deleted file mode 100644 index daac4c1..0000000 --- a/include/LibNectar/filesystem.h +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (See accompanying -// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) -// Official repository: https://github.com/nekernel-org/nectar - -#ifndef __NECTAR_FS_H__ -#define __NECTAR_FS_H__ - -class path; -class filesystem_error; -class directory_entry; -class directory_iterator; - -#ifndef __nec_lib_filesystem -#define __nec_lib_filesystem 201703L -#endif - -#endif // __NECTAR_FS_H__ diff --git a/include/LibNectar/iostream.nhh b/include/LibNectar/iostream.nhh new file mode 100644 index 0000000..9e14174 --- /dev/null +++ b/include/LibNectar/iostream.nhh @@ -0,0 +1,24 @@ +// Copyright 2026, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (See accompanying +// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) +// Official repository: https://github.com/nekernel-org/nectar + +#ifndef NECTAR_LIBNECTAR_IOSTREAM_NHH +#define NECTAR_LIBNECTAR_IOSTREAM_NHH + +import printf, scanf; + +struct ostream +{ + let write(let data, let size) + { + return nil; + } + + let read(let region, let size) + { + return nil; + } +}; + +#endif // NECTAR_LIBNECTAR_IOSTREAM_NHH \ No newline at end of file diff --git a/include/LibNectar/iterator.nhh b/include/LibNectar/iterator.nhh new file mode 100644 index 0000000..2f4e34e --- /dev/null +++ b/include/LibNectar/iterator.nhh @@ -0,0 +1,33 @@ +// Copyright 2026, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (See accompanying +// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) +// Official repository: https://github.com/nekernel-org/nectar + +#ifndef NECTAR_LIBNECTAR_ITERATOR_NHH +#define NECTAR_LIBNECTAR_ITERATOR_NHH + +struct iterator_traits +{ + let begin(let collection) + { + return collection._begin; + } + + let end(let collection) + { + return collection._begin + collection._size; + } + + let size(let collection) + { + return collection._size; + } +}; + +type +typedef It::pointer iterator_pointer; + +type +typedef It::reference iterator_reference; + +#endif // NECTAR_LIBNECTAR_ITERATOR_NHH \ No newline at end of file diff --git a/include/LibNectar/mkhdrs.sh b/include/LibNectar/mkhdrs.sh deleted file mode 100755 index b6b9f75..0000000 --- a/include/LibNectar/mkhdrs.sh +++ /dev/null @@ -1,15 +0,0 @@ -#! /bin/sh - -outputDir=libcnec/ - -mkdir -p $outputDir - -for f in *.h; do - -#This line splits the file name on the delimiter "." -baseName=`echo $f | cut -d "." -f 1` - -cp $f $outputDir$baseName - -done - diff --git a/include/LibNectar/new.h b/include/LibNectar/new.h deleted file mode 100644 index 31f80bf..0000000 --- a/include/LibNectar/new.h +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (See accompanying -// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) -// Official repository: https://github.com/nekernel-org/nectar - -#ifndef NECTAR_LIBNECTAR_NEW_H -#define NECTAR_LIBNECTAR_NEW_H - -#include - -struct nothrow_t final { - explicit nothrow_t() = default; - ~nothrow_t() = default; -}; - -struct placement_t final { - explicit placement_t() = default; - ~placement_t() = default; - - void* __base{}; - int32_t __align{}; - size_t __size{}; -}; - -// AMLALE: Define the placement_t feature. -#ifndef __nectar_has_placement -#define __nectar_has_placement 1 -#endif - -// AMLALE: Define nothrow -#ifndef __nectar_has_nothrow -#define __nectar_has_nothrow 1 -#endif - -void* operator new(size_t); -void* operator new[](size_t); - -/// \brief placement_t new and delete operators. Governs how the memory shall be placed. -/// \note This is a feature that shall be used wisely, failure to do so will produce Undefined -/// Behaviors at runtime. -void* operator pnew(struct placement_t*); -void operator pdelete(struct placement_t*, void*); - -/// \brief For all offsets within the base range and alignement 'align' -/// \brief Allocate offsets with respect to the `base` interval, apply alignement of `align` value. -/// Return `offsets` of length n as an aligned value within the domain of `base`. -using placeable_callable_type = void* /*offsets*/ (*) (void* base, size_t n, const int& align); - -/// \note This should NOT fail, failure to meet the conditions will cause the program's state to be -/// aborted. -/// \brief Set the placement policy of future memory allocations. -template -void set_placement_policy(const PlaceableCallable&) noexcept; - -void* operator new(size_t, const nothrow_t&) noexcept; -void* operator new(size_t, void*) noexcept; -void* operator new[](size_t, const nothrow_t&) noexcept; -void* operator new[](size_t, void*) noexcept; - -void operator delete(void*) noexcept; -void operator delete(void*, size_t) noexcept; - -void operator delete[](void*) noexcept; - -#endif // NECTAR_LIBNECTAR_NEW_H diff --git a/include/LibNectar/utility.h b/include/LibNectar/utility.h deleted file mode 100644 index 834b1a6..0000000 --- a/include/LibNectar/utility.h +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (See accompanying -// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) -// Official repository: https://github.com/nekernel-org/nectar - -#ifndef LIBNECTAR_UTILITY_H -#define LIBNECTAR_UTILITY_H - -/// @brief Forward object. -/// @tparam Args the object type. -/// @param arg the object. -/// @return object's rvalue -template -inline auto forward(Args& arg) -> Args&& { - return static_cast(arg); -} - -/// @brief Move object. -/// @tparam Args the object type. -/// @param arg the object. -/// @return object's rvalue -template -inline auto move(Args&& arg) -> Args&& { - return static_cast(arg); -} - -#endif // LIBNECTAR_UTILITY_H diff --git a/include/LibNectarCore/.gitignore b/include/LibNectarCore/.gitignore new file mode 100644 index 0000000..e3f10ea --- /dev/null +++ b/include/LibNectarCore/.gitignore @@ -0,0 +1 @@ +libc++/ diff --git a/include/LibNectarCore/__abi.h b/include/LibNectarCore/__abi.h new file mode 100644 index 0000000..802bd1c --- /dev/null +++ b/include/LibNectarCore/__abi.h @@ -0,0 +1,24 @@ +// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (See accompanying +// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) +// Official repository: https://github.com/nekernel-org/nectar + +#ifndef NECTAR_LIBNECTAR_ABI_H +#define NECTAR_LIBNECTAR_ABI_H + +#include +#include + +__init_decl() + +static constexpr int32_t __unreachable_code = 34; + +inline void __compilerkit_unreachable(void) { + signal(__unreachable_code); + + while (1); +} + +__fini_decl() + +#endif // NECTAR_LIBNECTAR_ABI_H diff --git a/include/LibNectarCore/__power64.inc b/include/LibNectarCore/__power64.inc new file mode 100644 index 0000000..8b65ad7 --- /dev/null +++ b/include/LibNectarCore/__power64.inc @@ -0,0 +1,40 @@ +# Path: LibC++/__power64.inc +# Language: CompilerKit POWER Assembly support for GNU. +# Build Date: 2024-6-4 + +#ifdef __NECTAR__ + +#ifdef __ASSEMBLER__ + +#define lda li +#define sta stw +#define ldw li + +#define r0 0 +#define r1 1 +#define r2 2 +#define r3 3 +#define r4 4 +#define r5 5 +#define r6 6 +#define r7 7 +#define r8 8 +#define r9 9 +#define r10 10 +#define r11 11 +#define r12 12 +#define r13 13 +#define r14 14 +#define r15 15 +#define r16 16 +#define r17 17 +#define r18 18 +#define r19 19 +#define r20 20 + +#define nop mr 0, 0 + +#endif + +#endif + diff --git a/include/LibNectarCore/base_alloc.h b/include/LibNectarCore/base_alloc.h new file mode 100644 index 0000000..e68efdb --- /dev/null +++ b/include/LibNectarCore/base_alloc.h @@ -0,0 +1,43 @@ +// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (See accompanying +// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) +// Official repository: https://github.com/nekernel-org/nectar + +#ifndef NECTAR_LIBNECTAR_BASE_ALLOC_H +#define NECTAR_LIBNECTAR_BASE_ALLOC_H + +#include + +/// @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); +} + +#endif // NECTAR_LIBNECTAR_BASE_ALLOC_H diff --git a/include/LibNectarCore/base_exception.h b/include/LibNectarCore/base_exception.h new file mode 100644 index 0000000..992f76f --- /dev/null +++ b/include/LibNectarCore/base_exception.h @@ -0,0 +1,35 @@ +// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (See accompanying +// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) +// Official repository: https://github.com/nekernel-org/nectar + +#ifndef NECTAR_LIBCXX_BASE_EXCEPTION_H +#define NECTAR_LIBCXX_BASE_EXCEPTION_H + +#include +#include +#include + +/// @author Amlal El Mahrouss (amlal@nekernel.org) + +inline constexpr int __terminate_id = 33; + +/// @note This function is internal, don't call it. +extern void __unwind_object_list(); + +inline void __throw_general(const char* what) { + __unwind_object_list(); + exit(__terminate_id); +} + +inline void __throw_domain_error(const char* what) { + __throw_general(what); + __builtin_unreachable(); // prevent from continuing. +} + +inline void __throw_bad_array_new_length(const char* what) { + __throw_general(what); + __builtin_unreachable(); // prevent from continuing. +} + +#endif // NECTAR_LIBCXX_BASE_EXCEPTION_H diff --git a/include/LibNectarCore/base_math.h b/include/LibNectarCore/base_math.h new file mode 100644 index 0000000..af31764 --- /dev/null +++ b/include/LibNectarCore/base_math.h @@ -0,0 +1,98 @@ +// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (See accompanying +// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) +// Official repository: https://github.com/nekernel-org/nectar + +#ifndef NECTAR_LIBCXX_BASE_MATH_H +#define NECTAR_LIBCXX_BASE_MATH_H + +#include + +#ifndef NAN +#define NAN (__builtin_nanf("")) +#endif // !NAN + +/// @file base_math.h +/// @brief Base Mathematic functions. + +#ifdef __LIBCXX_USE_DOUBLE__ +typedef double real_type; +#else +typedef float real_type; +#endif + +inline constexpr static auto not_a_number = NAN; + +/// =========================================================== /// +/// @brief Power of Exponent function. +/// =========================================================== /// +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. + + real_type result = 1; + + for (auto i = 0UL; i < Exponent; ++i) result *= in; + + return result; +} + +/// =========================================================== /// +/// @brief Square root function. +/// =========================================================== /// +inline real_type sqrt(real_type in) { + if (in == 0) return 0; + if (in == not_a_number) return not_a_number; + + auto constexpr const static Base = 2; + + auto x = in / Base; + + for (int i = 0; i < 10; ++i) { + x = (x + in / x) / Base; + } + + return x; +} + +/// =========================================================== /// +/// @brief Square of function, with Base template argument. +/// @param of Base argument to find the square of. +/// =========================================================== /// +template +inline real_type surd(real_type in) { + if (in == 0) return 0; + if (in == 1) return 1; + + if (Base == 1) return in; + if (Base == 2) return sqrt(in); + + return not_a_number; +} + +/// =========================================================== /// +/// @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); +} + +using real_domain = double; + +struct complex_domain final { + double Re; + double Im; +}; + +typename using callable_type = Result (*)(size_t n, ...); + +#ifdef __cpp_lib_base_math +#define __cpp_lib_base_math 1 +#endif + +#endif // NECTAR_LIBCXX_BASE_MATH_H diff --git a/include/LibNectarCore/base_process.h b/include/LibNectarCore/base_process.h new file mode 100644 index 0000000..15d601b --- /dev/null +++ b/include/LibNectarCore/base_process.h @@ -0,0 +1,44 @@ +// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (See accompanying +// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) +// Official repository: https://github.com/nekernel-org/nectar + +#ifndef NECTAR_LIBCXX_BASE_PROCESS_H +#define NECTAR_LIBCXX_BASE_PROCESS_H + +#include + +__init_decl() + +/// @brief CRT exit, with exit code (!!! exits all threads. !!!) +/// @param code the exit code. +/// @return the return > 0 for non successful. +extern int exit_(int code); + +/// @brief CRT signal handler. +/// @param code the signal code. +extern void signal_(int code); + +extern void (*__atexit_cdecl_ptr)(void); +extern void (**__atexit_lst_ptr)(void); +extern size_t __atexit_lst_cnt; + +__fini_decl() + + inline int signal(int code) { + signal_(code); + return -1; +} + +inline int32_t exit(const int32_t& code) { + for (auto idx = 0UL; idx < __atexit_lst_cnt; ++idx) { + __atexit_lst_ptr[idx](); + } + + if (__atexit_cdecl_ptr) __atexit_cdecl_ptr(); + + exit_(code); + return -1; +} + +#endif // NECTAR_LIBCXX_BASE_PROCESS_H diff --git a/include/LibNectarCore/defines.h b/include/LibNectarCore/defines.h new file mode 100644 index 0000000..061769c --- /dev/null +++ b/include/LibNectarCore/defines.h @@ -0,0 +1,80 @@ +// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (See accompanying +// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) +// Official repository: https://github.com/nekernel-org/nectar + +#ifndef __NECTAR_DEFINES_H__ +#define __NECTAR_DEFINES_H__ + +#define __ATTRIBUTE(X) __attribute__((X)) + +typedef __SIZE_TYPE__ size_t; +typedef __INT64_TYPE__ ssize_t; +typedef __INT32_TYPE__ int32_t; + +typedef void* ptr_type; +typedef __SIZE_TYPE__ size_type; + +typedef __INT64_TYPE__ 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 + +#define __alloca(sz) __ck_alloca(sz) + +#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; + +namespace std { +struct placement_t; +struct nothrow_t; +} // namespace std + +#endif /* __NECTAR_DEFINES_H__ */ diff --git a/include/LibNectarCore/filesystem.h b/include/LibNectarCore/filesystem.h new file mode 100644 index 0000000..daac4c1 --- /dev/null +++ b/include/LibNectarCore/filesystem.h @@ -0,0 +1,18 @@ +// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (See accompanying +// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) +// Official repository: https://github.com/nekernel-org/nectar + +#ifndef __NECTAR_FS_H__ +#define __NECTAR_FS_H__ + +class path; +class filesystem_error; +class directory_entry; +class directory_iterator; + +#ifndef __nec_lib_filesystem +#define __nec_lib_filesystem 201703L +#endif + +#endif // __NECTAR_FS_H__ diff --git a/include/LibNectarCore/mkhdrs.sh b/include/LibNectarCore/mkhdrs.sh new file mode 100755 index 0000000..b6b9f75 --- /dev/null +++ b/include/LibNectarCore/mkhdrs.sh @@ -0,0 +1,15 @@ +#! /bin/sh + +outputDir=libcnec/ + +mkdir -p $outputDir + +for f in *.h; do + +#This line splits the file name on the delimiter "." +baseName=`echo $f | cut -d "." -f 1` + +cp $f $outputDir$baseName + +done + diff --git a/include/LibNectarCore/new.h b/include/LibNectarCore/new.h new file mode 100644 index 0000000..58ea424 --- /dev/null +++ b/include/LibNectarCore/new.h @@ -0,0 +1,65 @@ +// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (See accompanying +// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) +// Official repository: https://github.com/nekernel-org/nectar + +#ifndef NECTAR_LIBNECTAR_NEW_H +#define NECTAR_LIBNECTAR_NEW_H + +#include + +struct nothrow_t final { + explicit nothrow_t() = default; + ~nothrow_t() = default; +}; + +struct placement_t final { + explicit placement_t() = default; + ~placement_t() = default; + + void* __base{}; + int32_t __align{}; + size_t __size{}; +}; + +// AMLALE: Define the placement_t feature. +#ifndef __nectar_has_placement +#define __nectar_has_placement 1 +#endif + +// AMLALE: Define nothrow +#ifndef __nectar_has_nothrow +#define __nectar_has_nothrow 1 +#endif + +void* operator new(size_t); +void* operator new[](size_t); + +/// \brief placement_t new and delete operators. Governs how the memory shall be placed. +/// \note This is a feature that shall be used wisely, failure to do so will produce Undefined +/// Behaviors at runtime. +void* operator pnew(struct placement_t*); +void operator pdelete(struct placement_t*, void*); + +/// \brief For all offsets within the base range and alignement 'align' +/// \brief Allocate offsets with respect to the `base` interval, apply alignement of `align` value. +/// Return `offsets` of length n as an aligned value within the domain of `base`. +using placeable_callable_type = void* /*offsets*/ (*) (void* base, size_t n, const int& align); + +/// \note This should NOT fail, failure to meet the conditions will cause the program's state to be +/// aborted. +/// \brief Set the placement policy of future memory allocations. +template +void set_placement_policy(const PlaceableCallable&) noexcept; + +void* operator new(size_t, const nothrow_t&) noexcept; +void* operator new(size_t, void*) noexcept; +void* operator new[](size_t, const nothrow_t&) noexcept; +void* operator new[](size_t, void*) noexcept; + +void operator delete(void*) noexcept; +void operator delete(void*, size_t) noexcept; + +void operator delete[](void*) noexcept; + +#endif // NECTAR_LIBNECTAR_NEW_H diff --git a/include/LibNectarCore/utility.h b/include/LibNectarCore/utility.h new file mode 100644 index 0000000..834b1a6 --- /dev/null +++ b/include/LibNectarCore/utility.h @@ -0,0 +1,27 @@ +// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (See accompanying +// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) +// Official repository: https://github.com/nekernel-org/nectar + +#ifndef LIBNECTAR_UTILITY_H +#define LIBNECTAR_UTILITY_H + +/// @brief Forward object. +/// @tparam Args the object type. +/// @param arg the object. +/// @return object's rvalue +template +inline auto forward(Args& arg) -> Args&& { + return static_cast(arg); +} + +/// @brief Move object. +/// @tparam Args the object type. +/// @param arg the object. +/// @return object's rvalue +template +inline auto move(Args&& arg) -> Args&& { + return static_cast(arg); +} + +#endif // LIBNECTAR_UTILITY_H diff --git a/include/LibNectarStd/iostream.ncpp b/include/LibNectarStd/iostream.ncpp deleted file mode 100644 index ecd1132..0000000 --- a/include/LibNectarStd/iostream.ncpp +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2026, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (See accompanying -// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0) -// Official repository: https://github.com/nekernel-org/nectar - -import printf, scanf; - -struct ostream -{ - let print(let data) - { - return nil; - } - - let scan(let fmt) - { - return nil; - } -}; \ No newline at end of file diff --git a/include/LibNectarStd/iterator.ncpp b/include/LibNectarStd/iterator.ncpp deleted file mode 100644 index fe9015d..0000000 --- a/include/LibNectarStd/iterator.ncpp +++ /dev/null @@ -1,13 +0,0 @@ - -struct iterator -{ - let begin(let collection) - { - return nil; - } - - let end(let collection) - { - return nil; - } -}; \ No newline at end of file diff --git a/test/test_samples/iostream.ncpp b/test/test_samples/iostream.ncpp index de1e6b8..6ba52ac 100644 --- a/test/test_samples/iostream.ncpp +++ b/test/test_samples/iostream.ncpp @@ -1,4 +1,4 @@ -import iostream; +#include "../../include/LibNectar/iostream.nhh" let main() { diff --git a/test/test_samples/return_value.ncpp b/test/test_samples/return_value.ncpp index 897ca15..812743a 100644 --- a/test/test_samples/return_value.ncpp +++ b/test/test_samples/return_value.ncpp @@ -1,3 +1,5 @@ +#include "../../include/LibNectar/iostream.nhh" + let main() { let six_seven = 67; -- cgit v1.2.3