2025-07-20 20:45:10 +08:00
|
|
|
|
using SVM.Core;
|
2025-07-21 01:47:31 +08:00
|
|
|
|
using System;
|
2025-07-20 20:45:10 +08:00
|
|
|
|
using System.Collections.Generic;
|
2025-07-21 01:47:31 +08:00
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Xml;
|
2025-07-21 04:06:11 +08:00
|
|
|
|
using System.Xml.Linq;
|
2025-07-21 01:47:31 +08:00
|
|
|
|
using System.Xml.Serialization;
|
2025-07-20 20:45:10 +08:00
|
|
|
|
|
|
|
|
|
|
namespace SVM.Assembler.Core
|
|
|
|
|
|
{
|
2025-07-21 01:47:31 +08:00
|
|
|
|
[Serializable]
|
2025-07-20 20:45:10 +08:00
|
|
|
|
public class ISADefinition
|
|
|
|
|
|
{
|
2025-07-21 01:47:31 +08:00
|
|
|
|
public Dictionary<PrimaryInstruction, InstructionDefinition> InstructionDefinitions = new Dictionary<PrimaryInstruction, InstructionDefinition>();
|
|
|
|
|
|
[NonSerialized]
|
|
|
|
|
|
public Dictionary<string, InstructionDefinition> InstructionDefinitionAliases = new Dictionary<string, InstructionDefinition>();
|
2025-07-20 20:45:10 +08:00
|
|
|
|
public void Init()
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var item in InstructionDefinitions)
|
|
|
|
|
|
{
|
2025-07-21 04:06:11 +08:00
|
|
|
|
foreach (var alias in item.Value.Aliases)
|
2025-07-20 20:45:10 +08:00
|
|
|
|
{
|
2025-07-21 01:47:31 +08:00
|
|
|
|
if (!InstructionDefinitionAliases.TryAdd(alias, item.Value))
|
|
|
|
|
|
{
|
|
|
|
|
|
InstructionDefinitionAliases[alias] = item.Value;
|
|
|
|
|
|
}
|
2025-07-20 20:45:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-21 01:47:31 +08:00
|
|
|
|
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}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-21 04:06:11 +08:00
|
|
|
|
static bool ParseDefinition(XmlNode node, ref ISADefinition definition)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
InstructionDefinition instDefinition = new InstructionDefinition();
|
|
|
|
|
|
foreach (XmlNode item in node.ChildNodes)
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (item.Name)
|
|
|
|
|
|
{
|
|
|
|
|
|
case "Aliases":
|
|
|
|
|
|
foreach (XmlNode aliasNode in item.ChildNodes)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (aliasNode.Name == "Alias")
|
|
|
|
|
|
{
|
|
|
|
|
|
instDefinition.Aliases.Add(aliasNode.Attributes["Name"].Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "Parameters":
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (item.Name != "InstructionDefinition")
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
static bool ParseDefinitions(XmlNode node, ref ISADefinition definition)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (XmlNode item in node.ChildNodes)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (item.Name != "InstructionDefinition")
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (ParseDefinition(node, ref definition) == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2025-07-21 01:47:31 +08:00
|
|
|
|
public static bool TryParse(Stream inputStream, [MaybeNullWhen(false)] out ISADefinition definition)
|
|
|
|
|
|
{
|
|
|
|
|
|
XmlDocument xmlDocument = new XmlDocument();
|
|
|
|
|
|
xmlDocument.Load(inputStream);
|
2025-07-21 04:06:11 +08:00
|
|
|
|
ISADefinition isaDefinition = new ISADefinition();
|
2025-07-21 01:47:31 +08:00
|
|
|
|
foreach (XmlNode item in xmlDocument.ChildNodes)
|
|
|
|
|
|
{
|
|
|
|
|
|
ShowNode(item, 0);
|
2025-07-21 04:06:11 +08:00
|
|
|
|
switch (item.Name)
|
|
|
|
|
|
{
|
|
|
|
|
|
case "Enums":
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "Definitions":
|
|
|
|
|
|
if (ParseDefinitions(item, ref isaDefinition) == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
definition = null;
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2025-07-21 01:47:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
definition = null;
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2025-07-20 20:45:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|