mirror of https://github.com/Nonannet/copapy.git
misc
This commit is contained in:
parent
b9d5671662
commit
92f9e7cfcf
|
|
@ -4,17 +4,21 @@ from typing import Generator, Iterable, Any
|
|||
import pelfy
|
||||
from . import binwrite as binw
|
||||
|
||||
|
||||
def get_var_name(var: Any, scope: dict[str, Any] = globals()) -> list[str]:
|
||||
return [name for name, value in scope.items() if value is var]
|
||||
|
||||
|
||||
def _get_c_function_definitions(code: str) -> dict[str, str]:
|
||||
ret = re.findall(r".*?void\s+([a-z_1-9]*)\s*\([^\)]*?\)[^\}]*?\{[^\}]*?result_([a-z_]*)\(.*?", code, flags=re.S)
|
||||
return {r[0]: r[1] for r in ret}
|
||||
|
||||
|
||||
_ccode = pkgutil.get_data(__name__, 'ops.c')
|
||||
assert _ccode is not None
|
||||
_function_definitions = _get_c_function_definitions(_ccode.decode('utf-8'))
|
||||
|
||||
|
||||
class Node:
|
||||
def __init__(self):
|
||||
self.args: list[Net] = []
|
||||
|
|
@ -24,9 +28,11 @@ class Node:
|
|||
#return f"Node:{self.name}({', '.join(str(a) for a in self.args) if self.args else self.value})"
|
||||
return f"Node:{self.name}({', '.join(str(a) for a in self.args) if self.args else (self.value if isinstance(self, Const) else '')})"
|
||||
|
||||
|
||||
class Device():
|
||||
pass
|
||||
|
||||
|
||||
class Net:
|
||||
def __init__(self, dtype: str, source: Node):
|
||||
self.dtype = dtype
|
||||
|
|
@ -71,6 +77,7 @@ class Const(Node):
|
|||
|
||||
self.args = []
|
||||
|
||||
|
||||
class Write(Node):
|
||||
def __init__(self, net: Net):
|
||||
self.name = 'write_' + net.dtype
|
||||
|
|
@ -79,12 +86,14 @@ class Write(Node):
|
|||
#if self.name not in _function_definitions:
|
||||
# raise ValueError(f"Unsupported operand type for write: {net.dtype}")
|
||||
|
||||
|
||||
class Op(Node):
|
||||
def __init__(self, typed_op_name: str, args: list[Net]):
|
||||
assert not args or any(isinstance(t, Net) for t in args), 'args parameter must be of type list[Net]'
|
||||
self.name: str = typed_op_name
|
||||
self.args: list[Net] = args
|
||||
|
||||
|
||||
def _add_op(op: str, args: list[Any], commutative: bool = False) -> Net:
|
||||
arg_nets = [a if isinstance(a, Net) else const(a) for a in args]
|
||||
|
||||
|
|
@ -102,14 +111,17 @@ def _add_op(op: str, args: list[Any], commutative: bool = False) -> Net:
|
|||
|
||||
return result_net
|
||||
|
||||
|
||||
#def read_input(hw: Device, test_value: float):
|
||||
# return Net(type(value))
|
||||
|
||||
|
||||
def const(value: Any) -> Net:
|
||||
assert isinstance(value, (int, float, bool)), f'Unsupported type for const: {type(value).__name__}'
|
||||
new_const = Const(value)
|
||||
return Net(new_const.dtype, new_const)
|
||||
|
||||
|
||||
def _get_data_and_dtype(value: Any) -> tuple[str, float | int]:
|
||||
if isinstance(value, int):
|
||||
return ('int', int(value))
|
||||
|
|
@ -273,7 +285,6 @@ def compile_to_instruction_list(end_nodes: Iterable[Node] | Node) -> binw.data_w
|
|||
auxiliary_objects = [s for s in elf.symbols if s.info == 'STT_OBJECT']
|
||||
|
||||
|
||||
|
||||
# write data sections
|
||||
object_list, data_section_lengths = binw.get_variable_data(auxiliary_objects)
|
||||
|
||||
|
|
@ -287,6 +298,16 @@ def compile_to_instruction_list(end_nodes: Iterable[Node] | Node) -> binw.data_w
|
|||
dw.write_int(lengths)
|
||||
dw.write_bytes(sym.data)
|
||||
|
||||
# write auxiliary_functions
|
||||
# TODO
|
||||
|
||||
# write program
|
||||
print(list(prototype_functions.keys()))
|
||||
for net, node in extended_output_ops:
|
||||
if node.name in prototype_functions:
|
||||
print(prototype_functions[node.name])
|
||||
else: print(f"- Warning: {node.name} prototype not found")
|
||||
|
||||
print('-----')
|
||||
|
||||
return dw
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
from enum import Enum
|
||||
from pelfy import elf_symbol
|
||||
from typing import Literal
|
||||
|
||||
Command = Enum('Command', [('ALLOCATE_DATA', 1), ('COPY_DATA', 2),
|
||||
('ALLOCATE_CODE', 3), ('COPY_CODE', 4),
|
||||
|
|
@ -49,7 +50,7 @@ def get_function_data(symbols: list[elf_symbol]) -> tuple[list[tuple[elf_symbol,
|
|||
|
||||
|
||||
def get_function_data_blob(symbols: list[elf_symbol]) -> tuple[list[tuple[elf_symbol, int, int, int]], int]:
|
||||
code_list = []
|
||||
code_list: list[tuple[elf_symbol, int, int, int]] = []
|
||||
out_offs = 0
|
||||
for sym in symbols:
|
||||
assert sym.info == 'STT_FUNC'
|
||||
|
|
@ -68,8 +69,8 @@ def get_function_data_blob(symbols: list[elf_symbol]) -> tuple[list[tuple[elf_sy
|
|||
|
||||
|
||||
class data_writer():
|
||||
def __init__(self, byteorder: str):
|
||||
self._data: list[(str, bytes)] = list()
|
||||
def __init__(self, byteorder: Literal['little', 'big']):
|
||||
self._data: list[tuple[str, bytes, int]] = list()
|
||||
self.byteorder = byteorder
|
||||
|
||||
def write_int(self, value: int, num_bytes: int = 4, signed: bool = False):
|
||||
|
|
@ -84,7 +85,7 @@ class data_writer():
|
|||
def write_bytes(self, value: bytes):
|
||||
self._data.append((f"BYTES {len(value)}", value, 0))
|
||||
|
||||
def print(self) -> str:
|
||||
def print(self) -> None:
|
||||
for name, dat, flag in self._data:
|
||||
if flag:
|
||||
print('')
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ def test_compile():
|
|||
|
||||
il = rc.compile_to_instruction_list(out)
|
||||
|
||||
print('#', il.print())
|
||||
#print('#', il.print())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
|||
Loading…
Reference in New Issue