From c3bd1d401f696a6a6d53b3cd222bb05fd39943ea Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 2 Jul 2020 16:18:51 +0200 Subject: [PATCH] Improved codegen for branches in interpreter --- .../TuringMachineState.ExecutionContext.g.cs | 289 ++++++++++-------- .../TuringMachineState.ExecutionContext.tt | 131 +++++--- 2 files changed, 245 insertions(+), 175 deletions(-) diff --git a/src/Brainf_ckSharp/Memory/TuringMachineState.ExecutionContext.g.cs b/src/Brainf_ckSharp/Memory/TuringMachineState.ExecutionContext.g.cs index 92b797e5..98a7c29f 100644 --- a/src/Brainf_ckSharp/Memory/TuringMachineState.ExecutionContext.g.cs +++ b/src/Brainf_ckSharp/Memory/TuringMachineState.ExecutionContext.g.cs @@ -93,62 +93,68 @@ public ushort Current [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryMoveNext() { - if (_Position == MaxIndex) return false; + if (_Position != MaxIndex) + { + _Position++; - _Position++; + return true; + } - return true; + return false; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryMoveNext(int count, ref int totalOperations) { - if (_Position + count > MaxIndex) + if (_Position + count <= MaxIndex) { - totalOperations += MaxIndex - _Position; + totalOperations += count; - _Position = MaxIndex; + _Position += count; - return false; + return true; } - totalOperations += count; + totalOperations += MaxIndex - _Position; - _Position += count; + _Position = MaxIndex; - return true; + return false; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryMoveBack() { - if (_Position == 0) return false; + if (_Position != 0) + { + _Position--; - _Position--; + return true; + } - return true; + return false; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryMoveBack(int count, ref int totalOperations) { - if (_Position - count < 0) + if (_Position - count >= 0) { - totalOperations += _Position; + totalOperations += count; - _Position = 0; + _Position -= count; - return false; + return true; } - totalOperations += count; + totalOperations += _Position; - _Position -= count; + _Position = 0; - return true; + return false; } /// @@ -250,62 +256,68 @@ public ushort Current [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryMoveNext() { - if (_Position == MaxIndex) return false; + if (_Position != MaxIndex) + { + _Position++; - _Position++; + return true; + } - return true; + return false; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryMoveNext(int count, ref int totalOperations) { - if (_Position + count > MaxIndex) + if (_Position + count <= MaxIndex) { - totalOperations += MaxIndex - _Position; + totalOperations += count; - _Position = MaxIndex; + _Position += count; - return false; + return true; } - totalOperations += count; + totalOperations += MaxIndex - _Position; - _Position += count; + _Position = MaxIndex; - return true; + return false; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryMoveBack() { - if (_Position == 0) return false; + if (_Position != 0) + { + _Position--; - _Position--; + return true; + } - return true; + return false; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryMoveBack(int count, ref int totalOperations) { - if (_Position - count < 0) + if (_Position - count >= 0) { - totalOperations += _Position; + totalOperations += count; - _Position = 0; + _Position -= count; - return false; + return true; } - totalOperations += count; + totalOperations += _Position; - _Position -= count; + _Position = 0; - return true; + return false; } /// @@ -314,11 +326,14 @@ public bool TryIncrement() { ushort* current = Ptr + _Position; - if (*current == byte.MaxValue) return false; - - *current = unchecked((byte)(*current + 1)); + if (*current != byte.MaxValue) + { + *current = unchecked((byte)(*current + 1)); - return true; + return true; + } + + return false; } /// @@ -327,20 +342,20 @@ public bool TryIncrement(int count, ref int totalOperations) { ushort* current = Ptr + _Position; - if (*current + count > byte.MaxValue) + if (*current + count <= byte.MaxValue) { - totalOperations += byte.MaxValue - *current; + *current = unchecked((byte)(*current + count)); - *current = byte.MaxValue; + totalOperations += count; - return false; + return true; } - *current = unchecked((byte)(*current + count)); + totalOperations += byte.MaxValue - *current; - totalOperations += count; + *current = byte.MaxValue; - return true; + return false; } /// @@ -349,11 +364,14 @@ public bool TryDecrement() { ushort* current = Ptr + _Position; - if (*current == 0) return false; + if (*current != 0) + { + *current = (ushort)(*current - 1); - *current = (ushort)(*current - 1); + return true; + } - return true; + return false; } /// @@ -362,20 +380,20 @@ public bool TryDecrement(int count, ref int totalOperations) { ushort* current = Ptr + _Position; - if (*current < count) + if (*current >= count) { - totalOperations += *current; + *current = (ushort)(*current - count); - *current = 0; + totalOperations += count; - return false; + return true; } - *current = (ushort)(*current - count); + totalOperations += *current; - totalOperations += count; + *current = 0; - return true; + return false; } /// @@ -384,11 +402,14 @@ public bool TryInput(char c) { ushort* current = Ptr + _Position; - if (c > byte.MaxValue) return false; + if (c <= byte.MaxValue) + { + *current = c; - *current = c; + return true; + } - return true; + return false; } /// @@ -431,62 +452,68 @@ public ushort Current [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryMoveNext() { - if (_Position == MaxIndex) return false; + if (_Position != MaxIndex) + { + _Position++; - _Position++; + return true; + } - return true; + return false; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryMoveNext(int count, ref int totalOperations) { - if (_Position + count > MaxIndex) + if (_Position + count <= MaxIndex) { - totalOperations += MaxIndex - _Position; + totalOperations += count; - _Position = MaxIndex; + _Position += count; - return false; + return true; } - totalOperations += count; + totalOperations += MaxIndex - _Position; - _Position += count; + _Position = MaxIndex; - return true; + return false; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryMoveBack() { - if (_Position == 0) return false; + if (_Position != 0) + { + _Position--; - _Position--; + return true; + } - return true; + return false; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryMoveBack(int count, ref int totalOperations) { - if (_Position - count < 0) + if (_Position - count >= 0) { - totalOperations += _Position; + totalOperations += count; - _Position = 0; + _Position -= count; - return false; + return true; } - totalOperations += count; + totalOperations += _Position; - _Position -= count; + _Position = 0; - return true; + return false; } /// @@ -588,62 +615,68 @@ public ushort Current [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryMoveNext() { - if (_Position == MaxIndex) return false; + if (_Position != MaxIndex) + { + _Position++; - _Position++; + return true; + } - return true; + return false; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryMoveNext(int count, ref int totalOperations) { - if (_Position + count > MaxIndex) + if (_Position + count <= MaxIndex) { - totalOperations += MaxIndex - _Position; + totalOperations += count; - _Position = MaxIndex; + _Position += count; - return false; + return true; } - totalOperations += count; + totalOperations += MaxIndex - _Position; - _Position += count; + _Position = MaxIndex; - return true; + return false; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryMoveBack() { - if (_Position == 0) return false; + if (_Position != 0) + { + _Position--; - _Position--; + return true; + } - return true; + return false; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryMoveBack(int count, ref int totalOperations) { - if (_Position - count < 0) + if (_Position - count >= 0) { - totalOperations += _Position; + totalOperations += count; - _Position = 0; + _Position -= count; - return false; + return true; } - totalOperations += count; + totalOperations += _Position; - _Position -= count; + _Position = 0; - return true; + return false; } /// @@ -652,11 +685,14 @@ public bool TryIncrement() { ushort* current = Ptr + _Position; - if (*current == ushort.MaxValue) return false; - - *current = unchecked((ushort)(*current + 1)); + if (*current != ushort.MaxValue) + { + *current = unchecked((ushort)(*current + 1)); - return true; + return true; + } + + return false; } /// @@ -665,20 +701,20 @@ public bool TryIncrement(int count, ref int totalOperations) { ushort* current = Ptr + _Position; - if (*current + count > ushort.MaxValue) + if (*current + count <= ushort.MaxValue) { - totalOperations += ushort.MaxValue - *current; + *current = unchecked((ushort)(*current + count)); - *current = ushort.MaxValue; + totalOperations += count; - return false; + return true; } - *current = unchecked((ushort)(*current + count)); + totalOperations += ushort.MaxValue - *current; - totalOperations += count; + *current = ushort.MaxValue; - return true; + return false; } /// @@ -687,11 +723,14 @@ public bool TryDecrement() { ushort* current = Ptr + _Position; - if (*current == 0) return false; + if (*current != 0) + { + *current = (ushort)(*current - 1); - *current = (ushort)(*current - 1); + return true; + } - return true; + return false; } /// @@ -700,20 +739,20 @@ public bool TryDecrement(int count, ref int totalOperations) { ushort* current = Ptr + _Position; - if (*current < count) + if (*current >= count) { - totalOperations += *current; + *current = (ushort)(*current - count); - *current = 0; + totalOperations += count; - return false; + return true; } - *current = (ushort)(*current - count); + totalOperations += *current; - totalOperations += count; + *current = 0; - return true; + return false; } /// diff --git a/src/Brainf_ckSharp/Memory/TuringMachineState.ExecutionContext.tt b/src/Brainf_ckSharp/Memory/TuringMachineState.ExecutionContext.tt index 40abda04..3e5a281b 100644 --- a/src/Brainf_ckSharp/Memory/TuringMachineState.ExecutionContext.tt +++ b/src/Brainf_ckSharp/Memory/TuringMachineState.ExecutionContext.tt @@ -104,62 +104,68 @@ foreach (var mode in (OverflowMode[])Enum.GetValues(typeof(OverflowMode))) [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryMoveNext() { - if (_Position == MaxIndex) return false; + if (_Position != MaxIndex) + { + _Position++; - _Position++; + return true; + } - return true; + return false; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryMoveNext(int count, ref int totalOperations) { - if (_Position + count > MaxIndex) + if (_Position + count <= MaxIndex) { - totalOperations += MaxIndex - _Position; + totalOperations += count; - _Position = MaxIndex; + _Position += count; - return false; + return true; } - totalOperations += count; + totalOperations += MaxIndex - _Position; - _Position += count; + _Position = MaxIndex; - return true; + return false; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryMoveBack() { - if (_Position == 0) return false; + if (_Position != 0) + { + _Position--; - _Position--; + return true; + } - return true; + return false; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryMoveBack(int count, ref int totalOperations) { - if (_Position - count < 0) + if (_Position - count >= 0) { - totalOperations += _Position; + totalOperations += count; - _Position = 0; + _Position -= count; - return false; + return true; } - totalOperations += count; + totalOperations += _Position; - _Position -= count; + _Position = 0; - return true; + return false; } /// @@ -175,22 +181,27 @@ switch (mode) case OverflowMode.UshortWithNoOverflow: case OverflowMode.ByteWithNoOverflow: #> - if (*current == <#=cellType#>.MaxValue) return false; + if (*current != <#=cellType#>.MaxValue) + { + *current = unchecked((<#=cellType#>)(*current + 1)); - *current = unchecked((<#=cellType#>)(*current + 1)); + return true; + } + + return false; <# break; case OverflowMode.UshortWithOverflow: case OverflowMode.ByteWithOverflow: #> *current = unchecked((<#=cellType#>)(*current + 1)); + + return true; <# break; default: throw new ArgumentException($"Invalid overflow mode: {mode}", nameof(mode)); } #> - - return true; } /// @@ -206,31 +217,35 @@ switch (mode) case OverflowMode.UshortWithNoOverflow: case OverflowMode.ByteWithNoOverflow: #> - if (*current + count > <#=cellType#>.MaxValue) + if (*current + count <= <#=cellType#>.MaxValue) { - totalOperations += <#=cellType#>.MaxValue - *current; + *current = unchecked((<#=cellType#>)(*current + count)); - *current = <#=cellType#>.MaxValue; + totalOperations += count; - return false; + return true; } - *current = unchecked((<#=cellType#>)(*current + count)); + totalOperations += <#=cellType#>.MaxValue - *current; + + *current = <#=cellType#>.MaxValue; + + return false; <# break; case OverflowMode.UshortWithOverflow: case OverflowMode.ByteWithOverflow: #> *current = unchecked((<#=cellType#>)(*current + count)); + + totalOperations += count; + + return true; <# break; default: throw new ArgumentException($"Invalid overflow mode: {mode}", nameof(mode)); } #> - - totalOperations += count; - - return true; } /// @@ -246,22 +261,27 @@ switch (mode) case OverflowMode.UshortWithNoOverflow: case OverflowMode.ByteWithNoOverflow: #> - if (*current == 0) return false; + if (*current != 0) + { + *current = (ushort)(*current - 1); + + return true; + } - *current = (ushort)(*current - 1); + return false; <# break; case OverflowMode.UshortWithOverflow: case OverflowMode.ByteWithOverflow: #> *current = unchecked((<#=cellType#>)(*current - 1)); + + return true; <# break; default: throw new ArgumentException($"Invalid overflow mode: {mode}", nameof(mode)); } #> - - return true; } /// @@ -277,31 +297,35 @@ switch (mode) case OverflowMode.UshortWithNoOverflow: case OverflowMode.ByteWithNoOverflow: #> - if (*current < count) + if (*current >= count) { - totalOperations += *current; + *current = (ushort)(*current - count); - *current = 0; + totalOperations += count; - return false; + return true; } - *current = (ushort)(*current - count); + totalOperations += *current; + + *current = 0; + + return false; <# break; case OverflowMode.UshortWithOverflow: case OverflowMode.ByteWithOverflow: #> *current = unchecked((<#=cellType#>)(*current - count)); + + totalOperations += count; + + return true; <# break; default: throw new ArgumentException($"Invalid overflow mode: {mode}", nameof(mode)); } #> - - totalOperations += count; - - return true; } /// @@ -318,25 +342,32 @@ switch (mode) case OverflowMode.UshortWithOverflow: #> *current = c; + + return true; <# break; case OverflowMode.ByteWithNoOverflow: #> - if (c > byte.MaxValue) return false; + if (c <= byte.MaxValue) + { + *current = c; - *current = c; + return true; + } + + return false; <# break; case OverflowMode.ByteWithOverflow: #> *current = unchecked((byte)c); + + return true; <# break; default: throw new ArgumentException($"Invalid overflow mode: {mode}", nameof(mode)); } #> - - return true; } ///