From 349c71e4ea632d1265d024bab803d1af282a9aa3 Mon Sep 17 00:00:00 2001 From: Creeper Lv Date: Sat, 11 Apr 2026 12:52:14 +0800 Subject: [PATCH] Initial Commit --- .gitignore | 2 + Headers/Sagittarius.h | 43 ++++++++++ Headers/SagittariusInst.h | 164 ++++++++++++++++++++++++++++++++++++++ Source/Standalone/main.c | 4 + Source/VM/Sagittarius.c | 23 ++++++ build.sh | 27 +++++++ 6 files changed, 263 insertions(+) create mode 100644 .gitignore create mode 100644 Headers/Sagittarius.h create mode 100644 Headers/SagittariusInst.h create mode 100644 Source/Standalone/main.c create mode 100644 Source/VM/Sagittarius.c create mode 100755 build.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..172a144 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +Binaries/ diff --git a/Headers/Sagittarius.h b/Headers/Sagittarius.h new file mode 100644 index 0000000..ec2859d --- /dev/null +++ b/Headers/Sagittarius.h @@ -0,0 +1,43 @@ +#ifndef __SAGITTARIUS_H_ +#define __SAGITTARIUS_H_ +#include "SagittariusInst.h" +#include +#include +#include +#ifdef _WIN32 +#define SAGITTARIUS_API __declspec(dllexport) +#else +#define SAGITTARIUS_API __attribute__((visibility("default"))) +#endif +typedef struct SagittariusRegister{ + //256 Usable + Overflow preventing 256 extra bytes. + uint8_t head[512]; +}SagittariusRegister; +typedef struct SagittariusMemory{ + uint8_t* data; + uint64_t size; +} SagittariusMemory; +typedef struct SagittariusCore{ + SagittariusRegister reg; + SagittariusMemory* memory; + uint64_t pc; +}SagittariusCore; +typedef int32_t(* SagittariusSyscall)(SagittariusCore* core, uint64_t arg_start); +typedef struct SagittariusSyscallEntry{ + uint64_t id; + SagittariusSyscall syscall; +}SagittariusSyscallEntry; +typedef struct SagittariusVM{ + SagittariusSyscallEntry* SagittariusSyscallEntries; + uint64_t SagittariusSyscallCount; + uint64_t SagittariusSyscallCapacity; + SagittariusMemory Memory; +}SagittariusVM; + +SAGITTARIUS_API SagittariusVM* sagittarius_vm_new(uint64_t memory_size); +SAGITTARIUS_API void sagittarius_vm_free(SagittariusVM* vm); +SAGITTARIUS_API void sagittarius_step(SagittariusVM* vm); +SAGITTARIUS_API void sagittarius_mem_resize(SagittariusVM* vm, uint64_t new_size); +SAGITTARIUS_API uint64_t sagittarius_mem_getsize(SagittariusVM* vm); + +#endif \ No newline at end of file diff --git a/Headers/SagittariusInst.h b/Headers/SagittariusInst.h new file mode 100644 index 0000000..b08e804 --- /dev/null +++ b/Headers/SagittariusInst.h @@ -0,0 +1,164 @@ +#ifndef __SAGITTARIUS_INST_H_ +#define __SAGITTARIUS_INST_H_ +#include +/** + * Generally, an instruction is 8 bytes, with the first byte being the instruction definition, and the remaining 7 bytes being the instruction data. + * Some special instructions take another full 8 bytes as data. + */ +typedef enum SagittariusInstDef +{ + /** + * Math function have 2 operands involved. + * Layout: + * |0|1|2|3|4|5| + * |math2|type|operation|L reg|R reg|Dest reg| + */ + math2, + /** + * Math function have 1 operand involved. + * Layout: + * |0|1|2|3|4| + * |math1|type|operation|L reg|Dest reg| + */ + math1, + /** + * Type conversion + * Layout: + * |0|1|2|3|4 + * |cvt|Src Reg| Src Type| Dst Reg| Dst Type| + */ + cvt, + /** + * Set register to a value. + * Layout: + * |0|1|2..7|8..15(another instruction struct)| + * |set|length(1-8 bytes)|padding|payload| + */ + set, + /** + * Move data from one register to another. + * Layout: + * |0|1|2|3| + * |mv|length(1-8 bytes)|Src Reg|Dest Reg| + * + */ + mv, + /** + * Copy data from one register to another + * Layout: + * |0|1|2|3| + * |cp|length(1-8 bytes)|Src Reg|Dest Reg| + */ + cp, + + /** + * Save data to destination memory + * Layout: + * |0|1|2|3| + * |save|length(1-8 bytes)|Src Reg|Dest Mem Ptr (Reg ID)| + */ + save, + /** + * Load data from target memory + * Layout: + * |0|1|2|3| + * |load|length(1-8 bytes)|Dest Reg|Src Mem Ptr (Reg ID)| + */ + load, + /** + * Jump to target PC. + * Layout: + * |0|1|2|3/3..6| + * |jmp|mode selector: absolute/relative|value mode selector: register/immeidate|reg id/int32| + */ + jmp, + /** + * Jump to target PC. + * Layout: + * |0|1|2|3|4/4..7| + * |jmp|mode selector: absolute/relative|value mode selector: register/immeidate|flag register|reg id/int32| + */ + jmp_if, + /** + * Call to PC. + * Register Mode: + * Layout: + * |0|1|2|3|4| + * |call|mode selector: absolute/relative|0|register to remember|register| + * Immeidate Value Mode: + * + * |0|1|2|3|4..7|8..15| + * |call|mode selector: absolute/relative|1|register to remember|padding|uint64_t| + */ + call, + /** + * Return + * Layout: + * |0|1| + * |ret|reg to restore (normally sp)| + */ + ret, + /** + * Compare + * Layout: + * + * |0|1|2|3|4|5| + * |math2|type|operation|L reg|R reg|Dest reg| + */ + cmp, + /** + * Vector Math + * Layout: + * |0|1|2|3|4|5|6| + * |math2|w|h|operation|L mem ptr reg|R mem ptr reg| Dest mem ptr reg| + */ + mathv, + /** + * Halt execution (Only resumeable from VM holder) + * Layout: + * |0| + * |halt| + */ + halt, + /** + * System Call + * Layout: + * |0|1|2| + * |syscall|namespace reg| function id reg| + */ + syscall, + /** + * Test System Call + * Layout: + * |0|1|2|3| + * |tsyscall|namespace reg| function id reg| result reg| + */ + tsyscall, +} SagittariusInstDef; +typedef enum math2op +{ + sag_math2_add, + sag_math2_sub, + sag_math2_mul, + sag_math2_div, + sag_math2_mod, + sag_math2_pow, + +} math2op; +typedef enum math1op{ + sag_math1_sin, + sag_math1_cos, + sag_math1_tan, + sag_math1_sinh, + sag_math1_cosh, + sag_math1_tanh, + sag_math1_asin, + sag_math1_acos, + sag_math1_atan, + sag_math1_abs, +}math1op; +typedef struct SagittariusInst +{ + uint64_t data; +} SagittariusInst; +#endif \ No newline at end of file diff --git a/Source/Standalone/main.c b/Source/Standalone/main.c new file mode 100644 index 0000000..0a9f854 --- /dev/null +++ b/Source/Standalone/main.c @@ -0,0 +1,4 @@ +int main(int ac,char** av){ + + return 0; +} \ No newline at end of file diff --git a/Source/VM/Sagittarius.c b/Source/VM/Sagittarius.c new file mode 100644 index 0000000..996fa67 --- /dev/null +++ b/Source/VM/Sagittarius.c @@ -0,0 +1,23 @@ +#include "../../Headers/Sagittarius.h" +#include +SAGITTARIUS_API SagittariusVM* sagittarius_vm_new(uint64_t memory_size){ + SagittariusVM* vm=malloc(sizeof(SagittariusVM)); + vm->Memory.data=malloc(memory_size); + vm->Memory.size=memory_size; + return vm; +} + +SAGITTARIUS_API void sagittarius_vm_free(SagittariusVM* vm){ + free(vm); +} + +SAGITTARIUS_API void sagittarius_step(SagittariusVM* vm){ + +} +SAGITTARIUS_API void sagittarius_mem_resize(SagittariusVM* vm, uint64_t new_size){ + vm->Memory.data=realloc(vm->Memory.data,new_size); + vm->Memory.size=new_size; +} +SAGITTARIUS_API uint64_t sagittarius_mem_getsize(SagittariusVM* vm){ + return vm->Memory.size; +} \ No newline at end of file diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..2319d98 --- /dev/null +++ b/build.sh @@ -0,0 +1,27 @@ +#!/bin/sh + +if [ -z "$CC" ]; +then + CC=cc +fi + +INDEX=0 +if [ -d ".git" ]; +then + echo "Project is tracked by git!" + git_str=$(git log | head -n 1 | cut -c 8-16) + echo $git_str +else + echo "Project is not tracked by git!" + git_str="" +fi + +if [ -z "$SKIP_STANDALONE"]; +then + EXEC="$CC Source/Standalone/*.c Source/VM/*.c -DVER=\"$git_str\" -o Binaries/Sagittarius" + echo "[$INDEX]$EXEC" + INDEX=$((INDEX+1)) + $EXEC +fi + +echo "$INDEX Task(s) completed." \ No newline at end of file