From 937d8446297e8ae58259d48a727635afae5501aa Mon Sep 17 00:00:00 2001 From: TollyH Date: Fri, 31 May 2024 19:13:36 +0100 Subject: [PATCH] Suggest %NUM over consecutive %DATs --- AssemblerWarnings.Messages.cs | 1 + AssemblerWarnings.cs | 34 ++++++++++++++----- .../Strings.AssemblerWarnings.Designer.cs | 9 +++++ .../Strings.AssemblerWarnings.resx | 3 ++ Test/KitchenSink.asm | 2 ++ 5 files changed, 41 insertions(+), 8 deletions(-) diff --git a/AssemblerWarnings.Messages.cs b/AssemblerWarnings.Messages.cs index 8de5b28..6404882 100644 --- a/AssemblerWarnings.Messages.cs +++ b/AssemblerWarnings.Messages.cs @@ -86,6 +86,7 @@ public partial class AssemblerWarnings { 0022, Strings_AssemblerWarnings.Suggestion_0022 }, { 0023, Strings_AssemblerWarnings.Suggestion_0023 }, { 0024, Strings_AssemblerWarnings.Suggestion_0024 }, + { 0025, Strings_AssemblerWarnings.Suggestion_0025 }, }; public static Dictionary GetMessagesForSeverity(WarningSeverity severity) diff --git a/AssemblerWarnings.cs b/AssemblerWarnings.cs index 0b59909..566a5ac 100644 --- a/AssemblerWarnings.cs +++ b/AssemblerWarnings.cs @@ -180,6 +180,7 @@ public void NewLabel(string labelName, FilePosition filePosition, string? macroN { UpdateDisabledPositions(filePosition); labelDefinitionPositions[labelName] = (filePosition, macroName); + consecutiveDatCount = 0; } /// @@ -289,6 +290,7 @@ public AssemblerWarnings(bool usingV1Format) { 0023, Analyzer_Rolling_Suggestion_0023 }, #endif { 0024, Analyzer_Rolling_Suggestion_0024 }, + { 0025, Analyzer_Rolling_Suggestion_0025 }, }; nonFatalErrorFinalAnalyzers = new Dictionary(); @@ -345,6 +347,7 @@ public AssemblerWarnings(bool usingV1Format) private string? lastMacroName; private int lastMacroLineDepth; private Stack lastImportStack = new(); + private int consecutiveDatCount = 0; private void PreAnalyzeStateUpdate() { @@ -383,25 +386,34 @@ private void PreAnalyzeStateUpdate() if (instructionIsData) { dataInsertionLines.Add((filePosition, macroName, macroLineDepth)); - if (operands[0][0] == '"' && ( - mnemonic.Equals("%DAT", StringComparison.OrdinalIgnoreCase) - || mnemonic.Equals("DAT", StringComparison.OrdinalIgnoreCase))) + if (mnemonic.Equals("%DAT", StringComparison.OrdinalIgnoreCase) + || mnemonic.Equals("DAT", StringComparison.OrdinalIgnoreCase)) { - instructionIsString = true; - if (lastInstructionWasString) + if (operands[0][0] == '"') + { + consecutiveDatCount = 0; + instructionIsString = true; + if (lastInstructionWasString) + { + // Only store the last in a chain of string insertions + endingStringInsertionLines.RemoveAt(endingStringInsertionLines.Count - 1); + } + endingStringInsertionLines.Add((filePosition, macroName, macroLineDepth)); + } + else { - // Only store the last in a chain of string insertions - endingStringInsertionLines.RemoveAt(endingStringInsertionLines.Count - 1); + consecutiveDatCount++; } - endingStringInsertionLines.Add((filePosition, macroName, macroLineDepth)); } } else if (instructionIsImport) { + consecutiveDatCount = 0; importLines.Add((filePosition, macroName, macroLineDepth)); } else if (newBytes.Length > 0) { + consecutiveDatCount = 0; lastExecutableLine[filePosition.File] = filePosition.Line; _ = executableAddresses.Add(currentAddress); instructionIsExecutable = true; @@ -1227,6 +1239,12 @@ private bool Analyzer_Rolling_Suggestion_0024() && !operands[1].Contains('['); } + private bool Analyzer_Rolling_Suggestion_0025() + { + // Suggestion 0025: Use the %NUM directive instead of 8 consecutive %DAT directives. + return newBytes.Length > 0 && consecutiveDatCount > 0 && consecutiveDatCount % 8 == 0; + } + #if DISPLACEMENT [GeneratedRegex(@"\[.*(?:[0-9]?\.[0-9]|[0-9]\.[0-9]?)")] private static partial Regex FloatingPointDisplacementRegex(); diff --git a/Resources/Localization/Strings.AssemblerWarnings.Designer.cs b/Resources/Localization/Strings.AssemblerWarnings.Designer.cs index 9cf3dc7..28347b2 100644 --- a/Resources/Localization/Strings.AssemblerWarnings.Designer.cs +++ b/Resources/Localization/Strings.AssemblerWarnings.Designer.cs @@ -339,6 +339,15 @@ internal static string Suggestion_0024 { } } + /// + /// Looks up a localized string similar to Use the %NUM directive instead of 8 consecutive %DAT directives.. + /// + internal static string Suggestion_0025 { + get { + return ResourceManager.GetString("Suggestion_0025", resourceCulture); + } + } + /// /// Looks up a localized string similar to Manually emitted warning.. /// diff --git a/Resources/Localization/Strings.AssemblerWarnings.resx b/Resources/Localization/Strings.AssemblerWarnings.resx index 7c693af..d07e2ed 100644 --- a/Resources/Localization/Strings.AssemblerWarnings.resx +++ b/Resources/Localization/Strings.AssemblerWarnings.resx @@ -243,4 +243,7 @@ Pointer size other than 8 bits (`B*`) used in a context where a single byte will always be read. + + Use the %NUM directive instead of 8 consecutive %DAT directives. + \ No newline at end of file diff --git a/Test/KitchenSink.asm b/Test/KitchenSink.asm index 92b27a7..ac21596 100644 --- a/Test/KitchenSink.asm +++ b/Test/KitchenSink.asm @@ -7,6 +7,8 @@ ; The exact binary output that this program should produce ; is located in KitchenSink.bin +%ANALYZER suggestion, 0025, 0 + :ENTRY MVQ rg0, 69.0