first ever upload

This commit is contained in:
2026-03-17 20:41:40 -06:00
parent 707b56703d
commit a8f30f6967
10 changed files with 271 additions and 0 deletions

0
.gitignore vendored Normal file
View File

View File

@@ -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.

View File

@@ -0,0 +1,12 @@
{
"folders": [
{
"path": "."
}
],
"settings": {
"C_Cpp.default.includePath": [
"./src"
]
}
}

View File

@@ -0,0 +1,11 @@
#pragma once
#include <spider/runtime/common.hpp>
namespace spider {
class Runtime;
class CPU;
class RAM;
}

View File

@@ -0,0 +1,34 @@
#pragma once
#include <cstdint>
#include <vector>
#include <deque>
#include <map>
#include <optional>
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;
}

View File

@@ -0,0 +1,29 @@
#pragma once
#include <spider/runtime/cpu/Register.hpp>
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();
};
}

View File

@@ -0,0 +1,77 @@
#pragma once
#include <spider/runtime/common.hpp>
#include <spider/runtime/native/machine.hpp>
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.");
}

View File

View File

@@ -0,0 +1,3 @@
#pragma once

View File

@@ -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 {}