diff --git a/Core/Intrinsics.hpp b/Core/Intrinsics.hpp index 74b606ae..ca7eb5ca 100644 --- a/Core/Intrinsics.hpp +++ b/Core/Intrinsics.hpp @@ -1,5 +1,3 @@ -#include - // TODO Eliminate this #include @@ -8,6 +6,9 @@ GLOBAL char CPU_BRAND_STRING[0x40] = { "N/A (__cpuid intrinsic not yet supported #ifdef RAGLITE_COMPILER_MSVC +#include + +// TODO: Should look into whether (and how much) dllimport improves performance? #define EXPORT extern "C" __declspec(dllexport) INTERNAL void IntrinsicsReadCPUID() { @@ -25,14 +26,25 @@ INTERNAL void IntrinsicsReadCPUID() { __cpuid((int*)CPU_INFO_MASK, 0x80000004); memcpy(CPU_BRAND_STRING + 32, CPU_INFO_MASK, sizeof(CPU_INFO_MASK)); } +} #define DebugTrap() __debugbreak(); #else -// TODO Support for the other toolchains -#endif + +#include + +// TODO: Only relevant if the build script (that doesn't currently exist) uses -fvisibility=hidden +#define EXPORT extern "C" __attribute__((visibility("default"))) + +INTERNAL void IntrinsicsReadCPUID() { + // NYI: Probably need to rewrite this anyway, so for now just leave it as a placeholder } +#define DebugTrap() __builtin_trap(); + +#endif + // TODO: typeof(x) could simplify this - look into toolchain support/extensions? #define Swap(first, second, type) \ do { \ diff --git a/Core/Platforms/Linux.cpp b/Core/Platforms/Linux.cpp new file mode 100644 index 00000000..15a3a40b --- /dev/null +++ b/Core/Platforms/Linux.cpp @@ -0,0 +1,51 @@ +// ABOUT: This is a placeholder program that has nothing in common with the Win32 platform runtime (delete when porting) + +#include +#include +#include +#include +#include + +void DebugPrintASCII(unsigned int value) { + for(int i = 0; i < 4; i++) { + char byte = (value >> (i * 8)) & 0xFF; + printf("%c", byte); + } +} + +// See https://en.wikipedia.org/wiki/CPUID and https://github.com/gcc-mirror/gcc/blob/master/gcc/config/i386/cpuid.h +INTERNAL void DebugDumpCPUID() { + unsigned int eax, ebx, ecx, edx; + // EAX=0: Highest Function Parameter and Manufacturer ID + uint EAX_input = 0; + unsigned int ret = __get_cpuid(EAX_input, &eax, &ebx, &ecx, &edx); + if(ret != 1) { + errx(EXIT_FAILURE, "Failed to call CPUID with EAX=%d", EAX_input); + } + + printf("CPU Manufacturer ID: "); + DebugPrintASCII(ebx); + DebugPrintASCII(edx); + DebugPrintASCII(ecx); + printf("\n"); +} + +INTERNAL void PlatformRuntimeMain() { + printf("There is no platform runtime for Linux yet. Instead, behold this placeholder program :3\n"); + + DebugDumpCPUID(); + // TODO: Maybe dump some of the other CPU details also? Not really useful right now... + + unsigned eax = 0; + unsigned ebx = 0; + unsigned ecx = 0; + unsigned edx = 0; + + int r = __get_cpuid_count(7, 0, &eax, &ebx, &ecx, &edx); + if(!r) { + fprintf(stderr, "cpuid leaf 7 unsupported ...\n"); + return; + } + printf("cpuid.07h.0: eax=0x%x ebx=0x%x ecx=0x%x edx=0x%x\n", + eax, ebx, ecx, edx); +} diff --git a/Core/Platforms/MacOS.cpp b/Core/Platforms/MacOS.cpp new file mode 100644 index 00000000..26e4b96c --- /dev/null +++ b/Core/Platforms/MacOS.cpp @@ -0,0 +1,6 @@ +// ABOUT: This is a placeholder program that has nothing in common with the Win32 platform runtime (delete when porting) + +#include +INTERNAL void PlatformRuntimeMain() { + printf("This is a placeholder program; there's no OSX platform runtime yet...\n"); +} \ No newline at end of file diff --git a/Core/Platforms/Win32.cpp b/Core/Platforms/Win32.cpp index 8b310f7e..9caecbec 100644 --- a/Core/Platforms/Win32.cpp +++ b/Core/Platforms/Win32.cpp @@ -280,8 +280,8 @@ LRESULT CALLBACK MainWindowProcessIncomingMessage(HWND window, UINT message, WPA return result; } -int WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR, - int) { +INTERNAL void PlatformRuntimeMain() { + HINSTANCE instance = GetModuleHandle(NULL); hardware_tick_t applicationStartTime = PerformanceMetricsNow(); IntrinsicsReadCPUID(); @@ -326,7 +326,7 @@ int WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR, GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR); ATOM classGUID = RegisterClassEx(&windowClass); - ASSUME(classGUID != NULL, "Failed to register window class (...really?)"); + ASSUME(classGUID, "Failed to register window class (...really?)"); // TODO: On modern Windows systems, MAX_PATH is insufficient and Unicode paths should ideally be supported (later?) TCHAR executableFileSystemPath[MAX_PATH]; @@ -385,6 +385,4 @@ int WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR, } timeEndPeriod(requestedSchedulerGranularityInMilliseconds); - - return 0; } diff --git a/Core/RagLite2.cpp b/Core/RagLite2.cpp index 23b04bab..b2ac9184 100644 --- a/Core/RagLite2.cpp +++ b/Core/RagLite2.cpp @@ -11,4 +11,18 @@ GLOBAL memory_arena_t TRANSIENT_MEMORY = {}; #ifdef RAGLITE_PLATFORM_WINDOWS #include "Platforms/Win32.cpp" -#endif \ No newline at end of file +#elifdef RAGLITE_PLATFORM_MACOS +#include "Platforms/MacOS.cpp" +#elifdef RAGLITE_PLATFORM_LINUX +#include "Platforms/Linux.cpp" +#endif + +#ifdef RAGLITE_PLATFORM_WINDOWS +int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int) +#else +int main() +#endif +{ + PlatformRuntimeMain(); + return 0; +} \ No newline at end of file diff --git a/Core/RagLite2.hpp b/Core/RagLite2.hpp index 9a09bcfa..8524dd1a 100644 --- a/Core/RagLite2.hpp +++ b/Core/RagLite2.hpp @@ -18,13 +18,13 @@ #error "Unsupported Platform: OS-specific code paths have yet to be ported" #endif -#define RAGLITE_COMPILER_GCC 0 -#define RAGLITE_COMPILER_LLVM 0 -#define RAGLITE_COMPILER_MSVC 0 - -#ifdef _MSC_VER -#undef RAGLITE_COMPILER_MSVC -#define RAGLITE_COMPILER_MSVC 1 +// NOTE: Should probably use feature detection macros, but for now assume the latest (tested) version will work +#if defined(__clang__) +#define RAGLITE_COMPILER_CLANG +#elif defined(_MSC_VER) +#define RAGLITE_COMPILER_MSVC +#elif defined(__GNUC__) +#define RAGLITE_COMPILER_GCC 1 #else #define RAGLITE_UNSUPPORTED_COMPILER #endif @@ -44,10 +44,10 @@ #define EXPAND_AS_STRING(x) #x #define TOSTRING(x) EXPAND_AS_STRING(x) -constexpr size_t BITS_PER_BYTE = 8ULL; +constexpr int BITS_PER_BYTE = 8; #define Bits(bits) ((bits) / BITS_PER_BYTE) -constexpr size_t PLATFORM_POINTER_SIZE = sizeof(void*); +constexpr int PLATFORM_POINTER_SIZE = sizeof(void*); static_assert(PLATFORM_POINTER_SIZE == Bits(64), "Only 64-bit platforms are currently supported"); #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ diff --git a/msys2build.sh b/msys2build.sh new file mode 100644 index 00000000..43ce135f --- /dev/null +++ b/msys2build.sh @@ -0,0 +1,6 @@ +# NOTE: This is not a full port of the MSVC build script, but rather a placeholder for local testing. +# NOTE: Eventually, a proper (more portable) solution will be required. But not today... so this is all there is + +mkdir -p BuildArtifacts +RUNTIME_LIBS="-l gdi32 -l shlwapi -l user32 -l xinput -l winmm -l imagehlp -l ws2_32" +gcc Core/RagLite2.cpp -o BuildArtifacts/RagLiteMSYS2.exe $RUNTIME_LIBS \ No newline at end of file diff --git a/unixbuild.sh b/unixbuild.sh new file mode 100755 index 00000000..bdaddbec --- /dev/null +++ b/unixbuild.sh @@ -0,0 +1,6 @@ +# NOTE: This is not a full port of the MSVC build script, but rather a placeholder for local testing. +# NOTE: Eventually, a proper (more portable) solution will be required. But not today... so this is all there is + +mkdir -p BuildArtifacts +RUNTIME_LIBS="" +gcc Core/RagLite2.cpp -o BuildArtifacts/RagLite2 $RUNTIME_LIBS -lm -fvisibility=hidden