From 47b4007aec60e57b6ff501406d291f2870117e97 Mon Sep 17 00:00:00 2001 From: Jose Rodriguez Date: Fri, 6 Dec 2024 23:19:46 +0100 Subject: [PATCH] fix: crash on NOT (bool expr) --- .../opts/024_o1_bool_norm_duble_neg.opt | 23 +++++++++ .../z80/visitor/translator_inst_visitor.py | 2 +- tests/functional/arch/zx48k/letnotbool.asm | 47 +++++++++++++++++++ tests/functional/arch/zx48k/letnotbool.bas | 3 ++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/arch/z80/peephole/opts/024_o1_bool_norm_duble_neg.opt create mode 100644 tests/functional/arch/zx48k/letnotbool.asm create mode 100644 tests/functional/arch/zx48k/letnotbool.bas diff --git a/src/arch/z80/peephole/opts/024_o1_bool_norm_duble_neg.opt b/src/arch/z80/peephole/opts/024_o1_bool_norm_duble_neg.opt new file mode 100644 index 000000000..315ec4ea2 --- /dev/null +++ b/src/arch/z80/peephole/opts/024_o1_bool_norm_duble_neg.opt @@ -0,0 +1,23 @@ +;; The sequence: +;; sbc a, a ; A is either 0 or -1 +;; sub 1 +;; sbc a, a +;; neg +;; can be replaced by +;; sbc a, a ; A is either 0 or -1 +;; inc a + +OLEVEL: 1 +OFLAG: 24 + +REPLACE {{ + sbc a, a + sub 1 + sbc a, a + neg +}} + +WITH {{ + sbc a, a + inc a +}} diff --git a/src/arch/z80/visitor/translator_inst_visitor.py b/src/arch/z80/visitor/translator_inst_visitor.py index a16a89640..c853744af 100644 --- a/src/arch/z80/visitor/translator_inst_visitor.py +++ b/src/arch/z80/visitor/translator_inst_visitor.py @@ -181,7 +181,7 @@ def ic_nop(self) -> None: self.emit("nop") def ic_not(self, type_: TYPE | symbols.BASICTYPE, t1, t2) -> None: - self.emit("not" + self.TSUFFIX(type_), t1, t2) + self.emit(f"not{self._no_bool(type_)}", t1, t2) def ic_or(self, type_: TYPE | symbols.BASICTYPE, t, t1, t2) -> None: self.emit(f"or{self._no_bool(type_)}", t, t1, t2) diff --git a/tests/functional/arch/zx48k/letnotbool.asm b/tests/functional/arch/zx48k/letnotbool.asm new file mode 100644 index 000000000..21f656701 --- /dev/null +++ b/tests/functional/arch/zx48k/letnotbool.asm @@ -0,0 +1,47 @@ + org 32768 +.core.__START_PROGRAM: + di + push ix + push iy + exx + push hl + exx + ld hl, 0 + add hl, sp + ld (.core.__CALL_BACK__), hl + ei + jp .core.__MAIN_PROGRAM__ +.core.__CALL_BACK__: + DEFW 0 +.core.ZXBASIC_USER_DATA: + ; Defines USER DATA Length in bytes +.core.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_END - .core.ZXBASIC_USER_DATA + .core.__LABEL__.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_LEN + .core.__LABEL__.ZXBASIC_USER_DATA EQU .core.ZXBASIC_USER_DATA +_a: + DEFB 00 +.core.ZXBASIC_USER_DATA_END: +.core.__MAIN_PROGRAM__: + ld hl, (_a - 1) + ld a, (_a) + sub h + sub 1 + sbc a, a + inc a + ld (_a), a + ld hl, 0 + ld b, h + ld c, l +.core.__END_PROGRAM: + di + ld hl, (.core.__CALL_BACK__) + ld sp, hl + exx + pop hl + exx + pop iy + pop ix + ei + ret + ;; --- end of user code --- + END diff --git a/tests/functional/arch/zx48k/letnotbool.bas b/tests/functional/arch/zx48k/letnotbool.bas new file mode 100644 index 000000000..cd01601f9 --- /dev/null +++ b/tests/functional/arch/zx48k/letnotbool.bas @@ -0,0 +1,3 @@ +DIM a As Ubyte + +LET a = NOT (a = a)