From 9f69a498921668f58fa3b51042f132b58fec0674 Mon Sep 17 00:00:00 2001 From: Jose Rodriguez Date: Thu, 19 Dec 2024 00:50:41 +0100 Subject: [PATCH] fix: crash with FOR loop using constants --- src/api/optimize.py | 4 +- .../arch/zx48k/for_const_crash1.asm | 53 +++++++++++++++++++ .../arch/zx48k/for_const_crash1.bas | 5 ++ 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 tests/functional/arch/zx48k/for_const_crash1.asm create mode 100644 tests/functional/arch/zx48k/for_const_crash1.bas diff --git a/src/api/optimize.py b/src/api/optimize.py index 87d2e7312..9f2e7046d 100644 --- a/src/api/optimize.py +++ b/src/api/optimize.py @@ -419,10 +419,10 @@ def visit_FOR(self, node): body_ = node.children[4] if self.O_LEVEL > 0 and chk.is_number(from_, to_, step_) and not chk.is_block_accessed(body_): - if from_ > to_ and step_ > 0: + if from_.value > to_.value and step_.value > 0: yield self.NOP return - if from_ < to_ and step_ < 0: + if from_.value < to_.value and step_.value < 0: yield self.NOP return diff --git a/tests/functional/arch/zx48k/for_const_crash1.asm b/tests/functional/arch/zx48k/for_const_crash1.asm new file mode 100644 index 000000000..4ecab1e64 --- /dev/null +++ b/tests/functional/arch/zx48k/for_const_crash1.asm @@ -0,0 +1,53 @@ + 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 +_i: + DEFB 00 +.core.ZXBASIC_USER_DATA_END: +.core.__MAIN_PROGRAM__: + ld a, 14 + ld (_i), a + jp .LABEL.__LABEL0 +.LABEL.__LABEL3: +.LABEL.__LABEL4: + ld hl, _i + inc (hl) +.LABEL.__LABEL0: + ld a, 23 + ld hl, (_i - 1) + cp h + jp nc, .LABEL.__LABEL3 +.LABEL.__LABEL2: + 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/for_const_crash1.bas b/tests/functional/arch/zx48k/for_const_crash1.bas new file mode 100644 index 000000000..8786a030d --- /dev/null +++ b/tests/functional/arch/zx48k/for_const_crash1.bas @@ -0,0 +1,5 @@ +const SCR_FIRST_LINE_ANSWER as ubyte = 14 +const SCR_MAX_LINES as ubyte = 23 + +for i = SCR_FIRST_LINE_ANSWER to SCR_MAX_LINES +next