Skip to content

Commit 2bdc0d5

Browse files
committed
feat: allow multiple archs include paths
1 parent 06fc36d commit 2bdc0d5

4 files changed

Lines changed: 42 additions & 24 deletions

File tree

src/arch/z80/optimizer/cpustate.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,9 @@ def set(self, r: str, val: int | str | None) -> None:
417417

418418
if is_unknown8(val):
419419
val = f"{new_tmp_val()}{HL_SEP}{val}"
420-
assert is_num or is_unknown16(val) or is_label(val), (
421-
f"val '{val}' is neither a number, nor a label nor an unknown16"
422-
)
420+
assert (
421+
is_num or is_unknown16(val) or is_label(val)
422+
), f"val '{val}' is neither a number, nor a label nor an unknown16"
423423

424424
self.regs[r] = val
425425
if is_16bit_composed_register(r): # sp register is not included. Special case

src/zxbasm/memory.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,9 @@ def declare_label(
229229

230230
fname = gl.FILENAME
231231
if label.isdecimal(): # Temporary label?
232-
assert not self._tmp_labels_lines[fname] or self._tmp_labels_lines[fname][-1] <= lineno, (
233-
"Temporary label out of order"
234-
)
232+
assert (
233+
not self._tmp_labels_lines[fname] or self._tmp_labels_lines[fname][-1] <= lineno
234+
), "Temporary label out of order"
235235
if not self._tmp_labels_lines[fname] or self._tmp_labels_lines[fname][-1] != lineno:
236236
self._tmp_labels_lines[fname].append(lineno)
237237

src/zxbc/args_config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,5 @@ def set_option_defines() -> None:
184184

185185
if OPTIONS.enable_break:
186186
OPTIONS.__DEFINES["__ENABLE_BREAK__"] = ""
187+
188+
OPTIONS.__DEFINES["__OPT_STRATEGY__"] = OPTIONS.opt_strategy

src/zxbpp/zxbpp.py

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
import sys
1414
from dataclasses import dataclass
1515
from enum import Enum, unique
16-
from typing import Any, NamedTuple
16+
from typing import Any, Final, NamedTuple
1717

1818
from src import arch
1919
from src.api import config, global_, utils
20+
from src.arch import AVAILABLE_ARCHITECTURES
2021
from src.ply import yacc
2122
from src.zxbpp import zxbasmpplex, zxbpplex
2223
from src.zxbpp.base_pplex import STDIN
@@ -51,6 +52,9 @@ class PreprocMode(str, Enum):
5152
# Default include path
5253
INCLUDEPATH: list[str] = ["stdlib", "runtime"]
5354

55+
# Include paths for every arch
56+
INCLUDE_MAP: Final[dict[str, list[str]]] = {}
57+
5458
# Enabled to FALSE if IFDEF failed
5559
ENABLED: bool = True
5660

@@ -71,12 +75,12 @@ class ParentIncludingFile(NamedTuple):
7175

7276
@dataclass
7377
class IncludedFileInfo:
74-
once: bool # whether this file is
78+
once: bool # whether this file is to be included only once (e.g. #pragma once)
7579
parents: list[ParentIncludingFile]
7680

7781

78-
# Files already includes, with a list of file, line where they were
79-
# included sinc a file can be included more than once.
82+
# Files already included, with a list of (file, line) tuples where they were
83+
# included, since a file can be included more than once.
8084
INCLUDED: dict[str, IncludedFileInfo] = {}
8185

8286
# IFDEFS array
@@ -135,17 +139,29 @@ def init():
135139
reset_id_table()
136140

137141

138-
def get_include_path() -> str:
139-
"""Default include path using a tricky sys calls."""
142+
def get_include_path(arch: str = "") -> str:
143+
"""Default include path using a tricky sys call."""
140144
return os.path.realpath(
141-
os.path.join(os.path.dirname(__file__), os.path.pardir, "lib", "arch", config.OPTIONS.architecture or "")
145+
os.path.join(
146+
os.path.dirname(__file__),
147+
os.path.pardir,
148+
"lib",
149+
"arch",
150+
arch or config.OPTIONS.architecture or "",
151+
)
142152
)
143153

144154

145155
def set_include_path():
146156
global INCLUDEPATH
147-
pwd = get_include_path()
148-
INCLUDEPATH = [os.path.join(pwd, "stdlib"), os.path.join(pwd, "runtime")]
157+
158+
INCLUDE_MAP.clear()
159+
160+
for arch_ in AVAILABLE_ARCHITECTURES:
161+
pwd = get_include_path(arch_)
162+
INCLUDE_MAP[arch_] = [os.path.join(pwd, "stdlib"), os.path.join(pwd, "runtime")]
163+
164+
INCLUDEPATH = INCLUDE_MAP.get(config.OPTIONS.architecture, [])
149165

150166

151167
def setMode(mode: PreprocMode) -> None:
@@ -163,15 +179,17 @@ def setMode(mode: PreprocMode) -> None:
163179
LEXER = lexers[PreprocMode(mode)]
164180

165181

166-
def search_filename(fname: str, lineno: int, local_first: bool) -> str:
182+
def search_filename(fname: str, lineno: int, local_first: bool, arch: str = "") -> str:
167183
"""Search a filename into the list of the include path.
168184
If local_first is true, it will try first in the current directory of
169185
the file being analyzed.
170186
"""
171187
fname = utils.sanitize_filename(fname)
172188

173189
assert CURRENT_DIR is not None
174-
i_path: list[str] = [CURRENT_DIR] + INCLUDEPATH if local_first else list(INCLUDEPATH)
190+
include_path = INCLUDE_MAP.get(arch, INCLUDEPATH)
191+
192+
i_path: list[str] = [CURRENT_DIR] + include_path if local_first else list(include_path)
175193
i_path.extend(config.OPTIONS.include_path.split(":") if config.OPTIONS.include_path else [])
176194

177195
if os.path.isabs(fname):
@@ -187,7 +205,7 @@ def search_filename(fname: str, lineno: int, local_first: bool) -> str:
187205
return ""
188206

189207

190-
def include_file(filename: str, lineno: int, local_first: bool) -> str:
208+
def include_file(filename: str, lineno: int, local_first: bool, arch: str = "") -> str:
191209
"""Performs a file inclusion (#include) in the preprocessor.
192210
Writes down that "filename" was included at the current file,
193211
at line <lineno>.
@@ -216,7 +234,7 @@ def include_file(filename: str, lineno: int, local_first: bool) -> str:
216234
return LEXER.include(filename)
217235

218236

219-
def include_once(filename: str, lineno: int, local_first: bool) -> str:
237+
def include_once(filename: str, lineno: int, local_first: bool, arch: str = "") -> str:
220238
"""Performs a file inclusion (#include) in the preprocessor.
221239
Writes down that "filename" was included at the current file,
222240
at line <lineno>.
@@ -231,15 +249,12 @@ def include_once(filename: str, lineno: int, local_first: bool) -> str:
231249
abs_filename = search_filename(filename, lineno, local_first)
232250

233251
if abs_filename not in INCLUDED: # If not already included
234-
return include_file(filename, lineno, local_first) # include it and return
252+
return include_file(filename, lineno, local_first, arch) # include it and return
235253

236254
# Now checks if the file has been included more than once
237255
if len(INCLUDED[abs_filename].parents) > 1:
238256
parent_file, lineno = INCLUDED[abs_filename].parents[0]
239-
warning(
240-
lineno,
241-
"file '%s' already included more than once, in file '%s' at line %i" % (filename, parent_file, lineno),
242-
)
257+
warning(lineno, f"file '{filename}' already included more than once, in file '{parent_file}' at line {lineno}")
243258

244259
# Empty file (already included)
245260
LEXER.next_token = "_ENDFILE_"
@@ -875,6 +890,7 @@ def main(argv):
875890
output.CURRENT_FILE.append(argv[0])
876891
else:
877892
output.CURRENT_FILE.append(global_.FILENAME)
893+
878894
CURRENT_DIR = os.path.dirname(output.CURRENT_FILE[-1])
879895

880896
if config.OPTIONS.sinclair:

0 commit comments

Comments
 (0)