Skip to content

Commit e181aac

Browse files
committed
fix: do not emit CHKBREAK after label
1 parent be0b538 commit e181aac

4 files changed

Lines changed: 130 additions & 4 deletions

File tree

src/api/optimize.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ def visit_FUNCTION(self, node: symbols.FUNCTION):
106106
yield (yield self.generic_visit(node))
107107

108108
def visit_BLOCK(self, node):
109+
# Remove CHKBREAK after labels
110+
111+
i = 0
112+
while i < len(node) - 1:
113+
child = node[i]
114+
if child.token == "LABEL" and node[i + 1].token == "CHKBREAK":
115+
node.pop(i + 1)
116+
continue
117+
i += 1
118+
109119
warning_emitted = False
110120
i = 0
111121
while i < len(node):

tests/functional/zx48k/break.asm

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ _a:
2323
.core.ZXBASIC_USER_DATA_END:
2424
.core.__MAIN_PROGRAM__:
2525
.LABEL._10:
26-
push hl
27-
ld hl, 4
28-
call .core.CHECK_BREAK
2926
.LABEL._20:
3027
ld a, 1
3128
ld (_a), a
@@ -119,5 +116,5 @@ NO_BREAK:
119116
TS_BRK EQU 8020
120117
ENDP
121118
pop namespace
122-
#line 40 "break.bas"
119+
#line 37 "zx48k/break.bas"
123120
END
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
org 32768
2+
.core.__START_PROGRAM:
3+
di
4+
push ix
5+
push iy
6+
exx
7+
push hl
8+
exx
9+
ld hl, 0
10+
add hl, sp
11+
ld (.core.__CALL_BACK__), hl
12+
ei
13+
jp .core.__MAIN_PROGRAM__
14+
.core.__CALL_BACK__:
15+
DEFW 0
16+
.core.ZXBASIC_USER_DATA:
17+
; Defines USER DATA Length in bytes
18+
.core.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_END - .core.ZXBASIC_USER_DATA
19+
.core.__LABEL__.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_LEN
20+
.core.__LABEL__.ZXBASIC_USER_DATA EQU .core.ZXBASIC_USER_DATA
21+
_a:
22+
DEFB 00
23+
.core.ZXBASIC_USER_DATA_END:
24+
.core.__MAIN_PROGRAM__:
25+
xor a
26+
ld (_a), a
27+
push hl
28+
ld hl, 5
29+
call .core.CHECK_BREAK
30+
.LABEL._test_label:
31+
ld a, 1
32+
ld (_a), a
33+
push hl
34+
ld hl, 7
35+
call .core.CHECK_BREAK
36+
ld hl, 0
37+
ld b, h
38+
ld c, l
39+
.core.__END_PROGRAM:
40+
di
41+
ld hl, (.core.__CALL_BACK__)
42+
ld sp, hl
43+
exx
44+
pop hl
45+
exx
46+
pop iy
47+
pop ix
48+
ei
49+
ret
50+
;; --- end of user code ---
51+
#line 1 "/zxbasic/src/arch/zx48k/library-asm/break.asm"
52+
#line 1 "/zxbasic/src/arch/zx48k/library-asm/error.asm"
53+
; Simple error control routines
54+
; vim:ts=4:et:
55+
push namespace core
56+
ERR_NR EQU 23610 ; Error code system variable
57+
; Error code definitions (as in ZX spectrum manual)
58+
; Set error code with:
59+
; ld a, ERROR_CODE
60+
; ld (ERR_NR), a
61+
ERROR_Ok EQU -1
62+
ERROR_SubscriptWrong EQU 2
63+
ERROR_OutOfMemory EQU 3
64+
ERROR_OutOfScreen EQU 4
65+
ERROR_NumberTooBig EQU 5
66+
ERROR_InvalidArg EQU 9
67+
ERROR_IntOutOfRange EQU 10
68+
ERROR_NonsenseInBasic EQU 11
69+
ERROR_InvalidFileName EQU 14
70+
ERROR_InvalidColour EQU 19
71+
ERROR_BreakIntoProgram EQU 20
72+
ERROR_TapeLoadingErr EQU 26
73+
; Raises error using RST #8
74+
__ERROR:
75+
ld (__ERROR_CODE), a
76+
rst 8
77+
__ERROR_CODE:
78+
nop
79+
ret
80+
; Sets the error system variable, but keeps running.
81+
; Usually this instruction if followed by the END intermediate instruction.
82+
__STOP:
83+
ld (ERR_NR), a
84+
ret
85+
pop namespace
86+
#line 2 "/zxbasic/src/arch/zx48k/library-asm/break.asm"
87+
; Check if BREAK is pressed
88+
; Return if not. Else Raises
89+
; L BREAK Into Program Error
90+
; HL contains the line number we want to appear in the error msg.
91+
push namespace core
92+
CHECK_BREAK:
93+
PROC
94+
LOCAL PPC, TS_BRK, NO_BREAK
95+
push af
96+
call TS_BRK
97+
jr c, NO_BREAK
98+
ld (PPC), hl ; line num
99+
ld a, ERROR_BreakIntoProgram
100+
jp __ERROR ; this stops the program and exits to BASIC
101+
NO_BREAK:
102+
pop af
103+
pop hl ; ret address
104+
ex (sp), hl ; puts it back into the stack and recovers initial HL
105+
ret
106+
PPC EQU 23621
107+
TS_BRK EQU 8020
108+
ENDP
109+
pop namespace
110+
#line 28 "zx48k/break_label0.bas"
111+
END
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma enable_break = true
2+
3+
DIM a as Ubyte
4+
5+
LET a = 0
6+
test_label:
7+
LET a = 1
8+

0 commit comments

Comments
 (0)