From 2b488048a98a31349e8744007cda09e9d373c83a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Szkud=C5=82apski?= Date: Sat, 12 Jul 2014 20:42:27 +0200 Subject: [PATCH] core: fix missing operand comma and operand label formatting, analyzer disassemble exported and global code ; ui_qt: remove test, add check before displaying invalid cfg ; arch_gb: fix operand formatting and operand types --- src/arch/gb/gameboy_architecture.cpp | 93 ++++++++-------------------- src/arch/gb/gameboy_architecture.hpp | 8 --- src/core/analyzer.cpp | 3 +- src/core/architecture.cpp | 12 ++-- src/ui/qt/ControlFlowGraphScene.cpp | 6 +- src/ui/qt/DisassemblyView.cpp | 6 -- 6 files changed, 41 insertions(+), 87 deletions(-) diff --git a/src/arch/gb/gameboy_architecture.cpp b/src/arch/gb/gameboy_architecture.cpp index bdc9a3b2..ce106c78 100644 --- a/src/arch/gb/gameboy_architecture.cpp +++ b/src/arch/gb/gameboy_architecture.cpp @@ -140,6 +140,33 @@ bool GameBoyArchitecture::Disassemble(BinaryStream const& rBinStrm, TOffset Offs else Result = (this->*m_OpcodeMap[Opcode])(rBinStrm, Offset, rInsn); + // LATER: clean this + for (u8 i = 0; i < OPERAND_NO; ++i) + { + auto& rOperand = *rInsn.Operand(i); + if (rOperand.GetType() & O_REG) + { + switch (rOperand.GetReg()) + { + case GB_RegA: case GB_RegB: case GB_RegC: case GB_RegD: + case GB_RegE: case GB_RegF: case GB_RegH: case GB_RegL: + rOperand.Type() |= O_REG8; break; + case GB_RegAF: case GB_RegBC: case GB_RegDE: case GB_RegHL: + case GB_RegPc: case GB_RegSp: + rOperand.Type() |= O_REG16; break; + default: break; + } + } + if (rOperand.GetType() & O_MEM) + rOperand.Type() |= O_MEM8; + + if (rOperand.GetType() & O_REL) + rOperand.Type() |= O_REL16; + + if (rOperand.GetType() & O_ABS) + rOperand.Type() |= O_ABS16; + } + return Result; } @@ -161,72 +188,6 @@ u16 GameBoyArchitecture::GetRegisterByOpcode(u8 Opcode) } } -bool GameBoyArchitecture::FormatOperand( - Document const& rDoc, - Address const& rAddress, - Instruction const& rInstruction, - Operand const& rOperand, - u8 OperandNo, - PrintData & rPrintData) const -{ - // TODO: Handle this info somewhere... - //if (rOperand.GetType() & O_REG) - //{ - // switch (rOperand.GetReg()) - // { - // case GB_RegA: case GB_RegB: case GB_RegC: case GB_RegD: - // case GB_RegE: case GB_RegF: case GB_RegH: case GB_RegL: - // rOperand.GetType() |= O_REG8; break; - // case GB_RegAF: case GB_RegBC: case GB_RegDE: case GB_RegHL: - // case GB_RegPc: case GB_RegSp: - // rOperand.GetType() |= O_REG16; break; - // default: break; - // } - //} - - rPrintData.MarkOffset(); - - if (rOperand.GetType() & O_MEM) - { - //rOperand.GetType() |= O_MEM8; - rPrintData.AppendOperator("["); - } - - if (rOperand.GetType() & O_REG) - { - for (GameBoyArchitecture::TRegName const* pRegName = m_RegName; - pRegName->m_Value != GB_Invalid_Reg; ++pRegName) - { - if (pRegName->m_Value == rOperand.GetReg()) - { - rPrintData.AppendRegister(pRegName->m_Name); - break; - } - } - } - - u16 Offset = 0; - if (rOperand.GetType() & O_REL) - { - //rOperand.GetType() |= O_REL16; - rPrintData.AppendImmediate(static_cast((Offset + rOperand.GetValue()) & 0xffff), 16); - } - - if (rOperand.GetType() & O_ABS) - { - //rOperand.GetType() |= O_ABS16; - rPrintData.AppendImmediate(static_cast((Offset + rOperand.GetValue()) & 0xffff), 16); - } - - if (rOperand.GetType() & O_IMM) - rPrintData.AppendImmediate(static_cast((Offset + rOperand.GetValue()) & 0xffff), 16); - - if (rOperand.GetType() & O_MEM) - rPrintData.AppendOperator("]"); - - return true; -} - bool GameBoyArchitecture::Insn_Invalid(BinaryStream const& rBinStrm, TOffset Offset, Instruction& rInsn) { rInsn.SetName("invalid"); diff --git a/src/arch/gb/gameboy_architecture.hpp b/src/arch/gb/gameboy_architecture.hpp index 78bf8579..4f7833b1 100644 --- a/src/arch/gb/gameboy_architecture.hpp +++ b/src/arch/gb/gameboy_architecture.hpp @@ -55,14 +55,6 @@ class GameBoyArchitecture : public Architecture virtual CpuContext* MakeCpuContext(void) const { return nullptr; } virtual MemoryContext* MakeMemoryContext(void) const { return new MemoryContext(m_CpuInfo); } - virtual bool FormatOperand( - Document const& rDoc, - Address const& rAddress, - Instruction const& rInstruction, - Operand const& rOperand, - u8 OperandNo, - PrintData & rPrintData) const; - private: typedef bool (GameBoyArchitecture:: *TDisassembler)(BinaryStream const& rBinStrm, TOffset Offset, Instruction& rInsn); diff --git a/src/core/analyzer.cpp b/src/core/analyzer.cpp index 1b4e879c..329eaca7 100644 --- a/src/core/analyzer.cpp +++ b/src/core/analyzer.cpp @@ -531,8 +531,9 @@ void Analyzer::DisassembleAllFunctionsTask::Run(void) { u16 LblType = rLabel.GetType() & Label::CellMask; bool IsExported = ((rLabel.GetType() & Label::AccessMask) == Label::Exported) ? true : false; + bool IsGlobal = ((rLabel.GetType() & Label::AccessMask) == Label::Global) ? true : false; - if (!(LblType == Label::Function || ((LblType == Label::Code) && IsExported))) + if (!(LblType == Label::Function || ((LblType == Label::Code) && (IsExported || IsGlobal)))) return; Log::Write("core") << "disassembling function " << rAddress << LogEnd; diff --git a/src/core/architecture.cpp b/src/core/architecture.cpp index 0ef5bca9..e6e94b56 100644 --- a/src/core/architecture.cpp +++ b/src/core/architecture.cpp @@ -149,7 +149,7 @@ bool Architecture::FormatInstruction( Instruction const& rInsn, PrintData & rPrintData) const { - char Sep = '\0'; + char const* Sep = nullptr; rPrintData.AppendMnemonic(rInsn.GetName()); @@ -164,8 +164,10 @@ bool Architecture::FormatInstruction( if (pOprd->GetType() == O_NONE) break; - if (Sep != '\0') - rPrintData.AppendOperator(",").AppendSpace(); + if (Sep != nullptr) + rPrintData.AppendOperator(Sep).AppendSpace(); + else + Sep = ","; if (!FormatOperand(rDoc, rAddr, rInsn, *pOprd, i, rPrintData)) return false; @@ -256,8 +258,8 @@ bool Architecture::FormatOperand( Address OprdAddr = rDoc.MakeAddress(rOprd.GetSegValue(), rOprd.GetValue()); auto Lbl = rDoc.GetLabelFromAddress(OprdAddr); - if (Lbl.GetType() == Label::Unknown) - rPrintData.AppendImmediate(rOprd.GetValue(), rAddr.GetOffsetSize()); + if (Lbl.GetType() != Label::Unknown) + rPrintData.AppendLabel(Lbl.GetLabel()); else rPrintData.AppendAddress(OprdAddr); } diff --git a/src/ui/qt/ControlFlowGraphScene.cpp b/src/ui/qt/ControlFlowGraphScene.cpp index ff7baeaf..3f9aee5a 100644 --- a/src/ui/qt/ControlFlowGraphScene.cpp +++ b/src/ui/qt/ControlFlowGraphScene.cpp @@ -19,7 +19,11 @@ ControlFlowGraphScene::ControlFlowGraphScene(QObject * parent, medusa::Medusa& c medusa::ControlFlowGraph cfg; - core.BuildControlFlowGraph(cfgAddr, cfg); + if (!core.BuildControlFlowGraph(cfgAddr, cfg)) + { + medusa::Log::Write("ui_qt") << "failed to build CFG for: " << cfgAddr << medusa::LogEnd; + return; + } qreal maxBbWidth = 0.0, maxBbHeight = 0.0; diff --git a/src/ui/qt/DisassemblyView.cpp b/src/ui/qt/DisassemblyView.cpp index 71016426..e15bb28b 100644 --- a/src/ui/qt/DisassemblyView.cpp +++ b/src/ui/qt/DisassemblyView.cpp @@ -653,12 +653,6 @@ void DisassemblyView::setCursorPosition(int x, int y) if (!SetCursor(x, y)) return; - medusa::u8 OperandNo; - if (m_PrintData.GetOperandNo(m_Cursor.m_Address, m_Cursor.m_xAddressOffset, m_Cursor.m_yAddressOffset, OperandNo)) - { - medusa::Log::Write("ui_qt") << "operand no: " << OperandNo << medusa::LogEnd; - } - _cursorTimer.start(); _cursorBlink = false; updateCursor();