Play-/Source/ui_qt/DebugSupport/QtDisAsmTableModel.cpp

300 lines
6.8 KiB
C++
Raw Normal View History

2019-12-19 13:51:41 +00:00
#include <QGuiApplication>
#include <QPalette>
2019-12-11 14:52:12 +00:00
#include <QPainter>
#include "QtDisAsmTableModel.h"
#include "string_cast.h"
#include "string_format.h"
#include "lexical_cast_ex.h"
2019-12-20 16:34:18 +00:00
CQtDisAsmTableModel::CQtDisAsmTableModel(QObject* parent, CVirtualMachine& virtualMachine, CMIPS* context, int memSize)
2019-12-11 14:52:12 +00:00
: QAbstractTableModel(parent)
2019-12-19 21:37:47 +00:00
, m_virtualMachine(virtualMachine)
2019-12-11 14:52:12 +00:00
, m_ctx(context)
, m_instructionSize(4)
2019-12-19 22:20:32 +00:00
, m_disAsmType(DISASM_TYPE::DISASM_STANDARD)
2019-12-20 16:34:18 +00:00
, m_memSize(memSize)
2019-12-11 14:52:12 +00:00
{
2019-12-19 21:37:47 +00:00
m_headers = {"S", "Address", "R", "Instr", "I-Mn", "I-Op", "Target"};
2019-12-11 14:52:12 +00:00
2019-12-19 13:51:41 +00:00
auto size = m_start_line.size();
auto start = 2;
auto middle = size.width() / 2;
auto end = size.width() - 2;
QPalette palette = QGuiApplication::palette();
2019-12-11 14:52:12 +00:00
QPen pen;
2019-12-19 13:51:41 +00:00
pen.setWidth(2);
pen.setColor(palette.color(QPalette::WindowText));
2019-12-11 14:52:12 +00:00
pen.setCapStyle(Qt::RoundCap);
pen.setJoinStyle(Qt::MiterJoin);
{
2019-12-19 13:51:41 +00:00
m_start_line.fill(Qt::transparent);
2019-12-11 14:52:12 +00:00
QPainterPath start_path;
2019-12-19 13:51:41 +00:00
start_path.moveTo(middle, end);
start_path.lineTo(middle, middle);
start_path.lineTo(end, middle);
2019-12-11 14:52:12 +00:00
2019-12-19 13:51:41 +00:00
QPainter painter(&m_start_line);
2019-12-11 14:52:12 +00:00
painter.setPen(pen);
painter.drawPath(start_path);
}
{
2019-12-19 13:51:41 +00:00
m_end_line.fill(Qt::transparent);
2019-12-11 14:52:12 +00:00
QPainterPath end_path;
2019-12-19 13:51:41 +00:00
end_path.moveTo(middle, start);
end_path.lineTo(middle, middle);
end_path.lineTo(end, middle);
2019-12-11 14:52:12 +00:00
2019-12-19 13:51:41 +00:00
QPainter painter(&m_end_line);
2019-12-11 14:52:12 +00:00
painter.setPen(pen);
painter.drawPath(end_path);
}
{
2019-12-19 13:51:41 +00:00
m_line.fill(Qt::transparent);
2019-12-11 14:52:12 +00:00
QPainterPath line_path;
2019-12-19 13:51:41 +00:00
line_path.moveTo(middle, start);
line_path.lineTo(middle, end);
2019-12-11 14:52:12 +00:00
2019-12-19 13:51:41 +00:00
QPainter painter(&m_line);
2019-12-11 14:52:12 +00:00
painter.setPen(pen);
painter.drawPath(line_path);
}
2019-12-19 13:51:41 +00:00
QPainterPath arrow_path;
2019-12-11 16:30:43 +00:00
{
2019-12-19 13:51:41 +00:00
m_arrow.fill(Qt::transparent);
2019-12-11 16:30:43 +00:00
2019-12-19 13:51:41 +00:00
arrow_path.moveTo(middle, start);
arrow_path.lineTo(end, middle);
arrow_path.lineTo(middle, end);
2019-12-11 16:30:43 +00:00
2019-12-19 13:51:41 +00:00
arrow_path.lineTo(middle, middle + 3);
2019-12-11 16:30:43 +00:00
2019-12-19 13:51:41 +00:00
arrow_path.lineTo(start, middle + 3);
arrow_path.lineTo(start, middle - 3);
2019-12-11 16:30:43 +00:00
2019-12-19 13:51:41 +00:00
arrow_path.lineTo(middle, middle - 3);
2019-12-12 01:18:27 +00:00
2019-12-19 13:51:41 +00:00
arrow_path.closeSubpath();
2019-12-12 01:18:27 +00:00
2019-12-19 13:51:41 +00:00
QPainter painter(&m_arrow);
pen.setColor(palette.color(QPalette::WindowText));
2019-12-12 01:18:27 +00:00
painter.setPen(pen);
2019-12-19 13:51:41 +00:00
painter.setBrush(QBrush(Qt::yellow));
painter.drawPath(arrow_path);
2019-12-12 01:18:27 +00:00
}
2019-12-19 13:51:41 +00:00
{
m_breakpoint.fill(Qt::transparent);
2019-12-12 01:18:27 +00:00
2019-12-19 13:51:41 +00:00
QPainter painter(&m_breakpoint);
painter.setBrush(QBrush(Qt::darkRed));
painter.setPen(Qt::NoPen);
painter.drawEllipse(QPointF(middle, middle), middle - 1, middle - 1);
}
{
m_breakpoint_arrow.fill(Qt::transparent);
2019-12-12 01:18:27 +00:00
2019-12-19 13:51:41 +00:00
QPainter painter(&m_breakpoint_arrow);
painter.setBrush(QBrush(Qt::darkRed));
painter.setPen(Qt::NoPen);
painter.drawEllipse(QPointF(middle, middle), middle - 1, middle - 1);
2019-12-12 01:18:27 +00:00
2019-12-19 13:51:41 +00:00
painter.setBrush(QBrush(Qt::yellow));
pen.setColor(palette.color(QPalette::WindowText));
2019-12-12 01:18:27 +00:00
painter.setPen(pen);
2019-12-19 13:51:41 +00:00
painter.drawPath(arrow_path);
2019-12-12 01:18:27 +00:00
}
2019-12-11 14:52:12 +00:00
}
int CQtDisAsmTableModel::rowCount(const QModelIndex& /*parent*/) const
{
2019-12-20 16:34:18 +00:00
return m_memSize / m_instructionSize;
2019-12-11 14:52:12 +00:00
}
int CQtDisAsmTableModel::columnCount(const QModelIndex& /*parent*/) const
{
return m_headers.size();
}
QVariant CQtDisAsmTableModel::data(const QModelIndex& index, int role) const
{
// "S", "Address", "R", "I-Mn", "I-Op", "Name",
uint32 address = (index.row() * m_instructionSize);
if(role == Qt::DisplayRole)
{
switch(index.column())
{
2019-12-19 21:37:47 +00:00
case 0: //SYMBOL:
return QVariant();
2019-12-11 14:52:12 +00:00
break;
2019-12-19 21:37:47 +00:00
case 1: //Address:
return string_format(("%08X"), address).c_str();
2019-12-11 14:52:12 +00:00
break;
2019-12-19 21:37:47 +00:00
case 2: //Relation:
return QVariant();
2019-12-11 14:52:12 +00:00
break;
}
2019-12-19 22:20:32 +00:00
auto size = (m_disAsmType == DISASM_TYPE::DISASM_STANDARD) ? 3 : 5;
auto subindex = index.column() - 3;
if(subindex < size)
return GetInstructionDetails(subindex, address).c_str();
else if(subindex == size)
return GetInstructionMetadata(address).c_str();
2019-12-11 14:52:12 +00:00
}
if(role == Qt::SizeHintRole)
{
2019-12-11 16:30:43 +00:00
if(index.column() == 0 || index.column() == 2)
2019-12-11 14:52:12 +00:00
{
2019-12-19 13:51:41 +00:00
auto size = m_line.size();
size.rheight() += 15;
size.rwidth() += 15;
return size;
2019-12-11 14:52:12 +00:00
}
}
if(role == Qt::DecorationRole)
{
2019-12-11 16:30:43 +00:00
switch(index.column())
2019-12-11 14:52:12 +00:00
{
2019-12-19 21:37:47 +00:00
case 0:
{
bool isBreakpoint = (m_ctx->m_breakpoints.find(address) != std::end(m_ctx->m_breakpoints));
bool isPC = (m_virtualMachine.GetStatus() != CVirtualMachine::RUNNING && address == m_ctx->m_State.nPC);
2019-12-12 01:18:27 +00:00
2019-12-19 21:37:47 +00:00
//Draw current instruction m_arrow and m_breakpoint icon
if(isBreakpoint && isPC)
{
return m_breakpoint_arrow;
}
//Draw m_breakpoint icon
if(isBreakpoint)
{
return m_breakpoint;
}
//Draw current instruction m_arrow
if(isPC)
{
return m_arrow;
}
}
break;
case 2:
{
const auto* sub = m_ctx->m_analysis->FindSubroutine(address);
if(sub != nullptr)
{
if(address == sub->start)
2019-12-12 01:18:27 +00:00
{
2019-12-19 21:37:47 +00:00
return m_start_line;
2019-12-12 01:18:27 +00:00
}
2019-12-19 21:37:47 +00:00
else if(address == sub->end)
2019-12-12 01:18:27 +00:00
{
2019-12-19 21:37:47 +00:00
return m_end_line;
2019-12-11 14:52:12 +00:00
}
2019-12-19 21:37:47 +00:00
else
2019-12-11 14:52:12 +00:00
{
2019-12-19 21:37:47 +00:00
return m_line;
2019-12-11 14:52:12 +00:00
}
}
}
2019-12-19 21:37:47 +00:00
}
2019-12-11 14:52:12 +00:00
}
return QVariant();
}
QVariant CQtDisAsmTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(orientation == Qt::Horizontal)
{
if(role == Qt::DisplayRole)
{
if(section < m_headers.size())
{
return m_headers.at(section);
}
}
}
return QAbstractTableModel::headerData(section, orientation, role);
}
void CQtDisAsmTableModel::Redraw()
{
emit QAbstractTableModel::dataChanged(index(0, 0), index(rowCount(), columnCount()));
}
void CQtDisAsmTableModel::Redraw(uint32 address)
{
emit QAbstractTableModel::dataChanged(index(address / m_instructionSize, 0), index(address / m_instructionSize, columnCount()));
}
uint32 CQtDisAsmTableModel::GetInstruction(uint32 address) const
{
//Address translation perhaps?
return m_ctx->m_pMemoryMap->GetInstruction(address);
2019-12-19 22:20:32 +00:00
}
std::string CQtDisAsmTableModel::GetInstructionDetails(int index, uint32 address) const
{
assert((address & 0x07) == 0);
uint32 instruction = GetInstruction(address);
switch(index)
{
2019-12-19 21:37:47 +00:00
case 0:
{
std::string instructionCode = lexical_cast_hex<std::string>(instruction, 8);
return instructionCode;
}
case 1:
{
char disAsm[256];
m_ctx->m_pArch->GetInstructionMnemonic(m_ctx, address + 4, instruction, disAsm, 256);
return disAsm;
}
case 2:
{
char disAsm[256];
m_ctx->m_pArch->GetInstructionOperands(m_ctx, address + 4, instruction, disAsm, 256);
return disAsm;
}
2019-12-19 22:20:32 +00:00
}
2019-12-19 13:51:41 +00:00
return "";
2019-12-19 22:20:32 +00:00
}
std::string CQtDisAsmTableModel::GetInstructionMetadata(uint32 address) const
{
bool commentDrawn = false;
std::string disAsm;
//Draw function name
{
const char* tag = m_ctx->m_Functions.Find(address);
if(tag != nullptr)
{
disAsm += ("@");
disAsm += tag;
commentDrawn = true;
}
}
//Draw target function call if applicable
if(!commentDrawn)
{
uint32 opcode = GetInstruction(address);
if(m_ctx->m_pArch->IsInstructionBranch(m_ctx, address, opcode) == MIPS_BRANCH_NORMAL)
{
uint32 effAddr = m_ctx->m_pArch->GetInstructionEffectiveAddress(m_ctx, address, opcode);
const char* tag = m_ctx->m_Functions.Find(effAddr);
if(tag != nullptr)
{
disAsm += ("-> ");
disAsm += tag;
commentDrawn = true;
}
}
}
return disAsm.c_str();
}