Skip to content

Commit 6dfb9ad

Browse files
authored
Merge pull request #216 from boriel/bugfix/optimizer
Bugfix/optimizer
2 parents 17ea665 + 447fda6 commit 6dfb9ad

File tree

6 files changed

+88
-3
lines changed

6 files changed

+88
-3
lines changed

arch/zx48k/optimizer/cpustate.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,12 @@ def execute(self, asm_code):
568568
return
569569

570570
if i == 'ex':
571-
for j in 'af', 'a', 'f':
572-
self.regs[j], self.regs["%s'" % j] = self.regs["%s'" % j], self.regs[j]
571+
if o == ['de', 'hl']:
572+
for a, b in [('de', 'hl'), ('d', 'h'), ('e', 'l')]:
573+
self.regs[a], self.regs[b] = self.regs[b], self.regs[a]
574+
else:
575+
for j in 'af', 'a', 'f':
576+
self.regs[j], self.regs["%s'" % j] = self.regs["%s'" % j], self.regs[j]
573577
return
574578

575579
if i == 'xor':

tests/arch/zx48k/optimizer/test_cpustate.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,29 @@ def test_xor_a_ld_n_a(self):
337337
self.assertFalse(helpers.is_unknown8(self.regs['a']))
338338
self.assertEqual(self.cpu_state.C, 0)
339339
self.assertEqual(self.cpu_state.Z, 1)
340+
341+
def test_ex_de_hl(self):
342+
code = """
343+
ld hl, 0x1234
344+
ld de, 0x5678
345+
ex de, hl
346+
"""
347+
self._eval(code)
348+
self.assertEqual(self.regs['h'], str(0x56))
349+
self.assertEqual(self.regs['l'], str(0x78))
350+
self.assertEqual(self.regs['d'], str(0x12))
351+
self.assertEqual(self.regs['e'], str(0x34))
352+
353+
def test_ex_de_hl_unkown(self):
354+
code = """
355+
ld hl, (x)
356+
ld de, (y)
357+
ex de, hl
358+
"""
359+
self._eval(code)
360+
self.assertEqual(self.regs['hl'], self.cpu_state.mem['y'])
361+
self.assertEqual(self.regs['de'], self.cpu_state.mem['x'])
362+
self.assertEqual(self.regs['h'], helpers.HI16_val(self.cpu_state.mem['y']))
363+
self.assertEqual(self.regs['l'], helpers.LO16_val(self.cpu_state.mem['y']))
364+
self.assertEqual(self.regs['d'], helpers.HI16_val(self.cpu_state.mem['x']))
365+
self.assertEqual(self.regs['e'], helpers.LO16_val(self.cpu_state.mem['x']))

tests/functional/opt3_fixed.asm

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
org 32768
2+
__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 (__CALL_BACK__), hl
12+
ei
13+
ld hl, (_x)
14+
ld de, (_x + 2)
15+
ld bc, (_dx)
16+
add hl, bc
17+
ex de, hl
18+
ld bc, (_dx + 2)
19+
adc hl, bc
20+
ex de, hl
21+
ld (_x), hl
22+
ld (_x + 2), de
23+
ex de, hl
24+
xor a
25+
ld (hl), a
26+
ld bc, 0
27+
__END_PROGRAM:
28+
di
29+
ld hl, (__CALL_BACK__)
30+
ld sp, hl
31+
exx
32+
pop hl
33+
pop iy
34+
pop ix
35+
exx
36+
ei
37+
ret
38+
__CALL_BACK__:
39+
DEFW 0
40+
ZXBASIC_USER_DATA:
41+
_x:
42+
DEFB 00, 00, 00, 00
43+
_dx:
44+
DEFB 00, 00, 00, 00
45+
; Defines DATA END --> HEAP size is 0
46+
ZXBASIC_USER_DATA_END EQU ZXBASIC_MEM_HEAP
47+
; Defines USER DATA Length in bytes
48+
ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA
49+
END

tests/functional/opt3_fixed.bas

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DIM x, dx as Fixed
2+
3+
let x = x + dx
4+
POKE x, 0
5+

tests/functional/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def get_file_lines(filename, ignore_regexp=None, replace_regexp=None,
119119
lines = [x.replace(replace_what, replace_with, 1) if r.search(x) else x for x in lines]
120120

121121
if strip_blanks:
122-
lines = [x.rstrip() for x in lines if x.rstrip()]
122+
lines = [x.rstrip(' \t') for x in lines if x.rstrip()]
123123

124124
return lines
125125

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ exclude =
2626
library-asm/,
2727
parsetab/,
2828
scratch/,
29+
.git/
2930

3031
[testenv:flake8]
3132
deps = flake8

0 commit comments

Comments
 (0)