Skip to content

Commit abf883a

Browse files
authored
Merge pull request #926 from boriel-basic/feat/use_memory_opt_when_enabled
Feat/use memory opt when enabled
2 parents 5befa88 + 48215d5 commit abf883a

6 files changed

Lines changed: 15 additions & 38 deletions

File tree

src/arch/z80/backend/common.py

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class DataType(StrEnum):
3434
i32 = "i32"
3535
f16 = "f16"
3636
f = "f"
37+
str = "str"
3738

3839

3940
# Handy constants, to not having to type long names :-)
@@ -46,7 +47,7 @@ class DataType(StrEnum):
4647
I32_t: Final[DataType] = DataType.i32
4748
F16_t: Final[DataType] = DataType.f16
4849
F_t: Final[DataType] = DataType.f
49-
50+
STR_t: Final[DataType] = DataType.str
5051

5152
# Internal data types definition, with its size in bytes, or -1 if it is variable (string)
5253
# Compound types are only arrays, and have the t
@@ -297,32 +298,6 @@ def get_bytes_size(elements: list[str]) -> int:
297298
return len(get_bytes(elements))
298299

299300

300-
def to_bool(stype: DataType) -> list[str]:
301-
"""Returns the instruction sequence for converting the number given number (in the stack)
302-
to boolean (just 0 (False) or non-zero (True))."""
303-
304-
if stype in (U8_t, I8_t):
305-
return []
306-
307-
if stype in (U16_t, I16_t):
308-
return [
309-
"ld a, h" "or l",
310-
]
311-
312-
if stype in (U32_t, I32_t, F16_t):
313-
return ["ld a, h" "or l" "or d", "or e,"]
314-
315-
if stype == F_t:
316-
return [
317-
"or b",
318-
"or c",
319-
"or d",
320-
"or e",
321-
]
322-
323-
raise NotImplementedError(f"type conversion from {stype} to bool is undefined")
324-
325-
326301
def normalize_boolean() -> list[str]:
327302
if OPTIONS.opt_strategy == OptimizationStrategy.Size:
328303
return [runtime_call(RuntimeLabel.NORMALIZE_BOOLEAN)]

src/arch/z80/backend/generic.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
get_bytes_size,
2525
new_ASMID,
2626
runtime_call,
27-
to_bool,
2827
to_byte,
2928
to_fixed,
3029
to_float,
@@ -365,8 +364,6 @@ def _cast(ins: Quad):
365364
output.extend(to_fixed(tA))
366365
elif tB == "f":
367366
output.extend(to_float(tA))
368-
elif tB == "bool":
369-
output.extend(to_bool(tA))
370367
else:
371368
raise exception.GenericError("Internal error: invalid typecast from %s to %s" % (tA, tB))
372369

src/arch/z80/visitor/translator_inst_visitor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from src.api.debug import __DEBUG__
33
from src.arch.interface.quad import Quad
44
from src.arch.z80.backend import Backend
5-
from src.arch.z80.backend.common import BOOL_t, F16_t, F_t, I8_t, I16_t, I32_t, U8_t, U16_t, U32_t
5+
from src.arch.z80.backend.common import BOOL_t, F16_t, F_t, I8_t, I16_t, I32_t, STR_t, U8_t, U16_t, U32_t
66
from src.ast import NodeVisitor
77
from src.symbols import sym as symbols
88

@@ -30,7 +30,7 @@ def TSUFFIX(type_: TYPE | symbols.TYPEREF | symbols.BASICTYPE) -> str:
3030
TYPE.ulong: U32_t,
3131
TYPE.fixed: F16_t,
3232
TYPE.float: F_t,
33-
TYPE.string: "str",
33+
TYPE.string: STR_t,
3434
TYPE.boolean: BOOL_t,
3535
}
3636

src/zxbpp/base_pplex.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import os
1111
import sys
12-
from collections.abc import Iterable
12+
from collections.abc import Callable, Iterable
1313
from dataclasses import dataclass
1414
from enum import Enum, unique
1515

@@ -88,6 +88,10 @@ def __init__(
8888
for macro_name, macro_func in self.builtin_macros.items():
8989
self.defines_table[macro_name] = BuiltinMacro(macro_name=macro_name, func=macro_func)
9090

91+
def set_macro(self, macro_name: str, func: Callable[[str], str]) -> None:
92+
assert self.defines_table is not None
93+
self.defines_table[macro_name] = func
94+
9195
def put_current_line(self, prefix: str = "", suffix: str = "") -> str:
9296
"""Returns line and file for include / end of include sequences."""
9397
assert self.lex is not None

src/zxbpp/prepro/builtinmacro.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from collections.abc import Callable
2+
13
from src.zxbpp import prepro
24

35
from .id_ import ID
@@ -7,12 +9,12 @@
79
class BuiltinMacro(ID):
810
"""A call to a builtin macro like __FILE__ or __LINE__
911
Every time the macro() is called, the macro returns
10-
it value.
12+
its value.
1113
"""
1214

13-
def __init__(self, macro_name: str, func):
15+
def __init__(self, macro_name: str, func: Callable[[MacroCall | None], str]):
1416
super().__init__(fname="", lineno=0, id_=macro_name)
1517
self.func = func
1618

17-
def __call__(self, symbolTable: "prepro.DefinesTable" = None, macro: MacroCall = None) -> str:
19+
def __call__(self, symbolTable: prepro.DefinesTable | None = None, macro: MacroCall | None = None) -> str:
1820
return self.func(macro)

src/zxbpp/zxbpp.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ def reset_id_table():
112112
ID_TABLE.define(name, value=val, lineno=0)
113113

114114
for macro_name, macro_func in LEXER.builtin_macros.items():
115-
# FIXME
116-
LEXER.defines_table[macro_name] = BuiltinMacro(macro_name=macro_name, func=macro_func) # type: ignore[index]
115+
LEXER.set_macro(macro_name, BuiltinMacro(macro_name=macro_name, func=macro_func))
117116

118117

119118
def init():

0 commit comments

Comments
 (0)