From 1219605c04b1555f87ca2fd0ec34b38f55cc7445 Mon Sep 17 00:00:00 2001 From: Tom Herman Date: Mon, 16 Dec 2024 18:56:35 +0200 Subject: [PATCH 1/5] print address on fjm_run memory error --- flipjump/fjm/fjm_reader.py | 7 ++++--- flipjump/interpretter/fjm_run.py | 14 ++++++++++---- flipjump/utils/exceptions.py | 4 +++- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/flipjump/fjm/fjm_reader.py b/flipjump/fjm/fjm_reader.py index b57229d..c19cc78 100644 --- a/flipjump/fjm/fjm_reader.py +++ b/flipjump/fjm/fjm_reader.py @@ -146,13 +146,14 @@ def _get_memory_word(self, word_address: int) -> int: return 0 garbage_val = _new_garbage_val() + memory_address = word_address << self.memory_width garbage_message = ( - f'Reading garbage word at mem[{hex(word_address << self.memory_width)[2:]}]' + f'Reading garbage word at mem[{hex(memory_address)[2:]}]' f' = {hex(garbage_val)[2:]}' ) if GarbageHandling.Stop == self.garbage_handling: - raise FlipJumpRuntimeMemoryException(garbage_message) + raise FlipJumpRuntimeMemoryException(garbage_message, memory_address) elif GarbageHandling.OnlyWarning == self.garbage_handling: print(f'\nWarning: {garbage_message}') elif GarbageHandling.SlowRead == self.garbage_handling: @@ -210,7 +211,7 @@ def get_word(self, bit_address: int) -> int: if bit_offset == 0: return self._get_memory_word(word_address) if word_address == ((1 << self.memory_width) - 1): - raise FlipJumpRuntimeMemoryException('Accessed outside of memory (beyond the last bit).') + raise FlipJumpRuntimeMemoryException('Accessed outside of memory (beyond the last bit).', bit_address) lsw = self._get_memory_word(word_address) msw = self._get_memory_word(word_address + 1) diff --git a/flipjump/interpretter/fjm_run.py b/flipjump/interpretter/fjm_run.py index 7ee6ede..36add62 100644 --- a/flipjump/interpretter/fjm_run.py +++ b/flipjump/interpretter/fjm_run.py @@ -20,7 +20,8 @@ class TerminationStatistics: also saves the program's output. """ - def __init__(self, run_statistics: RunStatistics, termination_cause: TerminationCause): + def __init__(self, run_statistics: RunStatistics, termination_cause: TerminationCause, + *, memory_error_address: Optional[int] = None) -> None: self.run_time = run_statistics.get_run_time() self.op_counter = run_statistics.op_counter @@ -29,6 +30,7 @@ def __init__(self, run_statistics: RunStatistics, termination_cause: Termination self.last_ops_addresses: Optional[Deque[int]] = run_statistics.last_ops_addresses self.termination_cause = termination_cause + self.memory_error_address = memory_error_address @staticmethod def beautify_address(address: int, breakpoint_handler: Optional[BreakpointHandler]) -> str: @@ -74,10 +76,13 @@ def print( if output_to_print is not None: output_str = f"Program's output before it was terminated: {output_to_print!r}\n\n" + termination_cause_str = str(self.termination_cause) + if self.termination_cause == TerminationCause.RuntimeMemoryError: + termination_cause_str += f" (address {self.memory_error_address:08x})" print( f'\n' f'{output_str}' - f'Finished by {str(self.termination_cause)} after {self.run_time:.3f}s ' + f'Finished by {termination_cause_str} after {self.run_time:.3f}s ' f'(' f'{self.op_counter:,} ops executed; ' f'{flips_percentage:.2f}% flips, ' @@ -192,8 +197,9 @@ def run( # JUMP! ip = jump_address - except FlipJumpRuntimeMemoryException: - return TerminationStatistics(statistics, TerminationCause.RuntimeMemoryError) + except FlipJumpRuntimeMemoryException as mem_e: + return TerminationStatistics(statistics, TerminationCause.RuntimeMemoryError, + memory_error_address=mem_e.memory_address) except FlipJumpException as fj_exception: raise fj_exception except KeyboardInterrupt: diff --git a/flipjump/utils/exceptions.py b/flipjump/utils/exceptions.py index f2b15d7..77452c1 100644 --- a/flipjump/utils/exceptions.py +++ b/flipjump/utils/exceptions.py @@ -31,7 +31,9 @@ class FlipJumpRuntimeException(FlipJumpException): class FlipJumpRuntimeMemoryException(FlipJumpRuntimeException): - pass + def __init__(self, message: str, memory_address: int): + super().__init__(message) + self.memory_address = memory_address class FlipJumpMissingImportException(FlipJumpException): From 34b7cf5601c0399ff72600d47adbcacbdedf80b7 Mon Sep 17 00:00:00 2001 From: Tom Herman Date: Mon, 16 Dec 2024 19:22:02 +0200 Subject: [PATCH 2/5] add comment for hex.add_constant --- flipjump/stl/hex/math.fj | 1 + 1 file changed, 1 insertion(+) diff --git a/flipjump/stl/hex/math.fj b/flipjump/stl/hex/math.fj index 9bea50b..dec13c0 100644 --- a/flipjump/stl/hex/math.fj +++ b/flipjump/stl/hex/math.fj @@ -40,6 +40,7 @@ ns hex { // Space Complexity: n_const(2.5@+39) + (dst_n - hex_shift)(1.5@+13) + 4@+29 // dst[:n] += const // @requires hex.add.init (or hex.init) + // const must be a positive constant. // n_const is the hex-length of const, without all of it's least-significant-hexes zeros. def add_constant n, dst, const @ end { rep(const!=0, _) .add.add_constant_with_leading_zeros n, dst, const, (#(const&(0-const)))-1 From 0699897153da1c747912c42e281df2d2c67819df Mon Sep 17 00:00:00 2001 From: Tom Herman Date: Mon, 16 Dec 2024 19:31:04 +0200 Subject: [PATCH 3/5] black --- flipjump/fjm/fjm_reader.py | 5 +---- flipjump/interpretter/fjm_run.py | 14 ++++++++++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/flipjump/fjm/fjm_reader.py b/flipjump/fjm/fjm_reader.py index c19cc78..4627795 100644 --- a/flipjump/fjm/fjm_reader.py +++ b/flipjump/fjm/fjm_reader.py @@ -147,10 +147,7 @@ def _get_memory_word(self, word_address: int) -> int: garbage_val = _new_garbage_val() memory_address = word_address << self.memory_width - garbage_message = ( - f'Reading garbage word at mem[{hex(memory_address)[2:]}]' - f' = {hex(garbage_val)[2:]}' - ) + garbage_message = f'Reading garbage word at mem[{hex(memory_address)[2:]}] = {hex(garbage_val)[2:]}' if GarbageHandling.Stop == self.garbage_handling: raise FlipJumpRuntimeMemoryException(garbage_message, memory_address) diff --git a/flipjump/interpretter/fjm_run.py b/flipjump/interpretter/fjm_run.py index 36add62..9667072 100644 --- a/flipjump/interpretter/fjm_run.py +++ b/flipjump/interpretter/fjm_run.py @@ -20,8 +20,13 @@ class TerminationStatistics: also saves the program's output. """ - def __init__(self, run_statistics: RunStatistics, termination_cause: TerminationCause, - *, memory_error_address: Optional[int] = None) -> None: + def __init__( + self, + run_statistics: RunStatistics, + termination_cause: TerminationCause, + *, + memory_error_address: Optional[int] = None, + ) -> None: self.run_time = run_statistics.get_run_time() self.op_counter = run_statistics.op_counter @@ -198,8 +203,9 @@ def run( ip = jump_address except FlipJumpRuntimeMemoryException as mem_e: - return TerminationStatistics(statistics, TerminationCause.RuntimeMemoryError, - memory_error_address=mem_e.memory_address) + return TerminationStatistics( + statistics, TerminationCause.RuntimeMemoryError, memory_error_address=mem_e.memory_address + ) except FlipJumpException as fj_exception: raise fj_exception except KeyboardInterrupt: From 9d0f136a98e0e9a530023ed2c88f0813bc18b8d5 Mon Sep 17 00:00:00 2001 From: Tom Herman Date: Mon, 16 Dec 2024 19:32:50 +0200 Subject: [PATCH 4/5] print address fix --- flipjump/interpretter/fjm_run.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flipjump/interpretter/fjm_run.py b/flipjump/interpretter/fjm_run.py index 9667072..7d7fe70 100644 --- a/flipjump/interpretter/fjm_run.py +++ b/flipjump/interpretter/fjm_run.py @@ -83,7 +83,7 @@ def print( termination_cause_str = str(self.termination_cause) if self.termination_cause == TerminationCause.RuntimeMemoryError: - termination_cause_str += f" (address {self.memory_error_address:08x})" + termination_cause_str += f" (address {hex(self.memory_error_address)})" print( f'\n' f'{output_str}' From 3ac52311cdbaa8671f0908b25769bb5719be9dda Mon Sep 17 00:00:00 2001 From: Tom Herman Date: Mon, 16 Dec 2024 19:43:27 +0200 Subject: [PATCH 5/5] mypy fix --- flipjump/interpretter/fjm_run.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flipjump/interpretter/fjm_run.py b/flipjump/interpretter/fjm_run.py index 7d7fe70..cd1a604 100644 --- a/flipjump/interpretter/fjm_run.py +++ b/flipjump/interpretter/fjm_run.py @@ -82,7 +82,7 @@ def print( output_str = f"Program's output before it was terminated: {output_to_print!r}\n\n" termination_cause_str = str(self.termination_cause) - if self.termination_cause == TerminationCause.RuntimeMemoryError: + if self.memory_error_address is not None: termination_cause_str += f" (address {hex(self.memory_error_address)})" print( f'\n'