From 1501cc71a892094ab9a3f5ff1f420a4ded6e9d5a Mon Sep 17 00:00:00 2001 From: Nicolas Kruse Date: Fri, 10 Oct 2025 23:22:16 +0200 Subject: [PATCH] marker removed --- src/copapy/__init__.py | 6 +++--- src/copapy/stencil_db.py | 38 +++++++++++++++++++------------------- tests/test_ops.py | 4 ++-- tools/generate_stencils.py | 15 +++------------ 4 files changed, 27 insertions(+), 36 deletions(-) diff --git a/src/copapy/__init__.py b/src/copapy/__init__.py index e651ac2..97d3ad7 100644 --- a/src/copapy/__init__.py +++ b/src/copapy/__init__.py @@ -387,13 +387,13 @@ def compile_to_instruction_list(node_list: Iterable[Node], sdb: stencil_database # print('object_addr_lookp: ', object_addr_lookp) - data = sdb.get_func_data('function_start') + data = sdb.get_function_body('function_start', 'start') data_list.append(data) offset += len(data) for associated_net, node in extended_output_ops: assert node.name in sdb.function_definitions, f"- Warning: {node.name} prototype not found" - data = sdb.get_func_data(node.name) + data = sdb.get_stencil_code(node.name) data_list.append(data) # print(f"* {node.name} ({offset}) " + ' '.join(f'{d:02X}' for d in data)) @@ -406,7 +406,7 @@ def compile_to_instruction_list(node_list: Iterable[Node], sdb: stencil_database offset += len(data) - data = sdb.get_func_data('function_end') + data = sdb.get_function_body('function_end', 'end') data_list.append(data) offset += len(data) # print('function_end', offset, data) diff --git a/src/copapy/stencil_db.py b/src/copapy/stencil_db.py index c060354..bbc1ac4 100644 --- a/src/copapy/stencil_db.py +++ b/src/copapy/stencil_db.py @@ -51,32 +51,24 @@ def get_return_function_type(symbol: elf_symbol) -> str: def strip_function(func: elf_symbol) -> bytes: - """Return striped function code based on NOP markers""" + """Return stencil code by striped stancil function""" start_index, end_index = get_stencil_position(func) return func.data[start_index:end_index] def get_stencil_position(func: elf_symbol) -> tuple[int, int]: - - #assert func.name != 'function_start', func.relocations - - # Find first start marker - marker_index = func.data.find(START_MARKER.to_bytes(MARKER_LENGTH, func.file.byteorder)) - start_index = 0 if marker_index < 0 else marker_index + MARKER_LENGTH - - # Find last end marker - end_index = func.data.rfind(END_MARKER.to_bytes(MARKER_LENGTH, func.file.byteorder)) - end_index = len(func.data) if end_index < 0 else end_index - LENGTH_CALL_INSTRUCTION - - reloc = func.relocations[-1] - end_index2 = reloc.fields['r_offset'] - func.fields['st_value'] - reloc.fields['r_addend'] - LENGTH_CALL_INSTRUCTION - - print(func.relocations[-1]) - assert end_index2 == end_index, func.name - + start_index = 0 # For a "naked" function + end_index = get_last_call_in_function(func) return start_index, end_index +def get_last_call_in_function(func: elf_symbol) -> int: + # Find last relocation in function + reloc = func.relocations[-1] + assert reloc, f'No call function in stencil function {func.name}.' + return reloc.fields['r_offset'] - func.fields['st_value'] - reloc.fields['r_addend'] - LENGTH_CALL_INSTRUCTION + + def get_stencil_position2(func: elf_symbol) -> tuple[int, int]: # Find first start marker marker_index = func.data.find(START_MARKER.to_bytes(MARKER_LENGTH, func.file.byteorder)) @@ -152,7 +144,7 @@ class stencil_database(): if patch.addr < end_index - start_index: yield patch - def get_func_data(self, name: str) -> bytes: + def get_stencil_code(self, name: str) -> bytes: """Return the striped function code for a provided function name Args: @@ -162,3 +154,11 @@ class stencil_database(): Striped function code """ return strip_function(self.elf.symbols[name]) + + def get_function_body(self, name: str, part: Literal['start', 'end']) -> bytes: + func = self.elf.symbols[name] + index = get_last_call_in_function(func) + if part == 'start': + return func.data[:index] + else: + return func.data[index + LENGTH_CALL_INSTRUCTION:] \ No newline at end of file diff --git a/tests/test_ops.py b/tests/test_ops.py index 68af956..8cbc799 100644 --- a/tests/test_ops.py +++ b/tests/test_ops.py @@ -12,7 +12,7 @@ def test_compile(): c1 = CPVariable(9) - ret = function1(c1) + ret = function2(c1) tg = Target() print('* compile and copy ...') @@ -22,7 +22,7 @@ def test_compile(): tg.run() #print('* finished') - ret_ref = function1(9) + ret_ref = function2(9) for test, ref, name in zip(ret, ret_ref, ['r1', 'r2', 'r3', 'r4', 'r5']): val = tg.read_value(test) diff --git a/tools/generate_stencils.py b/tools/generate_stencils.py index 3dc039b..82a28a5 100644 --- a/tools/generate_stencils.py +++ b/tools/generate_stencils.py @@ -6,13 +6,12 @@ op_signs = {'add': '+', 'sub': '-', 'mul': '*', 'div': '/', 'gt': '>', 'eq': '==', 'mod': '%'} entry_func_prefix = '' -stencil_func_prefix = '__attribute__((naked)) ' # Remove collee prolog +stencil_func_prefix = '__attribute__((naked)) ' # Remove callee prolog def get_function_start() -> str: return f""" {entry_func_prefix}int function_start(){{ - result_int(0); // dummy call instruction: call instruction before marker gets striped - asm volatile (".long 0xE2401F0F"); + result_int(0); return 1; }} """ @@ -21,8 +20,7 @@ def get_function_start() -> str: def get_function_end() -> str: return f""" {entry_func_prefix}int function_end(){{ - result_int(0); // dummy call instruction: call instruction before marker gets striped - asm volatile (".long 0xE1401F0F"); + result_int(0); return 1; }} """ @@ -32,7 +30,6 @@ def get_op_code(op: str, type1: str, type2: str, type_out: str) -> str: return f""" {stencil_func_prefix}void {op}_{type1}_{type2}({type1} arg1, {type2} arg2) {{ result_{type_out}_{type2}(arg1 {op_signs[op]} arg2, arg2); - asm volatile (".long 0xE2401F0F"); }} """ @@ -41,7 +38,6 @@ def get_conv_code(type1: str, type2: str, type_out: str) -> str: return f""" {stencil_func_prefix}void conv_{type1}_{type2}({type1} arg1, {type2} arg2) {{ result_{type_out}_{type2}(({type_out})arg1, arg2); - asm volatile (".long 0xE2401F0F"); }} """ @@ -50,7 +46,6 @@ def get_op_code_float(op: str, type1: str, type2: str) -> str: return f""" {stencil_func_prefix}void {op}_{type1}_{type2}({type1} arg1, {type2} arg2) {{ result_float_{type2}((float)arg1 {op_signs[op]} (float)arg2, arg2); - asm volatile (".long 0xE2401F0F"); }} """ @@ -62,7 +57,6 @@ def get_floordiv(op: str, type1: str, type2: str) -> str: int i = (int)x; if (x < 0 && x != (float)i) i -= 1; result_int_{type2}(i, arg2); - asm volatile (".long 0xE2401F0F"); }} """ @@ -83,7 +77,6 @@ def get_read_reg0_code(type1: str, type2: str, type_out: str) -> str: return f""" {stencil_func_prefix}void read_{type_out}_reg0_{type1}_{type2}({type1} arg1, {type2} arg2) {{ result_{type_out}_{type2}(dummy_{type_out}, arg2); - asm volatile (".long 0xE2401F0F"); }} """ @@ -92,7 +85,6 @@ def get_read_reg1_code(type1: str, type2: str, type_out: str) -> str: return f""" {stencil_func_prefix}void read_{type_out}_reg1_{type1}_{type2}({type1} arg1, {type2} arg2) {{ result_{type1}_{type_out}(arg1, dummy_{type_out}); - asm volatile (".long 0xE2401F0F"); }} """ @@ -102,7 +94,6 @@ def get_write_code(type1: str) -> str: {stencil_func_prefix}void write_{type1}({type1} arg1) {{ dummy_{type1} = arg1; result_{type1}(arg1); - asm volatile (".long 0xE2401F0F"); }} """