0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-06-20 08:18:36 +03:00

Refactored floating point handlers into specialized classes for better organization and maintainability

This commit is contained in:
bird_egop
2025-04-17 23:57:16 +03:00
parent 5916d13995
commit ec56576116
22 changed files with 1509 additions and 3 deletions

View File

@ -0,0 +1,94 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Control;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FLDCW instruction (D9 /5)
/// </summary>
public class FldcwHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FldcwHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FldcwHandler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
/// <param name="opcode">The opcode to check</param>
/// <returns>True if this handler can decode the opcode</returns>
public override bool CanHandle(byte opcode)
{
// FLDCW is D9 /5
if (opcode != 0xD9) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the ModR/M byte has reg field = 5
byte modRm = Decoder.PeakByte();
byte reg = (byte)((modRm >> 3) & 0x7);
byte mod = (byte)((modRm >> 6) & 0x3);
// Only handle memory operands (mod != 3)
return reg == 5 && mod != 3;
}
/// <summary>
/// Decodes a FLDCW instruction
/// </summary>
/// <param name="opcode">The opcode of the instruction</param>
/// <param name="instruction">The instruction object to populate</param>
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, rawOperand) = ModRMDecoder.ReadModRM();
// Set the instruction type
instruction.Type = InstructionType.Fldcw;
// Create a 16-bit memory operand for control word operations
Operand memoryOperand;
if (rawOperand is DirectMemoryOperand directMemory)
{
memoryOperand = OperandFactory.CreateDirectMemoryOperand16(directMemory.Address);
}
else if (rawOperand is BaseRegisterMemoryOperand baseRegMemory)
{
memoryOperand = OperandFactory.CreateBaseRegisterMemoryOperand16(baseRegMemory.BaseRegister);
}
else if (rawOperand is DisplacementMemoryOperand dispMemory)
{
memoryOperand = OperandFactory.CreateDisplacementMemoryOperand16(dispMemory.BaseRegister, dispMemory.Displacement);
}
else if (rawOperand is ScaledIndexMemoryOperand scaledMemory)
{
memoryOperand = OperandFactory.CreateScaledIndexMemoryOperand16(scaledMemory.IndexRegister, scaledMemory.Scale, scaledMemory.BaseRegister, scaledMemory.Displacement);
}
else
{
memoryOperand = rawOperand;
}
// Set the structured operands
instruction.StructuredOperands =
[
memoryOperand
];
return true;
}
}

View File

@ -0,0 +1,70 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Control;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FLDENV instruction (D9 /4)
/// </summary>
public class FldenvHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FldenvHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FldenvHandler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
/// <param name="opcode">The opcode to check</param>
/// <returns>True if this handler can decode the opcode</returns>
public override bool CanHandle(byte opcode)
{
// FLDENV is D9 /4
if (opcode != 0xD9) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the ModR/M byte has reg field = 4
byte modRm = Decoder.PeakByte();
byte reg = (byte)((modRm >> 3) & 0x7);
byte mod = (byte)((modRm >> 6) & 0x3);
// Only handle memory operands (mod != 3)
return reg == 4 && mod != 3;
}
/// <summary>
/// Decodes a FLDENV instruction
/// </summary>
/// <param name="opcode">The opcode of the instruction</param>
/// <param name="instruction">The instruction object to populate</param>
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, rawOperand) = ModRMDecoder.ReadModRM();
// Set the instruction type
instruction.Type = InstructionType.Fldenv;
// Set the structured operands
instruction.StructuredOperands =
[
rawOperand
];
return true;
}
}

View File

@ -0,0 +1,94 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Control;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FNSTCW instruction (D9 /7)
/// </summary>
public class FnstcwHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FnstcwHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FnstcwHandler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
/// <param name="opcode">The opcode to check</param>
/// <returns>True if this handler can decode the opcode</returns>
public override bool CanHandle(byte opcode)
{
// FNSTCW is D9 /7
if (opcode != 0xD9) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the ModR/M byte has reg field = 7
byte modRm = Decoder.PeakByte();
byte reg = (byte)((modRm >> 3) & 0x7);
byte mod = (byte)((modRm >> 6) & 0x3);
// Only handle memory operands (mod != 3)
return reg == 7 && mod != 3;
}
/// <summary>
/// Decodes a FNSTCW instruction
/// </summary>
/// <param name="opcode">The opcode of the instruction</param>
/// <param name="instruction">The instruction object to populate</param>
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, rawOperand) = ModRMDecoder.ReadModRM();
// Set the instruction type
instruction.Type = InstructionType.Fnstcw;
// Create a 16-bit memory operand for control word operations
Operand memoryOperand;
if (rawOperand is DirectMemoryOperand directMemory)
{
memoryOperand = OperandFactory.CreateDirectMemoryOperand16(directMemory.Address);
}
else if (rawOperand is BaseRegisterMemoryOperand baseRegMemory)
{
memoryOperand = OperandFactory.CreateBaseRegisterMemoryOperand16(baseRegMemory.BaseRegister);
}
else if (rawOperand is DisplacementMemoryOperand dispMemory)
{
memoryOperand = OperandFactory.CreateDisplacementMemoryOperand16(dispMemory.BaseRegister, dispMemory.Displacement);
}
else if (rawOperand is ScaledIndexMemoryOperand scaledMemory)
{
memoryOperand = OperandFactory.CreateScaledIndexMemoryOperand16(scaledMemory.IndexRegister, scaledMemory.Scale, scaledMemory.BaseRegister, scaledMemory.Displacement);
}
else
{
memoryOperand = rawOperand;
}
// Set the structured operands
instruction.StructuredOperands =
[
memoryOperand
];
return true;
}
}

View File

@ -0,0 +1,70 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Control;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FNSTENV instruction (D9 /6)
/// </summary>
public class FnstenvHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FnstenvHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FnstenvHandler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
/// <param name="opcode">The opcode to check</param>
/// <returns>True if this handler can decode the opcode</returns>
public override bool CanHandle(byte opcode)
{
// FNSTENV is D9 /6
if (opcode != 0xD9) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the ModR/M byte has reg field = 6
byte modRm = Decoder.PeakByte();
byte reg = (byte)((modRm >> 3) & 0x7);
byte mod = (byte)((modRm >> 6) & 0x3);
// Only handle memory operands (mod != 3)
return reg == 6 && mod != 3;
}
/// <summary>
/// Decodes a FNSTENV instruction
/// </summary>
/// <param name="opcode">The opcode of the instruction</param>
/// <param name="instruction">The instruction object to populate</param>
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, rawOperand) = ModRMDecoder.ReadModRM();
// Set the instruction type
instruction.Type = InstructionType.Fnstenv;
// Set the structured operands
instruction.StructuredOperands =
[
rawOperand
];
return true;
}
}

View File

@ -0,0 +1,63 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Control;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FXAM instruction (D9 E5)
/// </summary>
public class FxamHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FxamHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FxamHandler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
/// <param name="opcode">The opcode to check</param>
/// <returns>True if this handler can decode the opcode</returns>
public override bool CanHandle(byte opcode)
{
// FXAM is D9 E5
if (opcode != 0xD9) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the next byte is E5
byte nextByte = Decoder.PeakByte();
return nextByte == 0xE5;
}
/// <summary>
/// Decodes a FXAM instruction
/// </summary>
/// <param name="opcode">The opcode of the instruction</param>
/// <param name="instruction">The instruction object to populate</param>
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the second byte of the opcode
byte secondByte = Decoder.ReadByte();
// Set the instruction type
instruction.Type = InstructionType.Fxam;
// FXAM has no operands
instruction.StructuredOperands = [];
return true;
}
}

View File

@ -0,0 +1,87 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Control;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FXCH instruction (D9 /1 with mod=3)
/// </summary>
public class FxchHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FxchHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FxchHandler(InstructionDecoder decoder)
: base(decoder)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
/// <param name="opcode">The opcode to check</param>
/// <returns>True if this handler can decode the opcode</returns>
public override bool CanHandle(byte opcode)
{
// FXCH is D9 /1 with mod=3
if (opcode != 0xD9) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the ModR/M byte has reg field = 1 and mod = 3
byte modRm = Decoder.PeakByte();
byte reg = (byte)((modRm >> 3) & 0x7);
byte mod = (byte)((modRm >> 6) & 0x3);
// Only handle register operands (mod = 3) with reg = 1
return reg == 1 && mod == 3;
}
/// <summary>
/// Decodes a FXCH instruction
/// </summary>
/// <param name="opcode">The opcode of the instruction</param>
/// <param name="instruction">The instruction object to populate</param>
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, _) = ModRMDecoder.ReadModRM();
// Set the instruction type
instruction.Type = InstructionType.Fxch;
// Map rm field to FPU register index
FpuRegisterIndex stIndex = rm switch
{
RegisterIndex.A => FpuRegisterIndex.ST0,
RegisterIndex.C => FpuRegisterIndex.ST1,
RegisterIndex.D => FpuRegisterIndex.ST2,
RegisterIndex.B => FpuRegisterIndex.ST3,
RegisterIndex.Sp => FpuRegisterIndex.ST4,
RegisterIndex.Bp => FpuRegisterIndex.ST5,
RegisterIndex.Si => FpuRegisterIndex.ST6,
RegisterIndex.Di => FpuRegisterIndex.ST7,
_ => FpuRegisterIndex.ST0 // Default case, should not happen
};
// Create the FPU register operand
var operand = OperandFactory.CreateFPURegisterOperand(stIndex);
// Set the structured operands
instruction.StructuredOperands =
[
operand
];
return true;
}
}