mirror of
https://github.com/creeperlv/SVM.git
synced 2026-01-10 20:39:54 +00:00
Working on improving ISA Linker.
This commit is contained in:
@@ -115,7 +115,7 @@ LabelConstant InternalLbl
|
||||
{
|
||||
return operationResult;
|
||||
}
|
||||
if (!ISA.InstructionDefinitions.TryGetValue(LexDef.LexMatchedItemId, out var instructionDef))
|
||||
if (!ISA.InstructionDefinitionAliases.TryGetValue(LexDef.LexMatchedItemId, out var instructionDef))
|
||||
{
|
||||
return operationResult;
|
||||
}
|
||||
|
||||
@@ -1,21 +1,72 @@
|
||||
using SVM.Core;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Xml;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace SVM.Assembler.Core
|
||||
{
|
||||
[Serializable]
|
||||
public class ISADefinition
|
||||
{
|
||||
public Dictionary<string, InstructionDefinition> InstructionDefinitions = new Dictionary<string, InstructionDefinition>();
|
||||
public Dictionary<PrimaryInstruction, LinkerFunction> LinkerFunctions = new Dictionary<PrimaryInstruction, LinkerFunction>();
|
||||
public Dictionary<PrimaryInstruction, InstructionDefinition> InstructionDefinitions = new Dictionary<PrimaryInstruction, InstructionDefinition>();
|
||||
[NonSerialized]
|
||||
public Dictionary<string, InstructionDefinition> InstructionDefinitionAliases = new Dictionary<string, InstructionDefinition>();
|
||||
public void Init()
|
||||
{
|
||||
foreach (var item in InstructionDefinitions)
|
||||
{
|
||||
if (!LinkerFunctions.TryAdd(item.Value.PrimaryInstruction, item.Value.linkerFunction))
|
||||
foreach (var alias in item.Value.aliases)
|
||||
{
|
||||
LinkerFunctions[item.Value.PrimaryInstruction] = item.Value.linkerFunction;
|
||||
if (!InstructionDefinitionAliases.TryAdd(alias, item.Value))
|
||||
{
|
||||
InstructionDefinitionAliases[alias] = item.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
static void PrintDepth(int depth)
|
||||
{
|
||||
for (int i = 0; i < depth; i++)
|
||||
{
|
||||
Console.Write("\t");
|
||||
}
|
||||
}
|
||||
static void ShowNode(XmlNode node, int depth = 0)
|
||||
{
|
||||
PrintDepth(depth);
|
||||
Console.WriteLine($"[+]{node.NodeType}:{node.Name}");
|
||||
foreach (XmlAttribute item in node.Attributes)
|
||||
{
|
||||
PrintDepth(depth + 1);
|
||||
Console.WriteLine($"[i]{item.NodeType}:{item.Name}={item.InnerText}");
|
||||
|
||||
}
|
||||
foreach (XmlElement item in node.ChildNodes)
|
||||
{
|
||||
if (item is XmlNode cnode)
|
||||
{
|
||||
ShowNode(cnode, depth + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
PrintDepth(depth + 1);
|
||||
Console.Write($"[?]{item.NodeType}:{item.Name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
public static bool TryParse(Stream inputStream, [MaybeNullWhen(false)] out ISADefinition definition)
|
||||
{
|
||||
XmlDocument xmlDocument = new XmlDocument();
|
||||
xmlDocument.Load(inputStream);
|
||||
foreach (XmlNode item in xmlDocument.ChildNodes)
|
||||
{
|
||||
ShowNode(item, 0);
|
||||
}
|
||||
definition = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,15 +7,8 @@ namespace SVM.Assembler.Core
|
||||
[Serializable]
|
||||
public class InstructionDefinition
|
||||
{
|
||||
public string MatchID;
|
||||
public PrimaryInstruction PrimaryInstruction;
|
||||
public LinkerFunction linkerFunction;
|
||||
public InstructionDefinition(string matchID, LinkerFunction assemblerFunction)
|
||||
{
|
||||
MatchID = matchID;
|
||||
this.linkerFunction = assemblerFunction;
|
||||
}
|
||||
|
||||
public PrimaryInstruction PrimaryInstruction = PrimaryInstruction.Nop;
|
||||
public List<string> aliases = new List<string>();
|
||||
public List<InstructionParameter> ParameterPattern = new List<InstructionParameter>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,20 @@
|
||||
using System.Collections.Generic;
|
||||
using SVM.Core;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SVM.Assembler.Core
|
||||
{
|
||||
[Serializable]
|
||||
public class InstructionParameter
|
||||
{
|
||||
public List<string> AllowedTokenIds = new List<string>();
|
||||
public ExpectdValue ExpectdValue = new ExpectdValue();
|
||||
}
|
||||
[Serializable]
|
||||
public class ExpectdValue
|
||||
{
|
||||
public SVMNativeTypes Type;
|
||||
public int Pos;
|
||||
public string Converter = "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,6 +37,12 @@ namespace SVM.Assembler.Core
|
||||
}
|
||||
return operationResult;
|
||||
}
|
||||
public static OperationResult<SVMInstruction> translate(InstructionDefinition def, LinkingContext context, IntermediateInstruction iinstruction)
|
||||
{
|
||||
OperationResult<SVMInstruction> result = new OperationResult<SVMInstruction>();
|
||||
|
||||
return result;
|
||||
}
|
||||
public unsafe static OperationResult<ManagedSVMProgram?> Finialize(ISADefinition definition, IntermediateObject Obj)
|
||||
{
|
||||
OperationResult<ManagedSVMProgram?> operationResult = new OperationResult<ManagedSVMProgram?>(null);
|
||||
@@ -60,9 +66,10 @@ namespace SVM.Assembler.Core
|
||||
}
|
||||
foreach (var item in Obj.instructions)
|
||||
{
|
||||
if (definition.LinkerFunctions.TryGetValue(item.inst, out var func))
|
||||
if (definition.InstructionDefinitions.TryGetValue(item.inst, out var def))
|
||||
{
|
||||
var inst = func(context, item);
|
||||
|
||||
var inst = translate(def, context, item);
|
||||
if (operationResult.CheckAndInheritErrorAndWarnings(inst))
|
||||
{
|
||||
return operationResult;
|
||||
|
||||
46
src/SVM.Assembler/ISA.xml
Normal file
46
src/SVM.Assembler/ISA.xml
Normal file
@@ -0,0 +1,46 @@
|
||||
<ISARoot>
|
||||
<Enums>
|
||||
</Enums>
|
||||
<InstructionDefinition PrimaryInstruction="BMath">
|
||||
<aliases>
|
||||
<alias name="bmath"/>
|
||||
<alias name="b.math"/>
|
||||
<alias name="math.b"/>
|
||||
</aliases>
|
||||
<Parameters>
|
||||
<InstructionParameter>
|
||||
<AllowedIds>
|
||||
<Text content="String"/>
|
||||
</AllowedIds>
|
||||
<ExpectedValue Type="Byte" Pos="1" Converter="InternalEnum:bOp" />
|
||||
</InstructionParameter>
|
||||
<InstructionParameter>
|
||||
<AllowedIds>
|
||||
<Text content="String"/>
|
||||
</AllowedIds>
|
||||
<ExpectedValue Type="Byte" Pos="2" Converter="InternalEnum:NativeType" />
|
||||
</InstructionParameter>
|
||||
<InstructionParameter>
|
||||
<AllowedIds>
|
||||
<Text content="String"/>
|
||||
<Text content="Register"/>
|
||||
</AllowedIds>
|
||||
<ExpectedValue Type="Byte" Pos="3" Converter="Register" />
|
||||
</InstructionParameter>
|
||||
<InstructionParameter>
|
||||
<AllowedIds>
|
||||
<Text content="String"/>
|
||||
<Text content="Register"/>
|
||||
</AllowedIds>
|
||||
<ExpectedValue Type="Byte" Pos="4" Converter="Register" />
|
||||
</InstructionParameter>
|
||||
<InstructionParameter>
|
||||
<AllowedIds>
|
||||
<Text content="String"/>
|
||||
<Text content="Register"/>
|
||||
</AllowedIds>
|
||||
<ExpectedValue Type="Byte" Pos="5" Converter="Register" />
|
||||
</InstructionParameter>
|
||||
</Parameters>
|
||||
</InstructionDefinition>
|
||||
</ISARoot>
|
||||
25
src/SVM.Assembler/Program.cs
Normal file
25
src/SVM.Assembler/Program.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using SVM.Assembler.Core;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace SVM.Assembler
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var fs = Assembly.GetExecutingAssembly().GetManifestResourceStream("SVM.Assembler.ISA.xml");
|
||||
if (fs is null)
|
||||
{
|
||||
Console.WriteLine("Cannot find ISA definition!");
|
||||
return;
|
||||
}
|
||||
if(!ISADefinition.TryParse(fs, out var def))
|
||||
{
|
||||
Console.WriteLine("Cannot load ISA definition!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
24
src/SVM.Assembler/SVM.Assembler.csproj
Normal file
24
src/SVM.Assembler/SVM.Assembler.csproj
Normal file
@@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<PublishAot>true</PublishAot>
|
||||
<InvariantGlobalization>true</InvariantGlobalization>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="ISA.xml" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SVM.Assembler.Core\SVM.Assembler.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="ISA.xml" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -2,9 +2,14 @@
|
||||
{
|
||||
public enum PrimaryInstruction : byte
|
||||
{
|
||||
// 0 1 2 3 4 5 6
|
||||
// Math [I]Op [I]Type [R]L [R]R [R]T [I]CheckOF
|
||||
Nop,
|
||||
// 0 1 2 3 4 5
|
||||
// Math [I]Op [I]Type [R]L [R]R [R]T
|
||||
BMath,
|
||||
/// <summary>
|
||||
/// Checked Binary Math
|
||||
/// </summary>
|
||||
CBMath,
|
||||
// 0 1 2 3 4
|
||||
// Math [I]Op [I]Type [R]L [R]T
|
||||
UMath,
|
||||
|
||||
@@ -79,20 +79,48 @@ namespace SVM.Core
|
||||
var L = Instruction.GetData<byte>(3);
|
||||
var R = Instruction.GetData<byte>(4);
|
||||
var T = Instruction.GetData<byte>(5);
|
||||
var Of = Instruction.GetData<byte>(6);
|
||||
switch (Op)
|
||||
{
|
||||
case BMathOp.Add:
|
||||
MathImpl.MathAdd(registers, statePtr, NativeType, L, R, T, Of == 1);
|
||||
MathImpl.MathAdd(registers, statePtr, NativeType, L, R, T, false);
|
||||
break;
|
||||
case BMathOp.Sub:
|
||||
MathImpl.MathSub(registers, statePtr, NativeType, L, R, T, Of == 1);
|
||||
MathImpl.MathSub(registers, statePtr, NativeType, L, R, T, false);
|
||||
break;
|
||||
case BMathOp.Mul:
|
||||
MathImpl.MathMul(registers, statePtr, NativeType, L, R, T, Of == 1);
|
||||
MathImpl.MathMul(registers, statePtr, NativeType, L, R, T, false);
|
||||
break;
|
||||
case BMathOp.Div:
|
||||
MathImpl.MathDiv(registers, statePtr, NativeType, L, R, T, Of == 1);
|
||||
MathImpl.MathDiv(registers, statePtr, NativeType, L, R, T, false);
|
||||
break;
|
||||
case BMathOp.Mod:
|
||||
MathImpl.MathMod(registers, statePtr, NativeType, L, R, T);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case PrimaryInstruction.CBMath:
|
||||
{
|
||||
var Op = Instruction.GetData<BMathOp>(1);
|
||||
var NativeType = Instruction.GetData<SVMNativeTypes>(2);
|
||||
var L = Instruction.GetData<byte>(3);
|
||||
var R = Instruction.GetData<byte>(4);
|
||||
var T = Instruction.GetData<byte>(5);
|
||||
switch (Op)
|
||||
{
|
||||
case BMathOp.Add:
|
||||
MathImpl.MathAdd(registers, statePtr, NativeType, L, R, T, true);
|
||||
break;
|
||||
case BMathOp.Sub:
|
||||
MathImpl.MathSub(registers, statePtr, NativeType, L, R, T, true);
|
||||
break;
|
||||
case BMathOp.Mul:
|
||||
MathImpl.MathMul(registers, statePtr, NativeType, L, R, T, true);
|
||||
break;
|
||||
case BMathOp.Div:
|
||||
MathImpl.MathDiv(registers, statePtr, NativeType, L, R, T, true);
|
||||
break;
|
||||
case BMathOp.Mod:
|
||||
MathImpl.MathMod(registers, statePtr, NativeType, L, R, T);
|
||||
|
||||
14
src/SVM.sln
14
src/SVM.sln
@@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SVM.Standalone", "SVM.Stand
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SVM.Assembler.Core", "SVM.Assembler.Core\SVM.Assembler.Core.csproj", "{B51D33D7-920B-44DE-8EB8-F8E8DFAE4CA8}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SVM.Assembler", "SVM.Assembler\SVM.Assembler.csproj", "{E2B82455-3947-4C11-82D0-178E8D8C3D64}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -55,6 +57,18 @@ Global
|
||||
{B51D33D7-920B-44DE-8EB8-F8E8DFAE4CA8}.Release|x64.Build.0 = Release|Any CPU
|
||||
{B51D33D7-920B-44DE-8EB8-F8E8DFAE4CA8}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{B51D33D7-920B-44DE-8EB8-F8E8DFAE4CA8}.Release|x86.Build.0 = Release|Any CPU
|
||||
{E2B82455-3947-4C11-82D0-178E8D8C3D64}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{E2B82455-3947-4C11-82D0-178E8D8C3D64}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{E2B82455-3947-4C11-82D0-178E8D8C3D64}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{E2B82455-3947-4C11-82D0-178E8D8C3D64}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{E2B82455-3947-4C11-82D0-178E8D8C3D64}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{E2B82455-3947-4C11-82D0-178E8D8C3D64}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{E2B82455-3947-4C11-82D0-178E8D8C3D64}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{E2B82455-3947-4C11-82D0-178E8D8C3D64}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{E2B82455-3947-4C11-82D0-178E8D8C3D64}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{E2B82455-3947-4C11-82D0-178E8D8C3D64}.Release|x64.Build.0 = Release|Any CPU
|
||||
{E2B82455-3947-4C11-82D0-178E8D8C3D64}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{E2B82455-3947-4C11-82D0-178E8D8C3D64}.Release|x86.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
Reference in New Issue
Block a user