File: DialogEditFPU.cpp

package info (click to toggle)
edb-debugger 1.3.0-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,124 kB
  • sloc: cpp: 46,241; xml: 4,998; ansic: 3,088; sh: 52; asm: 33; makefile: 5
file content (174 lines) | stat: -rw-r--r-- 5,549 bytes parent folder | download | duplicates (4)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
Copyright (C) 2015 Ruslan Kabatsayev <b7.10110111@gmail.com>

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 2 of the License, or
(at your option) any later version.

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 <http://www.gnu.org/licenses/>.
*/

#include "DialogEditFPU.h"
#include "EntryGridKeyUpDownEventFilter.h"
#include "Float80Edit.h"
#include "util/Float.h"

#include <QDebug>
#include <QDialogButtonBox>
#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
#include <QRegExp>
#include <QVBoxLayout>
#include <array>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>

namespace ODbgRegisterView {
namespace {

long double readFloat(const QString &strInput, bool &ok) {
	ok = false;
	const QString str(strInput.toLower().trimmed());

	if (const auto value = util::full_string_to_float<long double>(str.toStdString())) {
		ok = true;
		return *value;
	}

	// OK, so either it is invalid/unfinished, or it's some special value
	// We still do want the user to be able to enter common special values
	long double value;

	static const std::array<std::uint8_t, 16> positiveInf{0, 0, 0, 0, 0, 0, 0, 0x80, 0xff, 0x7f, 0, 0, 0, 0, 0, 0};
	static const std::array<std::uint8_t, 16> negativeInf{0, 0, 0, 0, 0, 0, 0, 0x80, 0xff, 0xff, 0, 0, 0, 0, 0, 0};
	static const std::array<std::uint8_t, 16> positiveSNaN{0, 0, 0, 0, 0, 0, 0, 0x90, 0xff, 0x7f, 0, 0, 0, 0, 0, 0};
	static const std::array<std::uint8_t, 16> negativeSNaN{0, 0, 0, 0, 0, 0, 0, 0x90, 0xff, 0xff, 0, 0, 0, 0, 0, 0};

	// Indefinite values are used for QNaN
	static const std::array<std::uint8_t, 16> positiveQNaN{0, 0, 0, 0, 0, 0, 0, 0xc0, 0xff, 0x7f, 0, 0, 0, 0, 0, 0};
	static const std::array<std::uint8_t, 16> negativeQNaN{0, 0, 0, 0, 0, 0, 0, 0xc0, 0xff, 0xff, 0, 0, 0, 0, 0, 0};

	if (str == "+snan" || str == "snan")
		std::memcpy(&value, &positiveSNaN, sizeof(value));
	else if (str == "-snan")
		std::memcpy(&value, &negativeSNaN, sizeof(value));
	else if (str == "+qnan" || str == "qnan" || str == "nan")
		std::memcpy(&value, &positiveQNaN, sizeof(value));
	else if (str == "-qnan")
		std::memcpy(&value, &negativeQNaN, sizeof(value));
	else if (str == "+inf" || str == "inf")
		std::memcpy(&value, &positiveInf, sizeof(value));
	else if (str == "-inf")
		std::memcpy(&value, &negativeInf, sizeof(value));
	else
		return 0;

	ok = true;
	return value;
}
}

DialogEditFPU::DialogEditFPU(QWidget *parent, Qt::WindowFlags f)
	: QDialog(parent, f), floatEntry_(new ODbgRegisterView::Float80Edit(this)), hexEntry_(new QLineEdit(this)) {

	setWindowTitle(tr("Modify Register"));
	setModal(true);
	const auto allContentsGrid = new QGridLayout();

	allContentsGrid->addWidget(new QLabel(tr("Float"), this), 0, 0);
	allContentsGrid->addWidget(new QLabel(tr("Hex"), this), 1, 0);

	allContentsGrid->addWidget(floatEntry_, 0, 1);
	allContentsGrid->addWidget(hexEntry_, 1, 1);

	connect(floatEntry_, &Float80Edit::textEdited, this, &DialogEditFPU::onFloatEdited);
	connect(hexEntry_, &QLineEdit::textEdited, this, &DialogEditFPU::onHexEdited);

	hexEntry_->setValidator(new QRegExpValidator(QRegExp("[0-9a-fA-F ]{,20}"), this));
	connect(floatEntry_, &Float80Edit::defocussed, this, &DialogEditFPU::updateFloatEntry);

	hexEntry_->installEventFilter(this);
	floatEntry_->installEventFilter(this);

	const auto okCancel = new QDialogButtonBox(this);
	okCancel->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Ok);

	connect(okCancel, &QDialogButtonBox::accepted, this, &DialogEditFPU::accept);
	connect(okCancel, &QDialogButtonBox::rejected, this, &DialogEditFPU::reject);

	const auto dialogLayout = new QVBoxLayout(this);
	dialogLayout->addLayout(allContentsGrid);
	dialogLayout->addWidget(okCancel);

	setTabOrder(floatEntry_, hexEntry_);
	setTabOrder(hexEntry_, okCancel);
}

void DialogEditFPU::updateFloatEntry() {
	floatEntry_->setValue(value_);
}

void DialogEditFPU::updateHexEntry() {
	hexEntry_->setText(value_.toHexString());
}

bool DialogEditFPU::eventFilter(QObject *obj, QEvent *event) {
	return entry_grid_key_event_filter(this, obj, event);
}

void DialogEditFPU::setValue(const Register &newReg) {
	reg_   = newReg;
	value_ = reg_.value<edb::value80>();
	updateFloatEntry();
	updateHexEntry();
	setWindowTitle(tr("Modify %1").arg(reg_.name().toUpper()));
	floatEntry_->setFocus(Qt::OtherFocusReason);
}

Register DialogEditFPU::value() const {
	Register ret(reg_);
	ret.setValueFrom(value_);
	return ret;
}

void DialogEditFPU::onHexEdited(const QString &input) {
	QString readable(input.trimmed());
	readable.replace(' ', "");

	while (readable.size() < 20) {
		readable = '0' + readable;
	}

	const auto byteArray = QByteArray::fromHex(readable.toLatin1());
	auto source          = byteArray.constData();
	auto dest            = reinterpret_cast<unsigned char *>(&value_);

	for (std::size_t i = 0; i < sizeof(value_); ++i) {
		dest[i] = source[sizeof(value_) - i - 1];
	}

	updateFloatEntry();
}

void DialogEditFPU::onFloatEdited(const QString &str) {
	bool ok;
	const long double value = readFloat(str, ok);

	if (ok) {
		value_ = edb::value80(value);
	}

	updateHexEntry();
}

}