mirror of
https://github.com/creeperlv/SVM.git
synced 2026-01-11 12:59:54 +00:00
Finished basic Assembler and Linker framework.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user