mirror of
https://github.com/creeperlv/SVM.git
synced 2026-01-11 12:59:54 +00:00
SD is now properly working.
This commit is contained in:
@@ -238,6 +238,7 @@ namespace SVM.Assembler.Core
|
|||||||
{
|
{
|
||||||
if (Enum.TryParse<SVMNativeTypes>(input, out var enumV))
|
if (Enum.TryParse<SVMNativeTypes>(input, out var enumV))
|
||||||
{
|
{
|
||||||
|
//Console.WriteLine($"{input}=>{enumV}");
|
||||||
WriteData(inst, parameter.ExpectdValue.Type, parameter.ExpectdValue.Pos, (byte*)&enumV);
|
WriteData(inst, parameter.ExpectdValue.Type, parameter.ExpectdValue.Pos, (byte*)&enumV);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -211,6 +211,10 @@ namespace SVM.Core.Data
|
|||||||
{
|
{
|
||||||
return new CompactByte(Value % R.Value);
|
return new CompactByte(Value % R.Value);
|
||||||
}
|
}
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return this.Value.ToString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -209,6 +209,10 @@ namespace SVM.Core.Data
|
|||||||
{
|
{
|
||||||
return new CompactDouble(Value % R.Value);
|
return new CompactDouble(Value % R.Value);
|
||||||
}
|
}
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return this.Value.ToString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -211,6 +211,10 @@ namespace SVM.Core.Data
|
|||||||
{
|
{
|
||||||
return new CompactInt(Value % R.Value);
|
return new CompactInt(Value % R.Value);
|
||||||
}
|
}
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return this.Value.ToString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -208,6 +208,10 @@ namespace SVM.Core.Data
|
|||||||
{
|
{
|
||||||
return new CompactLong(Value % R.Value);
|
return new CompactLong(Value % R.Value);
|
||||||
}
|
}
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return this.Value.ToString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -209,6 +209,10 @@ namespace SVM.Core.Data
|
|||||||
{
|
{
|
||||||
return new CompactSByte(Value % R.Value);
|
return new CompactSByte(Value % R.Value);
|
||||||
}
|
}
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return this.Value.ToString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -209,5 +209,9 @@ namespace SVM.Core.Data
|
|||||||
{
|
{
|
||||||
return new CompactShort(Value % R.Value);
|
return new CompactShort(Value % R.Value);
|
||||||
}
|
}
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return this.Value.ToString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -208,6 +208,10 @@ namespace SVM.Core.Data
|
|||||||
{
|
{
|
||||||
return new CompactSingle(Value % R.Value);
|
return new CompactSingle(Value % R.Value);
|
||||||
}
|
}
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return this.Value.ToString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -208,6 +208,10 @@ namespace SVM.Core.Data
|
|||||||
{
|
{
|
||||||
return new CompactUInt(Value % R.Value);
|
return new CompactUInt(Value % R.Value);
|
||||||
}
|
}
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return this.Value.ToString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -208,6 +208,10 @@ namespace SVM.Core.Data
|
|||||||
{
|
{
|
||||||
return new CompactULong(Value % R.Value);
|
return new CompactULong(Value % R.Value);
|
||||||
}
|
}
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return this.Value.ToString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -211,6 +211,10 @@ namespace SVM.Core.Data
|
|||||||
{
|
{
|
||||||
return new CompactUShort(Value % R.Value);
|
return new CompactUShort(Value % R.Value);
|
||||||
}
|
}
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return this.Value.ToString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using SVM.Core.Data;
|
using SVM.Core.Data;
|
||||||
|
using System;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
namespace SVM.Core.FuncImpl
|
namespace SVM.Core.FuncImpl
|
||||||
@@ -216,6 +217,7 @@ namespace SVM.Core.FuncImpl
|
|||||||
{
|
{
|
||||||
TN = LN.Add(RN);
|
TN = LN.Add(RN);
|
||||||
}
|
}
|
||||||
|
Console.WriteLine($"SVM:Add:{typeof(N)}{LN}+{RN}={TN.ToString()}");
|
||||||
Register.SetData(T, TN);
|
Register.SetData(T, TN);
|
||||||
}
|
}
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
|||||||
@@ -15,7 +15,8 @@ namespace SVM.Core
|
|||||||
public void Init(uint size)
|
public void Init(uint size)
|
||||||
{
|
{
|
||||||
Size = size;
|
Size = size;
|
||||||
Data = malloc(size);
|
Console.WriteLine($"Allocating:{size}");
|
||||||
|
Data = calloc(size);
|
||||||
}
|
}
|
||||||
public T ReadData<T>(int RegisterID) where T : unmanaged
|
public T ReadData<T>(int RegisterID) where T : unmanaged
|
||||||
{
|
{
|
||||||
@@ -36,7 +37,19 @@ namespace SVM.Core
|
|||||||
{
|
{
|
||||||
if (offset == 0) return;
|
if (offset == 0) return;
|
||||||
if (offset + sizeof(T) > Size) return;
|
if (offset + sizeof(T) > Size) return;
|
||||||
((T*)(Data + offset))[0] = d;
|
((T*)Data + offset)[0] = d;
|
||||||
|
}
|
||||||
|
public unsafe void SetDataInRegister<T>(int offset, T d) where T : unmanaged
|
||||||
|
{
|
||||||
|
if (offset == 0) return;
|
||||||
|
if (offset * sizeof(ulong) + sizeof(T) > Size) return;
|
||||||
|
((T*)((byte*)Data + offset * sizeof(ulong)))[0] = d;
|
||||||
|
}
|
||||||
|
public unsafe void SetDataOffsetInBytes<T>(int offset, T d) where T : unmanaged
|
||||||
|
{
|
||||||
|
if (offset == 0) return;
|
||||||
|
if (offset + sizeof(T) > Size) return;
|
||||||
|
((T*)((byte*)Data + offset))[0] = d;
|
||||||
}
|
}
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -37,13 +37,19 @@ namespace SVM.Core
|
|||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
var dataPtr = malloc(dataSectionLength);
|
Console.WriteLine(dataSectionLength);
|
||||||
|
Console.WriteLine(codeCount);
|
||||||
|
IntPtr dataPtr = malloc(dataSectionLength);
|
||||||
|
if (dataSectionLength != 0)
|
||||||
|
{
|
||||||
|
|
||||||
Span<byte> dataBuffer = new Span<byte>((byte*)dataPtr, (int)dataSectionLength);
|
Span<byte> dataBuffer = new Span<byte>((byte*)dataPtr, (int)dataSectionLength);
|
||||||
if (stream.Read(dataBuffer) != dataSectionLength)
|
if (stream.Read(dataBuffer) != dataSectionLength)
|
||||||
{
|
{
|
||||||
free(dataPtr);
|
free(dataPtr);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
var codeLen = codeCount * sizeof(SVMInstruction);
|
var codeLen = codeCount * sizeof(SVMInstruction);
|
||||||
var codePtr = malloc((uint)codeLen);
|
var codePtr = malloc((uint)codeLen);
|
||||||
Span<byte> codeBuffer = new Span<byte>((byte*)codePtr, (int)codeLen);
|
Span<byte> codeBuffer = new Span<byte>((byte*)codePtr, (int)codeLen);
|
||||||
@@ -54,7 +60,7 @@ namespace SVM.Core
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
var program = (SVMProgram*)malloc(sizeof(SVMProgram));
|
var program = (SVMProgram*)malloc(sizeof(SVMProgram));
|
||||||
program->data = (byte*)dataPtr;
|
program->data = dataSectionLength == 0 ? null : (byte*)dataPtr;
|
||||||
program->DataSize = dataSectionLength;
|
program->DataSize = dataSectionLength;
|
||||||
program->instructions = (SVMInstruction*)codePtr;
|
program->instructions = (SVMInstruction*)codePtr;
|
||||||
program->InstructionCount = codeCount;
|
program->InstructionCount = codeCount;
|
||||||
|
|||||||
@@ -50,6 +50,22 @@ namespace SVM.Core
|
|||||||
registers.SetData<SVMPointer>((int)SPOffset, new SVMPointer() { index = 1, offset = 0 });
|
registers.SetData<SVMPointer>((int)SPOffset, new SVMPointer() { index = 1, offset = 0 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public bool isReachBinaryEnd()
|
||||||
|
{
|
||||||
|
uint SPOffset = 2;
|
||||||
|
uint PCOffset = 1;
|
||||||
|
uint ErrorIDOffset = 3;
|
||||||
|
if (Config != null)
|
||||||
|
{
|
||||||
|
SPOffset = Config.SPRegisterID;
|
||||||
|
PCOffset = Config.PCRegisterID;
|
||||||
|
ErrorIDOffset = Config.EIDRegisterID;
|
||||||
|
}
|
||||||
|
var PC = registers.GetData<ulong>((int)PCOffset);
|
||||||
|
Console.WriteLine($"{PC},{Program->InstructionCount}");
|
||||||
|
return PC >= Program->InstructionCount;
|
||||||
|
}
|
||||||
|
|
||||||
public void Step()
|
public void Step()
|
||||||
{
|
{
|
||||||
uint SPOffset = 2;
|
uint SPOffset = 2;
|
||||||
@@ -67,6 +83,7 @@ namespace SVM.Core
|
|||||||
var currentInstPtr = GetPointer(PC);
|
var currentInstPtr = GetPointer(PC);
|
||||||
var Instruction = currentInstPtr.GetData<SVMInstruction>();
|
var Instruction = currentInstPtr.GetData<SVMInstruction>();
|
||||||
var def = Instruction.GetDef();
|
var def = Instruction.GetDef();
|
||||||
|
Console.WriteLine(def);
|
||||||
fixed (MState* statePtr = &MachineState)
|
fixed (MState* statePtr = &MachineState)
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -144,8 +161,9 @@ namespace SVM.Core
|
|||||||
var Reg = Instruction.GetData<byte>(1);
|
var Reg = Instruction.GetData<byte>(1);
|
||||||
PC++;
|
PC++;
|
||||||
var dataPtr = GetPointer(PC);
|
var dataPtr = GetPointer(PC);
|
||||||
var data = currentInstPtr.GetData<ulong>();
|
var data = dataPtr.GetData<int>();
|
||||||
registers.SetData(Reg, data);
|
registers.SetDataInRegister(Reg, data);
|
||||||
|
Console.WriteLine($"SVM:SD:{data} form PC={PC}");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case PrimaryInstruction.JAL:
|
case PrimaryInstruction.JAL:
|
||||||
@@ -178,6 +196,7 @@ namespace SVM.Core
|
|||||||
}
|
}
|
||||||
PC++;
|
PC++;
|
||||||
registers.SetData<ulong>((int)PCOffset, PC);
|
registers.SetData<ulong>((int)PCOffset, PC);
|
||||||
|
PC = registers.GetData<ulong>((int)PCOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Convert(SVMInstruction Instruction)
|
private void Convert(SVMInstruction Instruction)
|
||||||
@@ -261,7 +280,7 @@ namespace SVM.Core
|
|||||||
|
|
||||||
public IntPtr GetPointer(ulong PC)
|
public IntPtr GetPointer(ulong PC)
|
||||||
{
|
{
|
||||||
return GetPointer(new SVMPointer() { offset = (uint)PC, index = 0 });
|
return GetPointer(new SVMPointer() { offset = (uint)(PC * (uint)sizeof(SVMInstruction)), index = 0 });
|
||||||
}
|
}
|
||||||
public IntPtr GetPointer(SVMPointer absoluteAddress)
|
public IntPtr GetPointer(SVMPointer absoluteAddress)
|
||||||
{
|
{
|
||||||
@@ -277,7 +296,7 @@ namespace SVM.Core
|
|||||||
if (absoluteAddress.offset < offset0)
|
if (absoluteAddress.offset < offset0)
|
||||||
{
|
{
|
||||||
|
|
||||||
return IntPtr.Add((IntPtr)Program->instructions, (int)absoluteAddress.offset);
|
return (IntPtr)(Program->instructions + absoluteAddress.offset / sizeof(SVMInstruction));
|
||||||
}
|
}
|
||||||
else if (absoluteAddress.offset < offset1)
|
else if (absoluteAddress.offset < offset1)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -9,9 +9,9 @@ namespace SVM.Core.Utils
|
|||||||
{
|
{
|
||||||
return Marshal.PtrToStructure<T>(ptr);
|
return Marshal.PtrToStructure<T>(ptr);
|
||||||
}
|
}
|
||||||
public static T GetDataWithOffsetInBytes<T>(this IntPtr ptr, int Offset) where T : unmanaged
|
public unsafe static T GetDataWithOffsetInBytes<T>(this IntPtr ptr, int Offset) where T : unmanaged
|
||||||
{
|
{
|
||||||
return Marshal.PtrToStructure<T>(IntPtr.Add(ptr, Offset));
|
return ((T*)((byte*)(ptr + Offset)))[0];
|
||||||
}
|
}
|
||||||
public static T GetDataWithOffsetInStructCount<T>(this IntPtr ptr, int Count) where T : unmanaged
|
public static T GetDataWithOffsetInStructCount<T>(this IntPtr ptr, int Count) where T : unmanaged
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using SVM.Core.Utils;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@@ -18,6 +19,24 @@ namespace SVM.Core.stdc
|
|||||||
{
|
{
|
||||||
return Marshal.AllocHGlobal(size);
|
return Marshal.AllocHGlobal(size);
|
||||||
}
|
}
|
||||||
|
public static IntPtr calloc(int size)
|
||||||
|
{
|
||||||
|
var ptr = Marshal.AllocHGlobal(size);
|
||||||
|
for (int i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
System.Runtime.InteropServices.Marshal.WriteByte(ptr, i, 0);
|
||||||
|
}
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
public static IntPtr calloc(uint size)
|
||||||
|
{
|
||||||
|
var ptr = Marshal.AllocHGlobal((int)size);
|
||||||
|
for (int i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
System.Runtime.InteropServices.Marshal.WriteByte(ptr, i, 0);
|
||||||
|
}
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
public static IntPtr realloc(IntPtr ptr, uint size)
|
public static IntPtr realloc(IntPtr ptr, uint size)
|
||||||
{
|
{
|
||||||
return Marshal.ReAllocHGlobal(ptr, (IntPtr)size);
|
return Marshal.ReAllocHGlobal(ptr, (IntPtr)size);
|
||||||
|
|||||||
@@ -1,9 +1,35 @@
|
|||||||
namespace SVM.Standalone;
|
using SVM.Core;
|
||||||
|
|
||||||
|
namespace SVM.Standalone;
|
||||||
|
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
static void Main(string[] args)
|
unsafe static void Main(string[] args)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Hello, World!");
|
string inputFile = "a.out";
|
||||||
|
foreach (string arg in args)
|
||||||
|
{
|
||||||
|
if (File.Exists(arg))
|
||||||
|
{
|
||||||
|
inputFile = arg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!File.Exists(inputFile))
|
||||||
|
{
|
||||||
|
Console.WriteLine("Not input file found!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
using var fs = File.OpenRead(inputFile);
|
||||||
|
var program = SVMProgram.LoadFromStream(fs);
|
||||||
|
SimpleVirtualMachine svm = new()
|
||||||
|
{
|
||||||
|
Program = program
|
||||||
|
};
|
||||||
|
svm.Init();
|
||||||
|
while (!svm.isReachBinaryEnd())
|
||||||
|
{
|
||||||
|
Console.WriteLine("Step");
|
||||||
|
svm.Step();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
<TargetFramework>net9.0</TargetFramework>
|
<TargetFramework>net9.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user