-
Notifications
You must be signed in to change notification settings - Fork 4
/
annotation_canvas.cpp
161 lines (147 loc) · 5.71 KB
/
annotation_canvas.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/************************************************************************
* Copyright (C) 2020-2021 Matthias Rosenthal
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>
***********************************************************************/
#include "annotation_canvas.hpp"
#include "wxMain.h"
#include "utils/utils.hpp"
#include <iostream>
BEGIN_EVENT_TABLE(AnnotationCanvas, wxWindow)
EVT_PAINT(AnnotationCanvas::OnPaint)
END_EVENT_TABLE()
AnnotationCanvas::AnnotationCanvas(Dis8051Frame* mainFrame, wxWindow *parent, wxWindowID id)
: wxWindow(parent, id, wxDefaultPosition, wxSize(90, 300))
{
this->mainFrame = mainFrame;
}
struct Jump
{
uint64_t from;
uint64_t to;
uint64_t getFirst() const {
return std::min(from, to);
}
uint64_t getLast() const {
return std::max(from, to);
}
};
void appendJump(uint64_t from, uint64_t to, std::vector<std::vector<Jump>> &jumps)
{
Jump newJump{from, to};
bool foundPlane = false;
for(std::size_t iPlane = 0; iPlane < jumps.size(); iPlane++) {
bool planeOk = true;
for(const Jump &jump : jumps[iPlane]) {
if((jump.getFirst() <= newJump.getLast()) && (jump.getLast() >= newJump.getFirst())) {
planeOk = false;
break;
}
}
if(planeOk) {
jumps[iPlane].push_back(newJump);
foundPlane = true;
break;
}
}
if(!foundPlane) {
jumps.push_back(std::vector<Jump>{newJump});
}
}
// this is because wxWidgets DC seems to have difficulties with integer values outside the 16-bit signed region
inline int boundY(int val, int max)
{
if(val < -1000) {
return -1000;
}
if(val > max+1000) {
return max+1000;
}
return val;
}
void AnnotationCanvas::OnPaint(wxPaintEvent &event)
{
const int PLANE_MARGIN = 12;
const int RIGHT_PLANE_MARGIN = 20;
const int RIGHT_MARGIN = 2;
const int ARROW_SIZE = 6;
wxSize ownSize = GetSize();
wxPaintDC dc(this);
int lineBegin, lineEnd;
mainFrame->GetDisassemblyLinesOnScreen(&lineBegin, &lineEnd);
auto itAddressBegin = mainFrame->getDisassembly()->addressByLine.find(lineBegin);
auto itAddressEnd = mainFrame->getDisassembly()->addressByLine.find(lineEnd);
if((itAddressBegin == mainFrame->getDisassembly()->addressByLine.end()) ||
(itAddressEnd == mainFrame->getDisassembly()->addressByLine.end())) {
return;
}
uint64_t addressBegin = itAddressBegin->second;
uint64_t addressEnd = itAddressEnd->second;
auto itInstruction = mainFrame->getDisassembly()->instructions.begin();
while(itInstruction->first < addressBegin) {
itInstruction++;
if(itInstruction == mainFrame->getDisassembly()->instructions.end()) {
return;
}
}
std::vector<std::vector<Jump>> jumps;
while(itInstruction->first < addressEnd) {
if(itInstruction->second.isJump || itInstruction->second.isCondJump) {
if(itInstruction->second.isCall()) {
if(annotateCalls) {
appendJump(itInstruction->first, itInstruction->second.address, jumps);
}
}
else {
appendJump(itInstruction->first, itInstruction->second.address, jumps);
}
}
auto itTarget = mainFrame->getDisassembly()->jumpTargets.find(itInstruction->first);
if(itTarget != mainFrame->getDisassembly()->jumpTargets.end()) {
for(uint64_t sourceAddress : itTarget->second) {
if((sourceAddress < addressBegin) || (sourceAddress >= addressEnd)) {
// otherwise we get / got this also by parsing the instruction
appendJump(sourceAddress, itInstruction->first, jumps);
}
}
}
itInstruction++;
if(itInstruction == mainFrame->getDisassembly()->instructions.end()) {
return;
}
}
wxPen jumpForwardsPen(wxColour(50, 50, 255), 2);
wxPen jumpBackwardsPen(wxColour(0, 0, 200), 2);
for(std::size_t iPlane = 0; iPlane < jumps.size(); iPlane++) {
for(Jump jump : jumps[iPlane]) {
// TODO maybe too unperformant!
int lineFrom = mainFrame->getDisassembly()->findLineByAddress(jump.from);
int lineTo = mainFrame->getDisassembly()->findLineByAddress(jump.to);
if(jump.from <= jump.to) {
dc.SetPen(jumpForwardsPen);
}
else { // jump backwards
dc.SetPen(jumpBackwardsPen);
}
int startX = ownSize.GetWidth() - RIGHT_MARGIN;
int betweenX = ownSize.GetWidth() - RIGHT_PLANE_MARGIN - iPlane * PLANE_MARGIN;
int fromY = boundY((lineFrom - lineBegin) * lineHeight + lineHeight / 2, ownSize.GetHeight());
int toY = boundY((lineTo - lineBegin) * lineHeight + lineHeight / 2, ownSize.GetHeight());
dc.DrawLine(startX, fromY, betweenX, fromY);
dc.DrawLine(betweenX, fromY, betweenX, toY);
dc.DrawLine(betweenX, toY, startX, toY);
dc.DrawLine(startX - ARROW_SIZE, toY - ARROW_SIZE, startX, toY);
dc.DrawLine(startX - ARROW_SIZE, toY + ARROW_SIZE, startX, toY);
}
}
}