From 0310b7778b79cf904e43a87a24efea030012b821 Mon Sep 17 00:00:00 2001 From: Jose Rodriguez Date: Wed, 26 Mar 2025 01:10:30 +0100 Subject: [PATCH] fix: crash when converting some from byte to bool --- src/arch/z80/backend/generic.py | 3 ++ tests/functional/arch/zx48k/bool_crash.asm | 55 ++++++++++++++++++++++ tests/functional/arch/zx48k/bool_crash.bas | 7 +++ 3 files changed, 65 insertions(+) create mode 100644 tests/functional/arch/zx48k/bool_crash.asm create mode 100644 tests/functional/arch/zx48k/bool_crash.bas diff --git a/src/arch/z80/backend/generic.py b/src/arch/z80/backend/generic.py index 04767f1b1..0f02a1efe 100644 --- a/src/arch/z80/backend/generic.py +++ b/src/arch/z80/backend/generic.py @@ -341,6 +341,9 @@ def _cast(ins: Quad): xsB = sB = YY_TYPES[tB] # Type sizes + if tA in ("u8", "i8") and tB == "bool": + return [] # bytes are booleans already (0 = False, not 0 = True) + output = [] if tA in ("u8", "i8", "bool"): output.extend(Bits8.get_oper(ins[4])) diff --git a/tests/functional/arch/zx48k/bool_crash.asm b/tests/functional/arch/zx48k/bool_crash.asm new file mode 100644 index 000000000..1b01a4b5f --- /dev/null +++ b/tests/functional/arch/zx48k/bool_crash.asm @@ -0,0 +1,55 @@ + 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 +_pldx: + DEFB 00 +.core.ZXBASIC_USER_DATA_END: +.core.__MAIN_PROGRAM__: + 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 +_ReadKeys: + push ix + ld ix, 0 + add ix, sp + pop af + ld hl, (_pldx - 1) + or h + jp z, .LABEL.__LABEL1 + ld a, 1 + ld (_pldx), a +.LABEL.__LABEL1: +_ReadKeys__leave: + ld sp, ix + pop ix + ret + ;; --- end of user code --- + END diff --git a/tests/functional/arch/zx48k/bool_crash.bas b/tests/functional/arch/zx48k/bool_crash.bas new file mode 100644 index 000000000..c06e0e75d --- /dev/null +++ b/tests/functional/arch/zx48k/bool_crash.bas @@ -0,0 +1,7 @@ +dim pldx as ubyte + +sub ReadKeys() + if pldx or (pldx and %1) then + pldx = 1 + endif +end sub