From b03f3d83efcbc012c4153da14eaf158bb50031d2 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Wed, 3 Jan 2024 23:40:16 +0100 Subject: tools: incremental changes, support for a C compiler will soon be here. alongside the 32x0. Signed-off-by: Amlal El Mahrouss --- C++Kit/AsmKit/Arch/32k.hpp | 4 +- C++Kit/AsmKit/Arch/64k.hpp | 4 +- C++Kit/AsmKit/AsmKit.cc | 47 +++++++++ C++Kit/AsmKit/AsmKit.cpp | 47 --------- C++Kit/AsmKit/AsmKit.hpp | 8 +- C++Kit/StdKit/ErrorID.hpp | 2 +- C++Kit/StdKit/ErrorOr.hpp | 4 +- C++Kit/StdKit/Ref.hpp | 2 +- C++Kit/StdKit/String.cc | 230 +++++++++++++++++++++++++++++++++++++++++++++ C++Kit/StdKit/String.cpp | 230 --------------------------------------------- C++Kit/StdKit/String.hpp | 4 +- 11 files changed, 291 insertions(+), 291 deletions(-) create mode 100644 C++Kit/AsmKit/AsmKit.cc delete mode 100644 C++Kit/AsmKit/AsmKit.cpp create mode 100644 C++Kit/StdKit/String.cc delete mode 100644 C++Kit/StdKit/String.cpp (limited to 'C++Kit') diff --git a/C++Kit/AsmKit/Arch/32k.hpp b/C++Kit/AsmKit/Arch/32k.hpp index cb948b5..2e73efa 100644 --- a/C++Kit/AsmKit/Arch/32k.hpp +++ b/C++Kit/AsmKit/Arch/32k.hpp @@ -28,7 +28,7 @@ #define kAsmHWord 1 #define kAsmWord 2 -struct NCOpcode +struct CpuCode32x0 { const char fName[16]; char fOpcode; @@ -41,7 +41,7 @@ struct NCOpcode #define kAsmHWordStr ".h" #define kAsmByteStr ".b" -inline std::vector kOpcodesStd = { +inline std::vector kOpcodes32x0 = { kAsmOpcodeDecl("nop", 0b0100011, 0b0000000, kAsmImmediate) // nothing to do. kAsmOpcodeDecl("jmp", 0b1110011, 0b0000011, kAsmJump) // jump to branch kAsmOpcodeDecl("move", 0b0100011, 0b101, kAsmImmediate) diff --git a/C++Kit/AsmKit/Arch/64k.hpp b/C++Kit/AsmKit/Arch/64k.hpp index c4ce648..00ab973 100644 --- a/C++Kit/AsmKit/Arch/64k.hpp +++ b/C++Kit/AsmKit/Arch/64k.hpp @@ -25,7 +25,7 @@ #define kAsmSyscall 0x02 #define kAsmJump 0x03 -struct NCOpcode +struct CpuCode64x0 { const char fName[16]; char fOpcode; @@ -33,7 +33,7 @@ struct NCOpcode char fFunct7; }; -inline std::vector kOpcodesStd = { +inline std::vector kOpcodes64x0 = { kAsmOpcodeDecl("np", 0b0100011, 0b0000000, kAsmImmediate) // mv r0, r0 kAsmOpcodeDecl("jb", 0b1110011, 0b0000011, kAsmJump) // jump to branch kAsmOpcodeDecl("jlr", 0b1110011, 0b0000111, kAsmJump) // jump and link return register diff --git a/C++Kit/AsmKit/AsmKit.cc b/C++Kit/AsmKit/AsmKit.cc new file mode 100644 index 0000000..d44874d --- /dev/null +++ b/C++Kit/AsmKit/AsmKit.cc @@ -0,0 +1,47 @@ +/* + * ======================================================== + * + * C++Kit + * Copyright Western Company, all rights reserved. + * + * ======================================================== + */ + +#include +#include + +#include + +//! @file AsmKit.cpp +//! @brief AssemblyKit + +namespace CxxKit +{ + //! @brief Compile for specific format (ELF, PEF, ZBIN) + Int32 AssemblyFactory::Compile(StringView& sourceFile, + const Int32& arch) noexcept + { + if (sourceFile.Length() < 1 || + !fMounted) + return CXXKIT_UNIMPLEMENTED; + + return fMounted->CompileToFormat(sourceFile, arch); + } + + //! @brief mount assembly backend. + void AssemblyFactory::Mount(AssemblyMountpoint* mountPtr) noexcept + { + if (mountPtr) + fMounted = mountPtr; + } + + AssemblyMountpoint* AssemblyFactory::Unmount() noexcept + { + auto mount_prev = fMounted; + + if (mount_prev) + fMounted = nullptr; + + return mount_prev; + } +} diff --git a/C++Kit/AsmKit/AsmKit.cpp b/C++Kit/AsmKit/AsmKit.cpp deleted file mode 100644 index d44874d..0000000 --- a/C++Kit/AsmKit/AsmKit.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/* - * ======================================================== - * - * C++Kit - * Copyright Western Company, all rights reserved. - * - * ======================================================== - */ - -#include -#include - -#include - -//! @file AsmKit.cpp -//! @brief AssemblyKit - -namespace CxxKit -{ - //! @brief Compile for specific format (ELF, PEF, ZBIN) - Int32 AssemblyFactory::Compile(StringView& sourceFile, - const Int32& arch) noexcept - { - if (sourceFile.Length() < 1 || - !fMounted) - return CXXKIT_UNIMPLEMENTED; - - return fMounted->CompileToFormat(sourceFile, arch); - } - - //! @brief mount assembly backend. - void AssemblyFactory::Mount(AssemblyMountpoint* mountPtr) noexcept - { - if (mountPtr) - fMounted = mountPtr; - } - - AssemblyMountpoint* AssemblyFactory::Unmount() noexcept - { - auto mount_prev = fMounted; - - if (mount_prev) - fMounted = nullptr; - - return mount_prev; - } -} diff --git a/C++Kit/AsmKit/AsmKit.hpp b/C++Kit/AsmKit/AsmKit.hpp index 8674922..68ea4c0 100644 --- a/C++Kit/AsmKit/AsmKit.hpp +++ b/C++Kit/AsmKit/AsmKit.hpp @@ -27,11 +27,12 @@ namespace CxxKit CXXKIT_COPY_DEFAULT(AssemblyMountpoint); //@ brief compile to object file. - // Example C++ -> Assembly -> AE object. + // Example C++ -> MASM -> AE object. virtual Int32 CompileToFormat(StringView& src, Int32 arch) = 0; }; + /// @brief Simple assembly factory class AssemblyFactory final { public: @@ -44,9 +45,8 @@ namespace CxxKit enum { kArchAMD64, - kArchARM64, - kArchPowerPC, - kArchARC, + kArch32x0, + kArch64x0, kArchRISCV, kArchUnknown, }; diff --git a/C++Kit/StdKit/ErrorID.hpp b/C++Kit/StdKit/ErrorID.hpp index 4935a8e..ddbf83b 100644 --- a/C++Kit/StdKit/ErrorID.hpp +++ b/C++Kit/StdKit/ErrorID.hpp @@ -1,7 +1,7 @@ /* * ======================================================== * - * NewOS + * CxxKit * Copyright Western Company, all rights reserved. * * ======================================================== diff --git a/C++Kit/StdKit/ErrorOr.hpp b/C++Kit/StdKit/ErrorOr.hpp index 05e74e4..4b5d1d2 100644 --- a/C++Kit/StdKit/ErrorOr.hpp +++ b/C++Kit/StdKit/ErrorOr.hpp @@ -1,7 +1,7 @@ /* * ======================================================== * - * NewOS + * CxxKit * Copyright Western Company, all rights reserved. * * ======================================================== @@ -55,4 +55,4 @@ class ErrorOr final using ErrorOrAny = ErrorOr; -} // namespace NewOS +} // namespace CxxKit diff --git a/C++Kit/StdKit/Ref.hpp b/C++Kit/StdKit/Ref.hpp index 2a32a60..4f4e822 100644 --- a/C++Kit/StdKit/Ref.hpp +++ b/C++Kit/StdKit/Ref.hpp @@ -2,7 +2,7 @@ /* * ======================================================== * - * NewOS + * CxxKit * Copyright Western Company, all rights reserved. * * ======================================================== diff --git a/C++Kit/StdKit/String.cc b/C++Kit/StdKit/String.cc new file mode 100644 index 0000000..be07c26 --- /dev/null +++ b/C++Kit/StdKit/String.cc @@ -0,0 +1,230 @@ +/* + * ======================================================== + * + * CxxKit + * Copyright Western Company, all rights reserved. + * + * ======================================================== + */ + +#include "String.hpp" +#include + +namespace CxxKit +{ + 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 CxxKit diff --git a/C++Kit/StdKit/String.cpp b/C++Kit/StdKit/String.cpp deleted file mode 100644 index b4199a8..0000000 --- a/C++Kit/StdKit/String.cpp +++ /dev/null @@ -1,230 +0,0 @@ -/* - * ======================================================== - * - * hCore - * Copyright Western Company, all rights reserved. - * - * ======================================================== - */ - -#include "String.hpp" -#include - -namespace CxxKit -{ - 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 hCore diff --git a/C++Kit/StdKit/String.hpp b/C++Kit/StdKit/String.hpp index a3b1f5f..4addea8 100644 --- a/C++Kit/StdKit/String.hpp +++ b/C++Kit/StdKit/String.hpp @@ -1,7 +1,7 @@ /* * ======================================================== * - * NewOS + * CxxKit * Copyright Western Company, all rights reserved. * * ======================================================== @@ -69,4 +69,4 @@ namespace CxxKit static bool Equals(const char *lhs, const char *rhs); }; -} // namespace NewOS +} // namespace CxxKit -- cgit v1.2.3