From df4f838b5c8833a69df048c9d26d3963417e7ff0 Mon Sep 17 00:00:00 2001 From: angie Date: Wed, 11 Sep 2024 12:26:41 -0300 Subject: [PATCH 1/5] Add option to indent code --- CHANGELOG.md | 7 +++++++ pyproject.toml | 2 +- spimdisasm/__init__.py | 4 ++-- spimdisasm/common/GlobalConfig.py | 10 +++++++++- spimdisasm/mips/symbols/MipsSymbolBase.py | 6 ++++-- 5 files changed, 23 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bdf3dc5..3be51c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add a way to indentate instructions and data. + - May be desirable to be used with IDEs that support collapsing code by + looking at the whitespace of each line. + - This can be controlled by setting the `GlobalConfig.ASM_INDENTATION` option. + ## [1.30.0] - 2024-09-10 ### Changed diff --git a/pyproject.toml b/pyproject.toml index bcdd24d..f8b855a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ [project] name = "spimdisasm" # Version should be synced with spimdisasm/__init__.py -version = "1.30.0" +version = "1.30.1-dev0" description = "MIPS disassembler" readme = "README.md" license = {file = "LICENSE"} diff --git a/spimdisasm/__init__.py b/spimdisasm/__init__.py index 0aac7be..a638a81 100644 --- a/spimdisasm/__init__.py +++ b/spimdisasm/__init__.py @@ -5,8 +5,8 @@ from __future__ import annotations -__version_info__: tuple[int, int, int] = (1, 30, 0) -__version__ = ".".join(map(str, __version_info__))# + "-dev0" +__version_info__: tuple[int, int, int] = (1, 30, 1) +__version__ = ".".join(map(str, __version_info__)) + "-dev0" __author__ = "Decompollaborate" from . import common as common diff --git a/spimdisasm/common/GlobalConfig.py b/spimdisasm/common/GlobalConfig.py index 5ae3e14..4f1a913 100644 --- a/spimdisasm/common/GlobalConfig.py +++ b/spimdisasm/common/GlobalConfig.py @@ -239,6 +239,9 @@ def AGGRESSIVE_STRING_GUESSER(self, value: bool) -> None: """Toggle the glabel count comment on functions""" ASM_REFERENCEE_SYMBOLS: bool = False + ASM_INDENTATION: int = 0 + """Sets the indentation used for every instruction and data""" + ASM_TEXT_LABEL: str = "glabel" ASM_TEXT_ALT_LABEL: str = "glabel" ASM_JTBL_LABEL: str = "jlabel" @@ -379,10 +382,12 @@ def addParametersToArgParse(self, parser: argparse.ArgumentParser) -> None: miscConfig = parser.add_argument_group("Disassembler misc options") miscConfig.add_argument("--asm-comments", help=f"Toggle the comments in generated assembly code. Defaults to {self.ASM_COMMENT}", action=Utils.BooleanOptionalAction) - miscConfig.add_argument("--comment-offset-width", help=f"Sets the zeroes width padding for the file offset comment. Defaults to {self.ASM_COMMENT_OFFSET_WIDTH}", action=Utils.BooleanOptionalAction) + miscConfig.add_argument("--comment-offset-width", help=f"Sets the zeroes width padding for the file offset comment. Defaults to {self.ASM_COMMENT_OFFSET_WIDTH}") miscConfig.add_argument("--glabel-count", help=f"Toggle glabel count comment. Defaults to {self.GLABEL_ASM_COUNT}", action=Utils.BooleanOptionalAction) miscConfig.add_argument("--asm-referencee-symbols", help=f"Toggle glabel count comment. Defaults to {self.ASM_REFERENCEE_SYMBOLS}", action=Utils.BooleanOptionalAction) + miscConfig.add_argument("--asm-indentation", help=f"Sets the indentation used for every instruction and data. Defaults to {self.ASM_INDENTATION}") + miscConfig.add_argument("--asm-text-label", help=f"Changes the label used to declare functions. Defaults to {self.ASM_TEXT_LABEL}") miscConfig.add_argument("--asm-text-alt-label", help=f"Changes the label used to declare symbols in the middle of functions. Defaults to {self.ASM_TEXT_ALT_LABEL}") miscConfig.add_argument("--asm-jtbl-label", help=f"Changes the label used to declare jumptable labels. Defaults to {self.ASM_JTBL_LABEL}") @@ -577,6 +582,9 @@ def parseArgs(self, args: argparse.Namespace) -> None: if args.asm_referencee_symbols is not None: self.ASM_REFERENCEE_SYMBOLS = args.asm_referencee_symbols + if args.asm_referencee_symbols is not None: + self.ASM_REFERENCEE_SYMBOLS = args.asm_referencee_symbols + if args.asm_text_label: self.ASM_TEXT_LABEL = args.asm_text_label if args.asm_text_alt_label: diff --git a/spimdisasm/mips/symbols/MipsSymbolBase.py b/spimdisasm/mips/symbols/MipsSymbolBase.py index 8cb89a8..38ef25a 100644 --- a/spimdisasm/mips/symbols/MipsSymbolBase.py +++ b/spimdisasm/mips/symbols/MipsSymbolBase.py @@ -65,8 +65,10 @@ def canUseConstantsOnData(self) -> bool: def generateAsmLineComment(self, localOffset: int, wordValue: int|None=None, *, isDouble: bool=False, emitRomOffset: bool=True) -> str: + indentation = " " * common.GlobalConfig.ASM_INDENTATION + if not common.GlobalConfig.ASM_COMMENT: - return "" + return indentation if emitRomOffset: offsetHex = "{0:0{1}X} ".format(localOffset + self.inFileOffset + self.commentOffset, common.GlobalConfig.ASM_COMMENT_OFFSET_WIDTH) @@ -83,7 +85,7 @@ def generateAsmLineComment(self, localOffset: int, wordValue: int|None=None, *, else: wordValueHex = f"{common.Utils.wordToCurrenEndian(wordValue):08X} " - return f"/* {offsetHex}{vramHex} {wordValueHex}*/" + return f"{indentation}/* {offsetHex}{vramHex} {wordValueHex}*/" def getSymbolAsmDeclaration(self, symName: str, useGlobalLabel: bool=True) -> str: From 7466eb1cd87def4ff2cebcd0f407e66a0eba6e44 Mon Sep 17 00:00:00 2001 From: angie Date: Wed, 11 Sep 2024 13:03:21 -0300 Subject: [PATCH 2/5] Indent labels too --- CHANGELOG.md | 5 +++++ spimdisasm/common/GlobalConfig.py | 13 +++++++++---- spimdisasm/mips/symbols/MipsSymbolFunction.py | 6 ++++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3be51c2..37fff3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - May be desirable to be used with IDEs that support collapsing code by looking at the whitespace of each line. - This can be controlled by setting the `GlobalConfig.ASM_INDENTATION` option. +- Add a way to indentate labels within functions. + - May be desirable to be used with IDEs that support collapsing code by + looking at the whitespace of each line. + - This can be controlled by setting the `GlobalConfig.ASM_INDENTATION_LABELS` + option. ## [1.30.0] - 2024-09-10 diff --git a/spimdisasm/common/GlobalConfig.py b/spimdisasm/common/GlobalConfig.py index 4f1a913..5c5aac0 100644 --- a/spimdisasm/common/GlobalConfig.py +++ b/spimdisasm/common/GlobalConfig.py @@ -239,8 +239,10 @@ def AGGRESSIVE_STRING_GUESSER(self, value: bool) -> None: """Toggle the glabel count comment on functions""" ASM_REFERENCEE_SYMBOLS: bool = False - ASM_INDENTATION: int = 0 + ASM_INDENTATION: int = 4 """Sets the indentation used for every instruction and data""" + ASM_INDENTATION_LABELS: int = 2 + """Sets the indentation used for labels within functions""" ASM_TEXT_LABEL: str = "glabel" ASM_TEXT_ALT_LABEL: str = "glabel" @@ -386,7 +388,8 @@ def addParametersToArgParse(self, parser: argparse.ArgumentParser) -> None: miscConfig.add_argument("--glabel-count", help=f"Toggle glabel count comment. Defaults to {self.GLABEL_ASM_COUNT}", action=Utils.BooleanOptionalAction) miscConfig.add_argument("--asm-referencee-symbols", help=f"Toggle glabel count comment. Defaults to {self.ASM_REFERENCEE_SYMBOLS}", action=Utils.BooleanOptionalAction) - miscConfig.add_argument("--asm-indentation", help=f"Sets the indentation used for every instruction and data. Defaults to {self.ASM_INDENTATION}") + miscConfig.add_argument("--asm-indentation", help=f"Sets the indentation used for every instruction and data. Defaults to {self.ASM_INDENTATION}", type=int) + miscConfig.add_argument("--asm-indentation-labels", help=f"Sets the indentation used for labels within functions. Defaults to {self.ASM_INDENTATION_LABELS}", type=int) miscConfig.add_argument("--asm-text-label", help=f"Changes the label used to declare functions. Defaults to {self.ASM_TEXT_LABEL}") miscConfig.add_argument("--asm-text-alt-label", help=f"Changes the label used to declare symbols in the middle of functions. Defaults to {self.ASM_TEXT_ALT_LABEL}") @@ -582,8 +585,10 @@ def parseArgs(self, args: argparse.Namespace) -> None: if args.asm_referencee_symbols is not None: self.ASM_REFERENCEE_SYMBOLS = args.asm_referencee_symbols - if args.asm_referencee_symbols is not None: - self.ASM_REFERENCEE_SYMBOLS = args.asm_referencee_symbols + if args.asm_indentation is not None: + self.ASM_INDENTATION = args.asm_indentation + if args.asm_indentation_labels is not None: + self.ASM_INDENTATION_LABELS = args.asm_indentation_labels if args.asm_text_label: self.ASM_TEXT_LABEL = args.asm_text_label diff --git a/spimdisasm/mips/symbols/MipsSymbolFunction.py b/spimdisasm/mips/symbols/MipsSymbolFunction.py index 050b41d..28efd02 100644 --- a/spimdisasm/mips/symbols/MipsSymbolFunction.py +++ b/spimdisasm/mips/symbols/MipsSymbolFunction.py @@ -729,8 +729,10 @@ def getLabelForOffset(self, instructionOffset: int, migrate: bool=False) -> str: label += f"{labelMacro} {labelSym.getName()}{common.GlobalConfig.LINE_ENDS}" if common.GlobalConfig.ASM_TEXT_FUNC_AS_LABEL: label += f"{labelSym.getName()}:{common.GlobalConfig.LINE_ENDS}" - return label - return labelSym.getName() + ":" + common.GlobalConfig.LINE_ENDS + else: + label = labelSym.getName() + ":" + common.GlobalConfig.LINE_ENDS + label = (" " * common.GlobalConfig.ASM_INDENTATION_LABELS) + label + return label def _emitInstruction(self, instr: rabbitizer.Instruction, instructionOffset: int, wasLastInstABranch: bool, isSplittedSymbol: bool=False) -> str: immOverride, relocInfo = self._getImmOverrideForInstruction(instr, instructionOffset, isSplittedSymbol=isSplittedSymbol) From ea94ae31800fe834314e65a2cd164f529574089f Mon Sep 17 00:00:00 2001 From: angie Date: Wed, 11 Sep 2024 13:07:59 -0300 Subject: [PATCH 3/5] forgor this --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37fff3d..f64445e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,11 +13,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - May be desirable to be used with IDEs that support collapsing code by looking at the whitespace of each line. - This can be controlled by setting the `GlobalConfig.ASM_INDENTATION` option. + - Defaults to 4. - Add a way to indentate labels within functions. - May be desirable to be used with IDEs that support collapsing code by looking at the whitespace of each line. - This can be controlled by setting the `GlobalConfig.ASM_INDENTATION_LABELS` option. + - Defaults to 2. + +### Changed + +- Assembly now gets indentated by default to 4 spaces (or 2 spaces for labels). + - Use `GlobalConfig.ASM_INDENTATION` and `GlobalConfig.ASM_INDENTATION_LABELS` + to disable this behavior. ## [1.30.0] - 2024-09-10 From 7f86bcd7d7417582a20da9e20bee80ff3bc943d0 Mon Sep 17 00:00:00 2001 From: angie Date: Thu, 19 Sep 2024 09:54:50 -0300 Subject: [PATCH 4/5] Prevent generating labels for ignored symbols that are referenced by function calls --- CHANGELOG.md | 5 +++++ spimdisasm/common/ContextSymbols.py | 6 ++++++ spimdisasm/mips/symbols/MipsSymbolFunction.py | 9 +++++++++ 3 files changed, 20 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f64445e..103ae9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Use `GlobalConfig.ASM_INDENTATION` and `GlobalConfig.ASM_INDENTATION_LABELS` to disable this behavior. +### Fixed + +- Prevent generating labels for ignored symbols that are referenced by function + calls (ie in a `jal`). + ## [1.30.0] - 2024-09-10 ### Changed diff --git a/spimdisasm/common/ContextSymbols.py b/spimdisasm/common/ContextSymbols.py index f0d8283..cfd811d 100644 --- a/spimdisasm/common/ContextSymbols.py +++ b/spimdisasm/common/ContextSymbols.py @@ -728,6 +728,9 @@ def getCsvHeader() -> str: output += "isDefined,isUserDeclared,isAutogenerated," output += "isMaybeString,failedStringDecoding,isMaybePascalString,failedPascalStringDecoding," output += "referenceCounter," + if False: + output += "referenceFunctions," + output += "referenceSymbols," output += "parentFunction," output += "parentFileName," output += "inFileOffset," @@ -760,6 +763,9 @@ def toCsv(self) -> str: output += f"{self.isDefined},{self.isUserDeclared},{self.isAutogenerated}," output += f"{self.isMaybeString},{self.failedStringDecoding},{self.isMaybePascalString},{self.failedPascalStringDecoding}," output += f"{self.referenceCounter}," + if False: + output += f"\"{','.join(x.getName() for x in self.referenceFunctions)}\"," + output += f"\"{','.join(x.getName() for x in self.referenceSymbols)}\"," if self.parentFunction is not None: output += f"{self.parentFunction.getName()}," diff --git a/spimdisasm/mips/symbols/MipsSymbolFunction.py b/spimdisasm/mips/symbols/MipsSymbolFunction.py index 28efd02..11544b4 100644 --- a/spimdisasm/mips/symbols/MipsSymbolFunction.py +++ b/spimdisasm/mips/symbols/MipsSymbolFunction.py @@ -428,6 +428,9 @@ def analyze(self) -> None: # Branches for instrOffset, targetBranchVram in self.instrAnalyzer.branchInstrOffsets.items(): + if self.context.isAddressBanned(targetBranchVram): + continue + if common.GlobalConfig.INPUT_FILE_TYPE == common.InputFileType.ELF: if self.getVromOffset(instrOffset) in self.context.globalRelocationOverrides: # Avoid creating wrong symbols on elf files @@ -442,6 +445,9 @@ def analyze(self) -> None: # Function calls for instrOffset, targetVram in self.instrAnalyzer.funcCallInstrOffsets.items(): + if self.context.isAddressBanned(targetVram): + continue + if common.GlobalConfig.INPUT_FILE_TYPE == common.InputFileType.ELF: if self.getVromOffset(instrOffset) in self.context.globalRelocationOverrides: # Avoid creating wrong symbols on elf files @@ -521,6 +527,9 @@ def analyze(self) -> None: # Jump tables for targetVram in self.instrAnalyzer.referencedJumpTableOffsets.values(): + if self.context.isAddressBanned(targetVram): + continue + jumpTable = self.addJumpTable(targetVram, isAutogenerated=True) jumpTable.parentFunction = self.contextSym self.contextSym.jumpTables.add(jumpTable.vram, jumpTable) From eb5e1c11df1a35261797b1ec48c9550962e62fe6 Mon Sep 17 00:00:00 2001 From: angie Date: Thu, 19 Sep 2024 10:00:16 -0300 Subject: [PATCH 5/5] version bump --- CHANGELOG.md | 3 +++ README.md | 2 +- pyproject.toml | 2 +- spimdisasm/__init__.py | 2 +- 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 103ae9e..d48dbfc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.30.1] - 2024-09-19 + ### Added - Add a way to indentate instructions and data. @@ -1680,6 +1682,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Version 1.0.0 [unreleased]: https://github.com/Decompollaborate/spimdisasm/compare/master...develop +[1.30.1]: https://github.com/Decompollaborate/spimdisasm/compare/1.30.0...1.30.1 [1.30.0]: https://github.com/Decompollaborate/spimdisasm/compare/1.29.0...1.30.0 [1.29.0]: https://github.com/Decompollaborate/spimdisasm/compare/1.28.1...1.29.0 [1.28.1]: https://github.com/Decompollaborate/spimdisasm/compare/1.28.0...1.28.1 diff --git a/README.md b/README.md index a3f006d..d1069d3 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ If you use a `requirements.txt` file in your repository, then you can add this library with the following line: ```txt -spimdisasm>=1.30.0,<2.0.0 +spimdisasm>=1.30.1,<2.0.0 ``` ### Development version diff --git a/pyproject.toml b/pyproject.toml index f8b855a..ff44d74 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ [project] name = "spimdisasm" # Version should be synced with spimdisasm/__init__.py -version = "1.30.1-dev0" +version = "1.30.1" description = "MIPS disassembler" readme = "README.md" license = {file = "LICENSE"} diff --git a/spimdisasm/__init__.py b/spimdisasm/__init__.py index a638a81..bc8b8bd 100644 --- a/spimdisasm/__init__.py +++ b/spimdisasm/__init__.py @@ -6,7 +6,7 @@ from __future__ import annotations __version_info__: tuple[int, int, int] = (1, 30, 1) -__version__ = ".".join(map(str, __version_info__)) + "-dev0" +__version__ = ".".join(map(str, __version_info__))# + "-dev0" __author__ = "Decompollaborate" from . import common as common