From 905254615df34a60ba0571c48a368aec850b5706 Mon Sep 17 00:00:00 2001 From: zilmar Date: Thu, 31 Oct 2024 06:50:17 +1030 Subject: [PATCH] Core: Change the handling of symbols inside asmjit usage --- .../Recompiler/x86/x86RecompilerOps.cpp | 22 +- .../N64System/Recompiler/x86/x86ops.cpp | 522 ++++++------------ .../N64System/Recompiler/x86/x86ops.h | 16 +- 3 files changed, 203 insertions(+), 357 deletions(-) diff --git a/Source/Project64-core/N64System/Recompiler/x86/x86RecompilerOps.cpp b/Source/Project64-core/N64System/Recompiler/x86/x86RecompilerOps.cpp index a9c70e259..8ea0928a5 100644 --- a/Source/Project64-core/N64System/Recompiler/x86/x86RecompilerOps.cpp +++ b/Source/Project64-core/N64System/Recompiler/x86/x86RecompilerOps.cpp @@ -2961,11 +2961,11 @@ void CX86RecompilerOps::LB_KnownAddress(const asmjit::x86::Gp & Reg, uint32_t VA { if (SignExtend) { - m_Assembler.MoveSxVariableToX86regByte(Reg, (PAddr ^ 3) + g_MMU->Rdram(), stdstr_f("RDRAM + (%X ^ 3)", PAddr).c_str()); + m_Assembler.MoveSxVariableToX86regByte(Reg, (PAddr ^ 3) + g_MMU->Rdram(), stdstr_f("RDRAM + (0x%X ^ 3)", PAddr).c_str()); } else { - m_Assembler.MoveZxVariableToX86regByte(Reg, (PAddr ^ 3) + g_MMU->Rdram(), stdstr_f("RDRAM + (%X ^ 3)", PAddr).c_str()); + m_Assembler.MoveZxVariableToX86regByte(Reg, (PAddr ^ 3) + g_MMU->Rdram(), stdstr_f("RDRAM + (0x%X ^ 3)", PAddr).c_str()); } } else @@ -3078,11 +3078,11 @@ void CX86RecompilerOps::LH_KnownAddress(const asmjit::x86::Gp & Reg, uint32_t VA { if (SignExtend) { - m_Assembler.MoveSxVariableToX86regHalf(Reg, (PAddr ^ 2) + g_MMU->Rdram(), stdstr_f("RDRAM + (%X ^ 2)", PAddr).c_str()); + m_Assembler.MoveSxVariableToX86regHalf(Reg, (PAddr ^ 2) + g_MMU->Rdram(), stdstr_f("RDRAM + (0x%X ^ 2)", PAddr).c_str()); } else { - m_Assembler.MoveZxVariableToX86regHalf(Reg, (PAddr ^ 2) + g_MMU->Rdram(), stdstr_f("RDRAM + (%X ^ 2)", PAddr).c_str()); + m_Assembler.MoveZxVariableToX86regHalf(Reg, (PAddr ^ 2) + g_MMU->Rdram(), stdstr_f("RDRAM + (0x%X ^ 2)", PAddr).c_str()); } } else @@ -9345,7 +9345,7 @@ void CX86RecompilerOps::UpdateSyncCPU(CRegInfo & RegSet, uint32_t Cycles) m_CodeBlock.Log(""); m_CodeBlock.Log(" // Updating sync CPU"); RegSet.BeforeCallDirect(); - m_Assembler.PushImm32(stdstr_f("%d", Cycles).c_str(), Cycles); + m_Assembler.push(Cycles); m_Assembler.CallThis((uint32_t)g_System, AddressOf(&CN64System::UpdateSyncCPU), "CN64System::UpdateSyncCPU", 8); RegSet.AfterCallDirect(); } @@ -10370,7 +10370,7 @@ void CX86RecompilerOps::SB_Const(uint32_t Value, uint32_t VAddr) } else if (PAddr < g_MMU->RdramSize()) { - m_Assembler.MoveConstByteToVariable((PAddr ^ 3) + g_MMU->Rdram(), stdstr_f("RDRAM + (%X ^ 3)", PAddr).c_str(), (uint8_t)Value); + m_Assembler.MoveConstByteToVariable((PAddr ^ 3) + g_MMU->Rdram(), stdstr_f("RDRAM + (0x%X ^ 3)", PAddr).c_str(), (uint8_t)Value); } break; case 0x04000000: @@ -10448,7 +10448,7 @@ void CX86RecompilerOps::SB_Register(const asmjit::x86::Gp & Reg, uint32_t VAddr) } else if (PAddr < g_MMU->RdramSize()) { - m_Assembler.MoveX86regByteToVariable((PAddr ^ 3) + g_MMU->Rdram(), stdstr_f("RDRAM + (%X ^ 3)", PAddr).c_str(), Reg); + m_Assembler.MoveX86regByteToVariable((PAddr ^ 3) + g_MMU->Rdram(), stdstr_f("RDRAM + (0x%X ^ 3)", PAddr).c_str(), Reg); } break; default: @@ -10498,7 +10498,7 @@ void CX86RecompilerOps::SH_Const(uint32_t Value, uint32_t VAddr) } else if (PAddr < g_MMU->RdramSize()) { - m_Assembler.MoveConstHalfToVariable((PAddr ^ 2) + g_MMU->Rdram(), stdstr_f("RDRAM + (%X ^ 2)", PAddr).c_str(), (uint16_t)Value); + m_Assembler.MoveConstHalfToVariable((PAddr ^ 2) + g_MMU->Rdram(), stdstr_f("RDRAM + (0x%X ^ 2)", PAddr).c_str(), (uint16_t)Value); } break; default: @@ -10551,7 +10551,7 @@ void CX86RecompilerOps::SH_Register(const asmjit::x86::Gp & Reg, uint32_t VAddr) } else if (PAddr < g_MMU->RdramSize()) { - m_Assembler.MoveX86regHalfToVariable((PAddr ^ 2) + g_MMU->Rdram(), stdstr_f("RDRAM + (%X ^ 2)", PAddr).c_str(), Reg); + m_Assembler.MoveX86regHalfToVariable((PAddr ^ 2) + g_MMU->Rdram(), stdstr_f("RDRAM + (0x%X ^ 2)", PAddr).c_str(), Reg); } break; default: @@ -10613,7 +10613,7 @@ void CX86RecompilerOps::SW_Const(uint32_t Value, uint32_t VAddr) } else if (PAddr < g_MMU->RdramSize()) { - m_Assembler.MoveConstToVariable(PAddr + g_MMU->Rdram(), stdstr_f("RDRAM + %X", PAddr).c_str(), Value); + m_Assembler.MoveConstToVariable(PAddr + g_MMU->Rdram(), stdstr_f("RDRAM + 0x%X", PAddr).c_str(), Value); } break; case 0x03F00000: @@ -11041,7 +11041,7 @@ void CX86RecompilerOps::SW_Register(const asmjit::x86::Gp & Reg, uint32_t VAddr) } else if (PAddr < g_MMU->RdramSize()) { - m_Assembler.MoveX86regToVariable(PAddr + g_MMU->Rdram(), stdstr_f("RDRAM + %X", PAddr).c_str(), Reg); + m_Assembler.MoveX86regToVariable(PAddr + g_MMU->Rdram(), stdstr_f("RDRAM + 0x%X", PAddr).c_str(), Reg); } break; case 0x04000000: diff --git a/Source/Project64-core/N64System/Recompiler/x86/x86ops.cpp b/Source/Project64-core/N64System/Recompiler/x86/x86ops.cpp index 5e6931458..38422c6da 100644 --- a/Source/Project64-core/N64System/Recompiler/x86/x86ops.cpp +++ b/Source/Project64-core/N64System/Recompiler/x86/x86ops.cpp @@ -21,30 +21,18 @@ void CX86Ops::AdcVariableToX86reg(const asmjit::x86::Gp & Reg, void * Variable, { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - adc(Reg, asmjit::x86::dword_ptr((uint64_t)Variable)); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - adc(Reg, asmjit::x86::dword_ptr((uint64_t)Variable)); + AddNumberSymbol((uint32_t)Variable, VariableName); } + adc(Reg, asmjit::x86::dword_ptr((uint64_t)Variable)); } void CX86Ops::AddConstToVariable(void * Variable, const char * VariableName, uint32_t Const) { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - add(asmjit::x86::dword_ptr((uint64_t)Variable), Const); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - add(asmjit::x86::dword_ptr((uint64_t)Variable), Const); + AddNumberSymbol((uint32_t)Variable, VariableName); } + add(asmjit::x86::dword_ptr((uint64_t)Variable), Const); } void CX86Ops::AddConstToX86Reg(const asmjit::x86::Gp & Reg, uint32_t Const) @@ -70,60 +58,36 @@ void CX86Ops::AddVariableToX86reg(const asmjit::x86::Gp & Reg, void * Variable, { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - add(Reg, asmjit::x86::dword_ptr((uint64_t)Variable)); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - add(Reg, asmjit::x86::dword_ptr((uint64_t)Variable)); + AddNumberSymbol((uint32_t)Variable, VariableName); } + add(Reg, asmjit::x86::dword_ptr((uint64_t)Variable)); } void CX86Ops::AndConstToVariable(void * Variable, const char * VariableName, uint32_t Const) { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - and_(asmjit::x86::dword_ptr((uint64_t)Variable), Const); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - and_(asmjit::x86::dword_ptr((uint64_t)Variable), Const); + AddNumberSymbol((uint32_t)Variable, VariableName); } + and_(asmjit::x86::dword_ptr((uint64_t)Variable), Const); } void CX86Ops::AndVariableDispToX86Reg(const asmjit::x86::Gp & Reg, void * Variable, const char * VariableName, const asmjit::x86::Gp & AddrReg, Multipler Multiply) { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - and_(Reg, asmjit::x86::dword_ptr((uint64_t)Variable, AddrReg, Multiply >> 1)); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - and_(Reg, asmjit::x86::dword_ptr((uint64_t)Variable, AddrReg, Multiply >> 1)); + AddNumberSymbol((uint32_t)Variable, VariableName); } + and_(Reg, asmjit::x86::dword_ptr((uint64_t)Variable, AddrReg, Multiply >> 1)); } void CX86Ops::AndVariableToX86Reg(const asmjit::x86::Gp & Reg, void * Variable, const char * VariableName) { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - and_(Reg, asmjit::x86::dword_ptr((uint64_t)Variable)); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - and_(Reg, asmjit::x86::dword_ptr((uint64_t)Variable)); + AddNumberSymbol((uint32_t)Variable, VariableName); } + and_(Reg, asmjit::x86::dword_ptr((uint64_t)Variable)); } void CX86Ops::BreakPointNotification(const char * FileName, int32_t LineNumber) @@ -150,15 +114,9 @@ void CX86Ops::CallFunc(uint32_t FunctPtr, const char * FunctName) { if (CDebugSettings::bRecordRecompilerAsm()) { - stdstr_f SymbolKey("0x%X", FunctPtr); - AddSymbol(SymbolKey.c_str(), FunctName); - call((uint64_t)FunctPtr); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - call((uint64_t)FunctPtr); + AddNumberSymbol(FunctPtr, FunctName); } + call((uint64_t)FunctPtr); } #ifdef _MSC_VER @@ -180,30 +138,18 @@ void CX86Ops::CompConstByteToVariable(void * Variable, const char * VariableName { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - cmp(asmjit::x86::byte_ptr((uint64_t)Variable), Const); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - cmp(asmjit::x86::byte_ptr((uint64_t)Variable), Const); + AddNumberSymbol((uint32_t)Variable, VariableName); } + cmp(asmjit::x86::byte_ptr((uint64_t)Variable), Const); } void CX86Ops::CompConstToVariable(void * Variable, const char * VariableName, uint32_t Const) { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - cmp(asmjit::x86::dword_ptr((uint64_t)Variable), Const); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - cmp(asmjit::x86::dword_ptr((uint64_t)Variable), Const); + AddNumberSymbol((uint32_t)Variable, VariableName); } + cmp(asmjit::x86::dword_ptr((uint64_t)Variable), Const); } void CX86Ops::CompConstToX86reg(const asmjit::x86::Gp & Reg, uint32_t Const) @@ -222,15 +168,9 @@ void CX86Ops::CompX86regToVariable(const asmjit::x86::Gp & Reg, void * Variable, { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - cmp(Reg, asmjit::x86::dword_ptr((uint64_t)Variable)); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - cmp(Reg, asmjit::x86::dword_ptr((uint64_t)Variable)); + AddNumberSymbol((uint32_t)Variable, VariableName); } + cmp(Reg, asmjit::x86::dword_ptr((uint64_t)Variable)); } void CX86Ops::Fabs(void) @@ -270,109 +210,109 @@ void CX86Ops::Fsub(const asmjit::x86::Mem & Mem) void CX86Ops::JaeLabel(const char * LabelName, asmjit::Label & JumpLabel) { - AddSymbol(stdstr_f("L%d", JumpLabel.id()).c_str(), LabelName); + AddLabelSymbol(JumpLabel, LabelName); jae(JumpLabel); } void CX86Ops::JaLabel(const char * LabelName, asmjit::Label & JumpLabel) { - AddSymbol(stdstr_f("L%d", JumpLabel.id()).c_str(), LabelName); + AddLabelSymbol(JumpLabel, LabelName); ja(JumpLabel); } void CX86Ops::JbLabel(const char * LabelName, asmjit::Label & JumpLabel) { - AddSymbol(stdstr_f("L%d", JumpLabel.id()).c_str(), LabelName); + AddLabelSymbol(JumpLabel, LabelName); jb(JumpLabel); } void CX86Ops::JbeLabel(const char * LabelName, asmjit::Label & JumpLabel) { - AddSymbol(stdstr_f("L%d", JumpLabel.id()).c_str(), LabelName); + AddLabelSymbol(JumpLabel, LabelName); jbe(JumpLabel); } void CX86Ops::JecxzLabel(const char * LabelName, asmjit::Label & JumpLabel) { - AddSymbol(stdstr_f("L%d", JumpLabel.id()).c_str(), LabelName); + AddLabelSymbol(JumpLabel, LabelName); jecxz(JumpLabel); } void CX86Ops::JeLabel(const char * LabelName, asmjit::Label & JumpLabel) { - AddSymbol(stdstr_f("L%d", JumpLabel.id()).c_str(), LabelName); + AddLabelSymbol(JumpLabel, LabelName); je(JumpLabel); } void CX86Ops::JgeLabel(const char * LabelName, asmjit::Label & JumpLabel) { - AddSymbol(stdstr_f("L%d", JumpLabel.id()).c_str(), LabelName); + AddLabelSymbol(JumpLabel, LabelName); jge(JumpLabel); } void CX86Ops::JgLabel(const char * LabelName, asmjit::Label & JumpLabel) { - AddSymbol(stdstr_f("L%d", JumpLabel.id()).c_str(), LabelName); + AddLabelSymbol(JumpLabel, LabelName); jg(JumpLabel); } void CX86Ops::JleLabel(const char * LabelName, asmjit::Label & JumpLabel) { - AddSymbol(stdstr_f("L%d", JumpLabel.id()).c_str(), LabelName); + AddLabelSymbol(JumpLabel, LabelName); jle(JumpLabel); } void CX86Ops::JlLabel(const char * LabelName, asmjit::Label & JumpLabel) { - AddSymbol(stdstr_f("L%d", JumpLabel.id()).c_str(), LabelName); + AddLabelSymbol(JumpLabel, LabelName); jl(JumpLabel); } void CX86Ops::JmpLabel(const char * LabelName, asmjit::Label & JumpLabel) { - AddSymbol(stdstr_f("L%d", JumpLabel.id()).c_str(), LabelName); + AddLabelSymbol(JumpLabel, LabelName); jmp(JumpLabel); } void CX86Ops::JneLabel(const char * LabelName, asmjit::Label & JumpLabel) { - AddSymbol(stdstr_f("L%d", JumpLabel.id()).c_str(), LabelName); + AddLabelSymbol(JumpLabel, LabelName); jne(JumpLabel); } void CX86Ops::JnpLabel(const char * LabelName, asmjit::Label & JumpLabel) { - AddSymbol(stdstr_f("L%d", JumpLabel.id()).c_str(), LabelName); + AddLabelSymbol(JumpLabel, LabelName); jnp(JumpLabel); } void CX86Ops::JnsLabel(const char * LabelName, asmjit::Label & JumpLabel) { - AddSymbol(stdstr_f("L%d", JumpLabel.id()).c_str(), LabelName); + AddLabelSymbol(JumpLabel, LabelName); jns(JumpLabel); } void CX86Ops::JnzLabel(const char * LabelName, asmjit::Label & JumpLabel) { - AddSymbol(stdstr_f("L%d", JumpLabel.id()).c_str(), LabelName); + AddLabelSymbol(JumpLabel, LabelName); jnz(JumpLabel); } void CX86Ops::JsLabel(const char * LabelName, asmjit::Label & JumpLabel) { - AddSymbol(stdstr_f("L%d", JumpLabel.id()).c_str(), LabelName); + AddLabelSymbol(JumpLabel, LabelName); js(JumpLabel); } void CX86Ops::JoLabel(const char * LabelName, asmjit::Label & JumpLabel) { - AddSymbol(stdstr_f("L%d", JumpLabel.id()).c_str(), LabelName); + AddLabelSymbol(JumpLabel, LabelName); jo(JumpLabel); } void CX86Ops::JzLabel(const char * LabelName, asmjit::Label & JumpLabel) { - AddSymbol(stdstr_f("L%d", JumpLabel.id()).c_str(), LabelName); + AddLabelSymbol(JumpLabel, LabelName); jz(JumpLabel); } @@ -380,61 +320,37 @@ void CX86Ops::MoveConstByteToVariable(void * Variable, const char * VariableName { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - mov(asmjit::x86::byte_ptr((uint64_t)Variable), Const); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - mov(asmjit::x86::byte_ptr((uint64_t)Variable), Const); + AddNumberSymbol((uint32_t)Variable, VariableName); } + mov(asmjit::x86::byte_ptr((uint64_t)Variable), Const); } void CX86Ops::MoveConstHalfToVariable(void * Variable, const char * VariableName, uint16_t Const) { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - mov(asmjit::x86::word_ptr((uint64_t)Variable), Const); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - mov(asmjit::x86::word_ptr((uint64_t)Variable), Const); + AddNumberSymbol((uint32_t)Variable, VariableName); } + mov(asmjit::x86::word_ptr((uint64_t)Variable), Const); } void CX86Ops::MoveConstToVariable(void * Variable, const char * VariableName, uint32_t Const) { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - mov(asmjit::x86::dword_ptr((uint64_t)Variable), Const); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - mov(asmjit::x86::dword_ptr((uint64_t)Variable), Const); + AddNumberSymbol((uint32_t)Variable, VariableName); } + mov(asmjit::x86::dword_ptr((uint64_t)Variable), Const); } void CX86Ops::MoveConst64ToVariable(void * Variable, const char * VariableName, uint64_t Const) { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - mov(asmjit::x86::dword_ptr((uint64_t)Variable), (uint32_t)Const); - mov(asmjit::x86::dword_ptr(((uint64_t)Variable) + 4), (uint32_t)(Const >> 32)); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - mov(asmjit::x86::dword_ptr((uint64_t)Variable), Const); + AddNumberSymbol((uint32_t)Variable, VariableName); } + mov(asmjit::x86::dword_ptr((uint64_t)Variable), (uint32_t)Const); + mov(asmjit::x86::dword_ptr(((uint64_t)Variable) + 4), (uint32_t)(Const >> 32)); } void CX86Ops::MoveConstToX86reg(const asmjit::x86::Gp & Reg, uint32_t Const) @@ -453,332 +369,198 @@ void CX86Ops::MoveSxVariableToX86regByte(const asmjit::x86::Gp & Reg, void * Var { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - movsx(Reg, asmjit::x86::byte_ptr((uint64_t)Variable)); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - movsx(Reg, asmjit::x86::byte_ptr((uint64_t)Variable)); + AddNumberSymbol((uint32_t)Variable, VariableName); } + movsx(Reg, asmjit::x86::byte_ptr((uint64_t)Variable)); } void CX86Ops::MoveSxVariableToX86regHalf(const asmjit::x86::Gp & Reg, void * Variable, const char * VariableName) { if (CDebugSettings::bRecordRecompilerAsm()) { - stdstr_f SymbolKey("0x%X", Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - movsx(Reg, asmjit::x86::word_ptr((uint64_t)Variable)); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - movsx(Reg, asmjit::x86::word_ptr((uint64_t)Variable)); + AddNumberSymbol((uint32_t)Variable, VariableName); } + movsx(Reg, asmjit::x86::word_ptr((uint64_t)Variable)); } void CX86Ops::MoveVariableToX86reg(const asmjit::x86::Gp & Reg, void * Variable, const char * VariableName) { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - mov(Reg, asmjit::x86::dword_ptr((uint64_t)Variable)); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - mov(Reg, asmjit::x86::dword_ptr((uint64_t)Variable)); + AddNumberSymbol((uint32_t)Variable, VariableName); } + mov(Reg, asmjit::x86::dword_ptr((uint64_t)Variable)); } void CX86Ops::MoveVariableDispToX86Reg(const asmjit::x86::Gp & Reg, void * Variable, const char * VariableName, const asmjit::x86::Gp & AddrReg, Multipler Multiplier) { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - mov(Reg, asmjit::x86::dword_ptr((uint64_t)Variable, AddrReg, Multiplier >> 1)); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - mov(Reg, asmjit::x86::dword_ptr((uint64_t)Variable, AddrReg, Multiplier >> 1)); + AddNumberSymbol((uint32_t)Variable, VariableName); } + mov(Reg, asmjit::x86::dword_ptr((uint64_t)Variable, AddrReg, Multiplier >> 1)); } void CX86Ops::MoveX86regByteToVariable(void * Variable, const char * VariableName, const asmjit::x86::Gp & Reg) { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - - AddSymbol(SymbolKey.c_str(), VariableName); - mov(asmjit::x86::byte_ptr((uint64_t)(Variable)), Reg.r8()); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - mov(asmjit::x86::byte_ptr((uint64_t)(Variable)), Reg.r8()); + AddNumberSymbol((uint32_t)Variable, VariableName); } + mov(asmjit::x86::byte_ptr((uint64_t)(Variable)), Reg.r8()); } void CX86Ops::MoveX86regHalfToVariable(void * Variable, const char * VariableName, const asmjit::x86::Gp & Reg) { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - - AddSymbol(SymbolKey.c_str(), VariableName); - mov(asmjit::x86::word_ptr((uint64_t)(Variable)), Reg.r16()); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - mov(asmjit::x86::word_ptr((uint64_t)(Variable)), Reg.r16()); + AddNumberSymbol((uint32_t)Variable, VariableName); } + mov(asmjit::x86::word_ptr((uint64_t)(Variable)), Reg.r16()); } void CX86Ops::MoveX86regToVariable(void * Variable, const char * VariableName, const asmjit::x86::Gp & Reg) { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - mov(asmjit::x86::dword_ptr((uint64_t)(Variable)), Reg); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - mov(asmjit::x86::dword_ptr((uint64_t)(Variable)), Reg); + AddNumberSymbol((uint32_t)Variable, VariableName); } + mov(asmjit::x86::dword_ptr((uint64_t)(Variable)), Reg); } void CX86Ops::MoveZxVariableToX86regByte(const asmjit::x86::Gp & Reg, void * Variable, const char * VariableName) { if (CDebugSettings::bRecordRecompilerAsm()) { - stdstr_f SymbolKey("0x%X", Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - movzx(Reg, asmjit::x86::byte_ptr((uint64_t)Variable)); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - movzx(Reg, asmjit::x86::byte_ptr((uint64_t)Variable)); + AddNumberSymbol((uint32_t)Variable, VariableName); } + movzx(Reg, asmjit::x86::byte_ptr((uint64_t)Variable)); } void CX86Ops::MoveZxVariableToX86regHalf(const asmjit::x86::Gp & Reg, void * Variable, const char * VariableName) { if (CDebugSettings::bRecordRecompilerAsm()) { - stdstr_f SymbolKey("0x%X", Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - movzx(Reg, asmjit::x86::word_ptr((uint64_t)Variable)); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - movzx(Reg, asmjit::x86::word_ptr((uint64_t)Variable)); + AddNumberSymbol((uint32_t)Variable, VariableName); } + movzx(Reg, asmjit::x86::word_ptr((uint64_t)Variable)); } void CX86Ops::OrConstToVariable(void * Variable, const char * VariableName, uint32_t Const) { if (CDebugSettings::bRecordRecompilerAsm()) { - stdstr_f SymbolKey("0x%X", Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - or_(asmjit::x86::dword_ptr((uint64_t)Variable), Const); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - or_(asmjit::x86::dword_ptr((uint64_t)Variable), Const); + AddNumberSymbol((uint32_t)Variable, VariableName); } + or_(asmjit::x86::dword_ptr((uint64_t)Variable), Const); } void CX86Ops::OrVariableToX86Reg(const asmjit::x86::Gp & Reg, void * Variable, const char * VariableName) { if (CDebugSettings::bRecordRecompilerAsm()) { - stdstr_f SymbolKey("0x%X", Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - or_(Reg, asmjit::x86::dword_ptr((uint64_t)Variable)); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - or_(Reg, asmjit::x86::dword_ptr((uint64_t)Variable)); + AddNumberSymbol((uint32_t)Variable, VariableName); } + or_(Reg, asmjit::x86::dword_ptr((uint64_t)Variable)); } void CX86Ops::OrX86RegToVariable(void * Variable, const char * VariableName, const asmjit::x86::Gp & Reg) { if (CDebugSettings::bRecordRecompilerAsm()) { - stdstr_f SymbolKey("0x%X", Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - or_(asmjit::x86::dword_ptr((uint64_t)Variable), Reg); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - or_(asmjit::x86::dword_ptr((uint64_t)Variable), Reg); + AddNumberSymbol((uint32_t)Variable, VariableName); } + or_(asmjit::x86::dword_ptr((uint64_t)Variable), Reg); } void CX86Ops::PushImm32(const char * String, uint32_t Value) { if (CDebugSettings::bRecordRecompilerAsm()) { - stdstr_f SymbolKey("0x%X", Value); - AddSymbol(SymbolKey.c_str(), String); - push(Value); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - push(Value); + AddNumberSymbol(Value, String); } + push(Value); } void CX86Ops::SetaVariable(void * Variable, const char * VariableName) { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - seta(asmjit::x86::byte_ptr((uint32_t)Variable)); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - seta(asmjit::x86::byte_ptr((uint32_t)Variable)); + AddNumberSymbol((uint32_t)Variable, VariableName); } + seta(asmjit::x86::byte_ptr((uint32_t)Variable)); } void CX86Ops::SetbVariable(void * Variable, const char * VariableName) { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - setb(asmjit::x86::byte_ptr((uint32_t)Variable)); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - setb(asmjit::x86::byte_ptr((uint32_t)Variable)); + AddNumberSymbol((uint32_t)Variable, VariableName); } + setb(asmjit::x86::byte_ptr((uint32_t)Variable)); } void CX86Ops::SetgVariable(void * Variable, const char * VariableName) { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - setg(asmjit::x86::byte_ptr((uint32_t)Variable)); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - setg(asmjit::x86::byte_ptr((uint32_t)Variable)); + AddNumberSymbol((uint32_t)Variable, VariableName); } + setg(asmjit::x86::byte_ptr((uint32_t)Variable)); } void CX86Ops::SetlVariable(void * Variable, const char * VariableName) { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - setl(asmjit::x86::byte_ptr((uint32_t)Variable)); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - setl(asmjit::x86::byte_ptr((uint32_t)Variable)); + AddNumberSymbol((uint32_t)Variable, VariableName); } + setl(asmjit::x86::byte_ptr((uint32_t)Variable)); } void CX86Ops::SbbVariableFromX86reg(const asmjit::x86::Gp & Reg, void * Variable, const char * VariableName) { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - sbb(Reg, asmjit::x86::dword_ptr((uint32_t)Variable)); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - sbb(Reg, asmjit::x86::dword_ptr((uint32_t)Variable)); + AddNumberSymbol((uint32_t)Variable, VariableName); } + sbb(Reg, asmjit::x86::dword_ptr((uint32_t)Variable)); } void CX86Ops::SubConstFromVariable(uint32_t Const, void * Variable, const char * VariableName) { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - sub(asmjit::x86::dword_ptr((uint32_t)Variable), Const); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - sub(asmjit::x86::dword_ptr((uint32_t)Variable), Const); + AddNumberSymbol((uint32_t)Variable, VariableName); } + sub(asmjit::x86::dword_ptr((uint32_t)Variable), Const); } void CX86Ops::SubVariableFromX86reg(const asmjit::x86::Gp & Reg, void * Variable, const char * VariableName) { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - sub(Reg, asmjit::x86::dword_ptr((uint64_t)Variable)); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - sub(Reg, asmjit::x86::dword_ptr((uint64_t)Variable)); + AddNumberSymbol((uint32_t)Variable, VariableName); } + sub(Reg, asmjit::x86::dword_ptr((uint64_t)Variable)); } void CX86Ops::TestVariable(void * Variable, const char * VariableName, uint32_t Const) { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - test(asmjit::x86::dword_ptr((uint64_t)Variable), Const); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - test(asmjit::x86::dword_ptr((uint64_t)Variable), Const); + AddNumberSymbol((uint32_t)Variable, VariableName); } + test(asmjit::x86::dword_ptr((uint64_t)Variable), Const); } void CX86Ops::XorVariableToX86reg(const asmjit::x86::Gp & Reg, void * Variable, const char * VariableName) { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - xor_(Reg, asmjit::x86::dword_ptr((uint64_t)Variable)); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - xor_(Reg, asmjit::x86::dword_ptr((uint64_t)Variable)); + AddNumberSymbol((uint32_t)Variable, VariableName); } + xor_(Reg, asmjit::x86::dword_ptr((uint64_t)Variable)); } void CX86Ops::fpuCompp(int32_t & StackPos) @@ -797,15 +579,9 @@ void CX86Ops::fpuLoadControl(void * Variable, const char * VariableName) { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - fldcw(asmjit::x86::ptr((uint64_t)Variable)); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - fldcw(asmjit::x86::ptr((uint64_t)Variable)); + AddNumberSymbol((uint32_t)Variable, VariableName); } + fldcw(asmjit::x86::ptr((uint64_t)Variable)); } void CX86Ops::fpuLoadDwordFromX86Reg(int32_t & StackPos, const asmjit::x86::Gp & x86reg) @@ -854,15 +630,9 @@ void CX86Ops::fpuStoreControl(void * Variable, const char * VariableName) { if (CDebugSettings::bRecordRecompilerAsm()) { - std::string SymbolKey = VariableSymbol(Variable); - AddSymbol(SymbolKey.c_str(), VariableName); - fnstcw(asmjit::x86::ptr((uint64_t)Variable)); - RemoveSymbol(SymbolKey.c_str()); - } - else - { - fnstcw(asmjit::x86::ptr((uint64_t)Variable)); + AddNumberSymbol((uint32_t)Variable, VariableName); } + fnstcw(asmjit::x86::ptr((uint64_t)Variable)); } void CX86Ops::fpuStoreDwordFromX86Reg(int32_t & StackPos, const asmjit::x86::Gp & x86reg, bool pop) @@ -1049,35 +819,105 @@ asmjit::Error CX86Ops::_log(const char * data, size_t size) noexcept { stdstr AsmjitLog(std::string(data, size)); AsmjitLog.Trim("\n"); - for (SymbolMap::const_iterator itr = m_Symbols.begin(); itr != m_Symbols.end(); itr++) + std::string::size_type Pos = AsmjitLog.find("0x"); + if (m_NumberSymbols.size() > 0 && Pos != std::string::npos) { - AsmjitLog.Replace(itr->first, itr->second); + uint32_t Value = 0; + if (AsmjitLog[Pos] == '0' && (AsmjitLog[Pos+1] == 'x' || AsmjitLog[Pos+1] == 'X')) + { + Pos += 2; + } + + for (int i = 0; i < 8; i++) + { + char c = AsmjitLog[Pos + i]; + if (c >= '0' && c <= '9') + { + Value = (Value << 4) | (c - '0'); + } + else if (c >= 'a' && c <= 'f') + { + Value = (Value << 4) | (c - 'a' + 10); + } + else if (c >= 'A' && c <= 'F') + { + Value = (Value << 4) | (c - 'A' + 10); + } + else + { + break; + } + } + NumberSymbolMap::iterator itr = m_NumberSymbols.find(Value); + if (itr != m_NumberSymbols.end()) + { + std::string::size_type endPos = Pos + 2; + for (std::string::size_type LenSize = AsmjitLog.length(); (endPos < LenSize && isxdigit((unsigned char)AsmjitLog[endPos])); endPos++) + { + } + std::string hexStr = AsmjitLog.substr(Pos, endPos - Pos); + AsmjitLog.replace(Pos, hexStr.length(), itr->second.Symbol); + itr->second.Count -= 1; + if (itr->second.Count == 0) + { + m_NumberSymbols.erase(itr); + } + } + } + Pos = AsmjitLog.find("L"); + if (m_LabelSymbols.size() > 0 && Pos != std::string::npos) + { + NumberSymbolMap::iterator itr = m_LabelSymbols.find(std::stoul(&AsmjitLog[Pos + 1], nullptr, 10)); + if (itr != m_LabelSymbols.end()) + { + + std::string::size_type endPos = Pos + 1; + for (std::string::size_type LenSize = AsmjitLog.length(); (endPos < LenSize && isdigit((unsigned char)AsmjitLog[endPos])); endPos++) + { + } + std::string LabelStr = AsmjitLog.substr(Pos, endPos - Pos); + AsmjitLog.replace(Pos, LabelStr.length(), itr->second.Symbol); + itr->second.Count -= 1; + if (itr->second.Count == 0) + { + m_LabelSymbols.erase(itr); + } + } } m_CodeBlock.Log(" %s", AsmjitLog.c_str()); return asmjit::kErrorOk; } -void CX86Ops::AddSymbol(const char * SymbolKey, const char * SymbolValue) +void CX86Ops::AddLabelSymbol(const asmjit::Label & Label, const char * Symbol) { - m_Symbols.emplace(std::make_pair(SymbolKey, SymbolValue)); -} - -void CX86Ops::RemoveSymbol(const char * SymbolKey) -{ - SymbolMap::iterator itr = m_Symbols.find(SymbolKey); - if (itr != m_Symbols.end()) + NumberSymbolMap::iterator itr = m_LabelSymbols.find(Label.id()); + if (itr != m_LabelSymbols.end()) { - m_Symbols.erase(itr); + __debugbreak(); + } + else + { + m_LabelSymbols.emplace(std::make_pair(Label.id(), NumberSymbol{Symbol, 1})); } } -std::string CX86Ops::VariableSymbol(void * Variable) const +void CX86Ops::AddNumberSymbol(uint32_t Value, const char * Symbol) { - if (int64_t(Variable) < 0) + NumberSymbolMap::iterator itr = m_NumberSymbols.find(Value); + if (itr != m_NumberSymbols.end()) { - return stdstr_f("-0x%0X", (uint32_t)(~(int64_t(Variable)) + 1)); + if (strcmp(itr->second.Symbol.c_str(), Symbol) == 0) + { + itr->second.Count += 1; + } + else + { + __debugbreak(); + } + } + else + { + m_NumberSymbols.emplace(std::make_pair(Value, NumberSymbol{Symbol, 1})); } - return stdstr_f("0x%X", (uint32_t)Variable); } - #endif diff --git a/Source/Project64-core/N64System/Recompiler/x86/x86ops.h b/Source/Project64-core/N64System/Recompiler/x86/x86ops.h index 80ee3463e..8f48d4c38 100644 --- a/Source/Project64-core/N64System/Recompiler/x86/x86ops.h +++ b/Source/Project64-core/N64System/Recompiler/x86/x86ops.h @@ -144,15 +144,21 @@ private: static x86Reg RegValue(const asmjit::x86::Gp & Reg); asmjit::Error _log(const char * data, size_t size) noexcept; - void AddSymbol(const char * SymbolKey, const char * SymbolValue); - void RemoveSymbol(const char * SymbolKey); - std::string VariableSymbol(void * Variable) const; + void AddLabelSymbol(const asmjit::Label & Label, const char * Symbol); + void AddNumberSymbol(uint32_t Value, const char * Symbol); static void BreakPointNotification(const char * FileName, int32_t LineNumber); - typedef std::map SymbolMap; + typedef struct + { + std::string Symbol; + uint32_t Count; + } NumberSymbol; - SymbolMap m_Symbols; + typedef std::map NumberSymbolMap; + + NumberSymbolMap m_LabelSymbols; + NumberSymbolMap m_NumberSymbols; CCodeBlock & m_CodeBlock; };