Skip to content

Commit

Permalink
Include address of imports in ADI files
Browse files Browse the repository at this point in the history
  • Loading branch information
TollyH committed May 31, 2023
1 parent 12ded00 commit f07ae17
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Assembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static byte[] AssembleLines(string[] lines, out string debugInfo)
// Used for debug files
List<(ulong Address, string Line)> assembledLines = new();
Dictionary<ulong, List<string>> addressLabelNames = new();
List<(string LocalPath, string FullPath)> resolvedImports = new();
List<(string LocalPath, string FullPath, ulong Address)> resolvedImports = new();

for (int l = 0; l < dynamicLines.Count; l++)
{
Expand Down Expand Up @@ -107,7 +107,7 @@ public static byte[] AssembleLines(string[] lines, out string debugInfo)
}
// Insert the contents of the imported file so they are assembled next
dynamicLines.InsertRange(l + 1, File.ReadAllLines(filepath));
resolvedImports.Add((filepath, new FileInfo(filepath).FullName));
resolvedImports.Add((filepath, new FileInfo(filepath).FullName, (uint)program.Count));
continue;
// Define macro
case "MAC":
Expand Down
27 changes: 19 additions & 8 deletions DebugInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace AssEmbly
{
public static class DebugInfo
{
public static readonly string FormatVersion = "0.1";
public static readonly string FormatVersion = "0.2";

public static readonly string Separator = "===============================================================================";

Expand Down Expand Up @@ -39,7 +39,7 @@ public static class DebugInfo
\[3\]: Resolved Imports
===============================================================================
(?:\r\n|.)*?(?:\r\n)?===============================================================================");
(?<Imports>(?:\r\n|.)*?)(?:\r\n)?===============================================================================");

/// <summary>
/// Generates the contents of a debug information file based on the provided parameters.
Expand All @@ -49,11 +49,13 @@ public static class DebugInfo
/// <param name="totalProgramSize">The total size in bytes of the generated file</param>
/// <param name="assembledInstructions">An array of addresses and the corresponding line of AssEmbly that generated them</param>
/// <param name="addressLabels">An array of addresses combined with an array of label names pointing to that address</param>
/// <param name="resolvedImports">An array of imported file names as seen in AssEmbly code along with the full file path to each one</param>
/// <param name="resolvedImports">
/// An array of imported file names as seen in AssEmbly code along with the full file path to each one, and the address they were inserted to
/// </param>
/// <returns>A completely formatted debug info file string ready to be saved.</returns>
public static string GenerateDebugInfoFile(ulong totalProgramSize,
IList<(ulong Address, string Line)> assembledInstructions, IList<(ulong Address, List<string> LabelNames)> addressLabels,
IList<(string LocalPath, string FullPath)> resolvedImports)
IList<(string LocalPath, string FullPath, ulong Address)> resolvedImports)
{
string fileText = string.Format(DebugInfoFileHeader, DateTime.Now, Environment.CommandLine, totalProgramSize);

Expand All @@ -70,9 +72,9 @@ public static string GenerateDebugInfoFile(ulong totalProgramSize,
}

fileText += $"\r\n{Separator}\r\n{ResolvedImportsHeader}";
foreach ((string sourceName, string resolvedName) in resolvedImports)
foreach ((string sourceName, string resolvedName, ulong address) in resolvedImports)
{
fileText += $"\r\n\"{sourceName}\" -> \"{resolvedName}\"";
fileText += $"\r\n{address:X16} @ \"{sourceName}\" -> \"{resolvedName}\"";
}

fileText += "\r\n" + Separator;
Expand All @@ -82,7 +84,8 @@ public static string GenerateDebugInfoFile(ulong totalProgramSize,

public readonly record struct DebugInfoFile(
Dictionary<ulong, string> AssembledInstructions,
Dictionary<ulong, string[]> AddressLabels);
Dictionary<ulong, string[]> AddressLabels,
Dictionary<ulong, string> ImportLocations);

public static DebugInfoFile ParseDebugInfoFile(string fileText)
{
Expand All @@ -98,6 +101,7 @@ public static DebugInfoFile ParseDebugInfoFile(string fileText)

List<(ulong Address, string Line)> assembledInstructions = new();
List<(ulong Address, string[] LabelNames)> addressLabels = new();
List<(ulong Address, string ImportName)> importLocations = new();

foreach (string line in fileMatch.Groups["Instructions"].Value.Split('\n'))
{
Expand All @@ -111,9 +115,16 @@ public static DebugInfoFile ParseDebugInfoFile(string fileText)
addressLabels.Add((Convert.ToUInt64(split[0], 16), split[1].Split(',')));
}

foreach (string line in fileMatch.Groups["Imports"].Value.Split('\n'))
{
string[] split = line.Split(" @ ");
importLocations.Add((Convert.ToUInt64(split[0], 16), split[1].Split(" -> ")[0].Trim('"')));
}

return new DebugInfoFile(
assembledInstructions.ToDictionary(x => x.Address, x => x.Line),
addressLabels.ToDictionary(x => x.Address, x => x.LabelNames));
addressLabels.ToDictionary(x => x.Address, x => x.LabelNames),
importLocations.ToDictionary(x => x.Address, x => x.ImportName));
}
}
}

0 comments on commit f07ae17

Please sign in to comment.