| 1 | #include "SymbolTable.h" |
| 2 | #include "Settings.h" |
| 3 | #include "DebuggerData.h" |
| 4 | #include <QFile> |
| 5 | #include <QTextStream> |
| 6 | #include <QStringList> |
| 7 | #include <QRegExp> |
| 8 | #include <QFileInfo> |
| 9 | #include <QXmlStreamReader> |
| 10 | #include <QXmlStreamWriter> |
| 11 | #include <QMap> |
| 12 | #include <algorithm> |
| 13 | #include <cassert> |
| 14 | #include <memory> |
| 15 | #include <optional> |
| 16 | |
| 17 | // class SymbolTable |
| 18 | |
| 19 | SymbolTable::SymbolTable() |
| 20 | { |
| 21 | connect(&fileWatcher, &QFileSystemWatcher::fileChanged, |
| 22 | this, &SymbolTable::fileChanged); |
| 23 | } |
| 24 | |
| 25 | Symbol* SymbolTable::add(std::unique_ptr<Symbol> symbol) |
| 26 | { |
| 27 | auto* p = symbol.get(); |
| 28 | symbols.push_back(std::move(symbol)); |
| 29 | p->table = this; |
| 30 | mapSymbol(p); |
| 31 | return p; |
| 32 | } |
| 33 | |
| 34 | std::unique_ptr<Symbol> SymbolTable::removeAt(size_t index) |
| 35 | { |
| 36 | assert(index < symbols.size()); |
| 37 | auto symbol = std::move(symbols[index]); |
| 38 | symbols.erase(symbols.begin() + index); // TODO optimize: swap with back(), then pop_back() |
| 39 | unmapSymbol(symbol.get()); |
| 40 | return symbol; |
| 41 | } |
| 42 | |
| 43 | std::unique_ptr<Symbol> SymbolTable::remove(Symbol* symbol) |
| 44 | { |
| 45 | auto it = std::find_if(symbols.begin(), symbols.end(), |
| 46 | [&](auto& e) { return e.get() == symbol; }); |
| 47 | if (it == symbols.end()) return {}; |
| 48 | |
| 49 | return removeAt(std::distance(symbols.begin(), it)); |
| 50 | } |
| 51 | |
| 52 | void SymbolTable::clear() |
| 53 | { |
| 54 | addressSymbols.clear(); |
| 55 | valueSymbols.clear(); |
| 56 | symbols.clear(); |
| 57 | } |
| 58 | |
| 59 | int SymbolTable::size() const |
| 60 | { |
| 61 | return symbols.size(); |
| 62 | } |
| 63 | |
| 64 | void SymbolTable::mapSymbol(Symbol* symbol) |
| 65 | { |
| 66 | if (symbol->type() != Symbol::VALUE) { |
| 67 | addressSymbols.insert(symbol->value(), symbol); |
| 68 | } |
| 69 | if (symbol->type() != Symbol::JUMPLABEL) { |
| 70 | valueSymbols.insert(symbol->value(), symbol); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void SymbolTable::unmapSymbol(Symbol* symbol) |
| 75 | { |
| 76 | QMutableMapIterator<int, Symbol*> i(addressSymbols); |
| 77 | while (i.hasNext()) { |
| 78 | i.next(); |
| 79 | if (i.value() == symbol) i.remove(); |
| 80 | } |
| 81 | QMutableHashIterator<int, Symbol*> j(valueSymbols); |
| 82 | while (j.hasNext()) { |
| 83 | j.next(); |
| 84 | if (j.value() == symbol) j.remove(); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void SymbolTable::symbolTypeChanged(Symbol* symbol) |
| 89 | { |
| 90 | unmapSymbol(symbol); |
| 91 | mapSymbol(symbol); |
| 92 | } |
| 93 | |
| 94 | void SymbolTable::symbolValueChanged(Symbol* symbol) |
| 95 | { |
| 96 | unmapSymbol(symbol); |
| 97 | mapSymbol(symbol); |
| 98 | } |
| 99 | |
| 100 | Symbol* SymbolTable::findFirstAddressSymbol(int addr, MemoryLayout* ml) |
| 101 | { |
| 102 | for (currentAddress = addressSymbols.begin(); |
| 103 | currentAddress != addressSymbols.end(); ++currentAddress) { |
| 104 | if ((*currentAddress)->value() >= addr) { |
| 105 | if ((*currentAddress)->isSlotValid(ml)) { |
| 106 | return *currentAddress; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | return nullptr; |
| 111 | } |
| 112 | |
| 113 | Symbol* SymbolTable::getCurrentAddressSymbol() |
| 114 | { |
| 115 | return currentAddress != addressSymbols.end() ? *currentAddress : nullptr; |
| 116 | } |
| 117 | |
| 118 | Symbol* SymbolTable::findNextAddressSymbol(MemoryLayout* ml) |
| 119 | { |
| 120 | for (++currentAddress; currentAddress != addressSymbols.end(); |
| 121 | ++currentAddress) { |
| 122 | if ((*currentAddress)->isSlotValid(ml)) { |
| 123 | return *currentAddress; |
| 124 | } |
| 125 | } |
| 126 | return nullptr; |
| 127 | } |
| 128 | |
| 129 | Symbol* SymbolTable::getValueSymbol(int val, Symbol::Register reg, MemoryLayout* ml) |
| 130 | { |
| 131 | for (auto it = valueSymbols.find(val); it != valueSymbols.end() && it.key() == val; ++it) { |
| 132 | if ((it.value()->validRegisters() & reg) && |
| 133 | it.value()->isSlotValid(ml)) { |
| 134 | return it.value(); |
| 135 | } |
| 136 | } |
| 137 | return nullptr; |
| 138 | } |
| 139 | |
| 140 | Symbol* SymbolTable::getAddressSymbol(int addr, MemoryLayout* ml) |
| 141 | { |
| 142 | for (auto it = addressSymbols.find(addr); it != addressSymbols.end() && it.key() == addr; ++it) { |
| 143 | if (it.value()->isSlotValid(ml)) { |
| 144 | return it.value(); |
| 145 | } |
| 146 | } |
| 147 | return nullptr; |
| 148 | } |
| 149 | |
| 150 | Symbol* SymbolTable::getAddressSymbol(const QString& label, bool case_sensitive) |
| 151 | { |
| 152 | for (auto it = addressSymbols.begin(); it != addressSymbols.end(); ++it) { |
| 153 | if (it.value()->text().compare(label, Qt::CaseSensitive)==0) |
| 154 | return it.value(); |
| 155 | if (!case_sensitive && it.value()->text().compare(label, Qt::CaseInsensitive)==0) |
| 156 | return it.value(); |
| 157 | } |
| 158 | return nullptr; |
| 159 | } |
| 160 | |
| 161 | QStringList SymbolTable::labelList(bool include_vars, const MemoryLayout* ml) const |
| 162 | { |
| 163 | QStringList labels; |
| 164 | for (auto it = addressSymbols.begin(); it != addressSymbols.end(); ++it) { |
| 165 | if (it.value()->type() == Symbol::JUMPLABEL || (include_vars && it.value()->type() == Symbol::VARIABLELABEL)) { |
| 166 | if (ml == nullptr || it.value()->isSlotValid(ml)) { |
| 167 | labels << it.value()->text(); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | return labels; |
| 172 | } |
| 173 | |
| 174 | int SymbolTable::symbolFilesSize() const |
| 175 | { |
| 176 | return symbolFiles.size(); |
| 177 | } |
| 178 | |
| 179 | const QString& SymbolTable::symbolFile(int index) const |
| 180 | { |
| 181 | return symbolFiles.at(index).fileName; |
| 182 | } |
| 183 | |
| 184 | const QDateTime& SymbolTable::symbolFileRefresh(int index) const |
| 185 | { |
| 186 | return symbolFiles.at(index).refreshTime; |
| 187 | } |
| 188 | |
| 189 | bool SymbolTable::readFile(const QString& filename, FileType type) |
| 190 | { |
| 191 | QString fname = filename.toLower(); |
| 192 | |
| 193 | if (type == DETECT_FILE) { |
| 194 | if (fname.endsWith(".omds")) { |
| 195 | // OpenMSX Debugger session file |
| 196 | type = OMDS_FILE; |
| 197 | } else if (fname.endsWith(".noi")) { |
| 198 | // NoICE command file |
| 199 | type = NOICE_FILE; |
| 200 | } else if (fname.endsWith(".map")) { |
| 201 | // HiTech link map file |
| 202 | type = LINKMAP_FILE; |
| 203 | } else if (fname.endsWith(".sym")) { |
| 204 | // auto detect which sym file |
| 205 | QFile file(filename); |
| 206 | if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { |
| 207 | QTextStream in(&file); |
| 208 | QString line = in.readLine(); |
| 209 | if (line[0] == ';') { |
| 210 | type = ASMSX_FILE; |
| 211 | } else if (line.contains("; last def. pass")) { |
| 212 | type = TNIASM0_FILE; |
| 213 | } else if (line.contains(": %equ ")) { |
| 214 | type = TNIASM1_FILE; |
| 215 | } else if (line.contains(": equ ", Qt::CaseInsensitive)) { |
| 216 | type = SJASM_FILE; |
| 217 | } else if (line.contains("Sections:", Qt::CaseInsensitive)) { |
| 218 | type = VASM_FILE; |
| 219 | } else { |
| 220 | // this is a blunt conclusion but I |
| 221 | // don't know a way to detect this file |
| 222 | // type |
| 223 | type = HTC_FILE; |
| 224 | } |
| 225 | } |
| 226 | } else if (fname.endsWith(".symbol") || fname.endsWith(".publics") || fname.endsWith(".sys")) { |
| 227 | /* They are the same type of file. For some reason the Debian |
| 228 | * manpage uses the extension ".sys" |
| 229 | * pasmo doc -> pasmo [options] file.asm file.bin [file.symbol [file.publics] ] |
| 230 | * pasmo manpage in Debian -> pasmo [options] file.asm file.bin [file.sys] |
| 231 | */ |
| 232 | type = PASMO_FILE; |
| 233 | } |
| 234 | } |
| 235 | switch (type) { |
| 236 | case OMDS_FILE: |
| 237 | return readOMDSFile(filename); |
| 238 | case TNIASM0_FILE: |
| 239 | return readTNIASM0File(filename); |
| 240 | case TNIASM1_FILE: |
| 241 | return readTNIASM1File(filename); |
| 242 | case SJASM_FILE: |
| 243 | return readSJASMFile(filename); |
| 244 | case ASMSX_FILE: |
| 245 | return readASMSXFile(filename); |
| 246 | case HTC_FILE: |
| 247 | return readHTCFile(filename); |
| 248 | case LINKMAP_FILE: |
| 249 | return readLinkMapFile(filename); |
| 250 | case NOICE_FILE: |
| 251 | return readNoICEFile(filename); |
| 252 | case PASMO_FILE: |
| 253 | return readPASMOFile(filename); |
| 254 | case VASM_FILE: |
| 255 | return readVASMFile(filename); |
| 256 | default: |
| 257 | return false; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | void SymbolTable::appendFile(const QString& file, FileType type) |
| 262 | { |
| 263 | SymbolFileRecord rec; |
| 264 | rec.fileName = file; |
| 265 | rec.fileType = type; |
| 266 | rec.refreshTime = QDateTime::currentDateTime(); |
| 267 | symbolFiles.append(rec); |
| 268 | fileWatcher.addPath(file); |
| 269 | } |
| 270 | |
| 271 | // Universal value parsing routine. Accepts: |
| 272 | // - 0123h, 1234h, 1234H (hex) |
| 273 | // - 0x1234 (hex) |
| 274 | // - 1234 (dec) |
| 275 | // - 0123 (oct) |
| 276 | // The string may (optionally) end with a '; comment' part (with or without |
| 277 | // whitespace around the ';' character). |
| 278 | static std::optional<int> parseValue(const QString& str) |
| 279 | { |
| 280 | QStringList l = str.split(";"); // ignore stuff after ';' |
| 281 | QString s = l.at(0).trimmed(); |
| 282 | bool success; |
| 283 | int result; |
| 284 | if (s.endsWith('h', Qt::CaseInsensitive)) { |
| 285 | s.chop(1); |
| 286 | result = s.toInt(&success, 16); |
| 287 | } else { |
| 288 | result = s.toInt(&success, 0); // any base (e.g. 0x..) |
| 289 | } |
| 290 | if (success) return result; |
| 291 | return {}; |
| 292 | } |
| 293 | |
| 294 | bool SymbolTable::readSymbolFile( |
| 295 | const QString& filename, FileType type, const QString& equ) |
| 296 | { |
| 297 | QFile file(filename); |
| 298 | if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | appendFile(filename, type); |
| 303 | const auto* source = &symbolFiles.back().fileName; |
| 304 | |
| 305 | QTextStream in(&file); |
| 306 | while (!in.atEnd()) { |
| 307 | QString line = in.readLine(); |
| 308 | QStringList l = line.split(equ, Qt::SplitBehaviorFlags::KeepEmptyParts, Qt::CaseInsensitive); |
| 309 | if (l.size() != 2) continue; |
| 310 | if (auto value = parseValue(l.at(1))) { |
| 311 | add(std::make_unique<Symbol>(l.at(0), *value, source)); |
| 312 | } |
| 313 | } |
| 314 | return true; |
| 315 | } |
| 316 | bool SymbolTable::readOMDSFile(const QString& filename) |
| 317 | { |
| 318 | QFile file(filename); |
| 319 | if (!file.open(QFile::ReadOnly | QFile::Text)) { |
| 320 | return false; |
| 321 | } |
| 322 | |
| 323 | QXmlStreamReader ses; |
| 324 | ses.setDevice(&file); |
| 325 | loadSymbols(ses); |
| 326 | return true; |
| 327 | } |
| 328 | bool SymbolTable::readTNIASM0File(const QString& filename) |
| 329 | { |
| 330 | return readSymbolFile(filename, TNIASM0_FILE, ": equ "); |
| 331 | } |
| 332 | bool SymbolTable::readTNIASM1File(const QString& filename) |
| 333 | { |
| 334 | return readSymbolFile(filename, TNIASM1_FILE, ": %equ "); |
| 335 | } |
| 336 | bool SymbolTable::readSJASMFile(const QString& filename) |
| 337 | { |
| 338 | return readSymbolFile(filename, SJASM_FILE, ": equ "); |
| 339 | } |
| 340 | |
| 341 | bool SymbolTable::readASMSXFile(const QString& filename) |
| 342 | { |
| 343 | QFile file(filename); |
| 344 | if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | appendFile(filename, ASMSX_FILE); |
| 349 | const auto* source = &symbolFiles.back().fileName; |
| 350 | |
| 351 | QTextStream in(&file); |
| 352 | int filePart = 0; |
| 353 | while (!in.atEnd()) { |
| 354 | QString line = in.readLine(); |
| 355 | if (line[0] == ';') { |
| 356 | if (line.startsWith("; global and local")) { |
| 357 | filePart = 1; |
| 358 | } else if (line.startsWith("; other")) { |
| 359 | filePart = 2; |
| 360 | } |
| 361 | } else { |
| 362 | if ((line[0] == '$') || (line[4] == 'h') || |
| 363 | (line[5] == 'h') || (line[8] == 'h')) { |
| 364 | if (filePart == 1) { |
| 365 | QStringList l = line.split(" "); |
| 366 | std::unique_ptr<Symbol> sym; |
| 367 | if (line[0] == '$') { |
| 368 | add(std::make_unique<Symbol>(l.at(1).trimmed(), l.at(0).right(4).toInt(nullptr, 16), source)); |
| 369 | } else if ((line[4] == 'h') || (line[5] == 'h')) { |
| 370 | add(std::make_unique<Symbol>(l.at(1).trimmed(), l.at(0).mid(l.at(0).indexOf('h') - 4, 4).toInt(nullptr, 16), source)); |
| 371 | } else { |
| 372 | QStringList n = l.at(0).split(":"); // n.at(0) = MegaROM page |
| 373 | add(std::make_unique<Symbol>(l.at(1).trimmed(), n.at(1).left(4).toInt(nullptr, 16), source)); |
| 374 | } |
| 375 | } else if (filePart == 2) { |
| 376 | // |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | return true; |
| 382 | } |
| 383 | |
| 384 | bool SymbolTable::readPASMOFile(const QString& filename) |
| 385 | { |
| 386 | QFile file(filename); |
| 387 | if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { |
| 388 | return false; |
| 389 | } |
| 390 | appendFile(filename, PASMO_FILE); |
| 391 | const auto* source = &symbolFiles.back().fileName; |
| 392 | |
| 393 | QTextStream in(&file); |
| 394 | while (!in.atEnd()) { |
| 395 | QString line; |
| 396 | QStringList l; |
| 397 | line = in.readLine(); |
| 398 | l = line.split(QRegExp("(\t+)|( +)")); |
| 399 | if (l.size() == 3) { |
| 400 | add(std::make_unique<Symbol>(l.at(0), l.at(2).left(5).toInt(nullptr, 16), source)); |
| 401 | } |
| 402 | } |
| 403 | return true; |
| 404 | } |
| 405 | |
| 406 | bool SymbolTable::readVASMFile(const QString& filename) { |
| 407 | QFile file(filename); |
| 408 | if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { |
| 409 | return false; |
| 410 | } |
| 411 | appendFile(filename, VASM_FILE); |
| 412 | const auto* source = &symbolFiles.back().fileName; |
| 413 | |
| 414 | bool skipline = true; |
| 415 | |
| 416 | QTextStream in(&file); |
| 417 | while (!in.atEnd()) { |
| 418 | QString line; |
| 419 | QStringList l; |
| 420 | line = in.readLine(); |
| 421 | if (line.startsWith("Symbols by value:")) |
| 422 | skipline = false; |
| 423 | if (skipline) |
| 424 | continue; |
| 425 | l = line.split(QRegExp("(\t+)|( +)")); |
| 426 | if (l.size() == 2) { |
| 427 | add(std::make_unique<Symbol>(l.at(1), l.at(0).toInt(nullptr, 16), source)); |
| 428 | } |
| 429 | } |
| 430 | return true; |
| 431 | } |
| 432 | |
| 433 | bool SymbolTable::readHTCFile(const QString& filename) |
| 434 | { |
| 435 | QFile file(filename); |
| 436 | if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { |
| 437 | return false; |
| 438 | } |
| 439 | |
| 440 | appendFile(filename, HTC_FILE); |
| 441 | const auto* source = &symbolFiles.back().fileName; |
| 442 | |
| 443 | QTextStream in(&file); |
| 444 | while (!in.atEnd()) { |
| 445 | QString line = in.readLine(); |
| 446 | QStringList l = line.split(' '); |
| 447 | if (l.size() != 3) continue; |
| 448 | if (auto value = parseValue("0x" + l.at(1))) { |
| 449 | add(std::make_unique<Symbol>(l.at(0), *value, source)); |
| 450 | } |
| 451 | } |
| 452 | return true; |
| 453 | } |
| 454 | |
| 455 | bool SymbolTable::readNoICEFile(const QString& filename) |
| 456 | { |
| 457 | QFile file(filename); |
| 458 | if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { |
| 459 | return false; |
| 460 | } |
| 461 | |
| 462 | appendFile(filename, NOICE_FILE); |
| 463 | const auto* source = &symbolFiles.back().fileName; |
| 464 | |
| 465 | QTextStream in(&file); |
| 466 | while (!in.atEnd()) { |
| 467 | QString line = in.readLine(); |
| 468 | QStringList l = line.split(" ", Qt::SplitBehaviorFlags::KeepEmptyParts, Qt::CaseInsensitive); |
| 469 | if (l.size() != 3) continue; |
| 470 | if (l.at(0).toLower() != "def") continue; |
| 471 | if (auto value = parseValue(l.at(2))) { |
| 472 | add(std::make_unique<Symbol>(l.at(1), *value, source)); |
| 473 | } |
| 474 | } |
| 475 | return true; |
| 476 | } |
| 477 | |
| 478 | bool SymbolTable::readLinkMapFile(const QString& filename) |
| 479 | { |
| 480 | const QString magic("Machine type"); |
| 481 | const QString tableStart("Symbol Table"); |
| 482 | |
| 483 | QRegExp rx(" [0-9A-Fa-f]{4} (?![ 0-9])"); |
| 484 | QRegExp rp("^([^ ]+) +[^ ]* +([0-9A-Fa-f]{4}) $"); |
| 485 | |
| 486 | QFile file(filename); |
| 487 | if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { |
| 488 | return false; |
| 489 | } |
| 490 | appendFile(filename, LINKMAP_FILE); |
| 491 | const auto* source = &symbolFiles.back().fileName; |
| 492 | |
| 493 | QTextStream in(&file); |
| 494 | if (in.atEnd()) return false; |
| 495 | while (true) { |
| 496 | if (in.atEnd()) return false; |
| 497 | if (in.readLine().startsWith(magic)) break; |
| 498 | } |
| 499 | while (true) { |
| 500 | if (in.atEnd()) return false; |
| 501 | if (in.readLine().contains(tableStart)) break; |
| 502 | } |
| 503 | |
| 504 | while (!in.atEnd()) { |
| 505 | QString line = in.readLine(); |
| 506 | Q_ASSERT(!line.endsWith("\r")); |
| 507 | if (line.isEmpty()) continue; |
| 508 | |
| 509 | line += " "; |
| 510 | int len = line.length(); |
| 511 | int l; |
| 512 | int pos = 0; |
| 513 | bool ok = false; |
| 514 | // HiTech uses multiple columns of non-fixed width and |
| 515 | // a column for psect may be blank so the address may in |
| 516 | // the first or second match. |
| 517 | for (int tries = 0; (tries < 2) && !ok; ++tries) { |
| 518 | pos = rx.indexIn(line, pos); |
| 519 | l = pos + rx.matchedLength(); |
| 520 | if ((l > 0) && (len % l) == 0) { |
| 521 | ok = true; |
| 522 | for (int posn = pos + l; (posn < len) && ok; posn += l) { |
| 523 | ok = (posn == rx.indexIn(line, posn)); |
| 524 | } |
| 525 | } |
| 526 | pos = l - 1; |
| 527 | } |
| 528 | if (!ok) continue; |
| 529 | |
| 530 | for (pos = 0; pos < len; pos += l) { |
| 531 | QString part = line.mid(pos, l); |
| 532 | if (rp.indexIn(part) == 0) { |
| 533 | QStringList l = rp.capturedTexts(); |
| 534 | add(std::make_unique<Symbol>(l.at(1), l.last().toInt(nullptr, 16), source)); |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | return true; |
| 539 | } |
| 540 | |
| 541 | void SymbolTable::fileChanged(const QString& path) |
| 542 | { |
| 543 | emit symbolFileChanged(); |
| 544 | if (QFile::exists(path)) { |
| 545 | fileWatcher.addPath(path); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | void SymbolTable::reloadFiles() |
| 550 | { |
| 551 | for (int i = 0; i < symbolFiles.size(); ++i) { |
| 552 | // check if file is newer |
| 553 | QFileInfo fi = QFileInfo(symbolFiles[i].fileName); |
| 554 | if (fi.lastModified() <= symbolFiles[i].refreshTime) continue; |
| 555 | |
| 556 | // file info |
| 557 | QString name = symbolFiles[i].fileName; |
| 558 | FileType type = symbolFiles[i].fileType; |
| 559 | // symbol file is newer |
| 560 | QMap<QString, Symbol> symCopy; |
| 561 | // copy all symbols originating from this file |
| 562 | for (const auto& s : symbols) { |
| 563 | if (s->source() == &symbolFiles[i].fileName) { |
| 564 | symCopy.insert(s->text(), Symbol(*s)); |
| 565 | } |
| 566 | } |
| 567 | // remove existing file |
| 568 | unloadFile(name); |
| 569 | // read the new file |
| 570 | readFile(name, type); |
| 571 | // find old symbols in newly loaded file |
| 572 | QString* newFile = &symbolFiles.back().fileName; |
| 573 | for (auto& s : symbols) { |
| 574 | if (s->source() != newFile) continue; |
| 575 | |
| 576 | // find symbol in old list |
| 577 | auto sit = symCopy.find(s->text()); |
| 578 | if (sit == symCopy.end()) continue; |
| 579 | |
| 580 | // symbol existed before, copy settings |
| 581 | s->setValidSlots(sit->validSlots()); |
| 582 | s->setValidRegisters(sit->validRegisters()); |
| 583 | s->setType(sit->type()); |
| 584 | s->setStatus((sit->status() == Symbol::LOST) ? Symbol::ACTIVE : sit->status()); |
| 585 | symCopy.erase(sit); |
| 586 | } |
| 587 | if (Settings::get().preserveLostSymbols()) { |
| 588 | // all symbols left in map are lost |
| 589 | for (auto sit = symCopy.begin(); sit != symCopy.end(); ++sit) { |
| 590 | auto* sym = add(std::make_unique<Symbol>(sit.value())); |
| 591 | sym->setStatus(Symbol::LOST); |
| 592 | sym->setSource(newFile); |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | void SymbolTable::unloadFile(const QString& file, bool keepSymbols) |
| 599 | { |
| 600 | int index = -1; |
| 601 | for (int i = 0; i < symbolFiles.size(); ++i) { |
| 602 | if (symbolFiles[i].fileName == file) { |
| 603 | index = i; |
| 604 | break; |
| 605 | } |
| 606 | } |
| 607 | if (index >= 0) { |
| 608 | QString* name = &symbolFiles[index].fileName; |
| 609 | |
| 610 | if (!keepSymbols) { |
| 611 | // remove symbols from address map |
| 612 | QMutableMapIterator<int, Symbol*> mi(addressSymbols); |
| 613 | while (mi.hasNext()) { |
| 614 | mi.next(); |
| 615 | if (mi.value()->source() == name) mi.remove(); |
| 616 | } |
| 617 | // remove symbols from value hash |
| 618 | QMutableHashIterator<int, Symbol*> hi(valueSymbols); |
| 619 | while (hi.hasNext()) { |
| 620 | hi.next(); |
| 621 | if (hi.value()->source() == name) hi.remove(); |
| 622 | } |
| 623 | } |
| 624 | // remove symbols from value hash |
| 625 | symbols.erase(std::remove_if(symbols.begin(), symbols.end(), |
| 626 | [&](auto& sym) { |
| 627 | if (sym->source() != name) { |
| 628 | return false; // keep symbols from different source |
| 629 | } |
| 630 | if (!keepSymbols) { |
| 631 | return true; // remove |
| 632 | } |
| 633 | // keep but clear source |
| 634 | sym->setSource(nullptr); |
| 635 | return false; // keep |
| 636 | }), symbols.end()); |
| 637 | // remove record |
| 638 | fileWatcher.removePath(symbolFiles[index].fileName); |
| 639 | symbolFiles.removeAt(index); |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | /* |
| 644 | * Session loading/saving |
| 645 | */ |
| 646 | void SymbolTable::saveSymbols(QXmlStreamWriter& xml) |
| 647 | { |
| 648 | // write files |
| 649 | QMap<const QString*, int> fileIds; |
| 650 | for (int i = 0; i < symbolFiles.size(); ++i) { |
| 651 | // add id mapping |
| 652 | fileIds[&symbolFiles[i].fileName] = i; |
| 653 | // write element |
| 654 | xml.writeStartElement("SymbolFile"); |
| 655 | switch (symbolFiles[i].fileType) { |
| 656 | case TNIASM0_FILE: |
| 657 | xml.writeAttribute("type","tniasm0"); |
| 658 | break; |
| 659 | case TNIASM1_FILE: |
| 660 | xml.writeAttribute("type","tniasm1"); |
| 661 | break; |
| 662 | case ASMSX_FILE: |
| 663 | xml.writeAttribute("type","asmsx"); |
| 664 | break; |
| 665 | case LINKMAP_FILE: |
| 666 | xml.writeAttribute("type","linkmap"); |
| 667 | break; |
| 668 | default: |
| 669 | break; |
| 670 | } |
| 671 | xml.writeAttribute("refreshTime", |
| 672 | QString::number(symbolFiles[i].refreshTime.toTime_t())); |
| 673 | xml.writeCharacters(symbolFiles[i].fileName); |
| 674 | xml.writeEndElement(); |
| 675 | } |
| 676 | // write symbols |
| 677 | for (const auto& sym : symbols) { |
| 678 | xml.writeStartElement("Symbol"); |
| 679 | // status |
| 680 | if (sym->status() == Symbol::HIDDEN) { |
| 681 | xml.writeAttribute("status", "hidden"); |
| 682 | } else if (sym->status() == Symbol::LOST) { |
| 683 | xml.writeAttribute("status", "lost"); |
| 684 | } |
| 685 | // type |
| 686 | if (sym->type() == Symbol::JUMPLABEL) { |
| 687 | xml.writeTextElement("type", "jump"); |
| 688 | } else if (sym->type() == Symbol::VARIABLELABEL) { |
| 689 | xml.writeTextElement("type", "variable"); |
| 690 | } else { |
| 691 | xml.writeTextElement("type", "value"); |
| 692 | } |
| 693 | // text, value, slots and registers |
| 694 | xml.writeTextElement("name", sym->text()); |
| 695 | xml.writeTextElement("value", QString::number(sym->value())); |
| 696 | xml.writeTextElement("validSlots", QString::number(sym->validSlots())); |
| 697 | xml.writeTextElement("validRegisters", QString::number(sym->validRegisters())); |
| 698 | // write source filename |
| 699 | if (sym->source()) { |
| 700 | xml.writeTextElement("source", QString::number(fileIds[sym->source()])); |
| 701 | } |
| 702 | // complete |
| 703 | xml.writeEndElement(); |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | void SymbolTable::loadSymbols(QXmlStreamReader& xml) |
| 708 | { |
| 709 | Symbol* sym = nullptr; |
| 710 | while (!xml.atEnd()) { |
| 711 | xml.readNext(); |
| 712 | // exit if closing of main tag |
| 713 | if (xml.isEndElement() && xml.name() == "Symbols") break; |
| 714 | |
| 715 | // begin tag |
| 716 | if (xml.isStartElement()) { |
| 717 | if (xml.name() == "SymbolFile") { |
| 718 | // read attributes and text |
| 719 | QString ftype = xml.attributes().value("type").toString().toLower(); |
| 720 | QString rtime = xml.attributes().value("refreshTime").toString(); |
| 721 | QString fname = xml.readElementText(); |
| 722 | // check type |
| 723 | FileType type = TNIASM0_FILE; |
| 724 | if (ftype == "tniasm1") { |
| 725 | type = TNIASM1_FILE; |
| 726 | } else if (ftype == "asmsx") { |
| 727 | type = ASMSX_FILE; |
| 728 | } else if (ftype == "linkmap") { |
| 729 | type = LINKMAP_FILE; |
| 730 | } |
| 731 | // append file |
| 732 | appendFile(fname, type); |
| 733 | // change time |
| 734 | symbolFiles.back().refreshTime.setTime_t(rtime.toUInt()); |
| 735 | |
| 736 | } else if (xml.name() == "Symbol") { |
| 737 | // add empty symbol |
| 738 | sym = add(std::make_unique<Symbol>("", 0)); |
| 739 | // get status attribute |
| 740 | QString stat = xml.attributes().value("status").toString().toLower(); |
| 741 | if (stat == "hidden") { |
| 742 | sym->setStatus(Symbol::HIDDEN); |
| 743 | } else if (stat == "lost") { |
| 744 | sym->setStatus(Symbol::LOST); |
| 745 | } |
| 746 | |
| 747 | } else if (sym && xml.name() == "type") { |
| 748 | // read symbol type element |
| 749 | QString type = xml.readElementText().trimmed().toLower(); |
| 750 | if (type == "jump") { |
| 751 | sym->setType(Symbol::JUMPLABEL); |
| 752 | } else if (type == "variable") { |
| 753 | sym->setType(Symbol::VARIABLELABEL); |
| 754 | } else if (type == "value") { |
| 755 | sym->setType(Symbol::VALUE); |
| 756 | } |
| 757 | |
| 758 | } else if (sym && xml.name() == "name") { |
| 759 | // read symbol name |
| 760 | sym->setText(xml.readElementText()); |
| 761 | |
| 762 | } else if (sym && xml.name() == "value") { |
| 763 | // read symbol value |
| 764 | sym->setValue(xml.readElementText().toInt()); |
| 765 | |
| 766 | } else if (sym && xml.name() == "validSlots") { |
| 767 | // read numeric valid slot mask |
| 768 | sym->setValidSlots(xml.readElementText().toInt()); |
| 769 | |
| 770 | } else if (sym && xml.name() == "validRegisters") { |
| 771 | // read numeric valid registers mask |
| 772 | sym->setValidRegisters(xml.readElementText().toInt()); |
| 773 | |
| 774 | } else if (sym && xml.name() == "source") { |
| 775 | // read source file id |
| 776 | int id = xml.readElementText().toInt(); |
| 777 | if (id >= 0 && id < symbolFiles.size()) { |
| 778 | sym->setSource(&symbolFiles[id].fileName); |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | |
| 786 | // class Symbol |
| 787 | |
| 788 | Symbol::Symbol(QString str, int addr, const QString* source) |
| 789 | : symText(std::move(str)), symValue(addr), symSource(source) |
| 790 | { |
| 791 | symRegisters = (addr & 0xFF00) ? REG_ALL16 : REG_ALL; |
| 792 | } |
| 793 | |
| 794 | Symbol::Symbol(const Symbol& symbol) |
| 795 | { |
| 796 | table = nullptr; |
| 797 | symStatus = symbol.symStatus; |
| 798 | symText = symbol.symText; |
| 799 | symValue = symbol.symValue; |
| 800 | symSlots = symbol.symSlots; |
| 801 | //symSegments = symbol.symSegments; |
| 802 | symRegisters = symbol.symRegisters; |
| 803 | symSource = symbol.symSource; |
| 804 | symType = symbol.symType; |
| 805 | } |
| 806 | |
| 807 | void Symbol::setValue(int addr) |
| 808 | { |
| 809 | if (addr == symValue) return; |
| 810 | |
| 811 | symValue = addr; |
| 812 | if (table) table->symbolValueChanged(this); |
| 813 | } |
| 814 | |
| 815 | void Symbol::setValidRegisters(int regs) |
| 816 | { |
| 817 | symRegisters = regs; |
| 818 | if (symValue & 0xFF00) { |
| 819 | symRegisters &= REG_ALL16; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | void Symbol::setType(SymbolType t) |
| 824 | { |
| 825 | if (symType == t) return; |
| 826 | |
| 827 | symType = t; |
| 828 | if (table) table->symbolTypeChanged(this); |
| 829 | } |
| 830 | |
| 831 | bool Symbol::isSlotValid(const MemoryLayout* ml) const |
| 832 | { |
| 833 | if (!ml) return true; |
| 834 | int page = symValue >> 14; |
| 835 | int ps = ml->primarySlot[page] & 3; |
| 836 | int ss = 0; |
| 837 | if (ml->isSubslotted[page]) ss = ml->secondarySlot[page] & 3; |
| 838 | if (symSlots & (1 << (4 * ps + ss))) { |
| 839 | return true; |
| 840 | //if (symSegments.empty()) return true; |
| 841 | //for (const auto& seg : symSegments) { |
| 842 | // if (ml->mapperSegment[page] == seg) { |
| 843 | // return true; |
| 844 | // } |
| 845 | //} |
| 846 | } |
| 847 | return false; |
| 848 | } |