Skip to content

Commit 2f57028

Browse files
committed
feat: make STRING operators bool normalized
1 parent 8c30906 commit 2f57028

9 files changed

Lines changed: 87 additions & 69 deletions

File tree

src/arch/z80/peephole/opts/020_o1_bool_norm_empty.opt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@ IF {{
2626
"call .core.__GEF", "call .core.__LEI16", "call .core.__LEI8", "call .core.__LTI8",
2727
"call .core.__ANDF", "call .core.__EQF", "call .core.__GTF", "call .core.__LTI16",
2828
"call .core.__LEF", "call .core.__LEI32", "call .core.__LTF", "call .core.__LTI32",
29-
"call .core.__NEF", "call .core.__NOTF", "call .core.__ORF", "call .core.__XORF")
29+
"call .core.__NEF", "call .core.__NOTF", "call .core.__ORF", "call .core.__XORF",
30+
"call .core.__STREQ", "call .core.__STRNE", "call .core.__STRLT", "call .core.__STRLE",
31+
"call .core.__STRGT", "call .core.__STRGE")
3032
}}

src/lib/arch/zx48k/runtime/string.asm

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,11 @@ __STREQ: ; Compares a$ == b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0
126126
pop de
127127
pop hl
128128
sub 1
129-
sbc a, a
129+
sbc a, a ; 0 if A register was 0, 0xFF if A was 1 or -1
130130
jp __FREE_STR
131131

132132

133-
__STRNE: ; Compares a$ != b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (False)
133+
__STRNE: ; Compares a$ != b$ (HL = ptr a$, DE = ptr b$). Returns 1 (True) or 0 (False)
134134
push hl
135135
push de
136136
call __STRCMP
@@ -139,7 +139,7 @@ __STRNE: ; Compares a$ != b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0
139139
jp __FREE_STR
140140

141141

