Skip to content

Commit 9fcc14a

Browse files
committed
fix: crash on SHL with bool expressions
1 parent 561a12a commit 9fcc14a

7 files changed

Lines changed: 155 additions & 3 deletions

File tree

src/arch/z80/visitor/translator_inst_visitor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def ic_return(self, addr) -> None:
223223
self.emit("ret", addr)
224224

225225
def ic_shl(self, type_: TYPE | sym.BASICTYPE, t, t1, t2) -> None:
226-
self.emit("shl" + self.TSUFFIX(type_), t, t1, t2)
226+
self.emit(f"shl{self._no_bool(type_)}", t, t1, t2)
227227

228228
def ic_shr(self, type_: TYPE | sym.BASICTYPE, t, t1, t2) -> None:
229229
self.emit("shr" + self.TSUFFIX(type_), t, t1, t2)

tests/functional/arch/zx48k/shlu16.asm

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,38 @@ _b:
4242
xor a
4343
ld l, a
4444
ld h, 0
45+
ld (_a), hl
46+
ld de, (_a)
47+
ld hl, (_a)
48+
call .core.__EQ16
49+
push af
50+
ld a, (_b)
51+
pop hl
52+
or a
53+
ld b, a
54+
ld a, h
55+
jr z, .LABEL.__LABEL7
56+
.LABEL.__LABEL6:
57+
add a, a
58+
djnz .LABEL.__LABEL6
59+
.LABEL.__LABEL7:
60+
ld l, a
61+
ld h, 0
62+
ld (_a), hl
63+
ld hl, (_b - 1)
64+
ld a, (_b)
65+
sub h
66+
sub 1
67+
sbc a, a
68+
neg
69+
ld b, a
70+
ld hl, (_a)
71+
or a
72+
jr z, .LABEL.__LABEL9
73+
.LABEL.__LABEL8:
74+
add hl, hl
75+
djnz .LABEL.__LABEL8
76+
.LABEL.__LABEL9:
4577
ld (_a), hl
4678
ld hl, 0
4779
ld b, h
@@ -58,4 +90,15 @@ _b:
5890
ei
5991
ret
6092
;; --- end of user code ---
93+
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/cmp/eq16.asm"
94+
push namespace core
95+
__EQ16: ; Test if 16bit values HL == DE
96+
; Returns result in A: 0 = False, FF = True
97+
xor a ; Reset carry flag
98+
sbc hl, de
99+
ret nz
100+
inc a
101+
ret
102+
pop namespace
103+
#line 68 "arch/zx48k/shlu16.bas"
61104
END

tests/functional/arch/zx48k/shlu16.bas

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ a = a << b
55
a = a << 1
66
a = a << 0
77
a = 0 << b
8+
a = (a = a) << b
9+
a = a << (b = b)
10+

tests/functional/arch/zx48k/shlu32.asm

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,47 @@ _b:
5151
ld h, 0
5252
ld e, h
5353
ld d, h
54+
ld (_a), hl
55+
ld (_a + 2), de
56+
ld hl, (_a + 2)
57+
push hl
58+
ld hl, (_a)
59+
push hl
60+
ld hl, (_a)
61+
ld de, (_a + 2)
62+
call .core.__EQ32
63+
push af
64+
ld a, (_b)
65+
pop hl
66+
or a
67+
ld b, a
68+
ld a, h
69+
jr z, .LABEL.__LABEL3
70+
.LABEL.__LABEL2:
71+
add a, a
72+
djnz .LABEL.__LABEL2
73+
.LABEL.__LABEL3:
74+
ld l, a
75+
ld h, 0
76+
ld e, h
77+
ld d, h
78+
ld (_a), hl
79+
ld (_a + 2), de
80+
ld hl, (_b - 1)
81+
ld a, (_b)
82+
sub h
83+
sub 1
84+
sbc a, a
85+
neg
86+
ld b, a
87+
ld hl, (_a)
88+
ld de, (_a + 2)
89+
or a
90+
jr z, .LABEL.__LABEL5
91+
.LABEL.__LABEL4:
92+
call .core.__SHL32
93+
djnz .LABEL.__LABEL4
94+
.LABEL.__LABEL5:
5495
ld (_a), hl
5596
ld (_a + 2), de
5697
ld hl, 0
@@ -68,7 +109,7 @@ _b:
68109
ei
69110
ret
70111
;; --- end of user code ---
71-
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/shl32.asm"
112+
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/bitwise/shl32.asm"
72113
push namespace core
73114
__SHL32: ; Left Logical Shift 32 bits
74115
sla l
@@ -77,5 +118,31 @@ __SHL32: ; Left Logical Shift 32 bits
77118
rl d
78119
ret
79120
pop namespace
80-
#line 46 "arch/zx48k/shlu32.bas"
121+
#line 87 "arch/zx48k/shlu32.bas"
122+
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/cmp/eq32.asm"
123+
push namespace core
124+
__EQ32: ; Test if 32bit value HLDE equals top of the stack
125+
; Returns result in A: 0 = False, FF = True
126+
exx
127+
pop bc ; Return address
128+
exx
129+
xor a ; Reset carry flag
130+
pop bc
131+
sbc hl, bc ; Low part
132+
ex de, hl
133+
pop bc
134+
sbc hl, bc ; High part
135+
exx
136+
push bc ; CALLEE
137+
exx
138+
ld a, h
139+
or l
140+
or d
141+
or e ; a = 0 and Z flag set only if HLDE = 0
142+
ld a, 1
143+
ret z
144+
xor a
145+
ret
146+
pop namespace
147+
#line 88 "arch/zx48k/shlu32.bas"
81148
END

tests/functional/arch/zx48k/shlu32.bas

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ a = a << b
55
a = a << 1
66
a = a << 0
77
a = 0 << b
8+
a = (a = a) << b
9+
a = a << (b = b)
10+

tests/functional/arch/zx48k/shlu8.asm

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,39 @@ _b:
4040
ld (_a), a
4141
ld a, (_b)
4242
xor a
43+
ld (_a), a
44+
ld hl, (_a - 1)
45+
ld a, (_a)
46+
sub h
47+
sub 1
48+
sbc a, a
49+
push af
50+
ld a, (_b)
51+
pop hl
52+
or a
53+
ld b, a
54+
ld a, h
55+
jr z, .LABEL.__LABEL3
56+
.LABEL.__LABEL2:
57+
add a, a
58+
djnz .LABEL.__LABEL2
59+
.LABEL.__LABEL3:
60+
ld (_a), a
61+
ld hl, (_b - 1)
62+
ld a, (_b)
63+
sub h
64+
sub 1
65+
sbc a, a
66+
neg
67+
ld hl, (_a - 1)
68+
or a
69+
ld b, a
70+
ld a, h
71+
jr z, .LABEL.__LABEL5
72+
.LABEL.__LABEL4:
73+
add a, a
74+
djnz .LABEL.__LABEL4
75+
.LABEL.__LABEL5:
4376
ld (_a), a
4477
ld hl, 0
4578
ld b, h

tests/functional/arch/zx48k/shlu8.bas

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ a = a << b
55
a = a << 1
66
a = a << 0
77
a = 0 << b
8+
a = (a = a) << b
9+
a = a << (b = b)
10+

0 commit comments

Comments
 (0)