mirror of
https://github.com/creeperlv/SVM.git
synced 2026-01-11 04:49:53 +00:00
51 lines
1.4 KiB
C#
51 lines
1.4 KiB
C#
using SVM.Core;
|
|
using System.Collections.Generic;
|
|
|
|
namespace SVM.Assembler.Core
|
|
{
|
|
public class LinkingContext
|
|
{
|
|
public ISADefinition Definition;
|
|
public Dictionary<string, uint> DataOffsets = new Dictionary<string, uint>();
|
|
public ManagedSVMProgram Program;
|
|
public Dictionary<string, int> label = new Dictionary<string, int>();
|
|
public IntermediateObject IntermediateObject;
|
|
public LinkingContext(ManagedSVMProgram program, IntermediateObject intermediateObject, ISADefinition definition)
|
|
{
|
|
Program = program;
|
|
IntermediateObject = intermediateObject;
|
|
Definition = definition;
|
|
}
|
|
public unsafe bool TryFindData(string label, out uint offset)
|
|
{
|
|
if (DataOffsets.TryGetValue(label, out offset))
|
|
{
|
|
//Don't directly give the length there.
|
|
offset += (uint)IntermediateObject.DetermineFinalInstructionCount(this) * (uint)sizeof(SVMInstruction)+sizeof(int);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
public bool TryFindLabel(string label, out int offset)
|
|
{
|
|
label = label + ":";
|
|
if (this.label.TryGetValue(label, out offset))
|
|
{
|
|
return true;
|
|
}
|
|
for (int i = 0; i < IntermediateObject.instructions.Count; i++)
|
|
{
|
|
IntermediateInstruction? item = IntermediateObject.instructions[i];
|
|
if (item.Label != null)
|
|
if (item.Label.Content == label)
|
|
{
|
|
this.label.Add(label, i);
|
|
offset = i;
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
}
|