Allow thumb script words and ldr commands to use anchors with offsets

This commit is contained in:
Benjamin Popp 2021-02-17 13:38:58 -06:00
parent 6a3ad1cf92
commit 92f73ddb87
2 changed files with 9 additions and 6 deletions

View File

@ -1,13 +1,10 @@
using HavenSoft.HexManiac.Core.Models.Runs;
using HavenSoft.HexManiac.Core.ViewModels.DataFormats;
using HavenSoft.HexManiac.Core.ViewModels.DataFormats;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace HavenSoft.HexManiac.Core.Models.Code {
public class LabelLibrary {
@ -15,8 +12,13 @@ namespace HavenSoft.HexManiac.Core.Models.Code {
private readonly IDictionary<string, int> labels;
public LabelLibrary(IDataModel data, IDictionary<string, int> additionalLabels) => (model, labels) = (data, additionalLabels);
public int ResolveLabel(string label) {
if (labels.TryGetValue(label, out int result)) return result;
return model.GetAddressFromAnchor(new NoDataChangeDeltaModel(), -1, label);
var offset = 0;
if (label.Split("+") is string[] parts && parts.Length == 2) {
label = parts[0];
int.TryParse(parts[1], NumberStyles.HexNumber, CultureInfo.CurrentCulture, out offset);
}
if (labels.TryGetValue(label, out int result)) return result + offset;
return model.GetAddressFromAnchor(new NoDataChangeDeltaModel(), -1, label) + offset;
}
}

View File

@ -396,6 +396,7 @@ namespace HavenSoft.HexManiac.Tests {
[InlineData("ldrh r1, [r2]", 0b10001_00000_010_001)]
[InlineData("str r1, [sp]", 0b10010_001_00000000)]
[InlineData("ldr r1, [sp]", 0b10011_001_00000000)]
[InlineData(".word <bob+10>", 0b0000_1000_0000_0000_0000_0000_1001_0000)] // test that we can use anchors + offset to get pointer locations (in base 16)
public void ThumbCompilerTests(string input, uint output) {
var bytes = new List<byte> { (byte)output, (byte)(output >> 8) };
var model = new PokemonModel(new byte[0x200]);