Skip to content

Commit 81dd8a3

Browse files
committed
fix: crash on SHL with bool expressions
1 parent 358c65b commit 81dd8a3

13 files changed

Lines changed: 319 additions & 5 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
@@ -226,7 +226,7 @@ def ic_shl(self, type_: TYPE | sym.BASICTYPE, t, t1, t2) -> None:
226226
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:
229-
self.emit("shr" + self.TSUFFIX(type_), t, t1, t2)
229+
self.emit(f"shr{self._no_bool(type_)}", t, t1, t2)
230230

231231
def ic_store(self, type_: TYPE | sym.BASICTYPE, t1, t2) -> None:
232232
self.emit("store" + self.TSUFFIX(type_), t1, t2)

tests/functional/arch/zx48k/shri16.asm

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,39 @@ _b:
4444
xor a
4545
ld l, a
4646
ld h, 0
47+
ld (_a), hl
48+
ld de, (_a)
49+
ld hl, (_a)
50+
call .core.__EQ16
51+
push af
52+
ld a, (_b)
53+
pop hl
54+
or a
55+
ld b, a
56+
ld a, h
57+
jr z, .LABEL.__LABEL7
58+
.LABEL.__LABEL6:
59+
srl a
60+
djnz .LABEL.__LABEL6
61+
.LABEL.__LABEL7:
62+
ld l, a
63+
ld h, 0
64+
ld (_a), hl
65+
ld de, (_a)
66+
ld hl, (_a)
67+
call .core.__EQ16
68+
sub 1
69+
sbc a, a
70+
inc a
71+
ld b, a
72+
ld hl, (_a)
73+
or a
74+
jr z, .LABEL.__LABEL9
75+
.LABEL.__LABEL8:
76+
sra h
77+
rr l
78+
djnz .LABEL.__LABEL8
79+
.LABEL.__LABEL9:
4780
ld (_a), hl
4881
ld hl, 0
4982
ld b, h
@@ -60,4 +93,15 @@ _b:
6093
ei
6194
ret
6295
;; --- end of user code ---
96+
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/cmp/eq16.asm"
97+
push namespace core
98+
__EQ16: ; Test if 16bit values HL == DE
99+
; Returns result in A: 0 = False, FF = True
100+
xor a ; Reset carry flag
101+
sbc hl, de
102+
ret nz
103+
inc a
104+
ret
105+
pop namespace
106+
#line 71 "arch/zx48k/shri16.bas"
63107
END

tests/functional/arch/zx48k/shri16.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 >> (a = a)
10+

tests/functional/arch/zx48k/shri32.asm

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,51 @@ _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+
srl 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, (_a + 2)
81+
push hl
82+
ld hl, (_a)
83+
push hl
84+
ld hl, (_a)
85+
ld de, (_a + 2)
86+
call .core.__EQ32
87+
sub 1
88+
sbc a, a
89+
inc a
90+
ld b, a
91+
ld hl, (_a)
92+
ld de, (_a + 2)
93+
or a
94+
jr z, .LABEL.__LABEL5
95+
.LABEL.__LABEL4:
96+
call .core.__SHRA32
97+
djnz .LABEL.__LABEL4
98+
.LABEL.__LABEL5:
5499
ld (_a), hl
55100
ld (_a + 2), de
56101
ld hl, 0
@@ -68,7 +113,7 @@ _b:
68113
ei
69114
ret
70115
;; --- end of user code ---
71-
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/shra32.asm"
116+
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/bitwise/shra32.asm"
72117
push namespace core
73118
__SHRA32: ; Right Arithmetical Shift 32 bits
74119
sra d
@@ -77,5 +122,31 @@ __SHRA32: ; Right Arithmetical Shift 32 bits
77122
rr l
78123
ret
79124
pop namespace
80-
#line 46 "arch/zx48k/shri32.bas"
125+
#line 91 "arch/zx48k/shri32.bas"
126+
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/cmp/eq32.asm"
127+
push namespace core
128+
__EQ32: ; Test if 32bit value HLDE equals top of the stack
129+
; Returns result in A: 0 = False, FF = True
130+
exx
131+
pop bc ; Return address
132+
exx
133+
xor a ; Reset carry flag
134+
pop bc
135+
sbc hl, bc ; Low part
136+
ex de, hl
137+
pop bc
138+
sbc hl, bc ; High part
139+
exx
140+
push bc ; CALLEE
141+
exx
142+
ld a, h
143+
or l
144+
or d
145+
or e ; a = 0 and Z flag set only if HLDE = 0
146+
ld a, 1
147+
ret z
148+
xor a
149+
ret
150+
pop namespace
151+
#line 92 "arch/zx48k/shri32.bas"
81152
END

