diff options
| author | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-01-04 21:36:54 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-01-04 21:48:09 +0100 |
| commit | 6cda526bd4efcee31b1ea7405dc46d7985ba64e6 (patch) | |
| tree | f7d138fd5652cdc0e7a2c4918b36d754969d6cf5 /CompilerKit/StdKit | |
| parent | 60271b79a91a06772241aed737426f5d097ca533 (diff) | |
masm: fix assembler bug where addr1, 0x0 (add r1, 0x0) doesn't error
out.
cc/ccplus: minor compiler changes, will get to them very soon...
refactor: rename C++Kit to CompilerKit.
Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'CompilerKit/StdKit')
| -rw-r--r-- | CompilerKit/StdKit/AE.hpp | 61 | ||||
| -rw-r--r-- | CompilerKit/StdKit/ErrorID.hpp | 22 | ||||
| -rw-r--r-- | CompilerKit/StdKit/ErrorOr.hpp | 58 | ||||
| -rw-r--r-- | CompilerKit/StdKit/PEF.hpp | 85 | ||||
| -rw-r--r-- | CompilerKit/StdKit/Ref.hpp | 89 | ||||
| -rw-r--r-- | CompilerKit/StdKit/String.cc | 230 | ||||
| -rw-r--r-- | CompilerKit/StdKit/String.hpp | 72 |
7 files changed, 617 insertions, 0 deletions
diff --git a/CompilerKit/StdKit/AE.hpp b/CompilerKit/StdKit/AE.hpp new file mode 100644 index 0000000..3aafc20 --- /dev/null +++ b/CompilerKit/StdKit/AE.hpp @@ -0,0 +1,61 @@ +/* + * ======================================================== + * + * C++Kit + * Copyright Western Company, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <CompilerKit/Defines.hpp> + +#define kAEMag0 'A' +#define kAEMag1 'E' + +#define kAESymbolLen 64 +#define kAEPad 8 +#define kAEMagLen 2 +#define kAEInvalidOpcode 0x00 + +// Advanced Executable File Format for MetroLink. +// Reloctable by offset is the default strategy. +// You can also relocate at runtime but that's up to the operating system loader. + +namespace CompilerKit +{ + // @brief Advanced Executable Header + // One thing to keep in mind. + // This object format, is reloctable. + typedef struct AEHeader final + { + CharType fMagic[kAEMagLen]; + CharType fArch; + SizeType fCount; + CharType fSize; + SizeType fStartCode; + SizeType fCodeSize; + CharType fPad[kAEPad]; + } __attribute__((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]; + } __attribute__((packed)) AERecordHeader, *AERecordHeaderPtr; + + enum + { + kKindRelocationByOffset = 0x23f, + kKindRelocationAtRuntime = 0x34f, + }; +} diff --git a/CompilerKit/StdKit/ErrorID.hpp b/CompilerKit/StdKit/ErrorID.hpp new file mode 100644 index 0000000..b4b204c --- /dev/null +++ b/CompilerKit/StdKit/ErrorID.hpp @@ -0,0 +1,22 @@ +/* + * ======================================================== + * + * CompilerKit + * Copyright Western Company, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <StdKit/ErrorOr.hpp> +#include <Defines.hpp> + +#define CXXKIT_EXEC_ERROR -30 +#define CXXKIT_FILE_NOT_FOUND -31 +#define CXXKIT_DIR_NOT_FOUND -32 +#define CXXKIT_FILE_EXISTS -33 +#define CXXKIT_TOO_LONG -34 +#define CXXKIT_INVALID_DATA -35 +#define CXXKIT_UNIMPLEMENTED -36 +#define CXXKIT_FAT_ERROR -37 diff --git a/CompilerKit/StdKit/ErrorOr.hpp b/CompilerKit/StdKit/ErrorOr.hpp new file mode 100644 index 0000000..c2457df --- /dev/null +++ b/CompilerKit/StdKit/ErrorOr.hpp @@ -0,0 +1,58 @@ +/* + * ======================================================== + * + * CompilerKit + * Copyright Western Company, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <CompilerKit/Defines.hpp> +#include <CompilerKit/StdKit/Ref.hpp> + +namespace CompilerKit +{ +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 CompilerKit diff --git a/CompilerKit/StdKit/PEF.hpp b/CompilerKit/StdKit/PEF.hpp new file mode 100644 index 0000000..5ff77f8 --- /dev/null +++ b/CompilerKit/StdKit/PEF.hpp @@ -0,0 +1,85 @@ +/* + * ======================================================== + * + * C++Kit + * Copyright Western Company, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <CompilerKit/Defines.hpp> + +#define kPefMagic "PEF" +#define kPefMagicFat "FEP" + +#define kPefMagicLen 3 + +#define kPefVersion 1 +#define kPefNameLen 64 + +// Protable Executable Format, a format designed for any computer. + +namespace CompilerKit +{ + enum + { + kPefArchIntel86S = 100, + kPefArchAMD64, + kPefArchRISCV, + kPefArch64000, /* Advanced RISC architecture. */ + kPefArch32000, + kPefArchInvalid = 0xFF, + }; + + enum + { + kPefKindExec = 1, /* .o/.pef/<none> */ + kPefKindSharedObject = 2, /* .lib */ + kPefKindObject = 4, /* .obj */ + kPefKindDwarf = 5, /* .dsym */ + }; + + /* 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; + SizeType HdrSz; /* Size of header */ + SizeType Count; /* container header count */ + } __attribute__((packed)) PEFContainer; + + /* 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 Flags; /* container flags */ + UInt16 Kind; /* container kind */ + UIntPtr Offset; /* file offset */ + SizeType Size; /* file size */ + } __attribute__((packed)) PEFCommandHeader; + + enum + { + kPefCode = 0xC, + kPefData = 0xD, + kPefZero = 0xE, + kPefLinkerID = 0x1, + }; +} + +#define kPefExt ".o" +#define kPefDylibExt ".so" +#define kPefObjectExt ".o" +#define kPefDebugExt ".dbg"
\ No newline at end of file diff --git a/CompilerKit/StdKit/Ref.hpp b/CompilerKit/StdKit/Ref.hpp new file mode 100644 index 0000000..2ca540a --- /dev/null +++ b/CompilerKit/StdKit/Ref.hpp @@ -0,0 +1,89 @@ + +/* + * ======================================================== + * + * CompilerKit + * Copyright Western Company, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <CompilerKit/Defines.hpp> + +namespace CompilerKit +{ + // @author Western Company + // @brief Reference class, refers to a pointer of data in static memory. + template <typename T> + class Ref final + { + public: + Ref() = default; + ~Ref() = default; + + public: + 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; + bool m_Strong{ false }; + + }; + + template <typename T> + class NonNullRef final + { + public: + NonNullRef() = delete; + NonNullRef(nullPtr) = delete; + + 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 CompilerKit diff --git a/CompilerKit/StdKit/String.cc b/CompilerKit/StdKit/String.cc new file mode 100644 index 0000000..6f11404 --- /dev/null +++ b/CompilerKit/StdKit/String.cc @@ -0,0 +1,230 @@ +/* + * ======================================================== + * + * CompilerKit + * Copyright Western Company, all rights reserved. + * + * ======================================================== + */ + +#include "String.hpp" +#include <utility> + +namespace CompilerKit +{ + CharType* StringView::Data() + { + return m_Data.data(); + } + + const CharType* StringView::CData() const + { + return m_Data.c_str(); + } + + SizeType StringView::Length() const + { + return m_Data.size(); + } + + bool StringView::operator==(const StringView &rhs) const + { + if (rhs.Length() != Length()) + return false; + + for (SizeType index = 0; index < Length(); ++index) + { + if (rhs.m_Data[index] != m_Data[index]) + return false; + } + + return true; + } + + bool StringView::operator==(const CharType *rhs) const + { + if (string_length(rhs) != Length()) + return false; + + for (SizeType index = 0; index < string_length(rhs); ++index) + { + if (rhs[index] != m_Data[index]) + return false; + } + + return true; + } + + bool StringView::operator!=(const StringView &rhs) const + { + if (rhs.Length() != Length()) + return false; + + for (SizeType index = 0; index < rhs.Length(); ++index) + { + if (rhs.m_Data[index] == m_Data[index]) + return false; + } + + return true; + } + + bool StringView::operator!=(const CharType *rhs) const + { + if (string_length(rhs) != Length()) + return false; + + for (SizeType index = 0; index < string_length(rhs); ++index) + { + if (rhs[index] == m_Data[index]) + return false; + } + + return true; + } + + StringView StringBuilder::Construct(const CharType *data) + { + if (!data || + *data == 0) + return StringView(0); + + StringView view(strlen(data)); + view += data; + + return view; + } + + const char* StringBuilder::FromInt(const char *fmt, int i) + { + if (!fmt) + return ("-1"); + + char *ret = new char[8 + string_length(fmt)]; + + if (!ret) + return ("-1"); + + CharType result[8]; + if (!to_str(result, sizeof(int), i)) + { + delete[] ret; + return ("-1"); + } + + const auto fmt_len = string_length(fmt); + const auto res_len = string_length(result); + + for (SizeType idx = 0; idx < fmt_len; ++idx) + { + if (fmt[idx] == '%') { + SizeType result_cnt = idx; + + for (auto y_idx = idx; y_idx < res_len; ++y_idx) { + ret[result_cnt] = result[y_idx]; + ++result_cnt; + } + + break; + } + + ret[idx] = fmt[idx]; + } + + return ret; /* Copy that ret into a buffer, Alloca allocates to the stack */ + } + + const char* StringBuilder::FromBool(const char *fmt, bool i) + { + if (!fmt) + return ("?"); + + const char *boolean_expr = i ? "true" : "false"; + char *ret = new char[i ? 4 : 5 + string_length(fmt)]; + + if (!ret) + return ("?"); + + const auto fmt_len = string_length(fmt); + const auto res_len = string_length(boolean_expr); + + for (SizeType idx = 0; idx < fmt_len; ++idx) + { + if (fmt[idx] == '%') { + SizeType result_cnt = idx; + + for (auto y_idx = idx; y_idx < res_len; ++y_idx) + { + ret[result_cnt] = boolean_expr[y_idx]; + ++result_cnt; + } + + break; + } + + ret[idx] = fmt[idx]; + } + + return ret; + } + + bool StringBuilder::Equals(const char *lhs, const char *rhs) + { + if (string_length(rhs) != string_length(lhs)) + return false; + + for (SizeType index = 0; index < string_length(rhs); ++index) + { + if (rhs[index] != lhs[index]) + return false; + } + + return true; + } + + const char *StringBuilder::Format(const char *fmt, const char *fmt2) + { + if (!fmt || !fmt2) + return ("?"); + + char *ret = new char[string_length(fmt2) + string_length(fmt2)]; + if (!ret) + return ("?"); + + for (SizeType idx = 0; idx < string_length(fmt); ++idx) + { + if (fmt[idx] == '%') { + SizeType result_cnt = idx; + for (SizeType y_idx = 0; y_idx < string_length(fmt2); ++y_idx) + { + ret[result_cnt] = fmt2[y_idx]; + ++result_cnt; + } + + break; + } + + ret[idx] = fmt[idx]; + } + + return ret; + } + + StringView &StringView::operator+=(const CharType *rhs) + { + this->m_Data += rhs; + this->m_Cur = this->m_Data.size(); + this->m_Sz = this->m_Data.size(); + + return *this; + } + + StringView &StringView::operator+=(const StringView &rhs) + { + this->m_Data += rhs.CData(); + this->m_Cur = this->m_Data.size(); + this->m_Sz = this->m_Data.size(); + + return *this; + } +} // namespace CompilerKit diff --git a/CompilerKit/StdKit/String.hpp b/CompilerKit/StdKit/String.hpp new file mode 100644 index 0000000..592c244 --- /dev/null +++ b/CompilerKit/StdKit/String.hpp @@ -0,0 +1,72 @@ +/* + * ======================================================== + * + * CompilerKit + * Copyright Western Company, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <CompilerKit/Defines.hpp> +#include <CompilerKit/StdKit/ErrorOr.hpp> + +namespace CompilerKit +{ + class StringView final + { + public: + StringView() = delete; + + explicit StringView(SizeType Sz) : m_Sz(Sz) + { + + } + + ~StringView() = default; + + CXXKIT_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.empty() == false; + } + + bool operator!() + { + return m_Data.empty() == true; + } + + private: + std::basic_string<char> m_Data{""}; + SizeType m_Sz{0}; + SizeType m_Cur{0}; + + friend class StringBuilder; + + }; + + 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 CompilerKit |
