From ac1dd252a69580fa488bb1c97d9cb00e3a005d02 Mon Sep 17 00:00:00 2001 From: Angie Date: Sat, 9 Jul 2022 14:48:56 -0400 Subject: [PATCH 01/13] Add build-backend to pyproject --- pyproject.toml | 3 ++- setup.cfg | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 6599df5..35a5cb2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,7 +2,8 @@ # SPDX-License-Identifier: MIT [build-system] -requires = ["setuptools", "wheel"] +requires = ["setuptools>=61.0", "wheel"] +build-backend = "setuptools.build_meta" [tool.cibuildwheel] skip = ["cp36-*"] diff --git a/setup.cfg b/setup.cfg index 2a81769..d5d3e97 100644 --- a/setup.cfg +++ b/setup.cfg @@ -3,7 +3,7 @@ [metadata] name = rabbitizer -version = 1.0.0 +version = 1.0.1 author = Decompollaborate license = MIT description = MIPS instruction decoder From 5b68266148d2d6015c9b6c0cf45c06c37753c504 Mon Sep 17 00:00:00 2001 From: angie Date: Sat, 9 Jul 2022 15:51:19 -0400 Subject: [PATCH 02/13] Build for every supported architecture on ci builds --- .github/workflows/ci.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 368ff14..8af75a9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,13 +9,17 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - os: [ubuntu-20.04, macos-10.15, windows-2019] + os: [ubuntu-latest, macos-latest, windows-latest] steps: - uses: actions/checkout@v2 - name: Build wheels uses: pypa/cibuildwheel@v2.5.0 + env: + CIBW_ARCHS_WINDOWS: "all" + CIBW_ARCHS_LINUX: "all" + CIBW_ARCHS_MACOS: "all" - uses: actions/upload-artifact@v2 with: From 5e59c935e8ff4cf72e5db491ea031a97ca154e26 Mon Sep 17 00:00:00 2001 From: angie Date: Sat, 9 Jul 2022 16:06:04 -0400 Subject: [PATCH 03/13] hopefully fix windows ci builds --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8af75a9..c4b6536 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,7 +17,7 @@ jobs: - name: Build wheels uses: pypa/cibuildwheel@v2.5.0 env: - CIBW_ARCHS_WINDOWS: "all" + CIBW_ARCHS_WINDOWS: "auto" CIBW_ARCHS_LINUX: "all" CIBW_ARCHS_MACOS: "all" From ecc8de8df2f36c151a91da9ee2fdda759b762e61 Mon Sep 17 00:00:00 2001 From: angie Date: Sat, 9 Jul 2022 16:54:42 -0400 Subject: [PATCH 04/13] Add a makefile for testing and fix one warning --- Makefile | 67 +++++++++++++++++++ include/instructions/RabbitizerInstruction.h | 4 +- .../RabbitizerInstruction_Disassemble.c | 8 +-- test.c | 2 +- 4 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7f9c05c --- /dev/null +++ b/Makefile @@ -0,0 +1,67 @@ +# Build options can be changed by modifying the makefile or by building with 'make SETTING=value'. +DEBUG ?= 0 +WERROR ?= 0 +ASAN ?= 0 +EXPERIMENTAL ?= 0 + +CC := clang +IINC := -I include +CSTD := -std=c11 +CFLAGS := +LDFLAGS := +WARNINGS := -Wall -Wextra +# WARNINGS := -Wall -Wextra -Wpedantic # binary constants :s +WARNINGS += -Wno-cast-function-type +WARNINGS += -Werror=implicit-function-declaration -Werror=incompatible-pointer-types -Werror=vla -Werror=switch -Werror=implicit-fallthrough -Werror=unused-function -Werror=unused-parameter -Werror=shadow + +ifeq ($(DEBUG),0) + OPTFLAGS := -O2 -g +else + OPTFLAGS := -O0 -g3 + CFLAGS += -DDEVELOPMENT=1 +endif + +ifneq ($(WERROR),0) + WARNINGS += -Werror +endif + +ifneq ($(ASAN),0) + CFLAGS += -fsanitize=address -fsanitize=pointer-compare -fsanitize=pointer-subtract -fsanitize=undefined +endif + +ifneq ($(EXPERIMENTAL),0) + CFLAGS += -DEXPERIMENTAL +endif + + +SRC_DIRS := $(shell find src -type d) +C_FILES := $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.c)) +H_FILES := $(foreach dir,$(IINC),$(wildcard $(dir)/**/*.h)) +O_FILES := $(foreach f,$(C_FILES:.c=.o),build/$f) + + +all: tests + +clean: + $(RM) -rf build + +distclean: clean + $(RM) -rf dist rabbitizer.egg-info .mypy_cache + +format: + @echo "TODO" + +tests: build/test.elf + +.PHONY: all clean distclean format tests +.SECONDARY: + + +# create build directories +$(shell mkdir -p $(foreach dir,$(SRC_DIRS),build/$(dir))) + +build/%.elf: %.c $(O_FILES) + $(CC) $(CSTD) $(OPTFLAGS) $(IINC) $(WARNINGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ + +build/%.o: %.c + $(CC) -c $(CSTD) $(OPTFLAGS) $(IINC) $(WARNINGS) $(CFLAGS) -o $@ $< diff --git a/include/instructions/RabbitizerInstruction.h b/include/instructions/RabbitizerInstruction.h index 8e4cfdf..21b50d1 100644 --- a/include/instructions/RabbitizerInstruction.h +++ b/include/instructions/RabbitizerInstruction.h @@ -166,8 +166,8 @@ bool RabbitizerInstruction_isValid(const RabbitizerInstruction *self); bool RabbitizerInstruction_mustDisasmAsData(const RabbitizerInstruction *self); -size_t RabbitizerInstruction_getSizeForBufferOperandsDisasm(const RabbitizerInstruction *self, size_t immOverrideLength, int extraLJust); -size_t RabbitizerInstruction_disassembleOperands(const RabbitizerInstruction *self, char *dst, const char *immOverride, size_t immOverrideLength, int extraLJust); +size_t RabbitizerInstruction_getSizeForBufferOperandsDisasm(const RabbitizerInstruction *self, size_t immOverrideLength); +size_t RabbitizerInstruction_disassembleOperands(const RabbitizerInstruction *self, char *dst, const char *immOverride, size_t immOverrideLength); size_t RabbitizerInstruction_getSizeForBufferInstrDisasm(const RabbitizerInstruction *self, size_t immOverrideLength, int extraLJust); size_t RabbitizerInstruction_disassembleInstruction(const RabbitizerInstruction *self, char *dst, const char *immOverride, size_t immOverrideLength, int extraLJust); diff --git a/src/instructions/RabbitizerInstruction/RabbitizerInstruction_Disassemble.c b/src/instructions/RabbitizerInstruction/RabbitizerInstruction_Disassemble.c index e99c0ae..5658bf0 100644 --- a/src/instructions/RabbitizerInstruction/RabbitizerInstruction_Disassemble.c +++ b/src/instructions/RabbitizerInstruction/RabbitizerInstruction_Disassemble.c @@ -363,7 +363,7 @@ const OperandCallback instrOpercandCallbacks[] = { }; -size_t RabbitizerInstruction_getSizeForBufferOperandsDisasm(const RabbitizerInstruction *self, size_t immOverrideLength, int extraLJust) { +size_t RabbitizerInstruction_getSizeForBufferOperandsDisasm(const RabbitizerInstruction *self, size_t immOverrideLength) { size_t totalSize = 0; for (size_t i = 0; i < ARRAY_COUNT(self->descriptor->operands) && self->descriptor->operands[i] != RABBITIZER_OPERAND_TYPE_INVALID; i++) { @@ -379,7 +379,7 @@ size_t RabbitizerInstruction_getSizeForBufferOperandsDisasm(const RabbitizerInst return totalSize; } -size_t RabbitizerInstruction_disassembleOperands(const RabbitizerInstruction *self, char *dst, const char *immOverride, size_t immOverrideLength, int extraLJust) { +size_t RabbitizerInstruction_disassembleOperands(const RabbitizerInstruction *self, char *dst, const char *immOverride, size_t immOverrideLength) { size_t totalSize = 0; for (size_t i = 0; i < ARRAY_COUNT(self->descriptor->operands) && self->descriptor->operands[i] != RABBITIZER_OPERAND_TYPE_INVALID; i++) { @@ -422,7 +422,7 @@ size_t RabbitizerInstruction_getSizeForBufferInstrDisasm(const RabbitizerInstruc totalSize += extraLJust; totalSize++; - totalSize += RabbitizerInstruction_getSizeForBufferOperandsDisasm(self, immOverrideLength, extraLJust); + totalSize += RabbitizerInstruction_getSizeForBufferOperandsDisasm(self, immOverrideLength); return totalSize; } @@ -442,7 +442,7 @@ size_t RabbitizerInstruction_disassembleInstruction(const RabbitizerInstruction RABUTILS_BUFFER_ADVANCE(dst, totalSize, RabbitizerUtils_CharFill(dst, RabbitizerConfig_Cfg.misc.opcodeLJust + extraLJust - totalSize, ' ')); RABUTILS_BUFFER_WRITE_CHAR(dst, totalSize, ' '); - RABUTILS_BUFFER_ADVANCE(dst, totalSize, RabbitizerInstruction_disassembleOperands(self, dst, immOverride, immOverrideLength, extraLJust)); + RABUTILS_BUFFER_ADVANCE(dst, totalSize, RabbitizerInstruction_disassembleOperands(self, dst, immOverride, immOverrideLength)); *dst = '\0'; return totalSize; diff --git a/test.c b/test.c index cc01ae3..505cab2 100644 --- a/test.c +++ b/test.c @@ -17,7 +17,7 @@ int main() { // word = 0x8D4A7E18; // lw word = 0x00004010; // mfhi - RabbitizerInstruction_init(&instr, word); + RabbitizerInstruction_init(&instr, word, 0x80000000); RabbitizerInstruction_processUniqueId(&instr); From f17c0ee5dcdf628a168869bec5c6b06598f1d997 Mon Sep 17 00:00:00 2001 From: angie Date: Sat, 9 Jul 2022 17:13:37 -0400 Subject: [PATCH 05/13] setup header dependencies on makefile --- Makefile | 22 +++++++++++++++------- src/analysis/RabbitizerRegistersTracker.c | 4 +++- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 7f9c05c..44be5a1 100644 --- a/Makefile +++ b/Makefile @@ -34,10 +34,11 @@ ifneq ($(EXPERIMENTAL),0) endif -SRC_DIRS := $(shell find src -type d) -C_FILES := $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.c)) -H_FILES := $(foreach dir,$(IINC),$(wildcard $(dir)/**/*.h)) -O_FILES := $(foreach f,$(C_FILES:.c=.o),build/$f) +SRC_DIRS := $(shell find src -type d) +C_FILES := $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.c)) +H_FILES := $(foreach dir,$(IINC),$(wildcard $(dir)/**/*.h)) +O_FILES := $(foreach f,$(C_FILES:.c=.o),build/$f) +DEP_FILES := $(O_FILES:%.o=%.d) all: tests @@ -51,9 +52,12 @@ distclean: clean format: @echo "TODO" +tidy: + @echo "TODO" + tests: build/test.elf -.PHONY: all clean distclean format tests +.PHONY: all clean distclean format tidy tests .SECONDARY: @@ -61,7 +65,11 @@ tests: build/test.elf $(shell mkdir -p $(foreach dir,$(SRC_DIRS),build/$(dir))) build/%.elf: %.c $(O_FILES) - $(CC) $(CSTD) $(OPTFLAGS) $(IINC) $(WARNINGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ + $(CC) -MMD $(CSTD) $(OPTFLAGS) $(IINC) $(WARNINGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ build/%.o: %.c - $(CC) -c $(CSTD) $(OPTFLAGS) $(IINC) $(WARNINGS) $(CFLAGS) -o $@ $< +# The -MMD flags additionaly creates a .d file with the same name as the .o file. + $(CC) -MMD -c $(CSTD) $(OPTFLAGS) $(IINC) $(WARNINGS) $(CFLAGS) -o $@ $< + + +-include $(DEP_FILES) diff --git a/src/analysis/RabbitizerRegistersTracker.c b/src/analysis/RabbitizerRegistersTracker.c index 9dea6b8..064bf87 100644 --- a/src/analysis/RabbitizerRegistersTracker.c +++ b/src/analysis/RabbitizerRegistersTracker.c @@ -13,7 +13,9 @@ // TODO: abi checks void RabbitizerRegistersTracker_init(RabbitizerRegistersTracker *self, const RabbitizerRegistersTracker *other) { - for (size_t i = 0; i < ARRAY_COUNT(self->registers); i++) { + size_t i; + + for (i = 0; i < ARRAY_COUNT(self->registers); i++) { RabbitizerTrackedRegisterState_init(&self->registers[i], i); if (other != NULL) { RabbitizerTrackedRegisterState_copyState(&self->registers[i], &other->registers[i]); From 63eb1cb1528da6115c3944f7110d19406d018776 Mon Sep 17 00:00:00 2001 From: angie Date: Sat, 9 Jul 2022 17:17:54 -0400 Subject: [PATCH 06/13] Don't use "all" for linux ci builds --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c4b6536..f5baa59 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,7 +18,7 @@ jobs: uses: pypa/cibuildwheel@v2.5.0 env: CIBW_ARCHS_WINDOWS: "auto" - CIBW_ARCHS_LINUX: "all" + CIBW_ARCHS_LINUX: "auto x86_64 aarch64" CIBW_ARCHS_MACOS: "all" - uses: actions/upload-artifact@v2 From baaef2f242ff1c3ca2023f14ea632f82e368c3e0 Mon Sep 17 00:00:00 2001 From: angie Date: Sat, 9 Jul 2022 17:40:53 -0400 Subject: [PATCH 07/13] Format files with clang-format and add a format rule to makefile --- .clang-format | 23 ++++ Makefile | 14 +- src/analysis/RabbitizerRegistersTracker.c | 31 ++--- src/analysis/RabbitizerTrackedRegisterState.c | 6 - src/common/RabbitizerConfig.c | 2 - src/common/Utils.c | 1 - src/instructions/RabbitizerInstrDescriptor.c | 13 +- src/instructions/RabbitizerInstrId.c | 13 +- .../RabbitizerInstruction.c | 9 +- .../RabbitizerInstruction_Disassemble.c | 127 ++++++++---------- .../RabbitizerInstruction_Examination.c | 15 +-- .../RabbitizerInstruction_ProcessUniqueId.c | 14 +- .../RabbitizerInstructionRsp.c | 2 - ...RabbitizerInstructionRsp_ProcessUniqueId.c | 10 +- src/instructions/RabbitizerRegister.c | 36 +++-- 15 files changed, 146 insertions(+), 170 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..414e3cf --- /dev/null +++ b/.clang-format @@ -0,0 +1,23 @@ +IndentWidth: 4 +Language: Cpp +UseTab: Never +ColumnLimit: 160 +PointerAlignment: Right +BreakBeforeBraces: Attach +SpaceAfterCStyleCast: false +Cpp11BracedListStyle: false +IndentCaseLabels: true +BinPackArguments: true +BinPackParameters: true +AlignAfterOpenBracket: Align +AlignOperands: true +BreakBeforeTernaryOperators: true +BreakBeforeBinaryOperators: None +AllowShortBlocksOnASingleLine: true +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: false +AlignEscapedNewlines: Left +AlignTrailingComments: true +SortIncludes: false diff --git a/Makefile b/Makefile index 44be5a1..3f10efc 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ CSTD := -std=c11 CFLAGS := LDFLAGS := WARNINGS := -Wall -Wextra -# WARNINGS := -Wall -Wextra -Wpedantic # binary constants :s +# WARNINGS := -Wall -Wextra -Wpedantic -Wpadded # binary constants :s WARNINGS += -Wno-cast-function-type WARNINGS += -Werror=implicit-function-declaration -Werror=incompatible-pointer-types -Werror=vla -Werror=switch -Werror=implicit-fallthrough -Werror=unused-function -Werror=unused-parameter -Werror=shadow @@ -41,6 +41,12 @@ O_FILES := $(foreach f,$(C_FILES:.c=.o),build/$f) DEP_FILES := $(O_FILES:%.o=%.d) +# create build directories +$(shell mkdir -p $(foreach dir,$(SRC_DIRS),build/$(dir))) + + +#### Main Targets ### + all: tests clean: @@ -50,7 +56,7 @@ distclean: clean $(RM) -rf dist rabbitizer.egg-info .mypy_cache format: - @echo "TODO" + clang-format-11 -i -style=file $(C_FILES) tidy: @echo "TODO" @@ -58,11 +64,11 @@ tidy: tests: build/test.elf .PHONY: all clean distclean format tidy tests +.DEFAULT_GOAL := all .SECONDARY: -# create build directories -$(shell mkdir -p $(foreach dir,$(SRC_DIRS),build/$(dir))) +#### Various Recipes #### build/%.elf: %.c $(O_FILES) $(CC) -MMD $(CSTD) $(OPTFLAGS) $(IINC) $(WARNINGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ diff --git a/src/analysis/RabbitizerRegistersTracker.c b/src/analysis/RabbitizerRegistersTracker.c index 064bf87..78477b2 100644 --- a/src/analysis/RabbitizerRegistersTracker.c +++ b/src/analysis/RabbitizerRegistersTracker.c @@ -9,7 +9,6 @@ #include "common/RabbitizerConfig.h" #include "instructions/RabbitizerRegister.h" - // TODO: abi checks void RabbitizerRegistersTracker_init(RabbitizerRegistersTracker *self, const RabbitizerRegistersTracker *other) { @@ -29,7 +28,6 @@ void RabbitizerRegistersTracker_destroy(RabbitizerRegistersTracker *self) { } } - bool RabbitizerRegistersTracker_moveRegisters(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr) { RabbitizerTrackedRegisterState *dstState; RabbitizerTrackedRegisterState *srcState; @@ -107,7 +105,8 @@ void RabbitizerRegistersTracker_overwriteRegisters(RabbitizerRegistersTracker *s default: break; } - } else if (RabbitizerInstrDescriptor_isRType(instr->descriptor) || (RabbitizerInstrDescriptor_isBranch(instr->descriptor) && RabbitizerInstrDescriptor_isIType(instr->descriptor))) { + } else if (RabbitizerInstrDescriptor_isRType(instr->descriptor) || + (RabbitizerInstrDescriptor_isBranch(instr->descriptor) && RabbitizerInstrDescriptor_isIType(instr->descriptor))) { // $at usually is a one-use reg uint8_t at = 0; @@ -139,11 +138,11 @@ void RabbitizerRegistersTracker_overwriteRegisters(RabbitizerRegistersTracker *s if (shouldRemove) { state = &self->registers[reg]; - #if 0 +#if 0 if (state->hasLuiValue) { self->_printDebugInfo_clearRegister(instr, reg) } - #endif +#endif RabbitizerTrackedRegisterState_clearHi(state); if (!RabbitizerTrackedRegisterState_wasSetInCurrentOffset(state, instrOffset)) { @@ -152,7 +151,8 @@ void RabbitizerRegistersTracker_overwriteRegisters(RabbitizerRegistersTracker *s } } -void RabbitizerRegistersTracker_unsetRegistersAfterFuncCall(RabbitizerRegistersTracker *self, UNUSED const RabbitizerInstruction *instr, const RabbitizerInstruction *prevInstr) { +void RabbitizerRegistersTracker_unsetRegistersAfterFuncCall(RabbitizerRegistersTracker *self, UNUSED const RabbitizerInstruction *instr, + const RabbitizerInstruction *prevInstr) { RabbitizerTrackedRegisterState *state = NULL; if (!RabbitizerInstrDescriptor_doesLink(prevInstr->descriptor)) { @@ -181,11 +181,11 @@ void RabbitizerRegistersTracker_unsetRegistersAfterFuncCall(RabbitizerRegistersT case RABBITIZER_REG_GPR_O32_t9: case RABBITIZER_REG_GPR_O32_ra: state = &self->registers[reg]; - #if 0 +#if 0 if (state.hasLuiValue) { self->_printDebugInfo_clearRegister(instr, reg) } - #endif +#endif RabbitizerTrackedRegisterState_clear(state); break; @@ -215,11 +215,11 @@ void RabbitizerRegistersTracker_unsetRegistersAfterFuncCall(RabbitizerRegistersT case RABBITIZER_REG_GPR_N32_t9: case RABBITIZER_REG_GPR_N32_ra: state = &self->registers[reg]; - #if 0 +#if 0 if (state.hasLuiValue) { self->_printDebugInfo_clearRegister(instr, reg) } - #endif +#endif RabbitizerTrackedRegisterState_clear(state); break; @@ -230,7 +230,8 @@ void RabbitizerRegistersTracker_unsetRegistersAfterFuncCall(RabbitizerRegistersT } } -bool RabbitizerRegistersTracker_getAddressIfCanSetType(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int instrOffset, uint32_t *dstAddress) { +bool RabbitizerRegistersTracker_getAddressIfCanSetType(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int instrOffset, + uint32_t *dstAddress) { RabbitizerTrackedRegisterState *state = &self->registers[RAB_INSTR_GET_rs(instr)]; if (!state->hasLoValue) { @@ -257,9 +258,9 @@ bool RabbitizerRegistersTracker_getJrInfo(RabbitizerRegistersTracker *self, cons return true; } - // prevInstr can be NULL -void RabbitizerRegistersTracker_processLui(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int instrOffset, const RabbitizerInstruction *prevInstr) { +void RabbitizerRegistersTracker_processLui(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int instrOffset, + const RabbitizerInstruction *prevInstr) { RabbitizerTrackedRegisterState *state = NULL; assert(RabbitizerInstrDescriptor_canBeHi(instr->descriptor)); @@ -292,7 +293,8 @@ void RabbitizerRegistersTracker_processConstant(RabbitizerRegistersTracker *self RabbitizerTrackedRegisterState_setLo(stateDst, value, offset); } -bool RabbitizerRegistersTracker_getLuiOffsetForLo(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int instrOffset, int *dstOffset, bool *dstIsGp) { +bool RabbitizerRegistersTracker_getLuiOffsetForLo(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int instrOffset, int *dstOffset, + bool *dstIsGp) { RabbitizerTrackedRegisterState *state = &self->registers[RAB_INSTR_GET_rs(instr)]; if (state->hasLuiValue && !state->luiSetOnBranchLikely) { @@ -344,7 +346,6 @@ bool RabbitizerRegistersTracker_hasLoButNoHi(RabbitizerRegistersTracker *self, c return state->hasLoValue && !state->hasLuiValue; } - #if 0 def _printDebugInfo_clearRegister(self, instr: rabbitizer.Instruction, reg: int, currentVram: int|None=None) -> None: if not common.GlobalConfig.PRINT_SYMBOL_FINDER_DEBUG_INFO: diff --git a/src/analysis/RabbitizerTrackedRegisterState.c b/src/analysis/RabbitizerTrackedRegisterState.c index 849dfbc..15e38e4 100644 --- a/src/analysis/RabbitizerTrackedRegisterState.c +++ b/src/analysis/RabbitizerTrackedRegisterState.c @@ -7,7 +7,6 @@ #include "common/Utils.h" - void RabbitizerTrackedRegisterState_init(RabbitizerTrackedRegisterState *self, int registerNum) { self->registerNum = registerNum; @@ -26,7 +25,6 @@ void RabbitizerTrackedRegisterState_init(RabbitizerTrackedRegisterState *self, i void RabbitizerTrackedRegisterState_destroy(UNUSED RabbitizerTrackedRegisterState *self) { } - void RabbitizerTrackedRegisterState_clear(RabbitizerTrackedRegisterState *self) { self->hasLuiValue = false; self->luiOffset = 0; @@ -52,7 +50,6 @@ void RabbitizerTrackedRegisterState_clearLo(RabbitizerTrackedRegisterState *self self->value = 0; } - void RabbitizerTrackedRegisterState_copyState(RabbitizerTrackedRegisterState *self, const RabbitizerTrackedRegisterState *other) { self->hasLuiValue = other->hasLuiValue; self->luiOffset = other->luiOffset; @@ -66,7 +63,6 @@ void RabbitizerTrackedRegisterState_copyState(RabbitizerTrackedRegisterState *se self->value = other->value; } - void RabbitizerTrackedRegisterState_setHi(RabbitizerTrackedRegisterState *self, uint32_t value, int offset) { self->hasLuiValue = true; self->luiOffset = offset; @@ -81,7 +77,6 @@ void RabbitizerTrackedRegisterState_setLo(RabbitizerTrackedRegisterState *self, self->dereferenceOffset = 0; } - void RabbitizerTrackedRegisterState_deref(RabbitizerTrackedRegisterState *self, int offset) { self->dereferenced = true; self->dereferenceOffset = offset; @@ -95,7 +90,6 @@ void RabbitizerTrackedRegisterState_dereferenceState(RabbitizerTrackedRegisterSt RabbitizerTrackedRegisterState_deref(self, offset); } - bool RabbitizerTrackedRegisterState_hasAnyValue(const RabbitizerTrackedRegisterState *self) { return self->hasLuiValue || self->hasLoValue; } diff --git a/src/common/RabbitizerConfig.c b/src/common/RabbitizerConfig.c index ff61691..a6756dc 100644 --- a/src/common/RabbitizerConfig.c +++ b/src/common/RabbitizerConfig.c @@ -5,7 +5,6 @@ #include - RabbitizerAbi RabbitizerAbi_fromStr(const char *name) { if (strcmp(name, "32") == 0 || strcmp(name, "o32") == 0 || strcmp(name, "O32") == 0) { return RABBITIZER_ABI_O32; @@ -19,7 +18,6 @@ RabbitizerAbi RabbitizerAbi_fromStr(const char *name) { return RABBITIZER_ABI_NUMERIC; } - RabbitizerConfig RabbitizerConfig_Cfg = { .regNames = { .namedRegisters = true, diff --git a/src/common/Utils.c b/src/common/Utils.c index 6cdfbd0..4ea97aa 100644 --- a/src/common/Utils.c +++ b/src/common/Utils.c @@ -6,7 +6,6 @@ #include #include - int32_t RabbitizerUtils_From2Complement(uint32_t number, int bits) { bool isNegative = number & (1 << (bits - 1)); diff --git a/src/instructions/RabbitizerInstrDescriptor.c b/src/instructions/RabbitizerInstrDescriptor.c index 73d5b6f..0a56d40 100644 --- a/src/instructions/RabbitizerInstrDescriptor.c +++ b/src/instructions/RabbitizerInstrDescriptor.c @@ -5,23 +5,18 @@ #include "instructions/RabbitizerInstruction.h" +#define RABBITIZER_DEF_INSTR_ID(prefix, name, ...) [RABBITIZER_INSTR_ID_##prefix##_##name] = { __VA_ARGS__ } -#define RABBITIZER_DEF_INSTR_ID(prefix, name, ...) \ - [RABBITIZER_INSTR_ID_##prefix##_##name] = { __VA_ARGS__ } - -#define RABBITIZER_DEF_INSTR_ID_ALTNAME(prefix, name, altname, ...) \ - [RABBITIZER_INSTR_ID_##prefix##_##name] = { __VA_ARGS__ } - +#define RABBITIZER_DEF_INSTR_ID_ALTNAME(prefix, name, altname, ...) [RABBITIZER_INSTR_ID_##prefix##_##name] = { __VA_ARGS__ } const RabbitizerInstrDescriptor RabbitizerInstrDescriptor_Descriptors[] = { - #include "instructions/instr_id/RabbitizerInstrId_cpu.inc" - #include "instructions/instr_id/RabbitizerInstrId_rsp.inc" +#include "instructions/instr_id/RabbitizerInstrId_cpu.inc" +#include "instructions/instr_id/RabbitizerInstrId_rsp.inc" }; #undef RABBITIZER_DEF_INSTR_ID #undef RABBITIZER_DEF_INSTR_ID_ALTNAME - bool RabbitizerInstrDescriptor_isUnknownType(const RabbitizerInstrDescriptor *self) { return self->instrType == RABBITIZER_INSTR_TYPE_UNKNOWN; } diff --git a/src/instructions/RabbitizerInstrId.c b/src/instructions/RabbitizerInstrId.c index 8934fb1..e5ba9a3 100644 --- a/src/instructions/RabbitizerInstrId.c +++ b/src/instructions/RabbitizerInstrId.c @@ -5,23 +5,18 @@ #include +#define RABBITIZER_DEF_INSTR_ID(prefix, name, ...) [RABBITIZER_INSTR_ID_##prefix##_##name] = #name -#define RABBITIZER_DEF_INSTR_ID(prefix, name, ...) \ - [RABBITIZER_INSTR_ID_##prefix##_##name] = #name - -#define RABBITIZER_DEF_INSTR_ID_ALTNAME(prefix, name, altname, ...) \ - [RABBITIZER_INSTR_ID_##prefix##_##name] = #altname - +#define RABBITIZER_DEF_INSTR_ID_ALTNAME(prefix, name, altname, ...) [RABBITIZER_INSTR_ID_##prefix##_##name] = #altname const char *RabbitizerInstrId_Names[] = { - #include "instructions/instr_id/RabbitizerInstrId_cpu.inc" - #include "instructions/instr_id/RabbitizerInstrId_rsp.inc" +#include "instructions/instr_id/RabbitizerInstrId_cpu.inc" +#include "instructions/instr_id/RabbitizerInstrId_rsp.inc" }; #undef RABBITIZER_DEF_INSTR_ID #undef RABBITIZER_DEF_INSTR_ID_ALTNAME - const char *RabbitizerInstrId_getOpcodeName(RabbitizerInstrId uniqueId) { assert(uniqueId >= RABBITIZER_INSTR_ID_cpu_INVALID && uniqueId < RABBITIZER_INSTR_ID_ALL_MAX); assert(uniqueId != RABBITIZER_INSTR_ID_cpu_MAX); diff --git a/src/instructions/RabbitizerInstruction/RabbitizerInstruction.c b/src/instructions/RabbitizerInstruction/RabbitizerInstruction.c index 45ebb8f..a354ead 100644 --- a/src/instructions/RabbitizerInstruction/RabbitizerInstruction.c +++ b/src/instructions/RabbitizerInstruction/RabbitizerInstruction.c @@ -9,7 +9,6 @@ #include "instructions/RabbitizerRegister.h" #include "instructions/RabbitizerInstructionRsp.h" - void RabbitizerInstruction_init(RabbitizerInstruction *self, uint32_t word, uint32_t vram) { self->word = word; self->_mandatorybits = 0; @@ -27,7 +26,6 @@ void RabbitizerInstruction_destroy(RabbitizerInstruction *self) { (void)self; } - /* General getters */ uint32_t RabbitizerInstruction_getRaw(const RabbitizerInstruction *self) { @@ -55,7 +53,7 @@ uint32_t RabbitizerInstruction_getInstrIndexAsVram(const RabbitizerInstruction * vram |= 0x80000000; } else { // Jumps are PC-region branches. The upper bits are filled with the address in the delay slot - vram |= (self->vram+4) & 0xFF000000; + vram |= (self->vram + 4) & 0xFF000000; } return vram; } @@ -63,7 +61,7 @@ uint32_t RabbitizerInstruction_getInstrIndexAsVram(const RabbitizerInstruction * int32_t RabbitizerInstruction_getBranchOffset(const RabbitizerInstruction *self) { int32_t diff = RabbitizerUtils_From2Complement(RabbitizerInstruction_getImmediate(self), 16); - return diff*4 + 4; + return diff * 4 + 4; } int32_t RabbitizerInstruction_getGenericBranchOffset(const RabbitizerInstruction *self, uint32_t currentVram) { @@ -75,7 +73,6 @@ int32_t RabbitizerInstruction_getGenericBranchOffset(const RabbitizerInstruction /* General getters */ - void RabbitizerInstruction_blankOut(RabbitizerInstruction *self) { size_t i; @@ -192,7 +189,7 @@ void RabbitizerInstruction_blankOut(RabbitizerInstruction *self) { case RABBITIZER_OPERAND_TYPE_RSP_offset_rs: self->word = RAB_INSTR_RSP_PACK_offset(self->word, 0); - self->word = RAB_INSTR_PACK_rs(self->word, 0);; + self->word = RAB_INSTR_PACK_rs(self->word, 0); break; case RABBITIZER_OPERAND_TYPE_INVALID: diff --git a/src/instructions/RabbitizerInstruction/RabbitizerInstruction_Disassemble.c b/src/instructions/RabbitizerInstruction/RabbitizerInstruction_Disassemble.c index 5658bf0..4780900 100644 --- a/src/instructions/RabbitizerInstruction/RabbitizerInstruction_Disassemble.c +++ b/src/instructions/RabbitizerInstruction/RabbitizerInstruction_Disassemble.c @@ -12,38 +12,35 @@ #include "common/RabbitizerConfig.h" #include "instructions/RabbitizerRegister.h" - #define RABUTILS_BUFFER_ADVANCE(buffer, totalSize, expression) \ - do { \ - size_t __tempSize = expression; \ - (buffer) += __tempSize; \ - (totalSize) += __tempSize; \ - } while(0) + do { \ + size_t __tempSize = expression; \ + (buffer) += __tempSize; \ + (totalSize) += __tempSize; \ + } while (0) #define RABUTILS_BUFFER_WRITE_CHAR(buffer, totalSize, character) \ - do { \ - *(buffer) = (character); \ - RABUTILS_BUFFER_ADVANCE(buffer, totalSize, 1); \ - } while(0) + do { \ + *(buffer) = (character); \ + RABUTILS_BUFFER_ADVANCE(buffer, totalSize, 1); \ + } while (0) #define RABUTILS_BUFFER_SPRINTF(buffer, totalSize, format, ...) \ - do { \ - int _len = sprintf(buffer, format, __VA_ARGS__); \ - assert(_len > 0); \ - RABUTILS_BUFFER_ADVANCE(buffer, totalSize, _len); \ - } while(0) + do { \ + int _len = sprintf(buffer, format, __VA_ARGS__); \ + assert(_len > 0); \ + RABUTILS_BUFFER_ADVANCE(buffer, totalSize, _len); \ + } while (0) -#define RABUTILS_BUFFER_CPY(buffer, totalSize, string) \ - do { \ - size_t _tempSize = strlen(string); \ - memcpy(buffer, string, _tempSize); \ +#define RABUTILS_BUFFER_CPY(buffer, totalSize, string) \ + do { \ + size_t _tempSize = strlen(string); \ + memcpy(buffer, string, _tempSize); \ RABUTILS_BUFFER_ADVANCE(buffer, totalSize, _tempSize); \ - } while(0) - + } while (0) typedef size_t (*OperandCallback)(const RabbitizerInstruction *self, char *dst, const char *immOverride, size_t immOverrideLength); - size_t RabbitizerOperandType_processRs(const RabbitizerInstruction *self, char *dst, UNUSED const char *immOverride, UNUSED size_t immOverrideLength) { size_t totalSize = 0; const char *reg = RabbitizerRegister_getNameGpr(RAB_INSTR_GET_rs(self)); @@ -119,14 +116,14 @@ size_t RabbitizerOperandType_processCop2t(const RabbitizerInstruction *self, cha size_t RabbitizerOperandType_processSa(const RabbitizerInstruction *self, char *dst, UNUSED const char *immOverride, UNUSED size_t immOverrideLength) { size_t totalSize = 0; - // TODO: consider making this a proper configuration - #if 0 +// TODO: consider making this a proper configuration +#if 0 if (RAB_INSTR_GET_sa(self) < 10) { RABUTILS_BUFFER_SPRINTF(dst, totalSize, "%i", RAB_INSTR_GET_sa(self)); } else { RABUTILS_BUFFER_SPRINTF(dst, totalSize, "0x%x", RAB_INSTR_GET_sa(self)); } - #endif +#endif RABUTILS_BUFFER_SPRINTF(dst, totalSize, "%i", RAB_INSTR_GET_sa(self)); return totalSize; } @@ -134,14 +131,14 @@ size_t RabbitizerOperandType_processSa(const RabbitizerInstruction *self, char * size_t RabbitizerOperandType_processOp(const RabbitizerInstruction *self, char *dst, UNUSED const char *immOverride, UNUSED size_t immOverrideLength) { size_t totalSize = 0; - // TODO: consider making this a proper configuration - #if 0 +// TODO: consider making this a proper configuration +#if 0 if (RAB_INSTR_GET_op(self) < 10) { RABUTILS_BUFFER_SPRINTF(dst, totalSize, "%i", RAB_INSTR_GET_op(self)); } else { RABUTILS_BUFFER_SPRINTF(dst, totalSize, "0x%x", RAB_INSTR_GET_op(self)); } - #endif +#endif RABUTILS_BUFFER_SPRINTF(dst, totalSize, "0x%02X", RAB_INSTR_GET_op(self)); return totalSize; } @@ -206,12 +203,12 @@ size_t RabbitizerOperandType_processImmediate(const RabbitizerInstruction *self, size_t RabbitizerOperandType_processImmediateBase(const RabbitizerInstruction *self, char *dst, const char *immOverride, size_t immOverrideLength) { size_t totalSize = 0; - // TODO: consider making this a proper configuration - #if 0 +// TODO: consider making this a proper configuration +#if 0 if (immOverride != NULL || RAB_INSTR_GET_immediate(self) != 0) { RABUTILS_BUFFER_ADVANCE(dst, totalSize, RabbitizerOperandType_processImmediate(self, dst, immOverride, immOverrideLength)); } - #endif +#endif RABUTILS_BUFFER_ADVANCE(dst, totalSize, RabbitizerOperandType_processImmediate(self, dst, immOverride, immOverrideLength)); RABUTILS_BUFFER_WRITE_CHAR(dst, totalSize, '('); @@ -331,38 +328,37 @@ size_t RabbitizerOperandTypeRsp_processOffsetVs(const RabbitizerInstruction *sel } const OperandCallback instrOpercandCallbacks[] = { - [RABBITIZER_OPERAND_TYPE_rs] = RabbitizerOperandType_processRs, - [RABBITIZER_OPERAND_TYPE_rt] = RabbitizerOperandType_processRt, - [RABBITIZER_OPERAND_TYPE_rd] = RabbitizerOperandType_processRd, - [RABBITIZER_OPERAND_TYPE_cop0d] = RabbitizerOperandType_processCop0d, - [RABBITIZER_OPERAND_TYPE_fs] = RabbitizerOperandType_processFs, - [RABBITIZER_OPERAND_TYPE_ft] = RabbitizerOperandType_processFt, - [RABBITIZER_OPERAND_TYPE_fd] = RabbitizerOperandType_processFd, - [RABBITIZER_OPERAND_TYPE_cop1cs] = RabbitizerOperandType_processCop1Cs, - [RABBITIZER_OPERAND_TYPE_cop2t] = RabbitizerOperandType_processCop2t, - [RABBITIZER_OPERAND_TYPE_sa] = RabbitizerOperandType_processSa, - [RABBITIZER_OPERAND_TYPE_op] = RabbitizerOperandType_processOp, - [RABBITIZER_OPERAND_TYPE_code] = RabbitizerOperandType_processCode, - [RABBITIZER_OPERAND_TYPE_LABEL] = RabbitizerOperandType_processLabel, - [RABBITIZER_OPERAND_TYPE_IMM] = RabbitizerOperandType_processImmediate, - [RABBITIZER_OPERAND_TYPE_IMM_base] = RabbitizerOperandType_processImmediateBase, + [RABBITIZER_OPERAND_TYPE_rs] = RabbitizerOperandType_processRs, + [RABBITIZER_OPERAND_TYPE_rt] = RabbitizerOperandType_processRt, + [RABBITIZER_OPERAND_TYPE_rd] = RabbitizerOperandType_processRd, + [RABBITIZER_OPERAND_TYPE_cop0d] = RabbitizerOperandType_processCop0d, + [RABBITIZER_OPERAND_TYPE_fs] = RabbitizerOperandType_processFs, + [RABBITIZER_OPERAND_TYPE_ft] = RabbitizerOperandType_processFt, + [RABBITIZER_OPERAND_TYPE_fd] = RabbitizerOperandType_processFd, + [RABBITIZER_OPERAND_TYPE_cop1cs] = RabbitizerOperandType_processCop1Cs, + [RABBITIZER_OPERAND_TYPE_cop2t] = RabbitizerOperandType_processCop2t, + [RABBITIZER_OPERAND_TYPE_sa] = RabbitizerOperandType_processSa, + [RABBITIZER_OPERAND_TYPE_op] = RabbitizerOperandType_processOp, + [RABBITIZER_OPERAND_TYPE_code] = RabbitizerOperandType_processCode, + [RABBITIZER_OPERAND_TYPE_LABEL] = RabbitizerOperandType_processLabel, + [RABBITIZER_OPERAND_TYPE_IMM] = RabbitizerOperandType_processImmediate, + [RABBITIZER_OPERAND_TYPE_IMM_base] = RabbitizerOperandType_processImmediateBase, // rsp - [RABBITIZER_OPERAND_TYPE_RSP_rs] = RabbitizerOperandTypeRsp_processRs, - [RABBITIZER_OPERAND_TYPE_RSP_rt] = RabbitizerOperandTypeRsp_processRt, - [RABBITIZER_OPERAND_TYPE_RSP_rd] = RabbitizerOperandTypeRsp_processRd, - [RABBITIZER_OPERAND_TYPE_RSP_cop0d] = RabbitizerOperandTypeRsp_processCop0d, - [RABBITIZER_OPERAND_TYPE_RSP_vs] = RabbitizerOperandTypeRsp_processVs, - [RABBITIZER_OPERAND_TYPE_RSP_vt] = RabbitizerOperandTypeRsp_processVt, - [RABBITIZER_OPERAND_TYPE_RSP_vd] = RabbitizerOperandTypeRsp_processVd, - [RABBITIZER_OPERAND_TYPE_RSP_vt_elementhigh] = RabbitizerOperandTypeRsp_processVtElementhigh, - [RABBITIZER_OPERAND_TYPE_RSP_vt_elementlow] = RabbitizerOperandTypeRsp_processVtElementlow, - [RABBITIZER_OPERAND_TYPE_RSP_vd_vs] = RabbitizerOperandTypeRsp_processVdVs, - [RABBITIZER_OPERAND_TYPE_RSP_vd_index] = RabbitizerOperandTypeRsp_processVdIndex, - [RABBITIZER_OPERAND_TYPE_RSP_offset_rs] = RabbitizerOperandTypeRsp_processOffsetVs, + [RABBITIZER_OPERAND_TYPE_RSP_rs] = RabbitizerOperandTypeRsp_processRs, + [RABBITIZER_OPERAND_TYPE_RSP_rt] = RabbitizerOperandTypeRsp_processRt, + [RABBITIZER_OPERAND_TYPE_RSP_rd] = RabbitizerOperandTypeRsp_processRd, + [RABBITIZER_OPERAND_TYPE_RSP_cop0d] = RabbitizerOperandTypeRsp_processCop0d, + [RABBITIZER_OPERAND_TYPE_RSP_vs] = RabbitizerOperandTypeRsp_processVs, + [RABBITIZER_OPERAND_TYPE_RSP_vt] = RabbitizerOperandTypeRsp_processVt, + [RABBITIZER_OPERAND_TYPE_RSP_vd] = RabbitizerOperandTypeRsp_processVd, + [RABBITIZER_OPERAND_TYPE_RSP_vt_elementhigh] = RabbitizerOperandTypeRsp_processVtElementhigh, + [RABBITIZER_OPERAND_TYPE_RSP_vt_elementlow] = RabbitizerOperandTypeRsp_processVtElementlow, + [RABBITIZER_OPERAND_TYPE_RSP_vd_vs] = RabbitizerOperandTypeRsp_processVdVs, + [RABBITIZER_OPERAND_TYPE_RSP_vd_index] = RabbitizerOperandTypeRsp_processVdIndex, + [RABBITIZER_OPERAND_TYPE_RSP_offset_rs] = RabbitizerOperandTypeRsp_processOffsetVs, }; - size_t RabbitizerInstruction_getSizeForBufferOperandsDisasm(const RabbitizerInstruction *self, size_t immOverrideLength) { size_t totalSize = 0; @@ -405,7 +401,6 @@ size_t RabbitizerInstruction_disassembleOperands(const RabbitizerInstruction *se return totalSize; } - size_t RabbitizerInstruction_getSizeForBufferInstrDisasm(const RabbitizerInstruction *self, size_t immOverrideLength, int extraLJust) { size_t totalSize = 0; size_t opcodeNameLength; @@ -427,7 +422,8 @@ size_t RabbitizerInstruction_getSizeForBufferInstrDisasm(const RabbitizerInstruc return totalSize; } -size_t RabbitizerInstruction_disassembleInstruction(const RabbitizerInstruction *self, char *dst, const char *immOverride, size_t immOverrideLength, int extraLJust) { +size_t RabbitizerInstruction_disassembleInstruction(const RabbitizerInstruction *self, char *dst, const char *immOverride, size_t immOverrideLength, + int extraLJust) { size_t totalSize = 0; const char *opcodeName = RabbitizerInstrId_getOpcodeName(self->uniqueId); @@ -448,7 +444,6 @@ size_t RabbitizerInstruction_disassembleInstruction(const RabbitizerInstruction return totalSize; } - size_t RabbitizerInstruction_getSizeForBufferDataDisasm(UNUSED const RabbitizerInstruction *self, int extraLJust) { size_t totalSize = 0; @@ -458,7 +453,6 @@ size_t RabbitizerInstruction_getSizeForBufferDataDisasm(UNUSED const RabbitizerI return totalSize; } - size_t RabbitizerInstruction_disassembleAsData(const RabbitizerInstruction *self, char *dst, int extraLJust) { size_t totalSize = 0; @@ -470,7 +464,6 @@ size_t RabbitizerInstruction_disassembleAsData(const RabbitizerInstruction *self return totalSize; } - bool RabbitizerInstruction_mustDisasmAsData(const RabbitizerInstruction *self) { if (RabbitizerConfig_Cfg.toolchainTweaks.sn64DivFix) { if (self->uniqueId == RABBITIZER_INSTR_ID_cpu_break) { @@ -484,7 +477,6 @@ bool RabbitizerInstruction_mustDisasmAsData(const RabbitizerInstruction *self) { return false; } - size_t RabbitizerInstruction_getSizeForBuffer(const RabbitizerInstruction *self, size_t immOverrideLength, int extraLJust) { if (!RabbitizerInstruction_isImplemented(self) || RabbitizerInstruction_mustDisasmAsData(self)) { size_t totalSize = RabbitizerInstruction_getSizeForBufferDataDisasm(self, extraLJust); @@ -497,10 +489,9 @@ size_t RabbitizerInstruction_getSizeForBuffer(const RabbitizerInstruction *self, return totalSize; } - return RabbitizerInstruction_getSizeForBufferInstrDisasm(self,immOverrideLength, extraLJust); + return RabbitizerInstruction_getSizeForBufferInstrDisasm(self, immOverrideLength, extraLJust); } - size_t RabbitizerInstruction_disassemble(const RabbitizerInstruction *self, char *dst, const char *immOverride, size_t immOverrideLength, int extraLJust) { assert(dst != NULL); @@ -510,7 +501,7 @@ size_t RabbitizerInstruction_disassemble(const RabbitizerInstruction *self, char RABUTILS_BUFFER_ADVANCE(dst, totalSize, RabbitizerInstruction_disassembleAsData(self, dst, extraLJust)); if (RabbitizerConfig_Cfg.misc.unknownInstrComment) { - RABUTILS_BUFFER_ADVANCE(dst, totalSize, RabbitizerUtils_CharFill(dst, 40-totalSize, ' ')); + RABUTILS_BUFFER_ADVANCE(dst, totalSize, RabbitizerUtils_CharFill(dst, 40 - totalSize, ' ')); RABUTILS_BUFFER_WRITE_CHAR(dst, totalSize, ' '); RABUTILS_BUFFER_WRITE_CHAR(dst, totalSize, '#'); diff --git a/src/instructions/RabbitizerInstruction/RabbitizerInstruction_Examination.c b/src/instructions/RabbitizerInstruction/RabbitizerInstruction_Examination.c index 0690834..b79e959 100644 --- a/src/instructions/RabbitizerInstruction/RabbitizerInstruction_Examination.c +++ b/src/instructions/RabbitizerInstruction/RabbitizerInstruction_Examination.c @@ -9,7 +9,6 @@ #include "instructions/RabbitizerInstructionRsp.h" #include "instructions/RabbitizerRegister.h" - bool RabbitizerInstruction_isImplemented(const RabbitizerInstruction *self) { if (self->uniqueId == RABBITIZER_INSTR_ID_cpu_INVALID) { return false; @@ -120,7 +119,6 @@ bool RabbitizerInstruction_sameOpcodeButDifferentArguments(const RabbitizerInstr return RabbitizerInstruction_getRaw(self) != RabbitizerInstruction_getRaw(other); } - bool RabbitizerInstruction_hasOperand(const RabbitizerInstruction *self, RabbitizerOperandType operand) { size_t i; @@ -187,7 +185,6 @@ bool RabbitizerInstruction_hasOperandAlias(const RabbitizerInstruction *self, Ra } break; - /* rsp */ case RABBITIZER_OPERAND_TYPE_RSP_rs: if (RabbitizerInstruction_hasOperand(self, RABBITIZER_OPERAND_TYPE_rs)) { @@ -213,10 +210,10 @@ bool RabbitizerInstruction_hasOperandAlias(const RabbitizerInstruction *self, Ra case RABBITIZER_OPERAND_TYPE_RSP_cop0d: break; - // case RABBITIZER_OPERAND_TYPE_RSP_elementhigh: - // case RABBITIZER_OPERAND_TYPE_RSP_elementlow: - // case RABBITIZER_OPERAND_TYPE_RSP_index: - // case RABBITIZER_OPERAND_TYPE_RSP_offset: + // case RABBITIZER_OPERAND_TYPE_RSP_elementhigh: + // case RABBITIZER_OPERAND_TYPE_RSP_elementlow: + // case RABBITIZER_OPERAND_TYPE_RSP_index: + // case RABBITIZER_OPERAND_TYPE_RSP_offset: case RABBITIZER_OPERAND_TYPE_RSP_vs: if (RabbitizerInstruction_hasOperand(self, RABBITIZER_OPERAND_TYPE_RSP_vd_vs)) { @@ -274,7 +271,7 @@ bool RabbitizerInstruction_hasOperandAlias(const RabbitizerInstruction *self, Ra return true; } break; - /* rsp */ + /* rsp */ case RABBITIZER_OPERAND_TYPE_INVALID: case RABBITIZER_OPERAND_TYPE_MAX: @@ -285,7 +282,6 @@ bool RabbitizerInstruction_hasOperandAlias(const RabbitizerInstruction *self, Ra return RabbitizerInstruction_hasOperand(self, operand); } - bool RabbitizerInstruction_isValid(const RabbitizerInstruction *self) { size_t i; uint32_t validbits; @@ -356,7 +352,6 @@ bool RabbitizerInstruction_isValid(const RabbitizerInstruction *self) { validbits = RAB_INSTR_PACK_rs(validbits, ~0); break; - case RABBITIZER_OPERAND_TYPE_RSP_rs: validbits = RAB_INSTR_PACK_rs(validbits, ~0); break; diff --git a/src/instructions/RabbitizerInstruction/RabbitizerInstruction_ProcessUniqueId.c b/src/instructions/RabbitizerInstruction/RabbitizerInstruction_ProcessUniqueId.c index bb5a1aa..52967be 100644 --- a/src/instructions/RabbitizerInstruction/RabbitizerInstruction_ProcessUniqueId.c +++ b/src/instructions/RabbitizerInstruction/RabbitizerInstruction_ProcessUniqueId.c @@ -6,7 +6,6 @@ #include "common/RabbitizerConfig.h" #include "instructions/RabbitizerRegister.h" - void RabbitizerInstruction_processUniqueId_Normal(RabbitizerInstruction *self) { uint32_t opcode = RAB_INSTR_GET_opcode(self); @@ -172,7 +171,7 @@ void RabbitizerInstruction_processUniqueId_Normal(RabbitizerInstruction *self) { break; case 0b111010: self->uniqueId = RABBITIZER_INSTR_ID_cpu_swc2; - // 0b111011: "", + // 0b111011: "", break; case 0b111100: self->uniqueId = RABBITIZER_INSTR_ID_cpu_scd; @@ -224,7 +223,6 @@ void RabbitizerInstruction_processUniqueId_Normal(RabbitizerInstruction *self) { self->descriptor = &RabbitizerInstrDescriptor_Descriptors[self->uniqueId]; } - void RabbitizerInstruction_processUniqueId_Special(RabbitizerInstruction *self) { uint32_t function = RAB_INSTR_GET_function(self); @@ -389,7 +387,7 @@ void RabbitizerInstruction_processUniqueId_Special(RabbitizerInstruction *self) case 0b110110: self->uniqueId = RABBITIZER_INSTR_ID_cpu_tne; break; - // 0b110_111: "", + // 0b110_111: "", case 0b111000: self->uniqueId = RABBITIZER_INSTR_ID_cpu_dsll; @@ -483,7 +481,6 @@ void RabbitizerInstruction_processUniqueId_Special(RabbitizerInstruction *self) } } - void RabbitizerInstruction_processUniqueId_Regimm(RabbitizerInstruction *self) { uint32_t rt = RAB_INSTR_GET_rt(self); @@ -544,7 +541,6 @@ void RabbitizerInstruction_processUniqueId_Regimm(RabbitizerInstruction *self) { self->descriptor = &RabbitizerInstrDescriptor_Descriptors[self->uniqueId]; } - void RabbitizerInstruction_processUniqueId_Coprocessor0(RabbitizerInstruction *self) { uint32_t fmt = RAB_INSTR_GET_fmt(self); uint32_t tf; @@ -574,7 +570,7 @@ void RabbitizerInstruction_processUniqueId_Coprocessor0(RabbitizerInstruction *s case 0b00110: self->uniqueId = RABBITIZER_INSTR_ID_cpu_ctc0; break; - // 0b00_111: "", + // 0b00_111: "", case 0b01000: tf = RAB_INSTR_GET_tf(self); @@ -626,7 +622,6 @@ void RabbitizerInstruction_processUniqueId_Coprocessor0(RabbitizerInstruction *s self->descriptor = &RabbitizerInstrDescriptor_Descriptors[self->uniqueId]; } - void RabbitizerInstruction_processUniqueId_Coprocessor1(RabbitizerInstruction *self) { uint8_t fmt = RAB_INSTR_GET_fmt(self); uint8_t fc; @@ -967,15 +962,12 @@ void RabbitizerInstruction_processUniqueId_Coprocessor1(RabbitizerInstruction *s self->descriptor = &RabbitizerInstrDescriptor_Descriptors[self->uniqueId]; } - void RabbitizerInstruction_processUniqueId_Coprocessor2(RabbitizerInstruction *self) { self->_handwrittenCategory = true; self->descriptor = &RabbitizerInstrDescriptor_Descriptors[self->uniqueId]; } - - void RabbitizerInstruction_processUniqueId(RabbitizerInstruction *self) { uint32_t opcode = RAB_INSTR_GET_opcode(self); diff --git a/src/instructions/RabbitizerInstructionRsp/RabbitizerInstructionRsp.c b/src/instructions/RabbitizerInstructionRsp/RabbitizerInstructionRsp.c index a3880fc..a5d347b 100644 --- a/src/instructions/RabbitizerInstructionRsp/RabbitizerInstructionRsp.c +++ b/src/instructions/RabbitizerInstructionRsp/RabbitizerInstructionRsp.c @@ -3,7 +3,6 @@ #include "instructions/RabbitizerInstructionRsp.h" - void RabbitizerInstructionRsp_init(RabbitizerInstruction *self, uint32_t word, uint32_t vram) { RabbitizerInstruction_init(self, word, vram); @@ -18,7 +17,6 @@ void RabbitizerInstructionRsp_destroy(RabbitizerInstruction *self) { RabbitizerInstruction_destroy(self); } - uint16_t RabbitizerInstructionRsp_GetOffsetVector(const RabbitizerInstruction *self) { uint16_t offset = RAB_INSTR_RSP_GET_OFFSET_VECTOR_RAW(self); diff --git a/src/instructions/RabbitizerInstructionRsp/RabbitizerInstructionRsp_ProcessUniqueId.c b/src/instructions/RabbitizerInstructionRsp/RabbitizerInstructionRsp_ProcessUniqueId.c index 1f48ce1..b6061b5 100644 --- a/src/instructions/RabbitizerInstructionRsp/RabbitizerInstructionRsp_ProcessUniqueId.c +++ b/src/instructions/RabbitizerInstructionRsp/RabbitizerInstructionRsp_ProcessUniqueId.c @@ -5,7 +5,6 @@ #include "common/RabbitizerConfig.h" - void RabbitizerInstructionRsp_processUniqueId_Normal(RabbitizerInstruction *self) { uint32_t opcode = RAB_INSTR_GET_opcode(self); uint32_t rd; @@ -221,7 +220,6 @@ void RabbitizerInstructionRsp_processUniqueId_Normal(RabbitizerInstruction *self self->descriptor = &RabbitizerInstrDescriptor_Descriptors[self->uniqueId]; } - void RabbitizerInstructionRsp_processUniqueId_Special(RabbitizerInstruction *self) { uint32_t function = RAB_INSTR_GET_function(self); @@ -333,7 +331,6 @@ void RabbitizerInstructionRsp_processUniqueId_Special(RabbitizerInstruction *sel } } - void RabbitizerInstructionRsp_processUniqueId_Regimm(RabbitizerInstruction *self) { uint32_t rt = RAB_INSTR_GET_rt(self); @@ -362,7 +359,6 @@ void RabbitizerInstructionRsp_processUniqueId_Regimm(RabbitizerInstruction *self self->descriptor = &RabbitizerInstrDescriptor_Descriptors[self->uniqueId]; } - void RabbitizerInstructionRsp_processUniqueId_Coprocessor0(RabbitizerInstruction *self) { uint32_t fmt = RAB_INSTR_GET_fmt(self); @@ -385,9 +381,8 @@ void RabbitizerInstructionRsp_processUniqueId_Coprocessor0(RabbitizerInstruction self->descriptor = &RabbitizerInstrDescriptor_Descriptors[self->uniqueId]; } - void RabbitizerInstructionRsp_processUniqueId_Coprocessor2(RabbitizerInstruction *self) { - uint32_t aux = SHIFTR(self->word, 25, 1); + uint32_t aux = SHIFTR(self->word, 25, 1); uint32_t elementhigh; uint32_t function; @@ -566,7 +561,6 @@ void RabbitizerInstructionRsp_processUniqueId_Coprocessor2(RabbitizerInstruction self->descriptor = &RabbitizerInstrDescriptor_Descriptors[self->uniqueId]; } - void RabbitizerInstructionRsp_processUniqueId(RabbitizerInstruction *self) { uint32_t opcode = RAB_INSTR_GET_opcode(self); @@ -585,7 +579,7 @@ void RabbitizerInstructionRsp_processUniqueId(RabbitizerInstruction *self) { case 0x10: RabbitizerInstructionRsp_processUniqueId_Coprocessor0(self); break; - //case 0x11: + // case 0x11: // RabbitizerInstructionRsp_processUniqueId_Coprocessor1(self); // break; case 0x12: diff --git a/src/instructions/RabbitizerRegister.c b/src/instructions/RabbitizerRegister.c index 643d6c1..9b40734 100644 --- a/src/instructions/RabbitizerRegister.c +++ b/src/instructions/RabbitizerRegister.c @@ -8,60 +8,56 @@ #include "common/Utils.h" #include "common/RabbitizerConfig.h" +#define RABBITIZER_DEF_REG(prefix, name, numeric) [RABBITIZER_REG_##prefix##_##name] = { "$" #numeric, "$" #name } -#define RABBITIZER_DEF_REG(prefix, name, numeric) \ - [RABBITIZER_REG_##prefix##_##name] = { "$" #numeric, "$" #name } - -#define RABBITIZER_DEF_REG_NODOLLAR(prefix, name, numeric) \ - [RABBITIZER_REG_##prefix##_##name] = { "$" #numeric, #name } +#define RABBITIZER_DEF_REG_NODOLLAR(prefix, name, numeric) [RABBITIZER_REG_##prefix##_##name] = { "$" #numeric, #name } // numeric, named const char *RabbitizerRegister_GprO32_Names[][2] = { - #include "instructions/registers/RabbitizerRegister_GprO32.inc" +#include "instructions/registers/RabbitizerRegister_GprO32.inc" }; const char *RabbitizerRegister_GprN32_Names[][2] = { - #include "instructions/registers/RabbitizerRegister_GprN32.inc" +#include "instructions/registers/RabbitizerRegister_GprN32.inc" }; const char *RabbitizerRegister_Cop0_Names[][2] = { - #include "instructions/registers/RabbitizerRegister_Cop0.inc" +#include "instructions/registers/RabbitizerRegister_Cop0.inc" }; const char *RabbitizerRegister_Cop1O32_Names[][2] = { - #include "instructions/registers/RabbitizerRegister_Cop1O32.inc" +#include "instructions/registers/RabbitizerRegister_Cop1O32.inc" }; const char *RabbitizerRegister_Cop1N32_Names[][2] = { - #include "instructions/registers/RabbitizerRegister_Cop1N32.inc" +#include "instructions/registers/RabbitizerRegister_Cop1N32.inc" }; const char *RabbitizerRegister_Cop1N64_Names[][2] = { - #include "instructions/registers/RabbitizerRegister_Cop1N64.inc" +#include "instructions/registers/RabbitizerRegister_Cop1N64.inc" }; const char *RabbitizerRegister_Cop1Control_Names[][2] = { - #include "instructions/registers/RabbitizerRegister_Cop1Control.inc" +#include "instructions/registers/RabbitizerRegister_Cop1Control.inc" }; const char *RabbitizerRegister_Cop2_Names[][2] = { - #include "instructions/registers/RabbitizerRegister_Cop2.inc" +#include "instructions/registers/RabbitizerRegister_Cop2.inc" }; const char *RabbitizerRegister_RspGpr_Names[][2] = { - #include "instructions/registers/RabbitizerRegister_RspGpr.inc" +#include "instructions/registers/RabbitizerRegister_RspGpr.inc" }; const char *RabbitizerRegister_RspCop0_Names[][2] = { - #include "instructions/registers/RabbitizerRegister_RspCop0.inc" +#include "instructions/registers/RabbitizerRegister_RspCop0.inc" }; const char *RabbitizerRegister_RspVector_Names[][2] = { - #include "instructions/registers/RabbitizerRegister_RspVector.inc" +#include "instructions/registers/RabbitizerRegister_RspVector.inc" }; - const char *RabbitizerRegister_getNameGpr(uint8_t regValue) { assert(regValue < ARRAY_COUNT(RabbitizerRegister_GprO32_Names)); @@ -82,7 +78,8 @@ const char *RabbitizerRegister_getNameGpr(uint8_t regValue) { const char *RabbitizerRegister_getNameCop0(uint8_t regValue) { assert(regValue < ARRAY_COUNT(RabbitizerRegister_Cop0_Names)); - return RabbitizerRegister_Cop0_Names[regValue][RabbitizerConfig_Cfg.regNames.namedRegisters && RabbitizerConfig_Cfg.regNames.vr4300Cop0NamedRegisters ? 1 : 0]; + return RabbitizerRegister_Cop0_Names[regValue] + [RabbitizerConfig_Cfg.regNames.namedRegisters && RabbitizerConfig_Cfg.regNames.vr4300Cop0NamedRegisters ? 1 : 0]; } const char *RabbitizerRegister_getNameCop1(uint8_t regValue) { @@ -125,7 +122,8 @@ const char *RabbitizerRegister_getNameRspGpr(uint8_t regValue) { const char *RabbitizerRegister_getNameRspCop0(uint8_t regValue) { assert(regValue < ARRAY_COUNT(RabbitizerRegister_RspCop0_Names)); - return RabbitizerRegister_RspCop0_Names[regValue][RabbitizerConfig_Cfg.regNames.namedRegisters && RabbitizerConfig_Cfg.regNames.vr4300RspCop0NamedRegisters ? 1 : 0]; + return RabbitizerRegister_RspCop0_Names[regValue] + [RabbitizerConfig_Cfg.regNames.namedRegisters && RabbitizerConfig_Cfg.regNames.vr4300RspCop0NamedRegisters ? 1 : 0]; } const char *RabbitizerRegister_getNameRspVector(uint8_t regValue) { From ef49315db0118fe5e85f173ced619d82234205b6 Mon Sep 17 00:00:00 2001 From: angie Date: Sat, 9 Jul 2022 17:55:57 -0400 Subject: [PATCH 08/13] tidy target on makefile --- .clang-tidy | 9 +++++++++ Makefile | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 .clang-tidy diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..caf62ab --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,9 @@ +Checks: '-*,readability-braces-around-statements,readability-inconsistent-declaration-parameter-name' +WarningsAsErrors: '' +HeaderFilterRegex: '(src|include)\/.*\.h$' +FormatStyle: 'file' +CheckOptions: + # Require argument names to match exactly (instead of allowing a name to be a prefix/suffix of another) + # Note: 'true' is expected by clang-tidy 12+ but '1' is used for compatibility with older versions + - key: readability-inconsistent-declaration-parameter-name.Strict + value: 1 diff --git a/Makefile b/Makefile index 3f10efc..f4e3d0c 100644 --- a/Makefile +++ b/Makefile @@ -59,7 +59,7 @@ format: clang-format-11 -i -style=file $(C_FILES) tidy: - @echo "TODO" + clang-tidy-11 -p . --fix --fix-errors $(C_FILES) $(H_FILES) -- $(CSTD) $(OPTFLAGS) $(IINC) $(WARNINGS) $(CFLAGS) tests: build/test.elf From ef1ce2634c4caa5b0b8356b719278857b2d3d87f Mon Sep 17 00:00:00 2001 From: angie Date: Sat, 9 Jul 2022 18:12:21 -0400 Subject: [PATCH 09/13] add more checks to clang tidy --- .clang-tidy | 2 +- include/instructions/RabbitizerRegister.h | 1 - .../RabbitizerInstruction_ProcessUniqueId.c | 5 +++++ .../RabbitizerInstructionRsp_ProcessUniqueId.c | 5 +++++ 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index caf62ab..8534b8a 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,4 +1,4 @@ -Checks: '-*,readability-braces-around-statements,readability-inconsistent-declaration-parameter-name' +Checks: 'readability-*,-readability-magic-numbers,clang-diagnostic-*,clang-analyzer-*,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling,bugprone*,-bugprone-branch-clone,modernize*,performance*,portability*,diagnostic-*,analyzer-*,misc*,-misc-no-recursion' WarningsAsErrors: '' HeaderFilterRegex: '(src|include)\/.*\.h$' FormatStyle: 'file' diff --git a/include/instructions/RabbitizerRegister.h b/include/instructions/RabbitizerRegister.h index c7b6cb1..efa1edb 100644 --- a/include/instructions/RabbitizerRegister.h +++ b/include/instructions/RabbitizerRegister.h @@ -66,7 +66,6 @@ typedef enum RabbitizerRegister_RspVector { extern const char *RabbitizerRegister_GprO32_Names[][2]; extern const char *RabbitizerRegister_GprN32_Names[][2]; extern const char *RabbitizerRegister_Cop0_Names[][2]; -extern const char *RabbitizerRegister_Cop0_Names[][2]; extern const char *RabbitizerRegister_Cop1O32_Names[][2]; extern const char *RabbitizerRegister_Cop1N32_Names[][2]; extern const char *RabbitizerRegister_Cop1N64_Names[][2]; diff --git a/src/instructions/RabbitizerInstruction/RabbitizerInstruction_ProcessUniqueId.c b/src/instructions/RabbitizerInstruction/RabbitizerInstruction_ProcessUniqueId.c index 52967be..0347853 100644 --- a/src/instructions/RabbitizerInstruction/RabbitizerInstruction_ProcessUniqueId.c +++ b/src/instructions/RabbitizerInstruction/RabbitizerInstruction_ProcessUniqueId.c @@ -6,6 +6,9 @@ #include "common/RabbitizerConfig.h" #include "instructions/RabbitizerRegister.h" + +// NOLINTBEGIN(readability-magic-numbers) + void RabbitizerInstruction_processUniqueId_Normal(RabbitizerInstruction *self) { uint32_t opcode = RAB_INSTR_GET_opcode(self); @@ -994,3 +997,5 @@ void RabbitizerInstruction_processUniqueId(RabbitizerInstruction *self) { break; } } + +// NOLINTEND(readability-magic-numbers) diff --git a/src/instructions/RabbitizerInstructionRsp/RabbitizerInstructionRsp_ProcessUniqueId.c b/src/instructions/RabbitizerInstructionRsp/RabbitizerInstructionRsp_ProcessUniqueId.c index b6061b5..550ef19 100644 --- a/src/instructions/RabbitizerInstructionRsp/RabbitizerInstructionRsp_ProcessUniqueId.c +++ b/src/instructions/RabbitizerInstructionRsp/RabbitizerInstructionRsp_ProcessUniqueId.c @@ -5,6 +5,9 @@ #include "common/RabbitizerConfig.h" + +// NOLINTBEGIN(readability-magic-numbers) + void RabbitizerInstructionRsp_processUniqueId_Normal(RabbitizerInstruction *self) { uint32_t opcode = RAB_INSTR_GET_opcode(self); uint32_t rd; @@ -587,3 +590,5 @@ void RabbitizerInstructionRsp_processUniqueId(RabbitizerInstruction *self) { break; } } + +// NOLINTEND(readability-magic-numbers) From a8703636445662ab45943f3ce77d008e78c65c3d Mon Sep 17 00:00:00 2001 From: angie Date: Sat, 9 Jul 2022 18:15:17 -0400 Subject: [PATCH 10/13] ci builds again... --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f5baa59..6baeb37 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,7 +18,7 @@ jobs: uses: pypa/cibuildwheel@v2.5.0 env: CIBW_ARCHS_WINDOWS: "auto" - CIBW_ARCHS_LINUX: "auto x86_64 aarch64" + CIBW_ARCHS_LINUX: "auto" CIBW_ARCHS_MACOS: "all" - uses: actions/upload-artifact@v2 From a539a8a39df182112f8f15160e74283d2d0c349c Mon Sep 17 00:00:00 2001 From: angie Date: Sat, 9 Jul 2022 19:19:53 -0400 Subject: [PATCH 11/13] Annotate functions with attributes --- include/analysis/RabbitizerRegistersTracker.h | 22 ++++++++-- .../analysis/RabbitizerTrackedRegisterState.h | 14 +++++++ include/common/Utils.h | 25 ++++++++---- .../instructions/RabbitizerInstrDescriptor.h | 25 ++++++++++++ include/instructions/RabbitizerInstrId.h | 3 ++ include/instructions/RabbitizerInstruction.h | 40 ++++++++++++++++++- .../instructions/RabbitizerInstructionRsp.h | 8 ++++ include/instructions/RabbitizerRegister.h | 11 +++++ rabbitizer/rabbitizer_global_config.c | 12 ++---- src/analysis/RabbitizerRegistersTracker.c | 19 ++++----- .../RabbitizerInstruction.c | 3 +- .../RabbitizerInstructionRsp.c | 4 +- 12 files changed, 152 insertions(+), 34 deletions(-) diff --git a/include/analysis/RabbitizerRegistersTracker.h b/include/analysis/RabbitizerRegistersTracker.h index 6ae2f11..2273d6e 100644 --- a/include/analysis/RabbitizerRegistersTracker.h +++ b/include/analysis/RabbitizerRegistersTracker.h @@ -5,6 +5,7 @@ #define RABBITIZER_REGISTERS_TRACKER_H #pragma once +#include "common/Utils.h" #include "RabbitizerTrackedRegisterState.h" #include "instructions/RabbitizerInstruction.h" @@ -14,22 +15,35 @@ typedef struct RabbitizerRegistersTracker { } RabbitizerRegistersTracker; +NON_NULL(1) void RabbitizerRegistersTracker_init(RabbitizerRegistersTracker *self, const RabbitizerRegistersTracker *other); +NON_NULL(1) void RabbitizerRegistersTracker_destroy(RabbitizerRegistersTracker *self); +NON_NULL(1, 2) bool RabbitizerRegistersTracker_moveRegisters(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr); +NON_NULL(1, 2) void RabbitizerRegistersTracker_overwriteRegisters(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int instrOffset); +NON_NULL(1, 2, 3) void RabbitizerRegistersTracker_unsetRegistersAfterFuncCall(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, const RabbitizerInstruction *prevInstr); -bool RabbitizerRegistersTracker_getAddressIfCanSetType(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int instrOffset, uint32_t *dstAddress); -bool RabbitizerRegistersTracker_getJrInfo(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int *dstOffset, uint32_t *dstAddress); +NON_NULL(1, 2, 4) +bool RabbitizerRegistersTracker_getAddressIfCanSetType(const RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int instrOffset, uint32_t *dstAddress); +NON_NULL(1, 2, 3, 4) +bool RabbitizerRegistersTracker_getJrInfo(const RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int *dstOffset, uint32_t *dstAddress); // prevInstr can be NULL +NON_NULL(1, 2) void RabbitizerRegistersTracker_processLui(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int instrOffset, const RabbitizerInstruction *prevInstr); -bool RabbitizerRegistersTracker_getLuiOffsetForConstant(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int *dstOffset); +NON_NULL(1, 2, 3) +bool RabbitizerRegistersTracker_getLuiOffsetForConstant(const RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int *dstOffset); +NON_NULL(1, 2) void RabbitizerRegistersTracker_processConstant(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, uint32_t value, int offset); +NON_NULL(1, 2, 4, 5) bool RabbitizerRegistersTracker_getLuiOffsetForLo(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int instrOffset, int *dstOffset, bool *dstIsGp); +NON_NULL(1, 2) void RabbitizerRegistersTracker_processLo(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, uint32_t value, int offset); -bool RabbitizerRegistersTracker_hasLoButNoHi(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr); +NON_NULL(1, 2) +bool RabbitizerRegistersTracker_hasLoButNoHi(const RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr); #endif diff --git a/include/analysis/RabbitizerTrackedRegisterState.h b/include/analysis/RabbitizerTrackedRegisterState.h index 5120291..2d2db48 100644 --- a/include/analysis/RabbitizerTrackedRegisterState.h +++ b/include/analysis/RabbitizerTrackedRegisterState.h @@ -8,6 +8,8 @@ #include #include +#include "common/Utils.h" + typedef struct RabbitizerTrackedRegisterState { int registerNum; @@ -25,22 +27,34 @@ typedef struct RabbitizerTrackedRegisterState { } RabbitizerTrackedRegisterState; +NON_NULL(1) void RabbitizerTrackedRegisterState_init(RabbitizerTrackedRegisterState *self, int registerNum); +NON_NULL(1) void RabbitizerTrackedRegisterState_destroy(RabbitizerTrackedRegisterState *self); +NON_NULL(1) void RabbitizerTrackedRegisterState_clear(RabbitizerTrackedRegisterState *self); +NON_NULL(1) void RabbitizerTrackedRegisterState_clearHi(RabbitizerTrackedRegisterState *self); +NON_NULL(1) void RabbitizerTrackedRegisterState_clearLo(RabbitizerTrackedRegisterState *self); +NON_NULL(1, 2) void RabbitizerTrackedRegisterState_copyState(RabbitizerTrackedRegisterState *self, const RabbitizerTrackedRegisterState *other); +NON_NULL(1) void RabbitizerTrackedRegisterState_setHi(RabbitizerTrackedRegisterState *self, uint32_t value, int offset); +NON_NULL(1) void RabbitizerTrackedRegisterState_setLo(RabbitizerTrackedRegisterState *self, uint32_t value, int offset); +NON_NULL(1) void RabbitizerTrackedRegisterState_deref(RabbitizerTrackedRegisterState *self, int offset); +NON_NULL(1, 2) void RabbitizerTrackedRegisterState_dereferenceState(RabbitizerTrackedRegisterState *self, const RabbitizerTrackedRegisterState *other, int offset); +NODISCARD NON_NULL(1) PURE bool RabbitizerTrackedRegisterState_hasAnyValue(const RabbitizerTrackedRegisterState *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerTrackedRegisterState_wasSetInCurrentOffset(const RabbitizerTrackedRegisterState *self, int offset); diff --git a/include/common/Utils.h b/include/common/Utils.h index e57cb74..f8c2429 100644 --- a/include/common/Utils.h +++ b/include/common/Utils.h @@ -12,30 +12,39 @@ #define __attribute__(x) #endif -#if __STDC_VERSION__ >= 202300L +#if __STDC_VERSION__ >= 202000L +#define CONST [[gnu::const]] #define DEPRECATED(reason) [[deprecated (reason)]] #define FALLTHROUGH [[fallthrough]] -#define NODISCARD(reason) [[nodiscard (reason)]] +#define NODISCARD [[nodiscard]] #define NORETURN [[noreturn]] +#define NON_NULL(...) [[gnu::nonnull (__VA_ARGS__)]] +#define PURE [[gnu::pure]] +#define RETURNS_NON_NULL [[gnu::returns_nonnull]] #define UNUSED [[maybe_unused]] #else +#define CONST __attribute__((const)) #define DEPRECATED(reason) __attribute__((deprecated (reason))) #define FALLTHROUGH __attribute__((fallthrough)) -#define NODISCARD(reason) __attribute__((warn_unused_result)) +#define NODISCARD __attribute__((warn_unused_result)) #define NORETURN _Noreturn +#define NON_NULL(...) __attribute__((nonnull (__VA_ARGS__))) +#define PURE __attribute__((pure)) +#define RETURNS_NON_NULL __attribute__((returns_nonnull)) #define UNUSED __attribute__((unused)) #endif + #if defined(_MSC_VER) # define UNREACHABLE __assume(0) -#else +#elif defined(__GNUC__) || defined(__clang__) # define UNREACHABLE __builtin_unreachable() +#else +# define UNREACHABLE #endif -#define PURE __attribute__((pure)) - -#define ARRAY_COUNT(arr) (sizeof(arr) / sizeof(arr[0])) +#define ARRAY_COUNT(arr) (sizeof(arr) / sizeof((arr)[0])) #define MASK(v, w) ((v) & ((1 << (w)) - 1)) @@ -58,7 +67,9 @@ #define BITREPACK_RIGHT(fullword, v, s, w) (SHIFTL((v), (s), (w)) | MASK((fullword), (s))) +CONST NODISCARD int32_t RabbitizerUtils_From2Complement(uint32_t number, int bits); +NON_NULL(1) size_t RabbitizerUtils_CharFill(char *dst, int count, char fillchar); #endif diff --git a/include/instructions/RabbitizerInstrDescriptor.h b/include/instructions/RabbitizerInstrDescriptor.h index f6bf645..ac40c46 100644 --- a/include/instructions/RabbitizerInstrDescriptor.h +++ b/include/instructions/RabbitizerInstrDescriptor.h @@ -7,6 +7,7 @@ #include +#include "common/Utils.h" #include "RabbitizerOperandType.h" #include "RabbitizerInstrId.h" @@ -64,37 +65,61 @@ typedef struct RabbitizerInstrDescriptor { extern const RabbitizerInstrDescriptor RabbitizerInstrDescriptor_Descriptors[]; +NODISCARD NON_NULL(1) PURE bool RabbitizerInstrDescriptor_isUnknownType(const RabbitizerInstrDescriptor *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstrDescriptor_isJType(const RabbitizerInstrDescriptor *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstrDescriptor_isIType(const RabbitizerInstrDescriptor *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstrDescriptor_isRType(const RabbitizerInstrDescriptor *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstrDescriptor_isRegimmType(const RabbitizerInstrDescriptor *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstrDescriptor_isBranch(const RabbitizerInstrDescriptor *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstrDescriptor_isBranchLikely(const RabbitizerInstrDescriptor *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstrDescriptor_isJump(const RabbitizerInstrDescriptor *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstrDescriptor_isTrap(const RabbitizerInstrDescriptor *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstrDescriptor_isFloat(const RabbitizerInstrDescriptor *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstrDescriptor_isDouble(const RabbitizerInstrDescriptor *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstrDescriptor_isUnsigned(const RabbitizerInstrDescriptor *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstrDescriptor_modifiesRt(const RabbitizerInstrDescriptor *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstrDescriptor_modifiesRd(const RabbitizerInstrDescriptor *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstrDescriptor_notEmitedByCompilers(const RabbitizerInstrDescriptor *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstrDescriptor_canBeHi(const RabbitizerInstrDescriptor *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstrDescriptor_canBeLo(const RabbitizerInstrDescriptor *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstrDescriptor_doesLink(const RabbitizerInstrDescriptor *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstrDescriptor_doesDereference(const RabbitizerInstrDescriptor *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstrDescriptor_doesLoad(const RabbitizerInstrDescriptor *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstrDescriptor_doesStore(const RabbitizerInstrDescriptor *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstrDescriptor_maybeIsMove(const RabbitizerInstrDescriptor *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstrDescriptor_isPseudo(const RabbitizerInstrDescriptor *self); +NODISCARD NON_NULL(1) PURE RabbitizerArchitectureVersion RabbitizerInstrDescriptor_getArchitectureVersion(const RabbitizerInstrDescriptor *self); #endif diff --git a/include/instructions/RabbitizerInstrId.h b/include/instructions/RabbitizerInstrId.h index 5f4615e..8e8fade 100644 --- a/include/instructions/RabbitizerInstrId.h +++ b/include/instructions/RabbitizerInstrId.h @@ -5,6 +5,8 @@ #define RABBITIZER_INSTRID_H #pragma once +#include "common/Utils.h" + #define RABBITIZER_DEF_INSTR_ID(prefix, name, ...) \ RABBITIZER_INSTR_ID_##prefix##_##name @@ -29,6 +31,7 @@ typedef enum RabbitizerInstrId { extern const char *RabbitizerInstrId_Names[]; +CONST NODISCARD RETURNS_NON_NULL const char *RabbitizerInstrId_getOpcodeName(RabbitizerInstrId uniqueId); #endif diff --git a/include/instructions/RabbitizerInstruction.h b/include/instructions/RabbitizerInstruction.h index 21b50d1..9946a1f 100644 --- a/include/instructions/RabbitizerInstruction.h +++ b/include/instructions/RabbitizerInstruction.h @@ -104,18 +104,27 @@ typedef struct RabbitizerInstruction { #define RAB_INSTR_PACK_nd(word, value) (BITREPACK((word), (value), 17, 1)) +NON_NULL(1) void RabbitizerInstruction_init(RabbitizerInstruction *self, uint32_t word, uint32_t vram); +NON_NULL(1) void RabbitizerInstruction_destroy(RabbitizerInstruction* self); /* Process uniqueId */ +NON_NULL(1) void RabbitizerInstruction_processUniqueId_Normal(RabbitizerInstruction *self); +NON_NULL(1) void RabbitizerInstruction_processUniqueId_Special(RabbitizerInstruction *self); +NON_NULL(1) void RabbitizerInstruction_processUniqueId_Regimm(RabbitizerInstruction *self); +NON_NULL(1) void RabbitizerInstruction_processUniqueId_Coprocessor0(RabbitizerInstruction *self); +NON_NULL(1) void RabbitizerInstruction_processUniqueId_Coprocessor1(RabbitizerInstruction *self); +NON_NULL(1) void RabbitizerInstruction_processUniqueId_Coprocessor2(RabbitizerInstruction *self); +NON_NULL(1) void RabbitizerInstruction_processUniqueId(RabbitizerInstruction *self); /* Process uniqueId */ @@ -123,40 +132,60 @@ void RabbitizerInstruction_processUniqueId(RabbitizerInstruction *self); /* General getters */ +NODISCARD NON_NULL(1) PURE uint32_t RabbitizerInstruction_getRaw(const RabbitizerInstruction *self); +NODISCARD NON_NULL(1) PURE uint32_t RabbitizerInstruction_getImmediate(const RabbitizerInstruction *self); +NODISCARD NON_NULL(1) PURE int32_t RabbitizerInstruction_getProcessedImmediate(const RabbitizerInstruction *self); +NODISCARD NON_NULL(1) PURE uint32_t RabbitizerInstruction_getInstrIndex(const RabbitizerInstruction *self); +NODISCARD NON_NULL(1) PURE uint32_t RabbitizerInstruction_getInstrIndexAsVram(const RabbitizerInstruction *self); +NODISCARD NON_NULL(1) PURE int32_t RabbitizerInstruction_getBranchOffset(const RabbitizerInstruction *self); +NODISCARD NON_NULL(1) PURE int32_t RabbitizerInstruction_getGenericBranchOffset(const RabbitizerInstruction *self, uint32_t currentVram); /* General getters */ - +NON_NULL(1) void RabbitizerInstruction_blankOut(RabbitizerInstruction *self); /* Instruction examination */ +NODISCARD NON_NULL(1) PURE bool RabbitizerInstruction_isImplemented(const RabbitizerInstruction *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstruction_isLikelyHandwritten(const RabbitizerInstruction *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstruction_isNop(const RabbitizerInstruction *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstruction_isUnconditionalBranch(const RabbitizerInstruction *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstruction_isJrRa(const RabbitizerInstruction *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstruction_isJrNotRa(const RabbitizerInstruction *self); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstruction_hasDelaySlot(const RabbitizerInstruction *self); +NODISCARD NON_NULL(1) PURE const char *RabbitizerInstruction_mapInstrToType(const RabbitizerInstruction *self); +NODISCARD NON_NULL(1, 2) PURE bool RabbitizerInstruction_sameOpcode(const RabbitizerInstruction *self, const RabbitizerInstruction *other); +NODISCARD NON_NULL(1, 2) PURE bool RabbitizerInstruction_sameOpcodeButDifferentArguments(const RabbitizerInstruction *self, const RabbitizerInstruction *other); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstruction_hasOperand(const RabbitizerInstruction *self, RabbitizerOperandType operand); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstruction_hasOperandAlias(const RabbitizerInstruction *self, RabbitizerOperandType operand); +NODISCARD NON_NULL(1) PURE bool RabbitizerInstruction_isValid(const RabbitizerInstruction *self); /* Instruction examination */ @@ -164,18 +193,27 @@ bool RabbitizerInstruction_isValid(const RabbitizerInstruction *self); /* Disassembly */ +NODISCARD NON_NULL(1) PURE bool RabbitizerInstruction_mustDisasmAsData(const RabbitizerInstruction *self); +NODISCARD NON_NULL(1) PURE size_t RabbitizerInstruction_getSizeForBufferOperandsDisasm(const RabbitizerInstruction *self, size_t immOverrideLength); +NON_NULL(1, 2) size_t RabbitizerInstruction_disassembleOperands(const RabbitizerInstruction *self, char *dst, const char *immOverride, size_t immOverrideLength); +NODISCARD NON_NULL(1) PURE size_t RabbitizerInstruction_getSizeForBufferInstrDisasm(const RabbitizerInstruction *self, size_t immOverrideLength, int extraLJust); +NON_NULL(1, 2) size_t RabbitizerInstruction_disassembleInstruction(const RabbitizerInstruction *self, char *dst, const char *immOverride, size_t immOverrideLength, int extraLJust); +NODISCARD NON_NULL(1) PURE size_t RabbitizerInstruction_getSizeForBufferDataDisasm(const RabbitizerInstruction *self, int extraLJust); +NON_NULL(1, 2) size_t RabbitizerInstruction_disassembleAsData(const RabbitizerInstruction *self, char *dst, int extraLJust); +NODISCARD NON_NULL(1) PURE size_t RabbitizerInstruction_getSizeForBuffer(const RabbitizerInstruction *self, size_t immOverrideLength, int extraLJust); +NON_NULL(1, 2) size_t RabbitizerInstruction_disassemble(const RabbitizerInstruction *self, char *dst, const char *immOverride, size_t immOverrideLength, int extraLJust); /* Disassembly */ diff --git a/include/instructions/RabbitizerInstructionRsp.h b/include/instructions/RabbitizerInstructionRsp.h index 05fdb8e..37b1fc8 100644 --- a/include/instructions/RabbitizerInstructionRsp.h +++ b/include/instructions/RabbitizerInstructionRsp.h @@ -30,19 +30,27 @@ #define RAB_INSTR_RSP_PACK_offset(word, value) (BITREPACK((word), value, 0, 7)) +NON_NULL(1) void RabbitizerInstructionRsp_init(RabbitizerInstruction *self, uint32_t word, uint32_t vram); +NON_NULL(1) void RabbitizerInstructionRsp_destroy(RabbitizerInstruction *self); +NON_NULL(1) void RabbitizerInstructionRsp_processUniqueId_Normal(RabbitizerInstruction *self); +NON_NULL(1) void RabbitizerInstructionRsp_processUniqueId_Special(RabbitizerInstruction *self); +NON_NULL(1) void RabbitizerInstructionRsp_processUniqueId_Regimm(RabbitizerInstruction *self); +NON_NULL(1) void RabbitizerInstructionRsp_processUniqueId(RabbitizerInstruction *self); +NODISCARD NON_NULL(1) PURE uint16_t RabbitizerInstructionRsp_GetOffsetVector(const RabbitizerInstruction *self); +NODISCARD NON_NULL(1) PURE uint8_t RabbitizerInstructionRsp_processVectorElement(const RabbitizerInstruction *self, uint8_t element); diff --git a/include/instructions/RabbitizerRegister.h b/include/instructions/RabbitizerRegister.h index efa1edb..4b871a0 100644 --- a/include/instructions/RabbitizerRegister.h +++ b/include/instructions/RabbitizerRegister.h @@ -7,6 +7,8 @@ #include +#include "common/Utils.h" + #define RABBITIZER_DEF_REG(prefix, name, numeric) \ RABBITIZER_REG_##prefix##_##name @@ -63,6 +65,7 @@ typedef enum RabbitizerRegister_RspVector { #undef RABBITIZER_DEF_REG #undef RABBITIZER_DEF_REG_NODOLLAR + extern const char *RabbitizerRegister_GprO32_Names[][2]; extern const char *RabbitizerRegister_GprN32_Names[][2]; extern const char *RabbitizerRegister_Cop0_Names[][2]; @@ -75,13 +78,21 @@ extern const char *RabbitizerRegister_RspCop0_Names[][2]; extern const char *RabbitizerRegister_RspVector_Names[][2]; +NODISCARD PURE RETURNS_NON_NULL const char *RabbitizerRegister_getNameGpr(uint8_t regValue); +NODISCARD PURE RETURNS_NON_NULL const char *RabbitizerRegister_getNameCop0(uint8_t regValue); +NODISCARD PURE RETURNS_NON_NULL const char *RabbitizerRegister_getNameCop1(uint8_t regValue); +NODISCARD PURE RETURNS_NON_NULL const char *RabbitizerRegister_getNameCop1Control(uint8_t regValue); +NODISCARD PURE RETURNS_NON_NULL const char *RabbitizerRegister_getNameCop2(uint8_t regValue); +NODISCARD PURE RETURNS_NON_NULL const char *RabbitizerRegister_getNameRspGpr(uint8_t regValue); +NODISCARD PURE RETURNS_NON_NULL const char *RabbitizerRegister_getNameRspCop0(uint8_t regValue); +NODISCARD PURE RETURNS_NON_NULL const char *RabbitizerRegister_getNameRspVector(uint8_t regValue); #endif diff --git a/rabbitizer/rabbitizer_global_config.c b/rabbitizer/rabbitizer_global_config.c index 61606ac..62ca47b 100644 --- a/rabbitizer/rabbitizer_global_config.c +++ b/rabbitizer/rabbitizer_global_config.c @@ -11,16 +11,14 @@ #define DEF_MEMBER_GET_BOOL(category, name) \ - static PyObject *rabbitizer_global_config_get_##category##_##name(PyObject *self, PyObject *Py_UNUSED(ignored)) { \ - (void)self; \ + static PyObject *rabbitizer_global_config_get_##category##_##name(UNUSED PyObject *self, UNUSED PyObject *closure) { \ if (RabbitizerConfig_Cfg.category.name) { \ Py_RETURN_TRUE; \ } \ Py_RETURN_FALSE; \ } #define DEF_MEMBER_SET_BOOL(category, name) \ - static int rabbitizer_global_config_set_##category##_##name(PyObject *self, PyObject *value, void *Py_UNUSED(closure)) { \ - (void)self; \ + static int rabbitizer_global_config_set_##category##_##name(UNUSED PyObject *self, PyObject *value, UNUSED void *closure) { \ if (value == NULL) { \ PyErr_SetString(PyExc_TypeError, "Cannot delete '" #category "." #name "' attribute"); \ return -1; \ @@ -35,14 +33,12 @@ #define DEF_MEMBER_GET_INT(category, name) \ - static PyObject *rabbitizer_global_config_get_##category##_##name(PyObject *self, PyObject *Py_UNUSED(ignored)) { \ - (void)self; \ + static PyObject *rabbitizer_global_config_get_##category##_##name(UNUSED PyObject *self, UNUSED PyObject *closure) { \ return PyLong_FromLong(RabbitizerConfig_Cfg.category.name); \ } #define DEF_MEMBER_SET_INT(category, name, rangeCheck, minVal, maxVal) \ - static int rabbitizer_global_config_set_##category##_##name(PyObject *self, PyObject *value, void *Py_UNUSED(closure)) { \ + static int rabbitizer_global_config_set_##category##_##name(UNUSED PyObject *self, PyObject *value, UNUSED void *closure) { \ long val; \ - (void)self; \ if (value == NULL) { \ PyErr_SetString(PyExc_TypeError, "Cannot delete '" #category "_" #name "' attribute"); \ return -1; \ diff --git a/src/analysis/RabbitizerRegistersTracker.c b/src/analysis/RabbitizerRegistersTracker.c index 78477b2..20e235a 100644 --- a/src/analysis/RabbitizerRegistersTracker.c +++ b/src/analysis/RabbitizerRegistersTracker.c @@ -230,9 +230,9 @@ void RabbitizerRegistersTracker_unsetRegistersAfterFuncCall(RabbitizerRegistersT } } -bool RabbitizerRegistersTracker_getAddressIfCanSetType(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int instrOffset, +bool RabbitizerRegistersTracker_getAddressIfCanSetType(const RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int instrOffset, uint32_t *dstAddress) { - RabbitizerTrackedRegisterState *state = &self->registers[RAB_INSTR_GET_rs(instr)]; + const RabbitizerTrackedRegisterState *state = &self->registers[RAB_INSTR_GET_rs(instr)]; if (!state->hasLoValue) { return false; @@ -246,8 +246,8 @@ bool RabbitizerRegistersTracker_getAddressIfCanSetType(RabbitizerRegistersTracke return false; } -bool RabbitizerRegistersTracker_getJrInfo(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int *dstOffset, uint32_t *dstAddress) { - RabbitizerTrackedRegisterState *state = &self->registers[RAB_INSTR_GET_rs(instr)]; +bool RabbitizerRegistersTracker_getJrInfo(const RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int *dstOffset, uint32_t *dstAddress) { + const RabbitizerTrackedRegisterState *state = &self->registers[RAB_INSTR_GET_rs(instr)]; if (!state->hasLoValue || !state->dereferenced) { return false; @@ -276,8 +276,8 @@ void RabbitizerRegistersTracker_processLui(RabbitizerRegistersTracker *self, con } } -bool RabbitizerRegistersTracker_getLuiOffsetForConstant(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int *dstOffset) { - RabbitizerTrackedRegisterState *state = &self->registers[RAB_INSTR_GET_rs(instr)]; +bool RabbitizerRegistersTracker_getLuiOffsetForConstant(const RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int *dstOffset) { + const RabbitizerTrackedRegisterState *state = &self->registers[RAB_INSTR_GET_rs(instr)]; if (!state->hasLuiValue) { return false; @@ -293,9 +293,10 @@ void RabbitizerRegistersTracker_processConstant(RabbitizerRegistersTracker *self RabbitizerTrackedRegisterState_setLo(stateDst, value, offset); } +// TODO: this function should not be changing the state of the tracker bool RabbitizerRegistersTracker_getLuiOffsetForLo(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr, int instrOffset, int *dstOffset, bool *dstIsGp) { - RabbitizerTrackedRegisterState *state = &self->registers[RAB_INSTR_GET_rs(instr)]; + const RabbitizerTrackedRegisterState *state = &self->registers[RAB_INSTR_GET_rs(instr)]; if (state->hasLuiValue && !state->luiSetOnBranchLikely) { *dstOffset = state->luiOffset; @@ -337,8 +338,8 @@ void RabbitizerRegistersTracker_processLo(RabbitizerRegistersTracker *self, cons } } -bool RabbitizerRegistersTracker_hasLoButNoHi(RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr) { - RabbitizerTrackedRegisterState *state; +bool RabbitizerRegistersTracker_hasLoButNoHi(const RabbitizerRegistersTracker *self, const RabbitizerInstruction *instr) { + const RabbitizerTrackedRegisterState *state; assert(instr != NULL); diff --git a/src/instructions/RabbitizerInstruction/RabbitizerInstruction.c b/src/instructions/RabbitizerInstruction/RabbitizerInstruction.c index a354ead..641a1fb 100644 --- a/src/instructions/RabbitizerInstruction/RabbitizerInstruction.c +++ b/src/instructions/RabbitizerInstruction/RabbitizerInstruction.c @@ -22,8 +22,7 @@ void RabbitizerInstruction_init(RabbitizerInstruction *self, uint32_t word, uint self->category = RABBITIZER_INSTRCAT_CPU; } -void RabbitizerInstruction_destroy(RabbitizerInstruction *self) { - (void)self; +void RabbitizerInstruction_destroy(UNUSED RabbitizerInstruction *self) { } /* General getters */ diff --git a/src/instructions/RabbitizerInstructionRsp/RabbitizerInstructionRsp.c b/src/instructions/RabbitizerInstructionRsp/RabbitizerInstructionRsp.c index a5d347b..e2cafd8 100644 --- a/src/instructions/RabbitizerInstructionRsp/RabbitizerInstructionRsp.c +++ b/src/instructions/RabbitizerInstructionRsp/RabbitizerInstructionRsp.c @@ -55,9 +55,7 @@ uint16_t RabbitizerInstructionRsp_GetOffsetVector(const RabbitizerInstruction *s } } -uint8_t RabbitizerInstructionRsp_processVectorElement(const RabbitizerInstruction *self, uint8_t element) { - (void)self; - +uint8_t RabbitizerInstructionRsp_processVectorElement(UNUSED const RabbitizerInstruction *self, uint8_t element) { if ((element & 0x8) == 0x8) { return element & 7; } From a5ef766311a1ebbda58fc97a159a3468ad632f72 Mon Sep 17 00:00:00 2001 From: Angie Date: Sun, 10 Jul 2022 16:04:39 -0400 Subject: [PATCH 12/13] Utils.escapeString --- Makefile | 5 ++- include/common/Utils.h | 30 +++++++++++++ rabbitizer/Utils.pyi | 3 ++ rabbitizer/rabbitizer_submodule_Utils.c | 32 +++++++++++++ src/common/Utils.c | 45 +++++++++++++++++++ .../RabbitizerInstruction_Disassemble.c | 27 ----------- test.c | 7 +++ 7 files changed, 121 insertions(+), 28 deletions(-) diff --git a/Makefile b/Makefile index f4e3d0c..9c7465b 100644 --- a/Makefile +++ b/Makefile @@ -11,9 +11,12 @@ CFLAGS := LDFLAGS := WARNINGS := -Wall -Wextra # WARNINGS := -Wall -Wextra -Wpedantic -Wpadded # binary constants :s -WARNINGS += -Wno-cast-function-type WARNINGS += -Werror=implicit-function-declaration -Werror=incompatible-pointer-types -Werror=vla -Werror=switch -Werror=implicit-fallthrough -Werror=unused-function -Werror=unused-parameter -Werror=shadow +ifeq ($(CC),gcc) + WARNINGS += -Wno-cast-function-type +endif + ifeq ($(DEBUG),0) OPTFLAGS := -O2 -g else diff --git a/include/common/Utils.h b/include/common/Utils.h index f8c2429..872461b 100644 --- a/include/common/Utils.h +++ b/include/common/Utils.h @@ -67,9 +67,39 @@ #define BITREPACK_RIGHT(fullword, v, s, w) (SHIFTL((v), (s), (w)) | MASK((fullword), (s))) +#define RABUTILS_BUFFER_ADVANCE(buffer, totalSize, expression) \ + do { \ + size_t __tempSize = expression; \ + (buffer) += __tempSize; \ + (totalSize) += __tempSize; \ + } while (0) + +#define RABUTILS_BUFFER_WRITE_CHAR(buffer, totalSize, character) \ + do { \ + *(buffer) = (character); \ + RABUTILS_BUFFER_ADVANCE(buffer, totalSize, 1); \ + } while (0) + +#define RABUTILS_BUFFER_SPRINTF(buffer, totalSize, format, ...) \ + do { \ + int _len = sprintf(buffer, format, __VA_ARGS__); \ + assert(_len > 0); \ + RABUTILS_BUFFER_ADVANCE(buffer, totalSize, _len); \ + } while (0) + +#define RABUTILS_BUFFER_CPY(buffer, totalSize, string) \ + do { \ + size_t _tempSize = strlen(string); \ + memcpy(buffer, string, _tempSize); \ + RABUTILS_BUFFER_ADVANCE(buffer, totalSize, _tempSize); \ + } while (0) + + CONST NODISCARD int32_t RabbitizerUtils_From2Complement(uint32_t number, int bits); NON_NULL(1) size_t RabbitizerUtils_CharFill(char *dst, int count, char fillchar); +NON_NULL(1, 3) +size_t RabbitizerUtils_escapeString(char *dst, size_t dstSize, const char *src, size_t srcSize); #endif diff --git a/rabbitizer/Utils.pyi b/rabbitizer/Utils.pyi index 45b29f3..7215556 100644 --- a/rabbitizer/Utils.pyi +++ b/rabbitizer/Utils.pyi @@ -9,3 +9,6 @@ from __future__ import annotations class Utils: @staticmethod def from2Complement(number: int, bits: int) -> int: ... + + @staticmethod + def escapeString(src: str) -> str: ... diff --git a/rabbitizer/rabbitizer_submodule_Utils.c b/rabbitizer/rabbitizer_submodule_Utils.c index c9451ce..31b844a 100644 --- a/rabbitizer/rabbitizer_submodule_Utils.c +++ b/rabbitizer/rabbitizer_submodule_Utils.c @@ -18,12 +18,44 @@ static PyObject *rabbitizer_submodule_Utils_from2Complement(UNUSED PyObject *sel return PyLong_FromLong(RabbitizerUtils_From2Complement(number, bits)); } +static PyObject *rabbitizer_submodule_Utils_escapeString(UNUSED PyObject *self, PyObject *args, PyObject *kwds) { + static char *kwlist[] = { "src", NULL }; + const char *src = NULL; + Py_ssize_t srcSize = 0; + char *dst; + size_t dstSize; + size_t wroteBytes; + PyObject *ret; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "s#", kwlist, &src, &srcSize)) { + return NULL; + } + + dstSize = 2 * srcSize; + dst = malloc(dstSize * sizeof(char)); + if (dst == NULL) { + PyErr_SetString(PyExc_RuntimeError, "Internal error on 'escapeString'"); + return NULL; + } + + wroteBytes = RabbitizerUtils_escapeString(dst, dstSize, src, srcSize); + if (wroteBytes > dstSize) { + PyErr_SetString(PyExc_RuntimeError, "Internal error on 'escapeString'"); + return NULL; + } + + ret = PyUnicode_FromStringAndSize(dst, wroteBytes); + free(dst); + return ret; +} + #define METHOD_NO_ARGS(name, docs) { #name, (PyCFunction) rabbitizer_submodule_Utils_##name, METH_NOARGS, PyDoc_STR(docs) } #define METHOD_ARGS(name, docs) { #name, (PyCFunction) rabbitizer_submodule_Utils_##name, METH_VARARGS | METH_KEYWORDS, PyDoc_STR(docs) } static PyMethodDef rabbitizer_submodule_Utils_methods[] = { METHOD_ARGS(from2Complement, ""), + METHOD_ARGS(escapeString, ""), { 0 }, }; diff --git a/src/common/Utils.c b/src/common/Utils.c index 4ea97aa..63f3a5f 100644 --- a/src/common/Utils.c +++ b/src/common/Utils.c @@ -25,3 +25,48 @@ size_t RabbitizerUtils_CharFill(char *dst, int count, char fillchar) { return count; } + +size_t RabbitizerUtils_escapeString(char *dst, size_t dstSize, const char *src, size_t srcSize) { + size_t srcPos = 0; + size_t dstpos = 0; + + for (; srcPos < srcSize && dstpos < dstSize; srcPos++, src++) { + switch (*src) { + case '\a': + RABUTILS_BUFFER_WRITE_CHAR(dst, dstpos, '\\'); + RABUTILS_BUFFER_WRITE_CHAR(dst, dstpos, 'a'); + break; + + case '\t': + RABUTILS_BUFFER_WRITE_CHAR(dst, dstpos, '\\'); + RABUTILS_BUFFER_WRITE_CHAR(dst, dstpos, 't'); + break; + + case '\n': + RABUTILS_BUFFER_WRITE_CHAR(dst, dstpos, '\\'); + RABUTILS_BUFFER_WRITE_CHAR(dst, dstpos, 'n'); + break; + + case '\f': + RABUTILS_BUFFER_WRITE_CHAR(dst, dstpos, '\\'); + RABUTILS_BUFFER_WRITE_CHAR(dst, dstpos, 'f'); + break; + + case '\r': + RABUTILS_BUFFER_WRITE_CHAR(dst, dstpos, '\\'); + RABUTILS_BUFFER_WRITE_CHAR(dst, dstpos, 'r'); + break; + + case '"': + RABUTILS_BUFFER_WRITE_CHAR(dst, dstpos, '\\'); + RABUTILS_BUFFER_WRITE_CHAR(dst, dstpos, '"'); + break; + + default: + RABUTILS_BUFFER_WRITE_CHAR(dst, dstpos, *src); + break; + } + } + + return dstpos; +} diff --git a/src/instructions/RabbitizerInstruction/RabbitizerInstruction_Disassemble.c b/src/instructions/RabbitizerInstruction/RabbitizerInstruction_Disassemble.c index 4780900..2f52232 100644 --- a/src/instructions/RabbitizerInstruction/RabbitizerInstruction_Disassemble.c +++ b/src/instructions/RabbitizerInstruction/RabbitizerInstruction_Disassemble.c @@ -12,33 +12,6 @@ #include "common/RabbitizerConfig.h" #include "instructions/RabbitizerRegister.h" -#define RABUTILS_BUFFER_ADVANCE(buffer, totalSize, expression) \ - do { \ - size_t __tempSize = expression; \ - (buffer) += __tempSize; \ - (totalSize) += __tempSize; \ - } while (0) - -#define RABUTILS_BUFFER_WRITE_CHAR(buffer, totalSize, character) \ - do { \ - *(buffer) = (character); \ - RABUTILS_BUFFER_ADVANCE(buffer, totalSize, 1); \ - } while (0) - -#define RABUTILS_BUFFER_SPRINTF(buffer, totalSize, format, ...) \ - do { \ - int _len = sprintf(buffer, format, __VA_ARGS__); \ - assert(_len > 0); \ - RABUTILS_BUFFER_ADVANCE(buffer, totalSize, _len); \ - } while (0) - -#define RABUTILS_BUFFER_CPY(buffer, totalSize, string) \ - do { \ - size_t _tempSize = strlen(string); \ - memcpy(buffer, string, _tempSize); \ - RABUTILS_BUFFER_ADVANCE(buffer, totalSize, _tempSize); \ - } while (0) - typedef size_t (*OperandCallback)(const RabbitizerInstruction *self, char *dst, const char *immOverride, size_t immOverrideLength); size_t RabbitizerOperandType_processRs(const RabbitizerInstruction *self, char *dst, UNUSED const char *immOverride, UNUSED size_t immOverrideLength) { diff --git a/test.c b/test.c index 505cab2..3588943 100644 --- a/test.c +++ b/test.c @@ -5,6 +5,7 @@ #include #include +#include #include @@ -31,5 +32,11 @@ int main() { free(buffer); RabbitizerInstruction_destroy(&instr); + char someMagicBuffer[0x1000]; + + RabbitizerUtils_escapeString(someMagicBuffer, ARRAY_COUNT(someMagicBuffer), "\tsomeExample\n", strlen("\tsomeExample\n")); + + printf("%s\n", someMagicBuffer); + return 0; } From 9300da772e8a63f143d46302586c670c5c5f5f49 Mon Sep 17 00:00:00 2001 From: Angie Date: Tue, 12 Jul 2022 19:01:36 -0400 Subject: [PATCH 13/13] Allow taking `None` in `Abi.fromStr` --- rabbitizer/Config.pyi | 2 +- rabbitizer/enums/rabbitizer_enum_Abi.c | 2 +- src/common/RabbitizerConfig.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rabbitizer/Config.pyi b/rabbitizer/Config.pyi index 4894310..786642f 100644 --- a/rabbitizer/Config.pyi +++ b/rabbitizer/Config.pyi @@ -15,7 +15,7 @@ class Abi: N64: Enum @staticmethod - def fromStr(name: str) -> Enum: ... + def fromStr(name: str | None) -> Enum: ... class _RabbitizerConfig: diff --git a/rabbitizer/enums/rabbitizer_enum_Abi.c b/rabbitizer/enums/rabbitizer_enum_Abi.c index 634058f..a0d5fb0 100644 --- a/rabbitizer/enums/rabbitizer_enum_Abi.c +++ b/rabbitizer/enums/rabbitizer_enum_Abi.c @@ -24,7 +24,7 @@ static PyObject *rabbitizer_enum_Abi_fromStr(UNUSED PyObject *self, PyObject *ar RabbitizerAbi abi; PyObject *ret; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &name)) { + if (!PyArg_ParseTupleAndKeywords(args, kwds, "z", kwlist, &name)) { return NULL; } diff --git a/src/common/RabbitizerConfig.c b/src/common/RabbitizerConfig.c index a6756dc..37d43e2 100644 --- a/src/common/RabbitizerConfig.c +++ b/src/common/RabbitizerConfig.c @@ -6,7 +6,7 @@ #include RabbitizerAbi RabbitizerAbi_fromStr(const char *name) { - if (strcmp(name, "32") == 0 || strcmp(name, "o32") == 0 || strcmp(name, "O32") == 0) { + if (name == NULL || strcmp(name, "32") == 0 || strcmp(name, "o32") == 0 || strcmp(name, "O32") == 0) { return RABBITIZER_ABI_O32; } if (strcmp(name, "n32") == 0 || strcmp(name, "N32") == 0) {