summaryrefslogtreecommitdiffhomepage
path: root/dev/LibCompiler/NFC
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-04-19 17:33:26 +0200
committerAmlal El Mahrouss <amlal@nekernel.org>2025-04-19 17:33:26 +0200
commitbefde76cfa46c766e81f74eb5ac65d3dae2dde87 (patch)
tree45b2f9fd6b3f9605c2747485bd24483192f99e73 /dev/LibCompiler/NFC
parent3afc481dc64a07fe7fcaff9ce7a12a492c3ec8e7 (diff)
dev, LibCompiler, tooling: refactor and separate components into modules
(cppdrv, cxxdrv) Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'dev/LibCompiler/NFC')
-rw-r--r--dev/LibCompiler/NFC/AE.h142
-rw-r--r--dev/LibCompiler/NFC/ErrorID.h23
-rw-r--r--dev/LibCompiler/NFC/ErrorOr.h61
-rw-r--r--dev/LibCompiler/NFC/PEF.h144
-rw-r--r--dev/LibCompiler/NFC/Ref.h103
-rw-r--r--dev/LibCompiler/NFC/StringView.h90
-rw-r--r--dev/LibCompiler/NFC/XCOFF.h41
7 files changed, 0 insertions, 604 deletions
diff --git a/dev/LibCompiler/NFC/AE.h b/dev/LibCompiler/NFC/AE.h
deleted file mode 100644
index fdf42a5..0000000
--- a/dev/LibCompiler/NFC/AE.h
+++ /dev/null
@@ -1,142 +0,0 @@
-/*
- * ========================================================
- *
- * LibCompiler
- * Copyright (C) 2024-2025 Amlal El Mahrouss, all rights reserved.
- *
- * ========================================================
- */
-
-#pragma once
-
-#include <LibCompiler/Defines.h>
-
-#define kAEMag0 'A'
-#define kAEMag1 'E'
-
-#define kAESymbolLen (255)
-#define kAEPad (8)
-#define kAEMagLen (2)
-#define kAENullType (0x00)
-
-// Advanced Executable File Format for ld64.
-// Reloctable by offset is the default strategy.
-// You can also relocate at runtime but that's up to the operating system loader.
-
-namespace LibCompiler
-{
- // @brief Advanced Executable Header
- // One thing to keep in mind.
- // This object format, is reloctable.
- typedef struct AEHeader final
- {
- CharType fMagic[kAEMagLen];
- CharType fArch;
- CharType fSubArch;
- SizeType fCount;
- CharType fSize;
- SizeType fStartCode;
- SizeType fCodeSize;
- CharType fPad[kAEPad];
- } PACKED AEHeader, *AEHeaderPtr;
-
- // @brief Advanced Executable Record.
- // Could be data, code or bss.
- // fKind must be filled with PEF fields.
-
- typedef struct AERecordHeader final
- {
- CharType fName[kAESymbolLen];
- SizeType fKind;
- SizeType fSize;
- SizeType fFlags;
- UIntPtr fOffset;
- CharType fPad[kAEPad];
- } PACKED AERecordHeader, *AERecordHeaderPtr;
-
- enum
- {
- kKindRelocationByOffset = 0x23f,
- kKindRelocationAtRuntime = 0x34f,
- };
-} // namespace LibCompiler
-
-// provide operator<< for AE
-
-inline std::ofstream& operator<<(std::ofstream& fp, LibCompiler::AEHeader& container)
-{
- fp.write((char*)&container, sizeof(LibCompiler::AEHeader));
-
- return fp;
-}
-
-inline std::ofstream& operator<<(std::ofstream& fp,
- LibCompiler::AERecordHeader& container)
-{
- fp.write((char*)&container, sizeof(LibCompiler::AERecordHeader));
-
- return fp;
-}
-
-inline std::ifstream& operator>>(std::ifstream& fp, LibCompiler::AEHeader& container)
-{
- fp.read((char*)&container, sizeof(LibCompiler::AEHeader));
- return fp;
-}
-
-inline std::ifstream& operator>>(std::ifstream& fp,
- LibCompiler::AERecordHeader& container)
-{
- fp.read((char*)&container, sizeof(LibCompiler::AERecordHeader));
- return fp;
-}
-
-namespace LibCompiler::Utils
-{
- /**
- * @brief AE Reader protocol
- *
- */
- class AEReadableProtocol final
- {
- public:
- std::ifstream FP;
-
- public:
- explicit AEReadableProtocol() = default;
- ~AEReadableProtocol() = default;
-
- LIBCOMPILER_COPY_DELETE(AEReadableProtocol);
-
- /**
- * @brief Read AE Record headers.
- *
- * @param raw the containing buffer
- * @param sz it's size (1 = one AERecordHeader, 2 two AERecordHeader(s))
- * @return AERecordHeaderPtr
- */
- AERecordHeaderPtr Read(char* raw, std::size_t sz)
- {
- if (!raw)
- return nullptr;
-
- return this->_Read<AERecordHeader>(raw, sz * sizeof(AERecordHeader));
- }
-
- private:
- /**
- * @brief Implementation of Read for raw classes.
- *
- * @tparam TypeClass The class to read.
- * @param raw the buffer
- * @param sz the size
- * @return TypeClass* the returning class.
- */
- template <typename TypeClass>
- TypeClass* _Read(char* raw, std::size_t sz)
- {
- FP.read(raw, std::streamsize(sz));
- return reinterpret_cast<TypeClass*>(raw);
- }
- };
-} // namespace LibCompiler::Utils
diff --git a/dev/LibCompiler/NFC/ErrorID.h b/dev/LibCompiler/NFC/ErrorID.h
deleted file mode 100644
index e7f06e1..0000000
--- a/dev/LibCompiler/NFC/ErrorID.h
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- * ========================================================
- *
- * LibCompiler
- * Copyright (C) 2024-2025 Amlal El Mahrouss, all rights reserved.
- *
- * ========================================================
- */
-
-#pragma once
-
-#include <LibCompiler/Defines.h>
-#include <LibCompiler/NFC/ErrorOr.h>
-
-#define LIBCOMPILER_SUCCESSS 0
-#define LIBCOMPILER_EXEC_ERROR -30
-#define LIBCOMPILER_FILE_NOT_FOUND -31
-#define LIBCOMPILER_DIR_NOT_FOUND -32
-#define LIBCOMPILER_FILE_EXISTS -33
-#define LIBCOMPILER_TOO_LONG -34
-#define LIBCOMPILER_INVALID_DATA -35
-#define LIBCOMPILER_UNIMPLEMENTED -36
-#define LIBCOMPILER_FAT_ERROR -37
diff --git a/dev/LibCompiler/NFC/ErrorOr.h b/dev/LibCompiler/NFC/ErrorOr.h
deleted file mode 100644
index 9dc607e..0000000
--- a/dev/LibCompiler/NFC/ErrorOr.h
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * ========================================================
- *
- * LibCompiler
- * Copyright (C) 2024-2025 Amlal El Mahrouss, all rights reserved.
- *
- * ========================================================
- */
-
-#pragma once
-
-#include <LibCompiler/Defines.h>
-#include <LibCompiler/NFC/Ref.h>
-
-namespace LibCompiler
-{
- using ErrorT = UInt32;
-
- template <typename T>
- class ErrorOr final
- {
- public:
- ErrorOr() = default;
- ~ErrorOr() = default;
-
- public:
- explicit ErrorOr(Int32 err)
- : mId(err)
- {
- }
-
- explicit ErrorOr(nullPtr Null)
- {
- }
-
- explicit ErrorOr(T Class)
- : mRef(Class)
- {
- }
-
- ErrorOr& operator=(const ErrorOr&) = default;
- ErrorOr(const ErrorOr&) = default;
-
- Ref<T> Leak()
- {
- return mRef;
- }
-
- operator bool()
- {
- return mRef;
- }
-
- private:
- Ref<T> mRef;
- Int32 mId{0};
- };
-
- using ErrorOrAny = ErrorOr<voidPtr>;
-
-} // namespace LibCompiler
diff --git a/dev/LibCompiler/NFC/PEF.h b/dev/LibCompiler/NFC/PEF.h
deleted file mode 100644
index 1148dea..0000000
--- a/dev/LibCompiler/NFC/PEF.h
+++ /dev/null
@@ -1,144 +0,0 @@
-/* -------------------------------------------
-
- Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved
-
-------------------------------------------- */
-
-#pragma once
-
-#include <LibCompiler/Defines.h>
-
-// @file PEF.hpp
-// @brief Preferred Executable Format
-
-#define kPefMagic "Joy!"
-#define kPefMagicFat "yoJ!"
-
-#define kPefExt ".exec"
-#define kPefDylibExt ".dylib"
-#define kPefLibExt ".lib"
-#define kPefObjectExt ".obj"
-#define kPefDebugExt ".dbg"
-#define kPefDriverExt ".sys"
-
-#define kPefZero128 ".zero128"
-#define kPefCode128 ".code128"
-#define kPefData128 ".data128"
-
-#define kPefZero64 ".zero64"
-#define kPefCode64 ".code64"
-#define kPefData64 ".data64"
-
-#define kPefMagicLen (5)
-
-#define kPefVersion (3)
-#define kPefNameLen (255)
-
-#define kPefBaseOrigin (0x40000000)
-
-#define kPefStart "__ImageStart"
-
-namespace LibCompiler
-{
- enum
- {
- kPefArchStart = 99,
- kPefArchIntel86S = 100,
- kPefArchAMD64,
- kPefArchRISCV,
- kPefArch64000, /* 64x0 RISC architecture. */
- kPefArch32000,
- kPefArchPowerPC, /* 64-bit POWER architecture. */
- kPefArchARM64,
- kPefArchCount = (kPefArchPowerPC - kPefArchIntel86S) + 1,
- kPefArchInvalid = 0xFF,
- };
-
- enum
- {
- kPefSubArchAMD,
- kPefSubArchIntel,
- kPefSubArchARM,
- kPefSubArchGeneric,
- kPefSubArchIBM,
- };
-
- enum
- {
- kPefKindExec = 1, /* .o */
- kPefKindDylib = 2, /* .dylib */
- kPefKindObject = 4, /* .obj */
- kPefKindDebug = 5, /* .dbg */
- kPefKindDriver = 6,
- kPefKindCount,
- };
-
- /* PEF container */
- typedef struct PEFContainer final
- {
- CharType Magic[kPefMagicLen];
- UInt32 Linker;
- UInt32 Version;
- UInt32 Kind;
- UInt32 Abi;
- UInt32 Cpu;
- UInt32 SubCpu; /* Cpu specific information */
- UIntPtr Start; /* Origin of code */
- SizeType HdrSz; /* Size of header */
- SizeType Count; /* container header count */
- } PACKED PEFContainer, *PEFContainerPtr;
-
- /* First PEFCommandHeader starts after PEFContainer */
- /* Last container is __exec_end */
-
- /* PEF executable section and commands. */
-
- typedef struct PEFCommandHeader final
- {
- CharType Name[kPefNameLen]; /* container name */
- UInt32 Cpu; /* container cpu */
- UInt32 SubCpu; /* container sub-cpu */
- UInt32 Flags; /* container flags */
- UInt16 Kind; /* container kind */
- UIntPtr Offset; /* file offset */
- SizeType Size; /* file size */
- } PACKED PEFCommandHeader, *PEFCommandHeaderPtr;
-
- enum
- {
- kPefCode = 0xC,
- kPefData = 0xD,
- kPefZero = 0xE,
- kPefLinkerID = 0x1,
- kPefCount = 4,
- kPefInvalid = 0xFF,
- };
-} // namespace LibCompiler
-
-inline std::ofstream& operator<<(std::ofstream& fp,
- LibCompiler::PEFContainer& container)
-{
- fp.write((char*)&container, sizeof(LibCompiler::PEFContainer));
- return fp;
-}
-
-inline std::ofstream& operator<<(std::ofstream& fp,
- LibCompiler::PEFCommandHeader& container)
-{
- fp.write((char*)&container, sizeof(LibCompiler::PEFCommandHeader));
- return fp;
-}
-
-inline std::ifstream& operator>>(std::ifstream& fp,
- LibCompiler::PEFContainer& container)
-{
- fp.read((char*)&container, sizeof(LibCompiler::PEFContainer));
- return fp;
-}
-
-inline std::ifstream& operator>>(std::ifstream& fp,
- LibCompiler::PEFCommandHeader& container)
-{
- fp.read((char*)&container, sizeof(LibCompiler::PEFCommandHeader));
- return fp;
-}
diff --git a/dev/LibCompiler/NFC/Ref.h b/dev/LibCompiler/NFC/Ref.h
deleted file mode 100644
index 117083c..0000000
--- a/dev/LibCompiler/NFC/Ref.h
+++ /dev/null
@@ -1,103 +0,0 @@
-
-/*
- * ========================================================
- *
- * LibCompiler
- * Copyright (C) 2024-2025 Amlal El Mahrouss, all rights reserved.
- *
- * ========================================================
- */
-
-#pragma once
-
-#include <LibCompiler/Defines.h>
-
-namespace LibCompiler
-{
- // @author EL Mahrouss Amlal
- // @brief Reference holder class, refers to a pointer of data in static memory.
- template <typename T>
- class Ref final
- {
- public:
- explicit Ref() = default;
-
- ~Ref()
- {
- if (m_Strong)
- {
- (*m_Class).~T();
- }
- }
-
- LIBCOMPILER_COPY_DEFAULT(Ref);
-
- public:
- explicit Ref(T cls, const Bool& strong = false)
- : m_Class(&cls), m_Strong(strong)
- {
- }
-
- Ref& operator=(T ref)
- {
- *m_Class = ref;
- return *this;
- }
-
- public:
- T* operator->() const
- {
- return m_Class;
- }
-
- T& Leak()
- {
- return *m_Class;
- }
-
- T operator*()
- {
- return *m_Class;
- }
-
- Bool IsStrong() const
- {
- return m_Strong;
- }
-
- operator bool()
- {
- return *m_Class;
- }
-
- private:
- T* m_Class{nullptr};
- Bool m_Strong{false};
- };
-
- // @author EL Mahrouss Amlal
- // @brief Non null Reference holder class, refers to a pointer of data in static memory.
- template <typename T>
- class NonNullRef final
- {
- public:
- explicit NonNullRef() = delete;
-
- explicit NonNullRef(T* ref)
- : m_Ref(ref, true)
- {
- }
-
- Ref<T>& operator->()
- {
- MUST_PASS(m_Ref);
- return m_Ref;
- }
-
- NonNullRef& operator=(const NonNullRef<T>& ref) = delete;
- NonNullRef(const NonNullRef<T>& ref) = default;
-
- private:
- Ref<T> m_Ref{nullptr};
- };
-} // namespace LibCompiler
diff --git a/dev/LibCompiler/NFC/StringView.h b/dev/LibCompiler/NFC/StringView.h
deleted file mode 100644
index 16578e0..0000000
--- a/dev/LibCompiler/NFC/StringView.h
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * ========================================================
- *
- * LibCompiler
- * Copyright (C) 2024-2025 Amlal El Mahrouss, all rights reserved.
- *
- * ========================================================
- */
-
-#pragma once
-
-#include <LibCompiler/Defines.h>
-#include <LibCompiler/NFC/ErrorOr.h>
-
-namespace LibCompiler
-{
- /**
- * @brief StringView class, contains a C string and manages it.
- * @note No need to manage it it's getting deleted by default.
- */
-
- class StringView final
- {
- public:
- explicit StringView() = delete;
-
- explicit StringView(SizeType Sz) noexcept
- : m_Sz(Sz)
- {
- m_Data = new CharType[Sz];
- assert(m_Data);
- }
-
- ~StringView() noexcept
- {
- if (m_Data)
- {
- memset(m_Data, 0, m_Sz);
- delete[] m_Data;
-
- m_Data = nullptr;
- }
- }
-
- LIBCOMPILER_COPY_DEFAULT(StringView);
-
- CharType* Data();
- const CharType* CData() const;
- SizeType Length() const;
-
- bool operator==(const CharType* rhs) const;
- bool operator!=(const CharType* rhs) const;
-
- bool operator==(const StringView& rhs) const;
- bool operator!=(const StringView& rhs) const;
-
- StringView& operator+=(const CharType* rhs);
- StringView& operator+=(const StringView& rhs);
-
- operator bool()
- {
- return m_Data && m_Data[0] != 0;
- }
-
- bool operator!()
- {
- return !m_Data || m_Data[0] == 0;
- }
-
- private:
- CharType* m_Data{nullptr};
- SizeType m_Sz{0};
- SizeType m_Cur{0};
-
- friend class StringBuilder;
- };
-
- /**
- * @brief StringBuilder class
- * @note These results shall call delete[] after they're used.
- */
- struct StringBuilder final
- {
- static StringView Construct(const CharType* data);
- static const char* FromInt(const char* fmt, int n);
- static const char* FromBool(const char* fmt, bool n);
- static const char* Format(const char* fmt, const char* from);
- static bool Equals(const char* lhs, const char* rhs);
- };
-} // namespace LibCompiler
diff --git a/dev/LibCompiler/NFC/XCOFF.h b/dev/LibCompiler/NFC/XCOFF.h
deleted file mode 100644
index 862c363..0000000
--- a/dev/LibCompiler/NFC/XCOFF.h
+++ /dev/null
@@ -1,41 +0,0 @@
-/* -------------------------------------------
-
- Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved
-
- File: XCOFF.hpp
- Purpose: XCOFF for NeKernel.
-
- Revision History:
-
- 04/07/24: Added file (amlel)
-
-------------------------------------------- */
-
-#ifndef __XCOFF__
-#define __XCOFF__
-
-#include <LibCompiler/Defines.h>
-
-#define kXCOFF64Magic 0x01F7
-
-#define kXCOFFRelFlg 0x0001
-#define kXCOFFExecutable 0x0002
-#define kXCOFFLnno 0x0004
-#define kXCOFFLSyms 0x0008
-
-namespace LibCompiler
-{
- /// @brief XCoff identification header.
- typedef struct XCoffFileHeader
- {
- UInt16 fMagic;
- UInt16 fTarget;
- UInt16 fNumSecs;
- UInt32 fTimeDat;
- UIntPtr fSymPtr;
- UInt32 fNumSyms;
- UInt16 fOptHdr; // ?: Number of bytes in optional header
- } XCoffFileHeader;
-} // namespace LibCompiler
-
-#endif // ifndef __XCOFF__