From a8f30f696703f466e36c526481068e678533f149 Mon Sep 17 00:00:00 2001 From: Kittycannon Date: Tue, 17 Mar 2026 20:41:40 -0600 Subject: [PATCH] first ever upload --- .gitignore | 0 README.md | 10 +++ spider-runtime.code-workspace | 12 ++++ src/spider/SpiderRuntime.hpp | 11 ++++ src/spider/runtime/common.hpp | 34 ++++++++++ src/spider/runtime/cpu/CPU.hpp | 29 ++++++++ src/spider/runtime/cpu/Register.hpp | 77 ++++++++++++++++++++++ src/spider/runtime/memory/RAM.hpp | 0 src/spider/runtime/native/distro.hpp | 3 + src/spider/runtime/native/machine.hpp | 95 +++++++++++++++++++++++++++ 10 files changed, 271 insertions(+) create mode 100644 .gitignore create mode 100644 spider-runtime.code-workspace create mode 100644 src/spider/SpiderRuntime.hpp create mode 100644 src/spider/runtime/common.hpp create mode 100644 src/spider/runtime/cpu/CPU.hpp create mode 100644 src/spider/runtime/cpu/Register.hpp create mode 100644 src/spider/runtime/memory/RAM.hpp create mode 100644 src/spider/runtime/native/distro.hpp create mode 100644 src/spider/runtime/native/machine.hpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/README.md b/README.md index 7ca6c56..5fae6c2 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,12 @@ # spider-runtime +This is the Spider runtime (aka, the virtual machine) that executes the Spider byte code. +## Code Etiquette + - Do not use \r\n + - Do not use any encoding besides UTF-8 + - Always comment global functions, variables, classes, member variables and methods. + - Do not modify the autogenerated files. + - If using an LLM, use private mode and tell it you're working on an old modem. + - If using an AI agent, don't. + +Failure to uphold the code etiquette will result in a slap in the wrist, with a hammer. diff --git a/spider-runtime.code-workspace b/spider-runtime.code-workspace new file mode 100644 index 0000000..5ba9520 --- /dev/null +++ b/spider-runtime.code-workspace @@ -0,0 +1,12 @@ +{ + "folders": [ + { + "path": "." + } + ], + "settings": { + "C_Cpp.default.includePath": [ + "./src" + ] + } +} \ No newline at end of file diff --git a/src/spider/SpiderRuntime.hpp b/src/spider/SpiderRuntime.hpp new file mode 100644 index 0000000..99afe03 --- /dev/null +++ b/src/spider/SpiderRuntime.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include + +namespace spider { + + class Runtime; + class CPU; + class RAM; + +} diff --git a/src/spider/runtime/common.hpp b/src/spider/runtime/common.hpp new file mode 100644 index 0000000..67f8ed8 --- /dev/null +++ b/src/spider/runtime/common.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace spider { + + using u8 = std::uint8_t; + using u16 = std::uint16_t; + using u32 = std::uint32_t; + using u64 = std::uint64_t; + + using i8 = std::int8_t; + using i16 = std::int16_t; + using i32 = std::int32_t; + using i64 = std::int64_t; + + using f32 = float; // TODO: SPIDER_EMULATE_FLOAT will control this + using f64 = double; + + // TODO: Check if we're on C++23, there is already stdfloat + static_assert(sizeof(f32) == 4, "The f32 type must be exactly 4 bytes."); + static_assert(sizeof(f64) == 8, "The f64 type must be exactly 8 bytes."); + + // Utility imports + using std::vector; + using std::deque; + using std::map; + using std::optional; + +} diff --git a/src/spider/runtime/cpu/CPU.hpp b/src/spider/runtime/cpu/CPU.hpp new file mode 100644 index 0000000..a7aa96d --- /dev/null +++ b/src/spider/runtime/cpu/CPU.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include + +namespace spider { + + class CPU { + public: // General Purpose Registers + register_t RA, RB, RC, RD, + RX, RY, R0, R1, + R2, R3, R4, R5, + R6, R7, R8, R9; + + public: // System Registers + u64 RF; + u64 RI; + u64 RS; + u64 RZ; + u64 RE; + u64 RN; // Epsilo(n) + u64 RV; + u64 RM; + + public: + CPU(); + ~CPU(); + }; + +} \ No newline at end of file diff --git a/src/spider/runtime/cpu/Register.hpp b/src/spider/runtime/cpu/Register.hpp new file mode 100644 index 0000000..a2acdd5 --- /dev/null +++ b/src/spider/runtime/cpu/Register.hpp @@ -0,0 +1,77 @@ +#pragma once + +#include +#include + +namespace spider { + + /** + * A register is a tiny piece of memory. + * I hate adding a _t suffix but for some idiotic + * reason "register" is a keyword in C++. + * + * Note that we have to check the endianness of the system + * at compile time to order the structure so that smaller + * types are actually the bottom part of the memory. + * + * Also, this has to be done with a compiler that allows + * type-punning which is the "standard" right now. + */ + union register_t { + u64 _u64; + i64 _i64; + f64 _f64; + u8 _bytes[8]; + + SPIDER_PACKED_STRUCT(struct { + #if SPIDER_LITTLE_ENDIAN + u8 _u8; u64 : 56; + #else + u64 : 56; u8 _u8; + #endif + }); + + SPIDER_PACKED_STRUCT(struct { + #if SPIDER_LITTLE_ENDIAN + u16 _u16; u64 : 48; + #else + u64 : 48; u16 _u16; + #endif + }); + + struct { + #if SPIDER_LITTLE_ENDIAN + u32 _u32; u32 : 32; + #else + u32 : 32; u32 _u32; + #endif + }; + + struct { + #if SPIDER_LITTLE_ENDIAN + f32 _f32; u32 : 32; + #else + u32 : 32; f32 _f32; + #endif + }; + + u8& operator[](size_t i) { // 0 is always LSB + #if SPIDER_LITTLE_ENDIAN + return _bytes[i]; + #else + return _bytes[7 - i]; + #endif + } + + // ngl I could get executed for not having a const version + const u8& operator[](size_t i) const { // 0 is always LSB + #if SPIDER_LITTLE_ENDIAN + return _bytes[i]; + #else + return _bytes[7 - i]; + #endif + } + }; + static_assert(sizeof(register_t) == 8, "The register type must be exactly 8 bytes."); + +} \ No newline at end of file diff --git a/src/spider/runtime/memory/RAM.hpp b/src/spider/runtime/memory/RAM.hpp new file mode 100644 index 0000000..e69de29 diff --git a/src/spider/runtime/native/distro.hpp b/src/spider/runtime/native/distro.hpp new file mode 100644 index 0000000..45dcbb0 --- /dev/null +++ b/src/spider/runtime/native/distro.hpp @@ -0,0 +1,3 @@ +#pragma once + + diff --git a/src/spider/runtime/native/machine.hpp b/src/spider/runtime/native/machine.hpp new file mode 100644 index 0000000..13ff024 --- /dev/null +++ b/src/spider/runtime/native/machine.hpp @@ -0,0 +1,95 @@ +#pragma once + +/* + * This file contains macros related to machine dependent + * things like alignment and type juggling +*/ + +// === ENDIANESS === // +// Used by GCC/Clang/WASM/ESP32/RP2040 and most compilers +#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__) + #define SPIDER_LITTLE_ENDIAN (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) + #define SPIDER_BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) + +// Fallbacks (For older/constrained compilers) +#else + #if defined(__AVR_ATmega328P__) || defined(__AVR__) // Arduino Uno/ATmega + #define SPIDER_LITTLE_ENDIAN 1 + #define SPIDER_BIG_ENDIAN 0 + #elif defined(__wasm__) || defined(__wasm32__) // WebAssembly + #define SPIDER_LITTLE_ENDIAN 1 + #define SPIDER_BIG_ENDIAN 0 + #elif defined(__arm__) || defined(__thumb__) // RP2040, STM32, etc. + #if defined(__ARMEB__) + #define SPIDER_LITTLE_ENDIAN 0 + #define SPIDER_BIG_ENDIAN 1 + #else + #define SPIDER_LITTLE_ENDIAN 1 + #define SPIDER_BIG_ENDIAN 0 + #endif + #elif defined(__xtensa__) || defined(__ESP32__) // ESP32 + #define SPIDER_LITTLE_ENDIAN 1 + #define SPIDER_BIG_ENDIAN 0 + // Likely will never use this clause + #elif defined(_WIN32) || defined(_M_IX86) || defined(_M_X64) + #define SPIDER_LITTLE_ENDIAN 1 + #define SPIDER_BIG_ENDIAN 0 + #else + #error "Unsupported or unknown architecture endianness!" + #endif +#endif + +// Safety checks +#if !defined(SPIDER_LITTLE_ENDIAN) || !defined(SPIDER_BIG_ENDIAN) + #error "Missed at least one little/big endian macros" +#endif +#if SPIDER_LITTLE_ENDIAN == 1 && SPIDER_BIG_ENDIAN == 1 + #warning "Mixed endian machine detected, unsupported! Be cautious adventurer!" +#endif + +// === PACKING === // +// TODO: move somewhere else +#if defined(__clang__) + #define SPIDER_COMPILER_CLANG 1 + #define SPIDER_COMPILER_GCC_LIKE 1 +#elif defined(__GNUC__) + #define SPIDER_COMPILER_GCC 1 + #define SPIDER_COMPILER_GCC_LIKE 1 +#elif defined(_MSC_VER) + #define SPIDER_COMPILER_MSVC 1 +#else + #define SPIDER_COMPILER_UNKNOWN 1 +#endif + +// TODO: Ditto of whatever I already have +#if defined(__EMSCRIPTEN__) || defined(__wasm__) + #define SPIDER_PLATFORM_WASM 1 +#elif defined(__AVR_ATmega328P__) || defined(__AVR__) + #define SPIDER_PLATFORM_AVR 1 // ATmega328 +#elif defined(ARDUINO_ARCH_RP2040) || defined(PICO_BOARD) + #define SPIDER_PLATFORM_RP2040 1 +#elif defined(ESP32) || defined(ARDUINO_ARCH_ESP32) + #define SPIDER_PLATFORM_ESP32 1 +#elif defined(STM32) || defined(ARDUINO_ARCH_STM32) + #define SPIDER_PLATFORM_STM32 1 +#endif + +// Macros... +#if defined(SPIDER_COMPILER_GCC_LIKE) + #define SPIDER_ATTRIBUTE_PACKED __attribute__((packed)) + #define SPIDER_PACKED_STRUCT(decl) decl SPIDER_ATTRIBUTE_PACKED + +#elif defined(SPIDER_COMPILER_MSVC) + #define SPIDER_BEGIN_PACKED __pragma(pack(push, 1)) + #define SPIDER_END_PACKED __pragma(pack(pop)) + #define SPIDER_PACKED_STRUCT(decl) SPIDER_BEGIN_PACKED decl SPIDER_END_PACKED + +#else + #define SPIDER_ATTRIBUTE_PACKED + #define SPIDER_BEGIN_PACKED + #define SPIDER_END_PACKED + #define SPIDER_PACKED_STRUCT(decl) decl + #warning "MODEM: Compiler packing not supported. Memory layout may be unstable!" +#endif + +namespace spider {}