Finished basic Assembler and Linker framework.

This commit is contained in:
2025-07-20 20:45:10 +08:00
parent bfec019c77
commit b164fc1798
9 changed files with 166 additions and 8 deletions

View File

@@ -1,6 +1,10 @@
using LibCLCC.NET.Operations;
using SVM.Core;
using SVM.Core.Utils;
using System;
using System.Collections.Generic;
using System.Reflection.Emit;
using System.Text;
namespace SVM.Assembler.Core
{
@@ -33,10 +37,46 @@ namespace SVM.Assembler.Core
}
return operationResult;
}
public static OperationResult<ManagedSVMProgram?> Freeze(List<IntermediateObject> objs)
public unsafe static OperationResult<ManagedSVMProgram?> Finialize(ISADefinition definition, IntermediateObject Obj)
{
OperationResult<ManagedSVMProgram?> operationResult = new OperationResult<ManagedSVMProgram?>(null);
ManagedSVMProgram program = new ManagedSVMProgram();
LinkingContext context = new LinkingContext(program, Obj);
List<byte[]> Data = new List<byte[]>();
uint offset = 0;
foreach (var item in Obj.data)
{
var data = Encoding.UTF8.GetBytes(item.Value);
byte[] data2 = new byte[data.Length + sizeof(int)];
fixed (byte* ptr = data2)
{
int len = data.Length;
((IntPtr)ptr).SetData(len);
}
Buffer.BlockCopy(data, 0, data2, sizeof(int), data.Length);
context.DataOffsets.Add(item.Key, offset);
offset += (uint)data2.Length;
Data.Add(data);
}
foreach (var item in Obj.instructions)
{
if (definition.LinkerFunctions.TryGetValue(item.inst, out var func))
{
var inst = func(context, item);
if (operationResult.CheckAndInheritErrorAndWarnings(inst))
{
return operationResult;
}
program.instructions.Add(inst.Result);
}
}
program.Datas = new byte[offset];
int offset2 = 0;
foreach (var item in Data)
{
Buffer.BlockCopy(item, 0, program.Datas, offset2, item.Length);
offset2 += item.Length;
}
return operationResult;
}
}