2025-07-20 03:35:04 +08:00
|
|
|
|
using LibCLCC.NET.Operations;
|
|
|
|
|
|
using SVM.Core;
|
2025-07-20 20:45:10 +08:00
|
|
|
|
using SVM.Core.Utils;
|
|
|
|
|
|
using System;
|
2025-07-20 03:35:04 +08:00
|
|
|
|
using System.Collections.Generic;
|
2025-07-20 20:45:10 +08:00
|
|
|
|
using System.Reflection.Emit;
|
|
|
|
|
|
using System.Text;
|
2025-07-20 03:35:04 +08:00
|
|
|
|
|
|
|
|
|
|
namespace SVM.Assembler.Core
|
|
|
|
|
|
{
|
|
|
|
|
|
public class Linker
|
|
|
|
|
|
{
|
|
|
|
|
|
public static OperationResult<IntermediateObject?> Link(List<IntermediateObject> objs)
|
|
|
|
|
|
{
|
|
|
|
|
|
OperationResult<IntermediateObject?> operationResult = new OperationResult<IntermediateObject?>(null);
|
|
|
|
|
|
IntermediateObject intermediateObject = new IntermediateObject();
|
|
|
|
|
|
foreach (var item in objs)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var inst in item.instructions)
|
|
|
|
|
|
{
|
|
|
|
|
|
intermediateObject.instructions.Add(inst);
|
|
|
|
|
|
}
|
|
|
|
|
|
foreach (var data in item.data)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!intermediateObject.data.TryAdd(data.Key, data.Value))
|
|
|
|
|
|
{
|
|
|
|
|
|
intermediateObject.data[data.Key] = data.Value;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
foreach (var kv in item.consts)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!intermediateObject.consts.TryAdd(kv.Key, kv.Value))
|
|
|
|
|
|
{
|
|
|
|
|
|
intermediateObject.consts[kv.Key] = kv.Value;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return operationResult;
|
|
|
|
|
|
}
|
2025-07-24 23:09:01 +10:00
|
|
|
|
public bool TryParseRegister(string input, IntermediateObject obj, LinkingContext context, out byte registerID)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (input.StartsWith("$"))
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
registerID = byte.MaxValue;
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2025-07-21 01:47:31 +08:00
|
|
|
|
public static OperationResult<SVMInstruction> translate(InstructionDefinition def, LinkingContext context, IntermediateInstruction iinstruction)
|
|
|
|
|
|
{
|
|
|
|
|
|
OperationResult<SVMInstruction> result = new OperationResult<SVMInstruction>();
|
2025-07-24 23:09:01 +10:00
|
|
|
|
for (int i = 0; i < iinstruction.Parameters.Count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
var para = iinstruction.Parameters[i];
|
|
|
|
|
|
var paraDef = def.ParameterPattern[i];
|
|
|
|
|
|
string converter = paraDef.ExpectdValue.Converter;
|
|
|
|
|
|
if (converter.StartsWith("InternalEnum:"))
|
|
|
|
|
|
{
|
|
|
|
|
|
var enumName = converter["InternalEnum:".Length..];
|
|
|
|
|
|
switch (enumName)
|
|
|
|
|
|
{
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (converter)
|
|
|
|
|
|
{
|
|
|
|
|
|
case "Register":
|
2025-07-21 01:47:31 +08:00
|
|
|
|
|
2025-07-24 23:09:01 +10:00
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-21 01:47:31 +08:00
|
|
|
|
return result;
|
|
|
|
|
|
}
|
2025-07-20 20:45:10 +08:00
|
|
|
|
public unsafe static OperationResult<ManagedSVMProgram?> Finialize(ISADefinition definition, IntermediateObject Obj)
|
2025-07-20 03:35:04 +08:00
|
|
|
|
{
|
|
|
|
|
|
OperationResult<ManagedSVMProgram?> operationResult = new OperationResult<ManagedSVMProgram?>(null);
|
2025-07-20 20:45:10 +08:00
|
|
|
|
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)
|
|
|
|
|
|
{
|
2025-07-21 01:47:31 +08:00
|
|
|
|
if (definition.InstructionDefinitions.TryGetValue(item.inst, out var def))
|
2025-07-20 20:45:10 +08:00
|
|
|
|
{
|
2025-07-21 01:47:31 +08:00
|
|
|
|
|
|
|
|
|
|
var inst = translate(def, context, item);
|
2025-07-20 20:45:10 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
2025-07-20 03:35:04 +08:00
|
|
|
|
return operationResult;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|