code style fixed and type hints in get_binaries.py fixed

This commit is contained in:
Nicolas Kruse 2025-11-03 22:49:31 +01:00 committed by Nicolas Kruse
parent ac98c42761
commit f97d21e42c
14 changed files with 43 additions and 43 deletions

View File

@ -42,7 +42,7 @@ case RUN_PROG:
rel_entr_point = *(uint32_t*)bytes; bytes += 4;
printf("RUN_PROG rel_entr_point=%i\n", rel_entr_point);
entr_point = (int (*)())(executable_memory + rel_entr_point);
mark_mem_executable(executable_memory, executable_memory_len);
int ret = entr_point();
printf("Return value: %i\n", ret);

View File

@ -32,7 +32,7 @@ def transl_type(t: str) -> str:
class Node:
"""A Node represents an computational operation like ADD or other operations
"""A Node represents an computational operation like ADD or other operations
like read and write from or to the memory or IOs. In the computation graph
Nodes are connected via Nets.

View File

@ -159,7 +159,7 @@ def get_nets(*inputs: Iterable[Iterable[Any]]) -> list[Net]:
def get_data_layout(variable_list: Iterable[Net], sdb: stencil_database, offset: int = 0) -> tuple[list[tuple[Net, int, int]], int]:
"""Get memory layout for the provided variables
Arguments:
variable_list: Variables to layout
sdb: Stencil database for size lookup
@ -184,7 +184,7 @@ def get_data_layout(variable_list: Iterable[Net], sdb: stencil_database, offset:
def get_section_layout(section_indexes: Iterable[int], sdb: stencil_database, offset: int = 0) -> tuple[list[tuple[int, int, int]], int]:
"""Get memory layout for the provided sections
Arguments:
section_indexes: Sections (by index) to layout
sdb: Stencil database for size lookup
@ -210,7 +210,7 @@ def get_aux_function_mem_layout(function_names: Iterable[str], sdb: stencil_data
function_names: Function names to layout
sdb: Stencil database for size lookup
offset: Starting offset for layout
Returns:
Tuple of list of (function_name, start_offset, length) and total length
"""
@ -320,7 +320,7 @@ def compile_to_dag(node_list: Iterable[Node], sdb: stencil_database) -> tuple[bi
#print(patch.type, patch.addr, binw.Command.PATCH_FUNC, node.name, '->', patch.target_symbol_name)
else:
raise ValueError(f"Unsupported: {node.name} {reloc.target_symbol_info} {reloc.target_symbol_name}")
patch_list.append(patch)
offset += len(data)
@ -358,7 +358,7 @@ def compile_to_dag(node_list: Iterable[Node], sdb: stencil_database) -> tuple[bi
else:
raise ValueError(f"Unsupported: {name=} {reloc.target_symbol_info=} {reloc.target_symbol_name=} {reloc.target_section_index}")
patch_list.append(patch)
#assert False, aux_function_mem_layout

View File

@ -12,10 +12,10 @@ def sqrt(x: float | int) -> float: ...
def sqrt(x: variable[Any]) -> variable[float]: ...
def sqrt(x: NumLike) -> variable[float] | float:
"""Square root function
Arguments:
x: Input value
Returns:
Square root of x
"""
@ -30,10 +30,10 @@ def sin(x: float | int) -> float: ...
def sin(x: variable[Any]) -> variable[float]: ...
def sin(x: NumLike) -> variable[float] | float:
"""Sine function
Arguments:
x: Input value
Returns:
Square root of x
"""
@ -47,10 +47,10 @@ def cos(x: float | int) -> float: ...
def cos(x: variable[Any]) -> variable[float]: ...
def cos(x: NumLike) -> variable[float] | float:
"""Cosine function
Arguments:
x: Input value
Returns:
Cosine of x
"""
@ -64,10 +64,10 @@ def tan(x: float | int) -> float: ...
def tan(x: variable[Any]) -> variable[float]: ...
def tan(x: NumLike) -> variable[float] | float:
"""Tangent function
Arguments:
x: Input value
Returns:
Tangent of x
"""
@ -83,10 +83,10 @@ def get_42() -> variable[float]:
def abs(x: T) -> T:
"""Absolute value function
Arguments:
x: Input value
Returns:
Absolute value of x
"""

View File

@ -143,7 +143,7 @@ class stencil_database():
end_index = symbol.fields['st_size']
for reloc in symbol.relocations:
# address to fist byte to patch relative to the start of the symbol
patch_offset = reloc.fields['r_offset'] - symbol.fields['st_value'] - start_index
@ -175,7 +175,7 @@ class stencil_database():
patch_offset = pr.fields['r_offset'] - relocation.function_offset - relocation.start + function_offset
#print(f"xx {pr.fields['r_offset'] - relocation.function_offset} {relocation.target_symbol_name=} {pr.fields['r_offset']=} {relocation.function_offset=} {relocation.start=} {function_offset=}")
scale = 1
if pr.type.endswith('_PLT32') or pr.type.endswith('_PC32'):
# S + A - P
mask = 0xFFFFFFFF # 32 bit
@ -233,7 +233,7 @@ class stencil_database():
"""Return recursively all functions called by stencils or by other functions
Args:
names: function or stencil names
Returns:
set of all sub function names
"""
@ -266,7 +266,7 @@ class stencil_database():
def get_function_code(self, name: str, part: Literal['full', 'start', 'end'] = 'full') -> bytes:
"""Returns machine code for a specified function name.
Args:
name: function name
part: part of the function to return ('full', 'start', 'end')

View File

@ -20,7 +20,7 @@ class Target():
"""
def __init__(self, arch: str = 'native', optimization: str = 'O3') -> None:
"""Initialize Target object
Arguments:
arch: Target architecture
optimization: Optimization level
@ -30,7 +30,7 @@ class Target():
def compile(self, *variables: int | float | variable[int] | variable[float] | variable[bool] | Iterable[int | float | variable[int] | variable[float] | variable[bool]]) -> None:
"""Compiles the code to compute the given variables.
Arguments:
variables: Variables to compute
"""
@ -76,7 +76,7 @@ class Target():
Arguments:
net: Variable to read
Returns:
Value of the variable
"""

View File

@ -23,7 +23,7 @@ uint8_t *executable_memory = NULL;
uint32_t executable_memory_len = 0;
entry_point_t entr_point = NULL;
int data_offs = 0;
void patch(uint8_t *patch_addr, uint32_t patch_mask, int32_t value) {
uint32_t *val_ptr = (uint32_t*)patch_addr;
uint32_t original = *val_ptr;
@ -84,7 +84,7 @@ int parse_commands(uint8_t *bytes) {
uint32_t size;
int end_flag = 0;
uint32_t rel_entr_point = 0;
while(!end_flag) {
command = *(uint32_t*)bytes;
bytes += 4;
@ -96,14 +96,14 @@ int parse_commands(uint8_t *bytes) {
LOG("ALLOCATE_DATA size=%i mem_addr=%p\n", size, (void*)data_memory);
if (!update_data_offs()) end_flag = -4;
break;
case COPY_DATA:
offs = *(uint32_t*)bytes; bytes += 4;
size = *(uint32_t*)bytes; bytes += 4;
LOG("COPY_DATA offs=%i size=%i\n", offs, size);
memcpy(data_memory + offs, bytes, size); bytes += size;
break;
case ALLOCATE_CODE:
size = *(uint32_t*)bytes; bytes += 4;
executable_memory = allocate_executable_memory(size);
@ -112,14 +112,14 @@ int parse_commands(uint8_t *bytes) {
//LOG("# d %i c %i off %i\n", data_memory, executable_memory, data_offs);
if (!update_data_offs()) end_flag = -4;
break;
case COPY_CODE:
offs = *(uint32_t*)bytes; bytes += 4;
size = *(uint32_t*)bytes; bytes += 4;
LOG("COPY_CODE offs=%i size=%i\n", offs, size);
memcpy(executable_memory + offs, bytes, size); bytes += size;
break;
case PATCH_FUNC:
offs = *(uint32_t*)bytes; bytes += 4;
patch_mask = *(uint32_t*)bytes; bytes += 4;
@ -129,7 +129,7 @@ int parse_commands(uint8_t *bytes) {
offs, patch_mask, patch_scale, value);
patch(executable_memory + offs, patch_mask, value / patch_scale);
break;
case PATCH_OBJECT:
offs = *(uint32_t*)bytes; bytes += 4;
patch_mask = *(uint32_t*)bytes; bytes += 4;
@ -166,13 +166,13 @@ int parse_commands(uint8_t *bytes) {
entr_point = (entry_point_t)(executable_memory + rel_entr_point);
mark_mem_executable(executable_memory, executable_memory_len);
break;
case RUN_PROG:
LOG("RUN_PROG\n");
int ret = entr_point();
BLOG("Return value: %i\n", ret);
break;
case READ_DATA:
offs = *(uint32_t*)bytes; bytes += 4;
size = *(uint32_t*)bytes; bytes += 4;
@ -197,7 +197,7 @@ int parse_commands(uint8_t *bytes) {
LOG("END_COM\n");
end_flag = 1;
break;
default:
LOG("Unknown command\n");
end_flag = -1;

View File

@ -216,7 +216,7 @@ if __name__ == "__main__":
# Scalar arithmetic:
types = ['int', 'float']
ops = ['add', 'sub', 'mul', 'div', 'floordiv', 'gt', 'ge', 'eq', 'ne', 'pow']
int_ops = ['bwand', 'bwor', 'bwxor', 'lshift', 'rshift']
int_ops = ['bwand', 'bwor', 'bwxor', 'lshift', 'rshift']
for t1 in types:
code += get_result_stubs1(t1)

View File

@ -1,4 +1,4 @@
from copapy import variable, NumLike
from copapy import NumLike
from copapy.backend import Write, compile_to_dag, add_read_command
import copapy as cp
import subprocess

View File

@ -1,4 +1,4 @@
from copapy import variable, NumLike
from copapy import NumLike
from copapy.backend import Write, compile_to_dag, add_read_command
import subprocess
from copapy import _binwrite
@ -26,7 +26,7 @@ def check_for_qemu() -> bool:
command = qemu_command + ['--version']
try:
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf8', check=False)
except:
except Exception:
return False
return result.returncode == 0

View File

@ -43,7 +43,7 @@ def test_fine():
cp.sin(c_f),
cp.cos(c_f),
cp.tan(c_f)) # , c_i & 3)
ret_refe = (a_f ** 2,
a_i ** -1,
cp.sqrt(a_i),

View File

@ -27,4 +27,4 @@ def test_readme_example():
assert tg.read_value(e) == pytest.approx(cp.sqrt(0.87), 0.001) # pyright: ignore[reportUnknownMemberType]
if __name__ == "__main__":
test_readme_example()
test_readme_example()

View File

@ -1,3 +1,4 @@
from typing import Any
import os
import json
from urllib.request import urlopen, Request
@ -5,13 +6,13 @@ from urllib.request import urlopen, Request
OWNER = "Nonannet"
REPO = "copapy"
def fetch_json(url: str):
def fetch_json(url: str) -> Any:
req = Request(url, headers={"User-Agent": "Python"})
with urlopen(req, timeout=10) as resp:
assert resp.status == 200
return json.load(resp)
def download_file(url: str, dest_path: str):
def download_file(url: str, dest_path: str) -> None:
req = Request(url, headers={"User-Agent": "Python"})
with urlopen(req, timeout=30) as resp, open(dest_path, "wb") as f:
f.write(resp.read())

View File

@ -1,6 +1,5 @@
from copapy import variable
from copapy.backend import Write, compile_to_dag, stencil_db_from_package
import copapy as cp
from copapy._binwrite import Command