From 29407c690b599a05c73d98c432ff8a251e6b7813 Mon Sep 17 00:00:00 2001 From: Creeper Lv Date: Wed, 30 Jul 2025 01:24:02 +1000 Subject: [PATCH] Working on making Linker to support multiple instruction data as one instruction. --- src/SVM.Assembler.Core/ISADefinition.cs | 8 ++ .../InstructionDefinition.cs | 1 + src/SVM.Assembler.Core/Linker.cs | 74 ++++++++++++++++--- src/SVM.Assembler/ISA.xml | 14 ++++ 4 files changed, 86 insertions(+), 11 deletions(-) diff --git a/src/SVM.Assembler.Core/ISADefinition.cs b/src/SVM.Assembler.Core/ISADefinition.cs index 0863be3..f4beeaf 100644 --- a/src/SVM.Assembler.Core/ISADefinition.cs +++ b/src/SVM.Assembler.Core/ISADefinition.cs @@ -116,7 +116,15 @@ namespace SVM.Assembler.Core Trace.WriteLine($"ParseDefinition:{node.Name}"); InstructionDefinition instDefinition = new InstructionDefinition(); var PIAttr = node.Attributes.GetNamedItem("PrimaryInstruction"); + var ICAttr = node.Attributes.GetNamedItem("InstructionCount"); if (PIAttr == null) return false; + if (ICAttr != null) + { + if (int.TryParse(ICAttr.InnerText, out var ic)) + { + instDefinition.InstructionCount = ic; + } + } if (!Enum.TryParse(PIAttr.InnerText, out var pi)) { return false; diff --git a/src/SVM.Assembler.Core/InstructionDefinition.cs b/src/SVM.Assembler.Core/InstructionDefinition.cs index b78b4a9..fb7d43c 100644 --- a/src/SVM.Assembler.Core/InstructionDefinition.cs +++ b/src/SVM.Assembler.Core/InstructionDefinition.cs @@ -8,6 +8,7 @@ namespace SVM.Assembler.Core public class InstructionDefinition { public PrimaryInstruction PrimaryInstruction = PrimaryInstruction.Nop; + public int InstructionCount = 1; public List Aliases = new List(); public List ParameterPattern = new List(); } diff --git a/src/SVM.Assembler.Core/Linker.cs b/src/SVM.Assembler.Core/Linker.cs index 3d09c11..b9e1ffc 100644 --- a/src/SVM.Assembler.Core/Linker.cs +++ b/src/SVM.Assembler.Core/Linker.cs @@ -64,6 +64,38 @@ namespace SVM.Assembler.Core registerID = byte.MaxValue; return false; } + public static bool TryParseInt32(string input, LinkingContext context, out int value) + { + if (int.TryParse(input, out value)) + { + return true; + } + else + { + if (context.IntermediateObject.TryGetConst(input, out var realStr)) + { + return TryParseInt32(realStr, context, out value); + } + } + value = byte.MaxValue; + return false; + } + public static bool TryParseInt64(string input, LinkingContext context, out long value) + { + if (long.TryParse(input, out value)) + { + return true; + } + else + { + if (context.IntermediateObject.TryGetConst(input, out var realStr)) + { + return TryParseInt64(realStr, context, out value); + } + } + value = byte.MaxValue; + return false; + } public unsafe static void WriteData(SVMInstruction* inst, SVMNativeTypes nativeType, int Pos, byte* dataStart) { var size = nativeType switch @@ -227,10 +259,10 @@ namespace SVM.Assembler.Core } return false; } - public unsafe static OperationResult translate(InstructionDefinition def, LinkingContext context, IntermediateInstruction iinstruction) + public unsafe static OperationResult translate(InstructionDefinition def, LinkingContext context, IntermediateInstruction iinstruction, SVMInstruction* instruction) { - OperationResult result = new OperationResult(); - SVMInstruction instruction = new SVMInstruction(); + OperationResult result = new OperationResult(false); + //SVMInstruction instruction = new SVMInstruction(); for (int i = 0; i < iinstruction.Parameters.Count; i++) { var para = iinstruction.Parameters[i]; @@ -243,31 +275,42 @@ namespace SVM.Assembler.Core if (converter.StartsWith("InternalEnum:")) { var enumName = converter["InternalEnum:".Length..]; - ProcessInternalEnum(enumName, para.Content, paraDef, context, &instruction); + ProcessInternalEnum(enumName, para.Content, paraDef, context, instruction); } else if (converter.StartsWith("Enum:")) { var enumName = converter["Enum:".Length..]; - ProcessInternalEnum(enumName, para.Content, paraDef, context, &instruction); + ProcessInternalEnum(enumName, para.Content, paraDef, context, instruction); } else { switch (converter) { case "Register": - if (!TryParseRegister(para.Content, context, out var registerID)) { - return result; + if (!TryParseRegister(para.Content, context, out var registerID)) + { + return result; + } + WriteData(instruction, paraDef.ExpectdValue.Type, paraDef.ExpectdValue.Pos, ®isterID); + } + break; + case "Integer32": + { + if (!TryParseInt32(para.Content, context, out var registerID)) + { + return result; + } + WriteData(instruction, paraDef.ExpectdValue.Type, paraDef.ExpectdValue.Pos, (byte*)®isterID); } - WriteData(&instruction, paraDef.ExpectdValue.Type, paraDef.ExpectdValue.Pos, ®isterID); break; default: break; } } } - result.Result = instruction; + result.Result = true; return result; } public unsafe static OperationResult Finialize(ISADefinition definition, IntermediateObject Obj) @@ -296,12 +339,21 @@ namespace SVM.Assembler.Core if (definition.InstructionDefinitions.TryGetValue(item.inst, out var def)) { - var inst = translate(def, context, item); + var instruction = stackalloc SVMInstruction[def.InstructionCount]; + var inst = translate(def, context, item, instruction); if (operationResult.CheckAndInheritErrorAndWarnings(inst)) { return operationResult; } - program.instructions.Add(inst.Result); + if (inst.Result) + { + for (int i = 0; i < def.InstructionCount; i++) + { + + program.instructions.Add(instruction[i]); + } + + } } } program.Datas = new byte[offset]; diff --git a/src/SVM.Assembler/ISA.xml b/src/SVM.Assembler/ISA.xml index 8f3d398..54f492e 100644 --- a/src/SVM.Assembler/ISA.xml +++ b/src/SVM.Assembler/ISA.xml @@ -51,5 +51,19 @@ + + + + + + + + + + + + + + \ No newline at end of file