From f102b94e3820c532c54f81f470191f73830aa6d9 Mon Sep 17 00:00:00 2001 From: Sour Date: Thu, 30 Jan 2025 17:56:16 +0900 Subject: [PATCH] Debugger: Improved SDCC integration by loading additional symbols from .sym files --- UI/Debugger/Integration/SdccSymbolImporter.cs | 85 +++++++++++++++++++ UI/Debugger/Integration/WlaDxImporter.cs | 1 - 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/UI/Debugger/Integration/SdccSymbolImporter.cs b/UI/Debugger/Integration/SdccSymbolImporter.cs index de65d2f8..1375eec1 100644 --- a/UI/Debugger/Integration/SdccSymbolImporter.cs +++ b/UI/Debugger/Integration/SdccSymbolImporter.cs @@ -16,6 +16,7 @@ public abstract class SdccSymbolImporter : ISymbolProvider { private static Regex _globalSymbolRegex = new Regex(@"^L:G\$([^$]+)\$([0-9_]+)\$(\d+):([0-9A-Fa-f]+)$", RegexOptions.Compiled); private static Regex _srcMappingRegex = new Regex(@"^L:C\$([^$]+)\$(\d+)\$([0-9_]+)\$(\d+):([0-9A-Fa-f]+)$", RegexOptions.Compiled); + private static Regex _symRegex = new Regex(@"^([0-9a-fA-F]{2,4})[:]{0,1}([0-9a-fA-F]{4}) ([^\s]*)", RegexOptions.Compiled); private Dictionary _sourceFiles = new(); private Dictionary _addressByLine = new(); @@ -230,6 +231,12 @@ public abstract class SdccSymbolImporter : ISymbolProvider } } + string symPath = Path.ChangeExtension(path, ".sym"); + if(File.Exists(symPath)) { + //Also load the no$ format symbol file, too (some symbols are missing from the .cdb files) + LoadSymFile(symPath, labels); + } + LabelManager.SetLabels(labels.Values, true); if(showResult) { @@ -241,6 +248,84 @@ public abstract class SdccSymbolImporter : ISymbolProvider } } + private record MemAddress(int addr, MemoryType type); + + private void LoadSymFile(string path, Dictionary labels) + { + string[] lines = File.ReadAllLines(path); + + HashSet existingLabels = labels.Values.Select(l => new MemAddress((int)l.Address, l.MemoryType)).ToHashSet(); + HashSet existingSymbols = _symbols.Values.Where(l => l.Address != null).Select(l => new MemAddress(l.Address!.Value.Address, l.Address.Value.Type)).ToHashSet(); + + for(int i = 0; i < lines.Length; i++) { + string str = lines[i].Trim(); + Match m; + if((m = _symRegex.Match(str)).Success) { + string name = m.Groups[3].Value; + if(name.StartsWith("C$") || name.StartsWith("A$") || name.StartsWith("s_") || name.StartsWith("l_") || name.Contains("$sloc") || name.StartsWith("XG$")) { + continue; + } + + if(name.StartsWith("G$")) { + string[] nameParts = name.Split('$'); + if(nameParts.Length > 1) { + name = nameParts[1]; + } else { + continue; + } + } else { + int index = name.IndexOf("$"); + if(index > 0) { + name = name.Substring(0, index); + } + } + + int bank = int.Parse(m.Groups[1].Value, System.Globalization.NumberStyles.HexNumber); + int addr = int.Parse(m.Groups[2].Value, System.Globalization.NumberStyles.HexNumber); + + if(!TryDecodeAddress(addr, out AddressInfo absAddr)) { + continue; + } + + MemAddress memAddr = new MemAddress(absAddr.Address, absAddr.Type); + string baseLabel = LabelManager.InvalidLabelRegex.Replace(name, "_"); + + if(!existingLabels.Contains(memAddr) && ConfigManager.Config.Debug.Integration.IsMemoryTypeImportEnabled(absAddr.Type)) { + string label = baseLabel; + int count = 0; + while(labels.ContainsKey(label)) { + //Ensure labels are unique + label = baseLabel + "_" + count.ToString(); + count++; + } + + labels[label] = new CodeLabel() { + Label = label, + Address = (UInt32)absAddr.Address, + MemoryType = absAddr.Type, + Comment = "", + Flags = CodeLabelFlags.None, + Length = 1 + }; + + existingLabels.Add(memAddr); + } + + if(!existingSymbols.Contains(memAddr)) { + string symName = name; + int count = 0; + while(_symbols.ContainsKey(symName)) { + //Ensure labels are unique + symName = name + "_" + count.ToString(); + count++; + } + _symbols[symName] = new SymbolInfo(symName, absAddr); + existingSymbols.Add(memAddr); + } + } + } + } + class SdccSourceFile : IFileDataProvider { public string[] Data { get; init; } = Array.Empty(); diff --git a/UI/Debugger/Integration/WlaDxImporter.cs b/UI/Debugger/Integration/WlaDxImporter.cs index 106c9889..9338cf95 100644 --- a/UI/Debugger/Integration/WlaDxImporter.cs +++ b/UI/Debugger/Integration/WlaDxImporter.cs @@ -21,7 +21,6 @@ public abstract class WlaDxImporter : ISymbolProvider private static Regex _fileV2Regex = new Regex(@"^([0-9a-fA-F]{4}):([0-9a-fA-F]{4}) ([0-9a-fA-F]{8}) (.*)", RegexOptions.Compiled); private static Regex _addrV2Regex = new Regex(@"^([0-9a-fA-F]{8}) ([0-9a-fA-F]{2}):([0-9a-fA-F]{4}) ([0-9a-fA-F]{4}) ([0-9a-fA-F]{4}):([0-9a-fA-F]{4}):([0-9a-fA-F]{8})", RegexOptions.Compiled); private static Regex _filePathRegex = new Regex(@"^(""([^;""]*)""\s*;{0,1}\s*(.*))|(.*)", RegexOptions.Compiled); - private static Regex _pceasSourceRegex = new Regex(@"^([0-9a-fA-F]{2}):([0-9a-fA-F]{4}) ([0-9a-fA-F]{8}) ([0-9a-fA-F]{4}):([0-9a-fA-F]{8})", RegexOptions.Compiled); private Dictionary _sourceFiles = new(); private Dictionary _addressByLine = new();