tests/functional/arch/zx48k/shri32.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 >> (a = a)
10+

tests/functional/arch/zx48k/shri8.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+
srl a
58+
djnz .LABEL.__LABEL2
59+
.LABEL.__LABEL3:
60+
ld (_a), a
61+
ld hl, (_a - 1)
62+
ld a, (_a)
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+
sra a
74+
djnz .LABEL.__LABEL4
75+
.LABEL.__LABEL5:
4376
ld (_a), a
4477
ld hl, 0
4578
ld b, h

tests/functional/arch/zx48k/shri8.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 >> (a = a)
10+

tests/functional/arch/zx48k/shru16.asm

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,39 @@ _b:
4444
xor a
4545
ld l, a
4646
ld h, 0
47+
ld (_a), hl
48+
ld de, (_a)
49+
ld hl, (_a)
50+
call .core.__EQ16
51+
push af
52+
ld a, (_b)
53+
pop hl
54+
or a
55+
ld b, a
56+
ld a, h
57+
jr z, .LABEL.__LABEL7
58+
.LABEL.__LABEL6:
59+
srl a
60+
djnz .LABEL.__LABEL6
61+
.LABEL.__LABEL7:
62+
ld l, a
63+
ld h, 0
64+
ld (_a), hl
65+
ld de, (_a)
66+
ld hl, (_a)
67+
call .core.__EQ16
68+
sub 1
69+
sbc a, a
70+
inc a
71+
ld b, a
72+
ld hl, (_a)
73+
or a
74+
jr z, .LABEL.__LABEL9
75+
.LABEL.__LABEL8:
76+
srl h
77+
rr l
78+
djnz .LABEL.__LABEL8
79+
.LABEL.__LABEL9:
4780
ld (_a), hl
4881
ld hl, 0
4982
ld b, h
@@ -60,4 +93,15 @@ _b:
6093
ei
6194
ret
6295
;; --- end of user code ---
96+
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/cmp/eq16.asm"
97+
push namespace core
98+
__EQ16: ; Test if 16bit values HL == DE
99+
; Returns result in A: 0 = False, FF = True
100+
xor a ; Reset carry flag
101+
sbc hl, de
102+
ret nz
103+
inc a
104+
ret
105+
pop namespace
106+
#line 71 "arch/zx48k/shru16.bas"
63107
END

tests/functional/arch/zx48k/shru16.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 >> (a = a)
10+

tests/functional/arch/zx48k/shru32.asm

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,51 @@ _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+
srl 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, (_a + 2)
81+
push hl
82+
ld hl, (_a)
83+
push hl
84+
ld hl, (_a)
85+
ld de, (_a + 2)
86+
call .core.__EQ32
87+
sub 1
88+
sbc a, a
89+
inc a
90+
ld b, a
91+
ld hl, (_a)
92+
ld de, (_a + 2)
93+
or a
94+
jr z, .LABEL.__LABEL5
95+
.LABEL.__LABEL4:
96+
call .core.__SHRL32
97+
djnz .LABEL.__LABEL4
98+
.LABEL.__LABEL5:
5499
ld (_a), hl
55100
ld (_a + 2), de
56101
ld hl, 0
@@ -68,7 +113,7 @@ _b:
68113
ei
69114
ret
70115
;; --- end of user code ---
71-
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/shrl32.asm"
116+
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/bitwise/shrl32.asm"
72117
push namespace core
73118
__SHRL32: ; Right Logical Shift 32 bits
74119
srl d
@@ -77,5 +122,31 @@ __SHRL32: ; Right Logical Shift 32 bits
77122
rr l
78123
ret
79124
pop namespace
80-
#line 46 "arch/zx48k/shru32.bas"
125+
#line 91 "arch/zx48k/shru32.bas"
126+
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/cmp/eq32.asm"
127+
push namespace core
128+
__EQ32: ; Test if 32bit value HLDE equals top of the stack
129+
; Returns result in A: 0 = False, FF = True
130+
exx
131+
pop bc ; Return address
132+
exx
133+
xor a ; Reset carry flag
134+
pop bc
135+
sbc hl, bc ; Low part
136+
ex de, hl
137+
pop bc
138+
sbc hl, bc ; High part
139+
exx
140+
push bc ; CALLEE
141+
exx
142+
ld a, h
143+
or l
144+
or d
145+
or e ; a = 0 and Z flag set only if HLDE = 0
146+
ld a, 1
147+
ret z
148+
xor a
149+
ret
150+
pop namespace
151+
#line 92 "arch/zx48k/shru32.bas"
81152
END

0 commit comments

Comments
 (0)