Skip to content

Commit 04b0b1b

Browse files
authored
Merge pull request #566 from boriel/feature/optimize_mul8_and_mul16_for_zxnext
feat: add optimization for mul8 in zxnext
2 parents a40b013 + d046e61 commit 04b0b1b

6 files changed

Lines changed: 57 additions & 9 deletions

File tree

src/arch/z80/peephole/engine.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,15 @@ def init():
146146
PATTERNS.clear()
147147

148148

149-
def main(list_of_directories: Optional[List[str]] = None):
149+
def main(list_of_directories: Optional[List[str]] = None, force: bool = False):
150150
"""Initializes the module and load all the *.opt files
151151
containing patterns and parses them. Valid .opt files will be stored in
152152
PATTERNS
153153
"""
154154
global PATTERNS
155155
global MAXLEN
156156

157-
if MAXLEN: # If already loaded, don't reload (cache)
157+
if not force and MAXLEN: # If already loaded, don't reload (cache)
158158
return
159159

160160
init()

src/arch/zxnext/backend/__init__.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
# -*- coding: utf-8 -*-
22

3-
from src.api.config import OPTIONS
4-
from src.arch.z80 import backend
3+
from src.api.config import OPTIONS, Action
4+
from src.arch.z80.backend.runtime.namespace import NAMESPACE
5+
6+
from src.arch.z80.backend import common
7+
from src.arch.z80.backend import engine
58
from src.arch.z80.backend import ICInfo
69

10+
from src.arch.zxnext.peephole import OPTS_PATH
11+
712
from src.arch.zxnext.backend._8bit import _mul8
813

914
from src.arch.z80.backend import tmp_label, _fpop, HI16, INITS, LO16, LABEL_COUNTER, MEMORY, MEMINITS
@@ -41,5 +46,19 @@
4146
def init():
4247
# ZXNext asm enabled by default for this arch
4348
OPTIONS.zxnext = True
49+
"""Initializes this module"""
50+
51+
common.init()
52+
53+
# Default code ORG
54+
OPTIONS(Action.ADD_IF_NOT_DEFINED, name="org", type=int, default=32768)
55+
# Default HEAP SIZE (Dynamic memory) in bytes
56+
OPTIONS(Action.ADD_IF_NOT_DEFINED, name="heap_size", type=int, default=4768, ignore_none=True) # A bit more than 4K
57+
# Labels for HEAP START (might not be used if not needed)
58+
OPTIONS(Action.ADD_IF_NOT_DEFINED, name="heap_start_label", type=str, default=f"{NAMESPACE}.ZXBASIC_MEM_HEAP")
59+
# Labels for HEAP SIZE (might not be used if not needed)
60+
OPTIONS(Action.ADD_IF_NOT_DEFINED, name="heap_size_label", type=str, default=f"{NAMESPACE}.ZXBASIC_HEAP_SIZE")
61+
# Flag for headerless mode (No prologue / epilogue)
62+
OPTIONS(Action.ADD_IF_NOT_DEFINED, name="headerless", type=bool, default=False, ignore_none=True)
4463

45-
backend.init()
64+
engine.main([engine.OPTS_PATH, OPTS_PATH], force=True) # inits the optimizer
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import os
4+
5+
# Optimization recipes paths for this arch
6+
OPTS_PATH = os.path.join(os.path.dirname(__file__), "opts")
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
;; Change sequence:
2+
;; ld h, XX
3+
;; ld d, h
4+
5+
;; into:
6+
7+
;; ld d, XX
8+
9+
;; if it's used for an 8 bit multiplication (mul d, e)
10+
11+
OLEVEL: 1
12+
OFLAG: 1
13+
14+
REPLACE {{
15+
ld h, $1
16+
ld d, h
17+
ld e, a
18+
mul d, e
19+
}}
20+
21+
WITH {{
22+
ld d, $1
23+
ld e, a
24+
mul d, e
25+
}}

tests/functional/zxnext/muli8.asm

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ _a:
2323
.core.ZXBASIC_USER_DATA_END:
2424
.core.__MAIN_PROGRAM__:
2525
ld a, (_a)
26-
ld h, 3
27-
ld d, h
26+
ld d, 3
2827
ld e, a
2928
mul d, e
3029
ld a, e

tests/functional/zxnext/mulu8.asm

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ _a:
2323
.core.ZXBASIC_USER_DATA_END:
2424
.core.__MAIN_PROGRAM__:
2525
ld a, (_a)
26-
ld h, 3
27-
ld d, h
26+
ld d, 3
2827
ld e, a
2928
mul d, e
3029
ld a, e

0 commit comments

Comments
 (0)