From 2eed4954c762bb8050e40798c3d9f1d3998324d1 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Fri, 23 May 2025 03:48:06 +0200 Subject: feat!(LibCompiler): Codebase and diagram has been improved. Signed-off-by: Amlal El Mahrouss --- dev/LibCompiler/Version.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'dev/LibCompiler/Version.h') diff --git a/dev/LibCompiler/Version.h b/dev/LibCompiler/Version.h index 601bb28..b94be42 100644 --- a/dev/LibCompiler/Version.h +++ b/dev/LibCompiler/Version.h @@ -1,12 +1,12 @@ /* ------------------------------------------- - Copyright (C) 2024-2025 Amlal EL Mahrous, all rights reserved + Copyright (C) 2024-2025 Amlal EL Mahrouss, all rights reserved ------------------------------------------- */ #pragma once -#define kDistVersion "v0.0.1" +#define kDistVersion "v0.0.1-libcompiler" #define kDistVersionBCD 0x0001 #define ToString(X) Stringify(X) -- cgit v1.2.3 From d12204e7302b8f3bd521c782094338733abfded1 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Tue, 27 May 2025 13:22:49 +0200 Subject: feat!: better crash reporting. Signed-off-by: Amlal El Mahrouss --- dev/LibCompiler/Frontend.h | 2 +- dev/LibCompiler/Util/LCClUtils.h | 48 ++++++++++++++++++++++-- dev/LibCompiler/Version.h | 4 +- dev/LibCompiler/src/Asm/Assembler64x0.cc | 2 +- dev/LibCompiler/src/Asm/AssemblerAMD64.cc | 2 +- dev/LibCompiler/src/Asm/AssemblerARM64.cc | 2 +- dev/LibCompiler/src/Asm/AssemblerPowerPC.cc | 2 +- dev/LibCompiler/src/Cl/CCompiler64x0.cc | 2 +- dev/LibCompiler/src/Cl/CCompilerARM64.cc | 2 +- dev/LibCompiler/src/Cl/CCompilerPower64.cc | 2 +- dev/LibCompiler/src/Cl/CPlusPlusCompilerAMD64.cc | 26 ++++--------- dev/LibCompiler/src/Linker/DynamicLinkerPEF.cc | 2 +- tools/cxxdrv.cc | 34 +++++++++++++++-- tools/cxxdrv.json | 2 - 14 files changed, 93 insertions(+), 39 deletions(-) (limited to 'dev/LibCompiler/Version.h') diff --git a/dev/LibCompiler/Frontend.h b/dev/LibCompiler/Frontend.h index 9f6fe71..5f054ac 100644 --- a/dev/LibCompiler/Frontend.h +++ b/dev/LibCompiler/Frontend.h @@ -81,7 +81,7 @@ enum KeywordKind { /// \brief Compiler keyword information struct. struct CompilerKeyword { - std::string keyword_name{""}; + STLString keyword_name{""}; KeywordKind keyword_kind{kKeywordKindInvalid}; }; diff --git a/dev/LibCompiler/Util/LCClUtils.h b/dev/LibCompiler/Util/LCClUtils.h index c88339d..8e7c4d7 100644 --- a/dev/LibCompiler/Util/LCClUtils.h +++ b/dev/LibCompiler/Util/LCClUtils.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #define kZero64Section ".zero64" @@ -51,18 +52,59 @@ inline void print_warning(std::string reason, std::string file) noexcept { /// @internal /// @brief Handler for SIGSEGV signal. -inline void drv_segfault_handler(std::int32_t id) { +inline void drvi_crash_handler(std::int32_t id) { + LibCompiler::STLString verbose_header = "LIBCOMPILER CRASH REPORT - "; + verbose_header += kDistVersion; + verbose_header += " - "; + verbose_header += LibCompiler::current_date(); + + for (auto& ch : verbose_header) { + std::cout << '='; + } + + std::cout << std::endl; + + std::cout << verbose_header << std::endl; + + for (auto& ch : verbose_header) { + std::cout << '='; + } + + std::cout << std::endl; + + kStdOut << "DATE: " << LibCompiler::current_date() << std::endl; + kStdOut << "VERSION: " << kDistVersion << std::endl; + kStdOut << "ERRNO: " << errno << std::endl; + kStdOut << "ERRNO(STRING): " << strerror(errno) << std::endl; + + switch (id) { case SIGSEGV: { - kStdErr << "SIGSEGV: Please report this on the GitHub issues page." << kBlank << std::endl; + kStdOut << "SIGSEGV: Segmentation Fault." << kBlank << std::endl; break; } case SIGABRT: { - kStdErr << "SIGABRT: Please report this on the GitHub issues page." << kBlank << std::endl; + kStdOut << "SIGABRT: Aborted." << kBlank << std::endl; break; } } + std::cout << kWhite; + + for (auto& ch : verbose_header) { + std::cout << '='; + } + + std::cout << std::endl; + + std::cout << verbose_header << std::endl; + + for (auto& ch : verbose_header) { + std::cout << '='; + } + + std::cout << std::endl; + std::exit(LIBCOMPILER_EXEC_ERROR); } } // namespace Detail diff --git a/dev/LibCompiler/Version.h b/dev/LibCompiler/Version.h index b94be42..93a7c00 100644 --- a/dev/LibCompiler/Version.h +++ b/dev/LibCompiler/Version.h @@ -6,8 +6,8 @@ #pragma once -#define kDistVersion "v0.0.1-libcompiler" -#define kDistVersionBCD 0x0001 +#define kDistVersion "v0.0.2-libcompiler" +#define kDistVersionBCD 0x0002 #define ToString(X) Stringify(X) #define Stringify(X) #X diff --git a/dev/LibCompiler/src/Asm/Assembler64x0.cc b/dev/LibCompiler/src/Asm/Assembler64x0.cc index 8c1adb5..fb96708 100644 --- a/dev/LibCompiler/src/Asm/Assembler64x0.cc +++ b/dev/LibCompiler/src/Asm/Assembler64x0.cc @@ -67,7 +67,7 @@ static bool asm_read_attributes(std::string line); ///////////////////////////////////////////////////////////////////////////////////////// LIBCOMPILER_MODULE(AssemblerMain64x0) { - ::signal(SIGSEGV, Detail::drv_segfault_handler); + ::signal(SIGSEGV, Detail::drvi_crash_handler); for (size_t i = 1; i < argc; ++i) { if (argv[i][0] == '-') { diff --git a/dev/LibCompiler/src/Asm/AssemblerAMD64.cc b/dev/LibCompiler/src/Asm/AssemblerAMD64.cc index 4a4ca41..e9f3ad5 100644 --- a/dev/LibCompiler/src/Asm/AssemblerAMD64.cc +++ b/dev/LibCompiler/src/Asm/AssemblerAMD64.cc @@ -85,7 +85,7 @@ static bool asm_read_attributes(std::string line); LIBCOMPILER_MODULE(AssemblerMainAMD64) { //////////////// CPU OPCODES BEGIN //////////////// - ::signal(SIGSEGV, Detail::drv_segfault_handler); + ::signal(SIGSEGV, Detail::drvi_crash_handler); std::string opcodes_jump[kJumpLimit] = {"ja", "jae", "jb", "jbe", "jc", "je", "jg", "jge", "jl", "jle", "jna", "jnae", "jnb", "jnbe", "jnc", "jne", diff --git a/dev/LibCompiler/src/Asm/AssemblerARM64.cc b/dev/LibCompiler/src/Asm/AssemblerARM64.cc index 59df5a7..8fad6b5 100644 --- a/dev/LibCompiler/src/Asm/AssemblerARM64.cc +++ b/dev/LibCompiler/src/Asm/AssemblerARM64.cc @@ -71,7 +71,7 @@ static bool asm_read_attributes(std::string line); ///////////////////////////////////////////////////////////////////////////////////////// LIBCOMPILER_MODULE(AssemblerMainARM64) { - ::signal(SIGSEGV, Detail::drv_segfault_handler); + ::signal(SIGSEGV, Detail::drvi_crash_handler); for (size_t i = 1; i < argc; ++i) { if (argv[i][0] == '-') { diff --git a/dev/LibCompiler/src/Asm/AssemblerPowerPC.cc b/dev/LibCompiler/src/Asm/AssemblerPowerPC.cc index bf7f115..86a70b9 100644 --- a/dev/LibCompiler/src/Asm/AssemblerPowerPC.cc +++ b/dev/LibCompiler/src/Asm/AssemblerPowerPC.cc @@ -71,7 +71,7 @@ static bool asm_read_attributes(std::string line); ///////////////////////////////////////////////////////////////////////////////////////// LIBCOMPILER_MODULE(AssemblerMainPower64) { - ::signal(SIGSEGV, Detail::drv_segfault_handler); + ::signal(SIGSEGV, Detail::drvi_crash_handler); for (size_t i = 1; i < argc; ++i) { if (argv[i][0] == '-') { diff --git a/dev/LibCompiler/src/Cl/CCompiler64x0.cc b/dev/LibCompiler/src/Cl/CCompiler64x0.cc index 2d37227..fc31ef9 100644 --- a/dev/LibCompiler/src/Cl/CCompiler64x0.cc +++ b/dev/LibCompiler/src/Cl/CCompiler64x0.cc @@ -1198,7 +1198,7 @@ static void cc_print_help() { #define kExt ".c" LIBCOMPILER_MODULE(NeOSCompilerCLang64x0) { - ::signal(SIGSEGV, Detail::drv_segfault_handler); + ::signal(SIGSEGV, Detail::drvi_crash_handler); kCompilerTypes.push_back({.fName = "void", .fValue = "void"}); kCompilerTypes.push_back({.fName = "char", .fValue = "byte"}); diff --git a/dev/LibCompiler/src/Cl/CCompilerARM64.cc b/dev/LibCompiler/src/Cl/CCompilerARM64.cc index 093c818..02e39f9 100644 --- a/dev/LibCompiler/src/Cl/CCompilerARM64.cc +++ b/dev/LibCompiler/src/Cl/CCompilerARM64.cc @@ -1196,7 +1196,7 @@ static void cc_print_help() { #define kCExtension ".c" LIBCOMPILER_MODULE(NeOSCompilerCLangARM64) { - ::signal(SIGSEGV, Detail::drv_segfault_handler); + ::signal(SIGSEGV, Detail::drvi_crash_handler); kCompilerTypes.push_back({.fName = "void", .fValue = "void"}); kCompilerTypes.push_back({.fName = "char", .fValue = "byte"}); diff --git a/dev/LibCompiler/src/Cl/CCompilerPower64.cc b/dev/LibCompiler/src/Cl/CCompilerPower64.cc index e0081f7..216891a 100644 --- a/dev/LibCompiler/src/Cl/CCompilerPower64.cc +++ b/dev/LibCompiler/src/Cl/CCompilerPower64.cc @@ -1215,7 +1215,7 @@ static void cc_print_help() { #define kExt ".c" LIBCOMPILER_MODULE(NeOSCompilerCLangPowerPC) { - ::signal(SIGSEGV, Detail::drv_segfault_handler); + ::signal(SIGSEGV, Detail::drvi_crash_handler); kCompilerTypes.push_back({.fName = "void", .fValue = "void"}); kCompilerTypes.push_back({.fName = "char", .fValue = "byte"}); diff --git a/dev/LibCompiler/src/Cl/CPlusPlusCompilerAMD64.cc b/dev/LibCompiler/src/Cl/CPlusPlusCompilerAMD64.cc index 856bc64..bfa1896 100644 --- a/dev/LibCompiler/src/Cl/CPlusPlusCompilerAMD64.cc +++ b/dev/LibCompiler/src/Cl/CPlusPlusCompilerAMD64.cc @@ -9,6 +9,7 @@ /// BUGS: 1 +#include "LibCompiler/Defines.h" #define kPrintF printf #define kExitOK (EXIT_SUCCESS) @@ -71,18 +72,14 @@ struct CompilerRegisterMap final { std::string fReg; }; -// \brief Offset based struct/class +/// \brief Offset based struct/class struct CompilerStructMap final { - std::string fName; - std::string fReg; - - // offset counter - std::size_t fOffsetsCnt; - - // offset array + std::string fName; + std::string fReg; std::vector> fOffsets; }; +/// \brief Compiler state structure. struct CompilerState final { std::vector fStackMapVector; std::vector fStructMapVector; @@ -99,7 +96,7 @@ static Int32 kOnClassScope = 0; ///////////////////////////////////////////////////////////////////////////////////////// // Target architecture. -static int kMachine = LibCompiler::AssemblyFactory::kArchAMD64; +static Int32 kMachine = LibCompiler::AssemblyFactory::kArchAMD64; ///////////////////////////////////////// @@ -740,21 +737,12 @@ class AssemblyCPlusPlusInterface final LC_ASSEMBLY_INTERFACE { std::string line_source; while (std::getline(src_fp, line_source)) { - if (kVerbose) { - kStdOut << line_source << std::endl; - kStdOut << line_source.length() << " bytes\n"; - } - kCompilerFrontend->Compile(line_source, src); out_fp << kState.fOutputValue; kState.fOutputValue.clear(); } - if (kVerbose) { - kStdOut << "Done compiling " << src << " to " << dest << "\n"; - } - return kExitOK; } }; @@ -832,7 +820,7 @@ LIBCOMPILER_MODULE(CompilerCPlusPlusAMD64) { kCompilerFrontend = new CompilerFrontendCPlusPlus(); kFactory.Mount(new AssemblyCPlusPlusInterface()); - ::signal(SIGSEGV, Detail::drv_segfault_handler); + ::signal(SIGSEGV, Detail::drvi_crash_handler); for (auto index = 1UL; index < argc; ++index) { if (argv[index][0] == '-') { diff --git a/dev/LibCompiler/src/Linker/DynamicLinkerPEF.cc b/dev/LibCompiler/src/Linker/DynamicLinkerPEF.cc index 0c6f3bf..edfd47a 100644 --- a/dev/LibCompiler/src/Linker/DynamicLinkerPEF.cc +++ b/dev/LibCompiler/src/Linker/DynamicLinkerPEF.cc @@ -101,7 +101,7 @@ static std::vector kObjectBytes; LIBCOMPILER_MODULE(DynamicLinker64PEF) { bool is_executable = true; - ::signal(SIGSEGV, Detail::drv_segfault_handler); + ::signal(SIGSEGV, Detail::drvi_crash_handler); /** * @brief parse flags and trigger options. diff --git a/tools/cxxdrv.cc b/tools/cxxdrv.cc index 1f58692..98cc613 100644 --- a/tools/cxxdrv.cc +++ b/tools/cxxdrv.cc @@ -10,12 +10,38 @@ #include #include #include -#include +#include +#include -LC_IMPORT_C Int32 CompilerCPlusPlusAMD64(Int32 argc, CharType const* argv[]); +typedef Int32(*LibCompilerEntrypoint)(Int32 argc, CharType const* argv[]); + +typedef VoidPtr DylibHandle; + +static auto kPath = "/usr/local/lib/libCompiler.dylib"; Int32 main(Int32 argc, CharType const* argv[]) { - auto ret = CompilerCPlusPlusAMD64(argc, argv); + DylibHandle handler = dlopen(kPath, RTLD_LAZY | RTLD_GLOBAL); + + if (!handler) { + kStdOut; + std::printf("error: Could not load dylib in %s: %s\n", kPath, dlerror()); + + return EXIT_FAILURE; + } + + LibCompilerEntrypoint entrypoint_cxx = (LibCompilerEntrypoint)dlsym(handler, "CompilerCPlusPlusAMD64"); + + if (!entrypoint_cxx) { + kStdOut; + std::printf("error: Could not find entrypoint in %s: %s\n", kPath, dlerror()); + dlclose(handler); + + return EXIT_FAILURE; + } + + auto ret = (entrypoint_cxx(argc, argv) == LIBCOMPILER_SUCCESS) ? EXIT_SUCCESS : EXIT_FAILURE; + + dlclose(handler); - return (ret == LIBCOMPILER_SUCCESS) ? EXIT_SUCCESS : EXIT_FAILURE; + return ret; } diff --git a/tools/cxxdrv.json b/tools/cxxdrv.json index fdb2e43..5b1919d 100644 --- a/tools/cxxdrv.json +++ b/tools/cxxdrv.json @@ -11,8 +11,6 @@ ], "output_name": "cxxdrv", "compiler_flags": [ - "-L/usr/local/lib", - "-lCompiler" ], "cpp_macros": [ "__CXXDRV__=202504", -- cgit v1.2.3