diff --git a/src/SVM.Assembler.Core/Assembler.cs b/src/SVM.Assembler.Core/Assembler.cs new file mode 100644 index 0000000..a9d0c17 --- /dev/null +++ b/src/SVM.Assembler.Core/Assembler.cs @@ -0,0 +1,52 @@ +using LibCLCC.NET.Lexer; +using System; + +namespace SVM.Assembler.Core +{ + public class Assembler + { + public const string LexDefinition = +@" +Match: + +D [0-9] +Number {D}+ +InstMath math +InstCvt cvt +InstSystem system +InstSys sys +InstSD sd +Register \${D}+ +LabelCode \.code\: +LabelData \.data\: +string "".*"" +LineEnd \n + +OpAdd add +OpSub sub +OpMul mul +OpDiv div + +Id: + +InstMath inst +InstCvt inst +InstSystem inst +InstSys inst +InstSD sd +string String +Number Number +Register Register +OpAdd BOp +OpSub BOp +OpMul BOp +OpDiv BOp +LineEnd LE +"; + LexerDefinition? definition; + public Assembler() + { + LexerDefinition.TryParse(LexDefinition, out definition); + } + } +} diff --git a/src/SVM.Assembler.Core/SVM.Assembler.Core.csproj b/src/SVM.Assembler.Core/SVM.Assembler.Core.csproj new file mode 100644 index 0000000..b4b43f4 --- /dev/null +++ b/src/SVM.Assembler.Core/SVM.Assembler.Core.csproj @@ -0,0 +1,8 @@ + + + + netstandard2.1 + enable + + + diff --git a/src/SVM.Core/Data/CompactByte.cs b/src/SVM.Core/Data/CompactByte.cs index 260d5d9..c24b26f 100644 --- a/src/SVM.Core/Data/CompactByte.cs +++ b/src/SVM.Core/Data/CompactByte.cs @@ -31,7 +31,7 @@ namespace SVM.Core.Data { return new CompactByte(Value / R.Value); } - public SCVMSimpleResult AddOF(CompactByte R) + public SVMSimpleResult AddOF(CompactByte R) { CompactByte result = default; bool IsOF = false; @@ -50,10 +50,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult SubOF(CompactByte R) + public SVMSimpleResult SubOF(CompactByte R) { CompactByte result = default; bool IsOF = false; @@ -72,10 +72,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult DivOF(CompactByte R) + public SVMSimpleResult DivOF(CompactByte R) { CompactByte result = default; bool IsOF = false; @@ -94,10 +94,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult MulOF(CompactByte R) + public SVMSimpleResult MulOF(CompactByte R) { CompactByte result = default; bool IsOF = false; @@ -116,7 +116,7 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } public bool LT(CompactByte R) diff --git a/src/SVM.Core/Data/CompactDouble.cs b/src/SVM.Core/Data/CompactDouble.cs index 65aaf5b..afd00af 100644 --- a/src/SVM.Core/Data/CompactDouble.cs +++ b/src/SVM.Core/Data/CompactDouble.cs @@ -30,7 +30,7 @@ namespace SVM.Core.Data return new CompactDouble(Value / R.Value); } - public SCVMSimpleResult AddOF(CompactDouble R) + public SVMSimpleResult AddOF(CompactDouble R) { CompactDouble result = default; bool IsOF = false; @@ -49,10 +49,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult SubOF(CompactDouble R) + public SVMSimpleResult SubOF(CompactDouble R) { CompactDouble result = default; bool IsOF = false; @@ -71,10 +71,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult DivOF(CompactDouble R) + public SVMSimpleResult DivOF(CompactDouble R) { CompactDouble result = default; bool IsOF = false; @@ -93,10 +93,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult MulOF(CompactDouble R) + public SVMSimpleResult MulOF(CompactDouble R) { CompactDouble result = default; bool IsOF = false; @@ -115,7 +115,7 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } public bool LT(CompactDouble R) diff --git a/src/SVM.Core/Data/CompactInt.cs b/src/SVM.Core/Data/CompactInt.cs index d24b2f7..f5abd51 100644 --- a/src/SVM.Core/Data/CompactInt.cs +++ b/src/SVM.Core/Data/CompactInt.cs @@ -31,7 +31,7 @@ namespace SVM.Core.Data return new CompactInt(Value / R.Value); } - public SCVMSimpleResult AddOF(CompactInt R) + public SVMSimpleResult AddOF(CompactInt R) { CompactInt result = default; bool IsOF = false; @@ -50,10 +50,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult SubOF(CompactInt R) + public SVMSimpleResult SubOF(CompactInt R) { CompactInt result = default; bool IsOF = false; @@ -72,10 +72,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult DivOF(CompactInt R) + public SVMSimpleResult DivOF(CompactInt R) { CompactInt result = default; bool IsOF = false; @@ -94,10 +94,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult MulOF(CompactInt R) + public SVMSimpleResult MulOF(CompactInt R) { CompactInt result = default; bool IsOF = false; @@ -116,7 +116,7 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } public bool LT(CompactInt R) diff --git a/src/SVM.Core/Data/CompactLong.cs b/src/SVM.Core/Data/CompactLong.cs index 8d29c47..6f68ded 100644 --- a/src/SVM.Core/Data/CompactLong.cs +++ b/src/SVM.Core/Data/CompactLong.cs @@ -29,7 +29,7 @@ namespace SVM.Core.Data { return new CompactLong(Value / R.Value); } - public SCVMSimpleResult AddOF(CompactLong R) + public SVMSimpleResult AddOF(CompactLong R) { CompactLong result = default; bool IsOF = false; @@ -48,10 +48,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult SubOF(CompactLong R) + public SVMSimpleResult SubOF(CompactLong R) { CompactLong result = default; bool IsOF = false; @@ -70,10 +70,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult DivOF(CompactLong R) + public SVMSimpleResult DivOF(CompactLong R) { CompactLong result = default; bool IsOF = false; @@ -92,10 +92,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult MulOF(CompactLong R) + public SVMSimpleResult MulOF(CompactLong R) { CompactLong result = default; bool IsOF = false; @@ -114,7 +114,7 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } public bool LT(CompactLong R) diff --git a/src/SVM.Core/Data/CompactSByte.cs b/src/SVM.Core/Data/CompactSByte.cs index 5f75380..25d067e 100644 --- a/src/SVM.Core/Data/CompactSByte.cs +++ b/src/SVM.Core/Data/CompactSByte.cs @@ -30,7 +30,7 @@ namespace SVM.Core.Data { return new CompactSByte(Value / R.Value); } - public SCVMSimpleResult AddOF(CompactSByte R) + public SVMSimpleResult AddOF(CompactSByte R) { CompactSByte result = default; bool IsOF = false; @@ -49,10 +49,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult SubOF(CompactSByte R) + public SVMSimpleResult SubOF(CompactSByte R) { CompactSByte result = default; bool IsOF = false; @@ -71,10 +71,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult DivOF(CompactSByte R) + public SVMSimpleResult DivOF(CompactSByte R) { CompactSByte result = default; bool IsOF = false; @@ -93,10 +93,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult MulOF(CompactSByte R) + public SVMSimpleResult MulOF(CompactSByte R) { CompactSByte result = default; bool IsOF = false; @@ -115,7 +115,7 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } public bool LT(CompactSByte R) diff --git a/src/SVM.Core/Data/CompactShort.cs b/src/SVM.Core/Data/CompactShort.cs index 333b9de..347ae09 100644 --- a/src/SVM.Core/Data/CompactShort.cs +++ b/src/SVM.Core/Data/CompactShort.cs @@ -30,7 +30,7 @@ namespace SVM.Core.Data { return new CompactShort(Value / R.Value); } - public SCVMSimpleResult AddOF(CompactShort R) + public SVMSimpleResult AddOF(CompactShort R) { CompactShort result = default; bool IsOF = false; @@ -49,10 +49,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult SubOF(CompactShort R) + public SVMSimpleResult SubOF(CompactShort R) { CompactShort result = default; bool IsOF = false; @@ -71,10 +71,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult DivOF(CompactShort R) + public SVMSimpleResult DivOF(CompactShort R) { CompactShort result = default; bool IsOF = false; @@ -93,10 +93,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult MulOF(CompactShort R) + public SVMSimpleResult MulOF(CompactShort R) { CompactShort result = default; bool IsOF = false; @@ -115,7 +115,7 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } public bool LT(CompactShort R) diff --git a/src/SVM.Core/Data/CompactSingle.cs b/src/SVM.Core/Data/CompactSingle.cs index 6766145..0a92611 100644 --- a/src/SVM.Core/Data/CompactSingle.cs +++ b/src/SVM.Core/Data/CompactSingle.cs @@ -29,7 +29,7 @@ namespace SVM.Core.Data { return new CompactSingle(Value / R.Value); } - public SCVMSimpleResult AddOF(CompactSingle R) + public SVMSimpleResult AddOF(CompactSingle R) { CompactSingle result = default; bool IsOF = false; @@ -48,10 +48,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult SubOF(CompactSingle R) + public SVMSimpleResult SubOF(CompactSingle R) { CompactSingle result = default; bool IsOF = false; @@ -70,10 +70,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult DivOF(CompactSingle R) + public SVMSimpleResult DivOF(CompactSingle R) { CompactSingle result = default; bool IsOF = false; @@ -92,10 +92,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult MulOF(CompactSingle R) + public SVMSimpleResult MulOF(CompactSingle R) { CompactSingle result = default; bool IsOF = false; @@ -114,7 +114,7 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } public bool LT(CompactSingle R) diff --git a/src/SVM.Core/Data/CompactUInt.cs b/src/SVM.Core/Data/CompactUInt.cs index f0520fd..9cb24cb 100644 --- a/src/SVM.Core/Data/CompactUInt.cs +++ b/src/SVM.Core/Data/CompactUInt.cs @@ -29,7 +29,7 @@ namespace SVM.Core.Data { return new CompactUInt(Value / R.Value); } - public SCVMSimpleResult AddOF(CompactUInt R) + public SVMSimpleResult AddOF(CompactUInt R) { CompactUInt result = default; bool IsOF = false; @@ -48,10 +48,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult SubOF(CompactUInt R) + public SVMSimpleResult SubOF(CompactUInt R) { CompactUInt result = default; bool IsOF = false; @@ -70,10 +70,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult DivOF(CompactUInt R) + public SVMSimpleResult DivOF(CompactUInt R) { CompactUInt result = default; bool IsOF = false; @@ -92,10 +92,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult MulOF(CompactUInt R) + public SVMSimpleResult MulOF(CompactUInt R) { CompactUInt result = default; bool IsOF = false; @@ -114,7 +114,7 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } public bool LT(CompactUInt R) diff --git a/src/SVM.Core/Data/CompactULong.cs b/src/SVM.Core/Data/CompactULong.cs index f069f32..c6e1beb 100644 --- a/src/SVM.Core/Data/CompactULong.cs +++ b/src/SVM.Core/Data/CompactULong.cs @@ -29,7 +29,7 @@ namespace SVM.Core.Data { return new CompactULong(Value / R.Value); } - public SCVMSimpleResult AddOF(CompactULong R) + public SVMSimpleResult AddOF(CompactULong R) { CompactULong result = default; bool IsOF = false; @@ -48,10 +48,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult SubOF(CompactULong R) + public SVMSimpleResult SubOF(CompactULong R) { CompactULong result = default; bool IsOF = false; @@ -70,10 +70,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult DivOF(CompactULong R) + public SVMSimpleResult DivOF(CompactULong R) { CompactULong result = default; bool IsOF = false; @@ -92,10 +92,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult MulOF(CompactULong R) + public SVMSimpleResult MulOF(CompactULong R) { CompactULong result = default; bool IsOF = false; @@ -114,7 +114,7 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } public bool LT(CompactULong R) diff --git a/src/SVM.Core/Data/CompactUShort.cs b/src/SVM.Core/Data/CompactUShort.cs index a37a885..88e5730 100644 --- a/src/SVM.Core/Data/CompactUShort.cs +++ b/src/SVM.Core/Data/CompactUShort.cs @@ -30,7 +30,7 @@ namespace SVM.Core.Data { return new CompactUShort(Value / R.Value); } - public SCVMSimpleResult AddOF(CompactUShort R) + public SVMSimpleResult AddOF(CompactUShort R) { CompactUShort result = default; bool IsOF = false; @@ -49,10 +49,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult SubOF(CompactUShort R) + public SVMSimpleResult SubOF(CompactUShort R) { CompactUShort result = default; bool IsOF = false; @@ -71,10 +71,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult DivOF(CompactUShort R) + public SVMSimpleResult DivOF(CompactUShort R) { CompactUShort result = default; bool IsOF = false; @@ -93,10 +93,10 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } - public SCVMSimpleResult MulOF(CompactUShort R) + public SVMSimpleResult MulOF(CompactUShort R) { CompactUShort result = default; bool IsOF = false; @@ -115,7 +115,7 @@ namespace SVM.Core.Data } } } - return new SCVMSimpleResult(IsOF, result); + return new SVMSimpleResult(IsOF, result); } public bool LT(CompactUShort R) diff --git a/src/SVM.Core/Data/INumberData.cs b/src/SVM.Core/Data/INumberData.cs index 330d6b2..76863a8 100644 --- a/src/SVM.Core/Data/INumberData.cs +++ b/src/SVM.Core/Data/INumberData.cs @@ -5,7 +5,7 @@ using System.Text; namespace SVM.Core.Data { - public interface INumbericData : IPointerWritable where T : unmanaged + public interface INumbericData : IPointerWritable, ICastable where T : unmanaged { [MethodImpl(MethodImplOptions.AggressiveInlining)] T Add(T R); @@ -19,13 +19,13 @@ namespace SVM.Core.Data T Mod(T R); [MethodImpl(MethodImplOptions.AggressiveInlining)] - SCVMSimpleResult AddOF(T R); + SVMSimpleResult AddOF(T R); [MethodImpl(MethodImplOptions.AggressiveInlining)] - SCVMSimpleResult SubOF(T R); + SVMSimpleResult SubOF(T R); [MethodImpl(MethodImplOptions.AggressiveInlining)] - SCVMSimpleResult DivOF(T R); + SVMSimpleResult DivOF(T R); [MethodImpl(MethodImplOptions.AggressiveInlining)] - SCVMSimpleResult MulOF(T R); + SVMSimpleResult MulOF(T R); [MethodImpl(MethodImplOptions.AggressiveInlining)] bool LT(T R); [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -38,6 +38,9 @@ namespace SVM.Core.Data bool EQ(T R); [MethodImpl(MethodImplOptions.AggressiveInlining)] bool NE(T R); + } + public interface ICastable + { INumbericData Cast_Byte(); INumbericData Cast_SByte(); INumbericData Cast_Short(); @@ -49,17 +52,18 @@ namespace SVM.Core.Data INumbericData Cast_Double(); INumbericData Cast_Float(); } + public unsafe interface IPointerWritable { public void Write(byte* targetPtr); public int SizeOf(); } - public struct SCVMSimpleResult where T : unmanaged + public struct SVMSimpleResult where T : unmanaged { public bool IsSuccess; public T Value; - public SCVMSimpleResult(bool isSuccess, T value) + public SVMSimpleResult(bool isSuccess, T value) { IsSuccess = isSuccess; Value = value; diff --git a/src/SVM.Core/DebugSymbol.cs b/src/SVM.Core/DebugSymbol.cs index a608ba3..b44eb77 100644 --- a/src/SVM.Core/DebugSymbol.cs +++ b/src/SVM.Core/DebugSymbol.cs @@ -7,9 +7,9 @@ namespace SVM.Core { public Dictionary Functions = new Dictionary(); } - public class RuntimeBinary + public unsafe class RuntimeBinary { - public uint offset; + public SVMProgram* program; public SimpleVirtualMachine? BindedMachine; public DebugSymbol? Symbol; public bool Invoke(string funcName) diff --git a/src/SVM.Core/Registers.cs b/src/SVM.Core/Registers.cs index 3f68c18..7b9625b 100644 --- a/src/SVM.Core/Registers.cs +++ b/src/SVM.Core/Registers.cs @@ -27,6 +27,11 @@ namespace SVM.Core if (RegisterID == 0) return default; return Data.GetDataWithOffsetInBytes(RegisterID * sizeof(Int64)); } + public unsafe byte* GetPtr(int RegisterID) + { + if (RegisterID == 0) return null; + return ((byte*)Data) + RegisterID * sizeof(long); + } public unsafe void SetData(int offset, T d) where T : unmanaged { if (offset == 0) return; diff --git a/src/SVM.Core/SVMInstDef.cs b/src/SVM.Core/SVMInstDef.cs index 550d507..a66407c 100644 --- a/src/SVM.Core/SVMInstDef.cs +++ b/src/SVM.Core/SVMInstDef.cs @@ -72,7 +72,13 @@ } public enum ConditionFlag { - Cmp, + /// + /// Compare Flag + /// + CF, + /// + /// Overflow Flag + /// Of, } public enum SIMDOperator : byte diff --git a/src/SVM.Core/SVMProgram.cs b/src/SVM.Core/SVMProgram.cs new file mode 100644 index 0000000..4e5b027 --- /dev/null +++ b/src/SVM.Core/SVMProgram.cs @@ -0,0 +1,53 @@ +using SVM.Core.Utils; +using System; +using System.IO; +using static SVM.Core.stdc.stdlib; +namespace SVM.Core +{ + public unsafe struct SVMProgram : IDisposable + { + public UInt64 InstructionCount; + public UInt64 DataSize; + public SVMInstruction* instructions; + public byte* data; + public static SVMProgram* LoadFromStream(Stream stream) + { + if (!stream.TryReadData(out uint dataSectionLength)) + { + return null; + } + if (!stream.TryReadData(out uint codeCount)) + { + return null; + } + var dataPtr = malloc(dataSectionLength); + Span dataBuffer = new Span((byte*)dataPtr, (int)dataSectionLength); + if (stream.Read(dataBuffer) != dataSectionLength) + { + free(dataPtr); + return null; + } + var codeLen = codeCount * sizeof(SVMInstruction); + var codePtr = malloc((uint)codeLen); + Span codeBuffer = new Span((byte*)codePtr, (int)codeLen); + if (stream.Read(codeBuffer) != codeLen) + { + free(dataPtr); + free(codePtr); + return null; + } + var program = (SVMProgram*)malloc(sizeof(SVMProgram)); + program->data = (byte*)dataPtr; + program->DataSize = dataSectionLength; + program->instructions = (SVMInstruction*)codePtr; + program->InstructionCount = codeCount; + return program; + } + + public void Dispose() + { + free((IntPtr)data); + free((IntPtr)instructions); + } + } +} diff --git a/src/SVM.Core/SimpleVirtualMachine.cs b/src/SVM.Core/SimpleVirtualMachine.cs index faf561e..ff6019d 100644 --- a/src/SVM.Core/SimpleVirtualMachine.cs +++ b/src/SVM.Core/SimpleVirtualMachine.cs @@ -1,9 +1,9 @@ -using SVM.Core.FuncImpl; +using SVM.Core.Data; +using SVM.Core.FuncImpl; using SVM.Core.Utils; using System; using System.Collections.Generic; using System.Drawing; -using System.IO; using System.Runtime.InteropServices; using static SVM.Core.stdc.stdlib; namespace SVM.Core @@ -105,6 +105,9 @@ namespace SVM.Core case SVMInstDef.UMath: break; case SVMInstDef.Cvt: + { + Convert(Instruction); + } break; case SVMInstDef.Cmp: break; @@ -148,6 +151,86 @@ namespace SVM.Core PC++; registers.SetData((int)PCOffset, PC); } + + private void Convert(SVMInstruction Instruction) + { + var SType = Instruction.GetData(1); + var TType = Instruction.GetData(2); + var L = Instruction.GetData(3); + var T = Instruction.GetData(4); + ICastable castable; + switch (SType) + { + case SVMNativeTypes.Int8: + castable = registers.GetData(L); + break; + case SVMNativeTypes.Int16: + castable = registers.GetData(L); + break; + case SVMNativeTypes.Int32: + castable = registers.GetData(L); + break; + case SVMNativeTypes.Int64: + castable = registers.GetData(L); + break; + case SVMNativeTypes.UInt8: + castable = registers.GetData(L); + break; + case SVMNativeTypes.UInt16: + castable = registers.GetData(L); + break; + case SVMNativeTypes.UInt32: + castable = registers.GetData(L); + break; + case SVMNativeTypes.UInt64: + castable = registers.GetData(L); + break; + case SVMNativeTypes.Float: + castable = registers.GetData(L); + break; + case SVMNativeTypes.Double: + castable = registers.GetData(L); + break; + default: + return; + } + switch (TType) + { + case SVMNativeTypes.Int8: + castable.Cast_SByte().Write(registers.GetPtr(T)); + break; + case SVMNativeTypes.Int16: + castable.Cast_Short().Write(registers.GetPtr(T)); + break; + case SVMNativeTypes.Int32: + castable.Cast_Int().Write(registers.GetPtr(T)); + break; + case SVMNativeTypes.Int64: + castable.Cast_Long().Write(registers.GetPtr(T)); + break; + case SVMNativeTypes.UInt8: + castable.Cast_Byte().Write(registers.GetPtr(T)); + break; + case SVMNativeTypes.UInt16: + castable.Cast_UShort().Write(registers.GetPtr(T)); + break; + case SVMNativeTypes.UInt32: + castable.Cast_UInt().Write(registers.GetPtr(T)); + break; + case SVMNativeTypes.UInt64: + castable.Cast_ULong().Write(registers.GetPtr(T)); + break; + case SVMNativeTypes.Float: + castable.Cast_Float().Write(registers.GetPtr(T)); + break; + case SVMNativeTypes.Double: + castable.Cast_Double().Write(registers.GetPtr(T)); + break; + default: + break; + } + } + public IntPtr GetPointer(ulong PC) { return GetPointer(new SVMPointer() { offset = (uint)PC, index = 0 }); @@ -229,24 +312,12 @@ namespace SVM.Core /// public uint EIDRegisterID; } - public unsafe struct SVMProgram - { - public UInt64 InstructionCount; - public UInt64 DataSize; - public SVMInstruction* instructions; - public byte* data; - public static SVMProgram* LoadFromStream(Stream stream) - { - var program = (SVMProgram*)malloc(sizeof(SVMProgram)); - - return program; - } - } public delegate void FuncCall(SimpleVirtualMachine machine); [StructLayout(LayoutKind.Sequential)] public struct MState { public byte OF; + public byte CF; } [StructLayout(LayoutKind.Sequential)] public struct MemoryBlock : IDisposable diff --git a/src/SVM.Core/Utils/StreamUtils.cs b/src/SVM.Core/Utils/StreamUtils.cs new file mode 100644 index 0000000..3551274 --- /dev/null +++ b/src/SVM.Core/Utils/StreamUtils.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace SVM.Core.Utils +{ + public static class StreamUtils + { + public unsafe static bool TryReadData(this Stream stream, out T data) where T : unmanaged + { + int len = sizeof(T); + var dataPtr = stackalloc byte[len]; + Span buffer = new Span(dataPtr, len); + var count = stream.Read(buffer); + if (count != len) + { + data = default; + return false; + } + data = ((T*)dataPtr)[0]; + return true; + } + } +}