Added support of debug symbol.

This commit is contained in:
2025-08-17 06:34:09 +10:00
parent 0069c573e7
commit 7e57c4ded6
3 changed files with 45 additions and 6 deletions

View File

@@ -420,11 +420,12 @@ namespace SVM.Assembler.Core
result.Result = true;
return result;
}
public unsafe static OperationResult<ManagedSVMProgram?> Finialize(ISADefinition definition, IntermediateObject Obj)
public unsafe static OperationResult<(ManagedSVMProgram program, LinkingContext context)?> Finialize(ISADefinition definition, IntermediateObject Obj,bool PreFinalizeLabels)
{
OperationResult<ManagedSVMProgram?> operationResult = new OperationResult<ManagedSVMProgram?>(null);
OperationResult<(ManagedSVMProgram program, LinkingContext context)?> operationResult = new OperationResult<(ManagedSVMProgram program, LinkingContext context)?>(null);
ManagedSVMProgram program = new ManagedSVMProgram();
LinkingContext context = new LinkingContext(program, Obj, definition);
List<byte[]> Data = new List<byte[]>();
uint offset = 0;
foreach (var item in Obj.data)
@@ -481,6 +482,10 @@ namespace SVM.Assembler.Core
Data.Add(data2);
}
}
if (PreFinalizeLabels)
{
context.FinalizeLabels();
}
foreach (var item in Obj.instructions)
{
if (definition.InstructionDefinitions.TryGetValue(item.InstDefID, out var def))
@@ -513,7 +518,7 @@ namespace SVM.Assembler.Core
Buffer.BlockCopy(item, 0, program.Datas, offset2, item.Length);
offset2 += item.Length;
}
operationResult.Result = program;
operationResult.Result = (program, context);
return operationResult;
}
}

View File

@@ -1,4 +1,5 @@
using SVM.Core;
using System;
using System.Collections.Generic;
namespace SVM.Assembler.Core
@@ -26,6 +27,21 @@ namespace SVM.Assembler.Core
}
return false;
}
public void FinalizeLabels()
{
int acc = 0;
for (int i = 0; i < IntermediateObject.instructions.Count; i++)
{
IntermediateInstruction? item = IntermediateObject.instructions[i];
if (item.Label != null)
if (item.Label.Content != null)
{
this.label.Add(item.Label.Content, acc);
}
acc += Definition.InstructionDefinitions[item.InstDefID].InstructionCount;
}
}
public bool TryFindLabel(string label, out int offset)
{
label = label + ":";
@@ -33,7 +49,7 @@ namespace SVM.Assembler.Core
{
return true;
}
int acc=0;
int acc = 0;
for (int i = 0; i < IntermediateObject.instructions.Count; i++)
{
IntermediateInstruction? item = IntermediateObject.instructions[i];

View File

@@ -22,6 +22,7 @@ namespace SVM.Assembler
List<string> files = new List<string>();
List<IntermediateObject> objs = new();
string outputfile = "a.out";
bool outputDebugSymbol = false;
for (int i = 0; i < args.Length; i++)
{
string? item = args[i];
@@ -31,6 +32,9 @@ namespace SVM.Assembler
outputfile = args[i + 1];
i++;
break;
case "-d":
outputDebugSymbol = true;
break;
default:
if (File.Exists(item))
{
@@ -71,7 +75,7 @@ namespace SVM.Assembler
Console.Error.WriteLine("Linker return no data!");
return;
}
var fResult = Linker.Finialize(def, lResult.Result);
var fResult = Linker.Finialize(def, lResult.Result, outputDebugSymbol);
if (fResult.HasError())
{
Console.Error.WriteLine("Finalizer error!");
@@ -88,7 +92,21 @@ namespace SVM.Assembler
}
if (File.Exists(outputfile)) File.Delete(outputfile);
using var stream = File.OpenWrite(outputfile);
fResult.Result.WriteToStream(stream);
if (!fResult.Result.HasValue)
{
return;
}
fResult.Result.Value.program.WriteToStream(stream);
if (outputDebugSymbol)
{
FileInfo fi = new FileInfo(outputfile);
var dbgFile = Path.Combine(fi.DirectoryName ?? ".", Path.GetFileNameWithoutExtension(outputfile) + ".dbg");
File.WriteAllText(dbgFile, JsonConvert.SerializeObject(fResult.Result.Value.context.label, Formatting.Indented, new JsonSerializerSettings()
{
}));
}
}
}
}