From 1ca24171fbc1e293cd2428577c1af3a310bf01f6 Mon Sep 17 00:00:00 2001 From: Creeper Lv Date: Wed, 16 Jul 2025 20:09:34 +0800 Subject: [PATCH] Initial Commit. --- src/SVM.Core/DebugSymbol.cs | 27 ++++++++ src/SVM.Core/SVM.Core.csproj | 10 +++ src/SVM.Core/SVMInst.cs | 68 +++++++++++++++++++ src/SVM.Core/SimpleVirtualMachine.cs | 85 ++++++++++++++++++++++++ src/SVM.Core/Utils/PointerUtils.cs | 45 +++++++++++++ src/SVM.Core/stdc/stdlib.cs | 40 +++++++++++ src/SVM.Standalone/Program.cs | 9 +++ src/SVM.Standalone/SVM.Standalone.csproj | 14 ++++ src/SVM.sln | 48 +++++++++++++ 9 files changed, 346 insertions(+) create mode 100644 src/SVM.Core/DebugSymbol.cs create mode 100644 src/SVM.Core/SVM.Core.csproj create mode 100644 src/SVM.Core/SVMInst.cs create mode 100644 src/SVM.Core/SimpleVirtualMachine.cs create mode 100644 src/SVM.Core/Utils/PointerUtils.cs create mode 100644 src/SVM.Core/stdc/stdlib.cs create mode 100644 src/SVM.Standalone/Program.cs create mode 100644 src/SVM.Standalone/SVM.Standalone.csproj create mode 100644 src/SVM.sln diff --git a/src/SVM.Core/DebugSymbol.cs b/src/SVM.Core/DebugSymbol.cs new file mode 100644 index 0000000..a608ba3 --- /dev/null +++ b/src/SVM.Core/DebugSymbol.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +namespace SVM.Core +{ + [Serializable] + public class DebugSymbol + { + public Dictionary Functions = new Dictionary(); + } + public class RuntimeBinary + { + public uint offset; + public SimpleVirtualMachine? BindedMachine; + public DebugSymbol? Symbol; + public bool Invoke(string funcName) + { + if (BindedMachine == null) return false; + if (Symbol == null) return false; + if (!Symbol.Functions.TryGetValue(funcName, out var func)) + { + return false; + } + + return false; + } + } +} diff --git a/src/SVM.Core/SVM.Core.csproj b/src/SVM.Core/SVM.Core.csproj new file mode 100644 index 0000000..1de3a88 --- /dev/null +++ b/src/SVM.Core/SVM.Core.csproj @@ -0,0 +1,10 @@ + + + + netstandard2.1 + enable + true + 9.0 + + + diff --git a/src/SVM.Core/SVMInst.cs b/src/SVM.Core/SVMInst.cs new file mode 100644 index 0000000..9b47f07 --- /dev/null +++ b/src/SVM.Core/SVMInst.cs @@ -0,0 +1,68 @@ +namespace SVM.Core +{ + public enum SVMInst : byte + { + // 0 1 2 3 4 + // Add [I]Type [R]L [R]R [R]T + Add, + Sub, + Mul, + Div, + Mod, + // Set Conditional Register to 1 if condition met. + // 0 1 2 3 4 + // Cmp [R]Op [R]L [R]R [R]T + Cmp, + // 0 1 + // JAL RD + // [I]Address (int32) + JAL, + // Jump And Link If Conditional Register is set. + // JALF RD + // [I]Address (int32) + JALF, + // 0 1 2 3 + // Load [R]Address [I]Len [R]T + Load, + Save, + // 0 + // Call + // [I]Address (int64) + Call, + // Return + Return, + // System + // [I]CallID (uint64) + System, + // 0 1 2 3 4 5 + // SIMD Op [R]LAddr [R]RAddr [R]TAddr [R]Len + SIMD, + // 0 1 2 3 4 + // AdvMath Op [R]L [R]R [R]T + AdvMath, + } + public enum CmpOperator : byte + { + Eq, Ne, LT, GT, LE, GE + } + public enum SVMNativeTypes : byte + { + Int8, + Int16, + Int32, + Int64, + UInt8, + UInt16, + UInt32, + UInt64, + Float, + Double, + } + public enum SIMDOperator : byte + { + Add, + Sub, + Mul, + Div, + } +} diff --git a/src/SVM.Core/SimpleVirtualMachine.cs b/src/SVM.Core/SimpleVirtualMachine.cs new file mode 100644 index 0000000..f59b116 --- /dev/null +++ b/src/SVM.Core/SimpleVirtualMachine.cs @@ -0,0 +1,85 @@ +using System; +using static SVM.Core.stdc.stdlib; +using System.Runtime.InteropServices; +using System.Collections.Generic; +using SVM.Core.Utils; +namespace SVM.Core +{ + public unsafe class SimpleVirtualMachine : IDisposable + { + public Registers registers; + public MemoryBlock Stack; + public MemoryBlock* GPMemory; + public SVMConfig? Config = null; + public void Init(uint StackSize = 1024 * 1024, uint RegisterSize = 512) + { + GPMemory = null; + } + public void Step() + { + uint SPOffset = 4; + uint PCOffset = 0; + if (Config != null) + { + SPOffset = Config.SPRegisterOffset; + PCOffset = Config.PCRegisterOffset; + } + } + public void SetGPMemory(MemoryBlock* ptr) + { + GPMemory = ptr; + } + public void Dispose() + { + registers.Dispose(); + Stack.Dispose(); + } + } + public class SVMConfig + { + public Dictionary FuncCalls = new Dictionary(); + public uint SPRegisterOffset; + public uint PCRegisterOffset; + } + public delegate void FuncCall(SimpleVirtualMachine machine); + [StructLayout(LayoutKind.Sequential)] + public struct MState + { + public uint PC; + } + [StructLayout(LayoutKind.Sequential)] + public struct MemoryBlock : IDisposable + { + public IntPtr StartAddress; + public int Size; + + public void Dispose() + { + free(StartAddress); + } + } + [StructLayout(LayoutKind.Sequential)] + public struct Registers : IDisposable + { + IntPtr Data; + public void Init(int size) + { + Data = malloc((uint)size); + } + public T ReadData(int RegisterID) where T : unmanaged + { + return Data.GetDataWithOffsetInBytes(RegisterID * sizeof(Int64)); + } + public void Dispose() + { + free(Data); + } + } + [StructLayout(LayoutKind.Sequential)] + public struct Callframe + { + public int PC; + public uint SP; + } + +} diff --git a/src/SVM.Core/Utils/PointerUtils.cs b/src/SVM.Core/Utils/PointerUtils.cs new file mode 100644 index 0000000..d3881b9 --- /dev/null +++ b/src/SVM.Core/Utils/PointerUtils.cs @@ -0,0 +1,45 @@ +using System; +using System.Runtime.InteropServices; +using static SVM.Core.stdc.cstring; +namespace SVM.Core.Utils +{ + public unsafe static class PointerUtils + { + public static T GetData(this IntPtr ptr) where T : unmanaged + { + return Marshal.PtrToStructure(ptr); + } + public static T GetDataWithOffsetInBytes(this IntPtr ptr, int Offset) where T : unmanaged + { + return Marshal.PtrToStructure(IntPtr.Add(ptr, Offset)); + } + public static T GetDataWithOffsetInStructCount(this IntPtr ptr, int Count) where T : unmanaged + { + return Marshal.PtrToStructure(IntPtr.Add(ptr, Count * sizeof(T))); + } + public static void GetData(this IntPtr ptr, IntPtr dest) where T : unmanaged + { + memcpy(ptr, dest, sizeof(T), 1); + } + public static void GetDataWithOffsetInBytes(this IntPtr ptr, IntPtr dest, int offset) where T : unmanaged + { + memcpy(IntPtr.Add(ptr, offset), dest, sizeof(T), 1); + } + public static void GetDataWithOffsetInStructCount(this IntPtr ptr, IntPtr dest, int offset) where T : unmanaged + { + memcpy(IntPtr.Add(ptr, offset * sizeof(T)), dest, sizeof(T), 1); + } + public static void GetData(this IntPtr ptr, T* dest) where T : unmanaged + { + GetData(ptr, (IntPtr)dest); + } + public static void GetDataWithOffsetInBytes(this IntPtr ptr, T* dest, int offset) where T : unmanaged + { + GetDataWithOffsetInBytes(ptr, (IntPtr)dest, offset); + } + public static void GetDataWithOffsetInStructCount(this IntPtr ptr, T* dest, int offset) where T : unmanaged + { + GetDataWithOffsetInStructCount(ptr, (IntPtr)dest, offset); + } + } +} diff --git a/src/SVM.Core/stdc/stdlib.cs b/src/SVM.Core/stdc/stdlib.cs new file mode 100644 index 0000000..89ed7ee --- /dev/null +++ b/src/SVM.Core/stdc/stdlib.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Text; + +namespace SVM.Core.stdc +{ + /// + /// A simple library contains POSIX-like impl of memory functions. + /// + public static class stdlib + { + public static IntPtr malloc(uint size) + { + return Marshal.AllocHGlobal((int)size); + } + public static IntPtr realloc(IntPtr ptr, uint size) + { + return Marshal.ReAllocHGlobal(ptr, (IntPtr)size); + } + public static void free(IntPtr ptr) + { + Marshal.FreeHGlobal(ptr); + } + } + public static class cstring + { + public static void memset(IntPtr ptr, byte value, int count) + { + for (int i = 0; i < count; i++) + { + Marshal.WriteByte(ptr, value); + } + } + public unsafe static void memcpy(IntPtr src, IntPtr dst, int size, int count) + { + Buffer.MemoryCopy((void*)src, (void*)dst, size, count); + } + } +} diff --git a/src/SVM.Standalone/Program.cs b/src/SVM.Standalone/Program.cs new file mode 100644 index 0000000..9c834a2 --- /dev/null +++ b/src/SVM.Standalone/Program.cs @@ -0,0 +1,9 @@ +namespace SVM.Standalone; + +class Program +{ + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } +} diff --git a/src/SVM.Standalone/SVM.Standalone.csproj b/src/SVM.Standalone/SVM.Standalone.csproj new file mode 100644 index 0000000..aa23c6f --- /dev/null +++ b/src/SVM.Standalone/SVM.Standalone.csproj @@ -0,0 +1,14 @@ + + + + + + + + Exe + net9.0 + enable + enable + + + diff --git a/src/SVM.sln b/src/SVM.sln new file mode 100644 index 0000000..9225cc1 --- /dev/null +++ b/src/SVM.sln @@ -0,0 +1,48 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SVM.Core", "SVM.Core\SVM.Core.csproj", "{5C52ADCB-596C-46F3-97E8-371F74CBFC36}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SVM.Standalone", "SVM.Standalone\SVM.Standalone.csproj", "{A2BC0739-0B24-4103-B35C-0475FEC2242C}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5C52ADCB-596C-46F3-97E8-371F74CBFC36}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5C52ADCB-596C-46F3-97E8-371F74CBFC36}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5C52ADCB-596C-46F3-97E8-371F74CBFC36}.Debug|x64.ActiveCfg = Debug|Any CPU + {5C52ADCB-596C-46F3-97E8-371F74CBFC36}.Debug|x64.Build.0 = Debug|Any CPU + {5C52ADCB-596C-46F3-97E8-371F74CBFC36}.Debug|x86.ActiveCfg = Debug|Any CPU + {5C52ADCB-596C-46F3-97E8-371F74CBFC36}.Debug|x86.Build.0 = Debug|Any CPU + {5C52ADCB-596C-46F3-97E8-371F74CBFC36}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5C52ADCB-596C-46F3-97E8-371F74CBFC36}.Release|Any CPU.Build.0 = Release|Any CPU + {5C52ADCB-596C-46F3-97E8-371F74CBFC36}.Release|x64.ActiveCfg = Release|Any CPU + {5C52ADCB-596C-46F3-97E8-371F74CBFC36}.Release|x64.Build.0 = Release|Any CPU + {5C52ADCB-596C-46F3-97E8-371F74CBFC36}.Release|x86.ActiveCfg = Release|Any CPU + {5C52ADCB-596C-46F3-97E8-371F74CBFC36}.Release|x86.Build.0 = Release|Any CPU + {A2BC0739-0B24-4103-B35C-0475FEC2242C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A2BC0739-0B24-4103-B35C-0475FEC2242C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A2BC0739-0B24-4103-B35C-0475FEC2242C}.Debug|x64.ActiveCfg = Debug|Any CPU + {A2BC0739-0B24-4103-B35C-0475FEC2242C}.Debug|x64.Build.0 = Debug|Any CPU + {A2BC0739-0B24-4103-B35C-0475FEC2242C}.Debug|x86.ActiveCfg = Debug|Any CPU + {A2BC0739-0B24-4103-B35C-0475FEC2242C}.Debug|x86.Build.0 = Debug|Any CPU + {A2BC0739-0B24-4103-B35C-0475FEC2242C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A2BC0739-0B24-4103-B35C-0475FEC2242C}.Release|Any CPU.Build.0 = Release|Any CPU + {A2BC0739-0B24-4103-B35C-0475FEC2242C}.Release|x64.ActiveCfg = Release|Any CPU + {A2BC0739-0B24-4103-B35C-0475FEC2242C}.Release|x64.Build.0 = Release|Any CPU + {A2BC0739-0B24-4103-B35C-0475FEC2242C}.Release|x86.ActiveCfg = Release|Any CPU + {A2BC0739-0B24-4103-B35C-0475FEC2242C}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal