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

Refactored floating point instruction handlers for better organization and maintainability. Split generic handlers into specialized classes for DD and DF opcodes.

This commit is contained in:
bird_egop
2025-04-18 00:22:02 +03:00
parent ec56576116
commit d216c29315
62 changed files with 4985 additions and 376 deletions

View File

@ -0,0 +1,63 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Control;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FCLEX instruction (DB E2)
/// </summary>
public class FclexHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FclexHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FclexHandler(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)
{
// FCLEX is DB E2
if (opcode != 0xDB) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the next byte is E2
byte nextByte = Decoder.PeakByte();
return nextByte == 0xE2;
}
/// <summary>
/// Decodes a FCLEX 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.Fclex;
// FCLEX 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 FFREE ST(i) instruction (DD C0-C7)
/// </summary>
public class FfreeHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FfreeHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FfreeHandler(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)
{
// FFREE ST(i) is DD C0-C7
if (opcode != 0xDD) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the ModR/M byte has reg field = 0 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 = 0
return reg == 0 && mod == 3;
}
/// <summary>
/// Decodes a FFREE ST(i) 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.Ffree;
// 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 fpuRegisterOperand = OperandFactory.CreateFPURegisterOperand(stIndex);
// Set the structured operands
instruction.StructuredOperands =
[
fpuRegisterOperand
];
return true;
}
}

View File

@ -0,0 +1,87 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Control;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FFREEP ST(i) instruction (DF C0-C7)
/// </summary>
public class FfreepHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FfreepHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FfreepHandler(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)
{
// FFREEP ST(i) is DF C0-C7
if (opcode != 0xDF) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the ModR/M byte has reg field = 0 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 = 0
return reg == 0 && mod == 3;
}
/// <summary>
/// Decodes a FFREEP ST(i) 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.Ffreep;
// 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 fpuRegisterOperand = OperandFactory.CreateFPURegisterOperand(stIndex);
// Set the structured operands
instruction.StructuredOperands =
[
fpuRegisterOperand
];
return true;
}
}

View File

@ -0,0 +1,63 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Control;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FINIT instruction (DB E3)
/// </summary>
public class FinitHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FinitHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FinitHandler(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)
{
// FINIT is DB E3
if (opcode != 0xDB) return false;
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the next byte is E3
byte nextByte = Decoder.PeakByte();
return nextByte == 0xE3;
}
/// <summary>
/// Decodes a FINIT 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.Finit;
// FINIT has no operands
instruction.StructuredOperands = [];
return true;
}
}

View File

@ -0,0 +1,70 @@
namespace X86Disassembler.X86.Handlers.FloatingPoint.Control;
using X86Disassembler.X86.Operands;
/// <summary>
/// Handler for FNSAVE instruction (DD /6)
/// </summary>
public class FnsaveHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FnsaveHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FnsaveHandler(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)
{
// FNSAVE is DD /6
if (opcode != 0xDD) 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 FNSAVE 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.Fnsave;
// 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 FNSTSW memory instruction (DD /7)
/// </summary>
public class FnstswMemoryHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FnstswMemoryHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FnstswMemoryHandler(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)
{
// FNSTSW is DD /7
if (opcode != 0xDD) 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 FNSTSW memory 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.Fnstsw;
// Create a 16-bit memory operand for status word
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 FRSTOR instruction (DD /4)
/// </summary>
public class FrstorHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the FrstorHandler class
/// </summary>
/// <param name="decoder">The instruction decoder that owns this handler</param>
public FrstorHandler(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)
{
// FRSTOR is DD /4
if (opcode != 0xDD) 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 FRSTOR 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.Frstor;
// Set the structured operands
instruction.StructuredOperands =
[
rawOperand
];
return true;
}
}