Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 2 additions & 27 deletions src/arch/z80/backend/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class DataType(StrEnum):
i32 = "i32"
f16 = "f16"
f = "f"
str = "str"


# Handy constants, to not having to type long names :-)
Expand All @@ -46,7 +47,7 @@ class DataType(StrEnum):
I32_t: Final[DataType] = DataType.i32
F16_t: Final[DataType] = DataType.f16
F_t: Final[DataType] = DataType.f

STR_t: Final[DataType] = DataType.str

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


def to_bool(stype: DataType) -> list[str]:
"""Returns the instruction sequence for converting the number given number (in the stack)
to boolean (just 0 (False) or non-zero (True))."""

if stype in (U8_t, I8_t):
return []

if stype in (U16_t, I16_t):
return [
"ld a, h" "or l",
]

if stype in (U32_t, I32_t, F16_t):
return ["ld a, h" "or l" "or d", "or e,"]

if stype == F_t:
return [
"or b",
"or c",
"or d",
"or e",
]

raise NotImplementedError(f"type conversion from {stype} to bool is undefined")


def normalize_boolean() -> list[str]:
if OPTIONS.opt_strategy == OptimizationStrategy.Size:
return [runtime_call(RuntimeLabel.NORMALIZE_BOOLEAN)]
Expand Down
3 changes: 0 additions & 3 deletions src/arch/z80/backend/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
get_bytes_size,
new_ASMID,
runtime_call,
to_bool,
to_byte,
to_fixed,
to_float,
Expand Down Expand Up @@ -365,8 +364,6 @@ def _cast(ins: Quad):
output.extend(to_fixed(tA))
elif tB == "f":
output.extend(to_float(tA))
elif tB == "bool":
output.extend(to_bool(tA))
else:
raise exception.GenericError("Internal error: invalid typecast from %s to %s" % (tA, tB))

Expand Down
4 changes: 2 additions & 2 deletions src/arch/z80/visitor/translator_inst_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from src.api.debug import __DEBUG__
from src.arch.interface.quad import Quad
from src.arch.z80.backend import Backend
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
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
from src.ast import NodeVisitor
from src.symbols import sym as symbols

Expand Down Expand Up @@ -30,7 +30,7 @@ def TSUFFIX(type_: TYPE | symbols.TYPEREF | symbols.BASICTYPE) -> str:
TYPE.ulong: U32_t,
TYPE.fixed: F16_t,
TYPE.float: F_t,
TYPE.string: "str",
TYPE.string: STR_t,
TYPE.boolean: BOOL_t,
}

Expand Down
6 changes: 5 additions & 1 deletion src/zxbpp/base_pplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import os
import sys
from collections.abc import Iterable
from collections.abc import Callable, Iterable
from dataclasses import dataclass
from enum import Enum, unique

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

def set_macro(self, macro_name: str, func: Callable[[str], str]) -> None:
assert self.defines_table is not None
self.defines_table[macro_name] = func

def put_current_line(self, prefix: str = "", suffix: str = "") -> str:
"""Returns line and file for include / end of include sequences."""
assert self.lex is not None
Expand Down
8 changes: 5 additions & 3 deletions src/zxbpp/prepro/builtinmacro.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from collections.abc import Callable

from src.zxbpp import prepro

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

def __init__(self, macro_name: str, func):
def __init__(self, macro_name: str, func: Callable[[MacroCall | None], str]):
super().__init__(fname="", lineno=0, id_=macro_name)
self.func = func

def __call__(self, symbolTable: "prepro.DefinesTable" = None, macro: MacroCall = None) -> str:
def __call__(self, symbolTable: prepro.DefinesTable | None = None, macro: MacroCall | None = None) -> str:
return self.func(macro)
3 changes: 1 addition & 2 deletions src/zxbpp/zxbpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ def reset_id_table():
ID_TABLE.define(name, value=val, lineno=0)

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


def init():
Expand Down
Loading