0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-05-19 11:51:17 +03:00

Simplified LoadStoreFloat64Handler by replacing if-else logic with a dictionary-based approach

This commit is contained in:
bird_egop 2025-04-13 23:52:00 +03:00
parent b531db77d5
commit 4e837f5c63

View File

@ -5,18 +5,72 @@ namespace X86Disassembler.X86.Handlers.FloatingPoint;
/// </summary> /// </summary>
public class LoadStoreFloat64Handler : InstructionHandler public class LoadStoreFloat64Handler : InstructionHandler
{ {
// DD opcode - load/store float64 // Memory operand mnemonics for DD opcode - load/store float64
private static readonly string[] Mnemonics = private static readonly string[] MemoryMnemonics =
[ [
"fld", "fld", // 0
"??", "??", // 1
"fst", "fst", // 2
"fstp", "fstp", // 3
"frstor", "frstor", // 4
"??", "??", // 5
"fnsave", "fnsave", // 6
"fnstsw", "fnstsw" // 7
]; ];
// Register-register operations mapping (mod=3)
private static readonly Dictionary<(RegisterIndex Reg, RegisterIndex Rm), (string Mnemonic, string Operands)> RegisterOperations = new()
{
// FFREE ST(i)
{ (RegisterIndex.A, RegisterIndex.A), ("ffree", "st(0)") },
{ (RegisterIndex.A, RegisterIndex.C), ("ffree", "st(1)") },
{ (RegisterIndex.A, RegisterIndex.D), ("ffree", "st(2)") },
{ (RegisterIndex.A, RegisterIndex.B), ("ffree", "st(3)") },
{ (RegisterIndex.A, RegisterIndex.Sp), ("ffree", "st(4)") },
{ (RegisterIndex.A, RegisterIndex.Bp), ("ffree", "st(5)") },
{ (RegisterIndex.A, RegisterIndex.Si), ("ffree", "st(6)") },
{ (RegisterIndex.A, RegisterIndex.Di), ("ffree", "st(7)") },
// FST ST(i)
{ (RegisterIndex.C, RegisterIndex.A), ("fst", "st(0)") },
{ (RegisterIndex.C, RegisterIndex.C), ("fst", "st(1)") },
{ (RegisterIndex.C, RegisterIndex.D), ("fst", "st(2)") },
{ (RegisterIndex.C, RegisterIndex.B), ("fst", "st(3)") },
{ (RegisterIndex.C, RegisterIndex.Sp), ("fst", "st(4)") },
{ (RegisterIndex.C, RegisterIndex.Bp), ("fst", "st(5)") },
{ (RegisterIndex.C, RegisterIndex.Si), ("fst", "st(6)") },
{ (RegisterIndex.C, RegisterIndex.Di), ("fst", "st(7)") },
// FSTP ST(i)
{ (RegisterIndex.D, RegisterIndex.A), ("fstp", "st(0)") },
{ (RegisterIndex.D, RegisterIndex.C), ("fstp", "st(1)") },
{ (RegisterIndex.D, RegisterIndex.D), ("fstp", "st(2)") },
{ (RegisterIndex.D, RegisterIndex.B), ("fstp", "st(3)") },
{ (RegisterIndex.D, RegisterIndex.Sp), ("fstp", "st(4)") },
{ (RegisterIndex.D, RegisterIndex.Bp), ("fstp", "st(5)") },
{ (RegisterIndex.D, RegisterIndex.Si), ("fstp", "st(6)") },
{ (RegisterIndex.D, RegisterIndex.Di), ("fstp", "st(7)") },
// FUCOM ST(i)
{ (RegisterIndex.Si, RegisterIndex.A), ("fucom", "st(0)") },
{ (RegisterIndex.Si, RegisterIndex.C), ("fucom", "st(1)") },
{ (RegisterIndex.Si, RegisterIndex.D), ("fucom", "st(2)") },
{ (RegisterIndex.Si, RegisterIndex.B), ("fucom", "st(3)") },
{ (RegisterIndex.Si, RegisterIndex.Sp), ("fucom", "st(4)") },
{ (RegisterIndex.Si, RegisterIndex.Bp), ("fucom", "st(5)") },
{ (RegisterIndex.Si, RegisterIndex.Si), ("fucom", "st(6)") },
{ (RegisterIndex.Si, RegisterIndex.Di), ("fucom", "st(7)") },
// FUCOMP ST(i)
{ (RegisterIndex.Di, RegisterIndex.A), ("fucomp", "st(0)") },
{ (RegisterIndex.Di, RegisterIndex.C), ("fucomp", "st(1)") },
{ (RegisterIndex.Di, RegisterIndex.D), ("fucomp", "st(2)") },
{ (RegisterIndex.Di, RegisterIndex.B), ("fucomp", "st(3)") },
{ (RegisterIndex.Di, RegisterIndex.Sp), ("fucomp", "st(4)") },
{ (RegisterIndex.Di, RegisterIndex.Bp), ("fucomp", "st(5)") },
{ (RegisterIndex.Di, RegisterIndex.Si), ("fucomp", "st(6)") },
{ (RegisterIndex.Di, RegisterIndex.Di), ("fucomp", "st(7)") }
};
/// <summary> /// <summary>
/// Initializes a new instance of the LoadStoreFloat64Handler class /// Initializes a new instance of the LoadStoreFloat64Handler class
@ -47,59 +101,38 @@ public class LoadStoreFloat64Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns> /// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction) public override bool Decode(byte opcode, Instruction instruction)
{ {
int position = Decoder.GetPosition(); if (!Decoder.CanReadByte())
if (position >= Length)
{ {
return false; return false;
} }
// Read the ModR/M byte // Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM(true);// true for 64-bit operand var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM(true); // true for 64-bit operand
// Set the mnemonic based on the opcode and reg field // Handle based on addressing mode
instruction.Mnemonic = Mnemonics[(int)reg];
// For memory operands, set the operand
if (mod != 3) // Memory operand if (mod != 3) // Memory operand
{ {
// Set the mnemonic based on the reg field
instruction.Mnemonic = MemoryMnemonics[(int)reg];
if (reg == RegisterIndex.A || reg == RegisterIndex.C || reg == RegisterIndex.D) // fld, fst, fstp if (reg == RegisterIndex.A || reg == RegisterIndex.C || reg == RegisterIndex.D) // fld, fst, fstp
{ {
instruction.Operands = destOperand; // Keep the qword ptr prefix from ModRMDecoder
instruction.Operands = memOperand;
} }
else // frstor, fnsave, fnstsw else // frstor, fnsave, fnstsw
{ {
// Remove the qword ptr prefix for these operations // Remove the qword ptr prefix for these operations
instruction.Operands = destOperand.Replace("qword ptr ", ""); instruction.Operands = memOperand.Replace("qword ptr ", "");
} }
} }
else // Register operand (ST(i)) else // Register operand (ST(i))
{ {
// Special handling for register-register operations // Look up the register operation in our dictionary
if (reg == RegisterIndex.A) // FFREE if (RegisterOperations.TryGetValue((reg, rm), out var operation))
{ {
instruction.Mnemonic = "ffree"; instruction.Mnemonic = operation.Mnemonic;
instruction.Operands = $"st({(int)rm})"; instruction.Operands = operation.Operands;
}
else if (reg == RegisterIndex.C) // FST
{
instruction.Mnemonic = "fst";
instruction.Operands = $"st({(int)rm})";
}
else if (reg == RegisterIndex.D) // FSTP
{
instruction.Mnemonic = "fstp";
instruction.Operands = $"st({(int)rm})";
}
else if (reg == RegisterIndex.Si) // FUCOM
{
instruction.Mnemonic = "fucom";
instruction.Operands = $"st({(int)rm})";
}
else if (reg == RegisterIndex.Di) // FUCOMP
{
instruction.Mnemonic = "fucomp";
instruction.Operands = $"st({(int)rm})";
} }
else else
{ {