142-
__STRLT: ; Compares a$ < b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (False)
142+
__STRLT: ; Compares a$ < b$ (HL = ptr a$, DE = ptr b$). Returns FE (True) or 0 (False)
143143
push hl
144144
push de
145145
call __STRCMP
@@ -152,7 +152,7 @@ __STRLT: ; Compares a$ < b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (
152152
jp __FREE_STR
153153

154154

155-
__STRLE: ; Compares a$ <= b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (False)
155+
__STRLE: ; Compares a$ <= b$ (HL = ptr a$, DE = ptr b$). Returns FF or FE (True) or 0 (False)
156156
push hl
157157
push de
158158
call __STRCMP
@@ -163,7 +163,7 @@ __STRLE: ; Compares a$ <= b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0
163163
jp __FREE_STR
164164

165165

166-
__STRGT: ; Compares a$ > b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (False)
166+
__STRGT: ; Compares a$ > b$ (HL = ptr a$, DE = ptr b$). Returns 2 (True) or 0 (False)
167167
push hl
168168
push de
169169
call __STRCMP
@@ -176,7 +176,7 @@ __STRGT: ; Compares a$ > b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (
176176
jp __FREE_STR
177177

178178

179-
__STRGE: ; Compares a$ >= b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (False)
179+
__STRGE: ; Compares a$ >= b$ (HL = ptr a$, DE = ptr b$). Returns 1 or 2 (True) or 0 (False)
180180
push hl
181181
push de
182182
call __STRCMP
@@ -193,6 +193,10 @@ __FREE_STR: ; This exit point will test A' for bits 0 and 1
193193

194194
LOCAL __FREE_STR2
195195
LOCAL __FREE_END
196+
;; normalize boolean
197+
sub 1
198+
sbc a, a
199+
inc a
196200

197201
ex af, af'
198202
bit 0, a

tests/functional/arch/zx48k/streq00.asm

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,16 +1020,16 @@ __STREQ: ; Compares a$ == b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0
10201020
pop de
10211021
pop hl
10221022
sub 1
1023-
sbc a, a
1023+
sbc a, a ; 0 if A register was 0, 0xFF if A was 1 or -1
10241024
jp __FREE_STR
1025-
__STRNE: ; Compares a$ != b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (False)
1025+
__STRNE: ; Compares a$ != b$ (HL = ptr a$, DE = ptr b$). Returns 1 (True) or 0 (False)
10261026
push hl
10271027
push de
10281028
call __STRCMP
10291029
pop de
10301030
pop hl
10311031
jp __FREE_STR
1032-
__STRLT: ; Compares a$ < b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (False)
1032+
__STRLT: ; Compares a$ < b$ (HL = ptr a$, DE = ptr b$). Returns FE (True) or 0 (False)
10331033
push hl
10341034
push de
10351035
call __STRCMP
@@ -1039,15 +1039,15 @@ __STRLT: ; Compares a$ < b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (
10391039
jp z, __FREE_STR ; Returns 0 if A == B
10401040
dec a ; Returns 0 if A == 1 => a$ > b$
10411041
jp __FREE_STR
1042-
__STRLE: ; Compares a$ <= b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (False)
1042+
__STRLE: ; Compares a$ <= b$ (HL = ptr a$, DE = ptr b$). Returns FF or FE (True) or 0 (False)
10431043
push hl
10441044
push de
10451045
call __STRCMP
10461046
pop de
10471047
pop hl
10481048
dec a ; Returns 0 if A == 1 => a$ < b$
10491049
jp __FREE_STR
1050-
__STRGT: ; Compares a$ > b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (False)
1050+
__STRGT: ; Compares a$ > b$ (HL = ptr a$, DE = ptr b$). Returns 2 (True) or 0 (False)
10511051
push hl
10521052
push de
10531053
call __STRCMP
@@ -1057,7 +1057,7 @@ __STRGT: ; Compares a$ > b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (
10571057
jp z, __FREE_STR ; Returns 0 if A == B
10581058
inc a ; Returns 0 if A == -1 => a$ < b$
10591059
jp __FREE_STR
1060-
__STRGE: ; Compares a$ >= b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (False)
1060+
__STRGE: ; Compares a$ >= b$ (HL = ptr a$, DE = ptr b$). Returns 1 or 2 (True) or 0 (False)
10611061
push hl
10621062
push de
10631063
call __STRCMP
@@ -1071,6 +1071,10 @@ __FREE_STR: ; This exit point will test A' for bits 0 and 1
10711071
PROC
10721072
LOCAL __FREE_STR2
10731073
LOCAL __FREE_END
1074+
;; normalize boolean
1075+
sub 1
1076+
sbc a, a
1077+
inc a
10741078
ex af, af'
10751079
bit 0, a
10761080
jr z, __FREE_STR2

tests/functional/arch/zx48k/strge00.asm

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,16 +1020,16 @@ __STREQ: ; Compares a$ == b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0
10201020
pop de
10211021
pop hl
10221022
sub 1
1023-
sbc a, a
1023+
sbc a, a ; 0 if A register was 0, 0xFF if A was 1 or -1
10241024
jp __FREE_STR
1025-
__STRNE: ; Compares a$ != b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (False)
1025+
__STRNE: ; Compares a$ != b$ (HL = ptr a$, DE = ptr b$). Returns 1 (True) or 0 (False)
10261026
push hl
10271027
push de
10281028
call __STRCMP
10291029
pop de
10301030
pop hl
10311031
jp __FREE_STR
1032-
__STRLT: ; Compares a$ < b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (False)
1032+
__STRLT: ; Compares a$ < b$ (HL = ptr a$, DE = ptr b$). Returns FE (True) or 0 (False)
10331033
push hl
10341034
push de
10351035
call __STRCMP
@@ -1039,15 +1039,15 @@ __STRLT: ; Compares a$ < b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (
10391039
jp z, __FREE_STR ; Returns 0 if A == B
10401040
dec a ; Returns 0 if A == 1 => a$ > b$
10411041
jp __FREE_STR
1042-
__STRLE: ; Compares a$ <= b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (False)
1042+
__STRLE: ; Compares a$ <= b$ (HL = ptr a$, DE = ptr b$). Returns FF or FE (True) or 0 (False)
10431043
push hl
10441044
push de
10451045
call __STRCMP
10461046
pop de
10471047
pop hl
10481048
dec a ; Returns 0 if A == 1 => a$ < b$
10491049
jp __FREE_STR
1050-
__STRGT: ; Compares a$ > b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (False)
1050+
__STRGT: ; Compares a$ > b$ (HL = ptr a$, DE = ptr b$). Returns 2 (True) or 0 (False)
10511051
push hl
10521052
push de
10531053
call __STRCMP
@@ -1057,7 +1057,7 @@ __STRGT: ; Compares a$ > b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (
10571057
jp z, __FREE_STR ; Returns 0 if A == B
10581058
inc a ; Returns 0 if A == -1 => a$ < b$
10591059
jp __FREE_STR
1060-
__STRGE: ; Compares a$ >= b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (False)
1060+
__STRGE: ; Compares a$ >= b$ (HL = ptr a$, DE = ptr b$). Returns 1 or 2 (True) or 0 (False)
10611061
push hl
10621062
push de
10631063
call __STRCMP
@@ -1071,6 +1071,10 @@ __FREE_STR: ; This exit point will test A' for bits 0 and 1
10711071
PROC
10721072
LOCAL __FREE_STR2
10731073
LOCAL __FREE_END
1074+
;; normalize boolean
1075+
sub 1
1076+
sbc a, a
1077+
inc a
10741078
ex af, af'
10751079
bit 0, a
10761080
jr z, __FREE_STR2

tests/functional/arch/zx48k/strgt00.asm

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,16 +1020,16 @@ __STREQ: ; Compares a$ == b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0
10201020
pop de
10211021
pop hl
10221022
sub 1
1023-
sbc a, a
1023+
sbc a, a ; 0 if A register was 0, 0xFF if A was 1 or -1
10241024
jp __FREE_STR
1025-
__STRNE: ; Compares a$ != b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (False)
1025+
__STRNE: ; Compares a$ != b$ (HL = ptr a$, DE = ptr b$). Returns 1 (True) or 0 (False)
10261026
push hl
10271027
push de
10281028
call __STRCMP
10291029
pop de
10301030
pop hl
10311031
jp __FREE_STR
1032-
__STRLT: ; Compares a$ < b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (False)
1032+
__STRLT: ; Compares a$ < b$ (HL = ptr a$, DE = ptr b$). Returns FE (True) or 0 (False)
10331033
push hl
10341034
push de
10351035
call __STRCMP
@@ -1039,15 +1039,15 @@ __STRLT: ; Compares a$ < b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (
10391039
jp z, __FREE_STR ; Returns 0 if A == B
10401040
dec a ; Returns 0 if A == 1 => a$ > b$
10411041
jp __FREE_STR
1042-
__STRLE: ; Compares a$ <= b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (False)
1042+
__STRLE: ; Compares a$ <= b$ (HL = ptr a$, DE = ptr b$). Returns FF or FE (True) or 0 (False)
10431043
push hl
10441044
push de
10451045
call __STRCMP
10461046
pop de
10471047
pop hl
10481048
dec a ; Returns 0 if A == 1 => a$ < b$
10491049
jp __FREE_STR
1050-
__STRGT: ; Compares a$ > b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (False)
1050+
__STRGT: ; Compares a$ > b$ (HL = ptr a$, DE = ptr b$). Returns 2 (True) or 0 (False)
10511051
push hl
10521052
push de
10531053
call __STRCMP
@@ -1057,7 +1057,7 @@ __STRGT: ; Compares a$ > b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (
10571057
jp z, __FREE_STR ; Returns 0 if A == B
10581058
inc a ; Returns 0 if A == -1 => a$ < b$
10591059
jp __FREE_STR
1060-
__STRGE: ; Compares a$ >= b$ (HL = ptr a$, DE = ptr b$). Returns FF (True) or 0 (False)
1060+
__STRGE: ; Compares a$ >= b$ (HL = ptr a$, DE = ptr b$). Returns 1 or 2 (True) or 0 (False)
10611061
push hl
10621062
push de
10631063
call __STRCMP
@@ -1071,6 +1071,10 @@ __FREE_STR: ; This exit point will test A' for bits 0 and 1
10711071
PROC
10721072
LOCAL __FREE_STR2
10731073
LOCAL __FREE_END
1074+
;; normalize boolean
1075+
sub 1
1076+
sbc a, a
1077+
inc a
10741078
ex af, af'
10751079
bit 0, a
10761080
jr z, __FREE_STR2

tests/functional/arch/zx48k/strict_bool.asm

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ _a:
2828
ld de, 00020h
2929
ld bc, 00000h
3030
call .core.__LTF
31-
call .core.__NORMALIZE_BOOLEAN
3231
call .core.__U8TOFREG
3332
ld hl, _a
3433
call .core.__STOREF
@@ -47,9 +46,9 @@ _a:
4746
ei
4847
ret
4948
;; --- end of user code ---
50-
#line 1 "/zxbasic/src/arch/zx48k/library-asm/ltf.asm"
51-
#line 1 "/zxbasic/src/arch/zx48k/library-asm/u32tofreg.asm"
52-
#line 1 "/zxbasic/src/arch/zx48k/library-asm/neg32.asm"
49+
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/cmp/ltf.asm"
50+
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/u32tofreg.asm"
51+
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/neg32.asm"
5352
push namespace core
5453
__ABS32:
5554
bit 7, d
@@ -74,7 +73,7 @@ __NEG32: ; Negates DEHL (Two's complement)
7473
inc de
7574
ret
7675
pop namespace
77-
#line 2 "/zxbasic/src/arch/zx48k/library-asm/u32tofreg.asm"
76+
#line 2 "/zxbasic/src/lib/arch/zx48k/runtime/u32tofreg.asm"
7877
push namespace core
7978
__I8TOFREG:
8079
ld l, a
@@ -144,8 +143,8 @@ __U32TOFREG_END:
144143
ret
145144
ENDP
146145
pop namespace
147-
#line 2 "/zxbasic/src/arch/zx48k/library-asm/ltf.asm"
148-
#line 1 "/zxbasic/src/arch/zx48k/library-asm/ftou32reg.asm"
146+
#line 2 "/zxbasic/src/lib/arch/zx48k/runtime/cmp/ltf.asm"
147+
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/ftou32reg.asm"
149148
push namespace core
150149
__FTOU32REG: ; Converts a Float to (un)signed 32 bit integer (NOTE: It's ALWAYS 32 bit signed)
151150
; Input FP number in A EDCB (A exponent, EDCB mantissa)
@@ -217,8 +216,8 @@ __FTOU8: ; Converts float in C ED LH to Unsigned byte in A
217216
ld a, l
218217
ret
219218
pop namespace
220-
#line 3 "/zxbasic/src/arch/zx48k/library-asm/ltf.asm"
221-
#line 1 "/zxbasic/src/arch/zx48k/library-asm/stackf.asm"
219+
#line 3 "/zxbasic/src/lib/arch/zx48k/runtime/cmp/ltf.asm"
220+
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/stackf.asm"
222221
; -------------------------------------------------------------
223222
; Functions to manage FP-Stack of the ZX Spectrum ROM CALC
224223
; -------------------------------------------------------------
@@ -257,7 +256,7 @@ __FPSTACK_I16: ; Pushes 16 bits integer in HL into the FP ROM STACK
257256
ld b, a
258257
jp __FPSTACK_PUSH
259258
pop namespace
260-
#line 4 "/zxbasic/src/arch/zx48k/library-asm/ltf.asm"
259+
#line 4 "/zxbasic/src/lib/arch/zx48k/runtime/cmp/ltf.asm"
261260
; -------------------------------------------------------------
262261
; Floating point library using the FP ROM Calculator (ZX 48K)
263262
; All of them uses A EDCB registers as 1st paramter.
@@ -277,8 +276,8 @@ __LTF: ; A < B
277276
call __FPSTACK_POP
278277
jp __FTOU8 ; Convert to 8 bits
279278
pop namespace
280-
#line 27 "strict_bool.bas"
281-
#line 1 "/zxbasic/src/arch/zx48k/library-asm/pushf.asm"
279+
#line 26 "arch/zx48k/strict_bool.bas"
280+
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/pushf.asm"
282281
; Routine to push Float pointed by HL
283282
; Into the stack. Notice that the hl points to the last
284283
; byte of the FP number.
@@ -305,8 +304,8 @@ __FP_PUSH_REV:
305304
exx
306305
ret
307306
pop namespace
308-
#line 28 "strict_bool.bas"
309-
#line 1 "/zxbasic/src/arch/zx48k/library-asm/storef.asm"
307+
#line 27 "arch/zx48k/strict_bool.bas"
308+
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/storef.asm"
310309
push namespace core
311310
__PISTOREF: ; Indect Stores a float (A, E, D, C, B) at location stored in memory, pointed by (IX + HL)
312311
push de
@@ -334,16 +333,5 @@ __STOREF: ; Stores the given FP number in A EDCB at address HL
334333
ld (hl), b
335334
ret
336335
pop namespace
337-
#line 29 "strict_bool.bas"
338-
#line 1 "/zxbasic/src/arch/zx48k/library-asm/strictbool.asm"
339-
; This routine is called if --strict-boolean was set at the command line.
340-
; It will make any boolean result to be always 0 or 1
341-
push namespace core
342-
__NORMALIZE_BOOLEAN:
343-
or a
344-
ret z
345-
ld a, 1
346-
ret
347-
pop namespace
348-
#line 30 "strict_bool.bas"
336+
#line 28 "arch/zx48k/strict_bool.bas"
349337
END

0 commit comments

Comments
 (0)