Skip to content

Commit

Permalink
Add OpCodeInfo.IsAvailableInMode()
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfsck committed Oct 5, 2019
1 parent 03d0b8a commit 1dccd15
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 31 deletions.
38 changes: 38 additions & 0 deletions Iced.UnitTests/Intel/BitnessUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright (C) 2018-2019 de4dot@gmail.com
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using System.Collections.Generic;

namespace Iced.UnitTests.Intel {
static class BitnessUtils {
public static IEnumerable<int> GetInvalidBitnessValues() {
yield return int.MinValue;
yield return int.MaxValue;
for (int bitness = -1; bitness <= 128; bitness++) {
if (bitness == 16 || bitness == 32 || bitness == 64)
continue;
yield return bitness;
}
}
}
}
11 changes: 1 addition & 10 deletions Iced.UnitTests/Intel/DecoderTests/MiscTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3424,16 +3424,7 @@ void Test_ByteArrayCodeReader_ctor_throws() {

[Fact]
void Test_Decoder_Create_throws() {
static IEnumerable<int> GetDecoderBitness() {
yield return int.MinValue;
yield return int.MaxValue;
for (int bitness = -1; bitness <= 128; bitness++) {
if (bitness == 16 || bitness == 32 || bitness == 64)
continue;
yield return bitness;
}
}
foreach (var bitness in GetDecoderBitness())
foreach (var bitness in BitnessUtils.GetInvalidBitnessValues())
Assert.Throws<ArgumentOutOfRangeException>(() => Decoder.Create(bitness, new ByteArrayCodeReader("90"), DecoderOptions.None));

foreach (var bitness in new[] { 16, 32, 64 })
Expand Down
13 changes: 2 additions & 11 deletions Iced.UnitTests/Intel/EncoderTests/BlockEncoderTest_Misc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,9 @@ void TryEncode_with_null_array_throws() =>

[Fact]
void TryEncode_with_invalid_bitness_throws() {
static IEnumerable<int> GetEncoderBitness() {
yield return int.MinValue;
yield return int.MaxValue;
for (int bitness = -1; bitness <= 128; bitness++) {
if (bitness == 16 || bitness == 32 || bitness == 64)
continue;
yield return bitness;
}
}
foreach (var bitness in GetEncoderBitness())
foreach (var bitness in BitnessUtils.GetInvalidBitnessValues())
Assert.Throws<ArgumentOutOfRangeException>(() => BlockEncoder.TryEncode(bitness, new InstructionBlock(new CodeWriterImpl(), new Instruction[1], 0), out _));
foreach (var bitness in GetEncoderBitness())
foreach (var bitness in BitnessUtils.GetInvalidBitnessValues())
Assert.Throws<ArgumentOutOfRangeException>(() => BlockEncoder.TryEncode(bitness, new[] { new InstructionBlock(new CodeWriterImpl(), new Instruction[1], 0) }, out _));
}

Expand Down
17 changes: 7 additions & 10 deletions Iced.UnitTests/Intel/EncoderTests/MiscTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,16 +382,7 @@ void Test_EVEX_WIG_LIG(string hexBytes, string expectedBytes, Code code, uint wi

[Fact]
void Test_Encoder_Create_throws() {
static IEnumerable<int> GetEncoderBitness() {
yield return int.MinValue;
yield return int.MaxValue;
for (int bitness = -1; bitness <= 128; bitness++) {
if (bitness == 16 || bitness == 32 || bitness == 64)
continue;
yield return bitness;
}
}
foreach (var bitness in GetEncoderBitness())
foreach (var bitness in BitnessUtils.GetInvalidBitnessValues())
Assert.Throws<ArgumentOutOfRangeException>(() => Encoder.Create(bitness, new CodeWriterImpl()));

foreach (var bitness in new[] { 16, 32, 64 })
Expand Down Expand Up @@ -519,6 +510,12 @@ void Verify_MemoryOperand_ctors() {
Assert.Equal(Register.None, op.SegmentPrefix);
}
}

[Fact]
void OpCodeInfo_IsAvailableInMode_throws_if_invalid_bitness() {
foreach (var bitness in BitnessUtils.GetInvalidBitnessValues())
Assert.Throws<ArgumentOutOfRangeException>(() => Code.Nopd.ToOpCode().IsAvailableInMode(bitness));
}
}
}
#endif
3 changes: 3 additions & 0 deletions Iced.UnitTests/Intel/EncoderTests/OpCodeInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ void Test_all_OpCodeInfos(int lineNo, Code code, string opCodeString, string ins
Assert.Equal(tc.Encoding, info.Encoding);
Assert.Equal(tc.IsInstruction, info.IsInstruction);
Assert.Equal(tc.Mode16, info.Mode16);
Assert.Equal(tc.Mode16, info.IsAvailableInMode(16));
Assert.Equal(tc.Mode32, info.Mode32);
Assert.Equal(tc.Mode32, info.IsAvailableInMode(32));
Assert.Equal(tc.Mode64, info.Mode64);
Assert.Equal(tc.Mode64, info.IsAvailableInMode(64));
Assert.Equal(tc.Fwait, info.Fwait);
Assert.Equal(tc.OperandSize, info.OperandSize);
Assert.Equal(tc.AddressSize, info.AddressSize);
Expand Down
13 changes: 13 additions & 0 deletions Iced/Intel/OpCodeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,19 @@ public OpCodeOperandKind GetOpKind(int operand) {
}
}

/// <summary>
/// Checks if the instruction is available in 16-bit mode, 32-bit mode or 64-bit mode
/// </summary>
/// <param name="bitness">16, 32 or 64</param>
/// <returns></returns>
public bool IsAvailableInMode(int bitness) =>
bitness switch {
16 => Mode16,
32 => Mode32,
64 => Mode64,
_ => throw new ArgumentOutOfRangeException(nameof(bitness)),
};

/// <summary>
/// Gets the opcode string, eg. "VEX.128.66.0F38.W0 78 /r", see also <see cref="ToInstructionString()"/>
/// </summary>
Expand Down

0 comments on commit 1dccd15

Please # to comment.