| 1 | #ifndef SYMBOLTABLE_H |
| 2 | #define SYMBOLTABLE_H |
| 3 | |
| 4 | #include <QString> |
| 5 | #include <QList> |
| 6 | #include <QMultiMap> |
| 7 | #include <QMultiHash> |
| 8 | #include <QDateTime> |
| 9 | #include <QXmlStreamReader> |
| 10 | #include <QXmlStreamWriter> |
| 11 | #include <QFileSystemWatcher> |
| 12 | #include <cstdint> |
| 13 | #include <memory> |
| 14 | #include <vector> |
| 15 | |
| 16 | struct MemoryLayout; |
| 17 | class SymbolTable; |
| 18 | |
| 19 | class Symbol |
| 20 | { |
| 21 | public: |
| 22 | Symbol(QString str, int addr, const QString* source = nullptr); |
| 23 | Symbol(const Symbol& symbol); |
| 24 | Symbol& operator=(const Symbol&) = default; |
| 25 | |
| 26 | // ACTIVE status is for regular symbols. HIDDEN is for symbols |
| 27 | // that are in the list but not shown anywhere. LOST is a special |
| 28 | // status for symbols that were once loaded from a symbol file, but |
| 29 | // weren't found later. These aren't deleted immediately because |
| 30 | // the possible custom settings would be lost even if the reload |
| 31 | // was of a bad file (after a failed assembler run for instance). |
| 32 | enum SymbolStatus { ACTIVE, HIDDEN, LOST }; |
| 33 | enum SymbolType { JUMPLABEL, VARIABLELABEL, VALUE }; |
| 34 | enum Register { REG_A = 1 << 0, REG_B = 1 << 1, REG_C = 1 << 2, REG_D = 1 << 3, REG_E = 1 << 4, |
| 35 | REG_H = 1 << 5, REG_L = 1 << 6, REG_BC = 1 << 7, REG_DE = 1 << 8, |
| 36 | REG_HL = 1 << 9, REG_IX = 1 << 10, REG_IY = 1 << 11, REG_IXL = 1 << 12, |
| 37 | REG_IXH = 1 << 13, REG_IYL = 1 << 15, REG_IYH = 1 << 15, |
| 38 | REG_OFFSET = 1 << 16, REG_I = 1 << 17, |
| 39 | REG_ALL8 = REG_A | REG_B | REG_C | REG_D | REG_E | REG_H | REG_L |
| 40 | | REG_IXL | REG_IXH | REG_IYL | REG_IYH |
| 41 | | REG_OFFSET | REG_I, |
| 42 | REG_ALL16 = REG_BC | REG_DE | REG_HL | REG_IX | REG_IY, |
| 43 | REG_ALL = REG_ALL8 | REG_ALL16 }; |
| 44 | |
| 45 | [[nodiscard]] const QString& text() const { return symText; } |
| 46 | void setText(const QString& str) { symText = str; } |
| 47 | [[nodiscard]] int value() const { return symValue; } |
| 48 | void setValue(int addr); |
| 49 | [[nodiscard]] uint16_t validSlots() const { return symSlots; } |
| 50 | void setValidSlots(uint16_t val) { symSlots = val; } |
| 51 | [[nodiscard]] int validRegisters() const { return symRegisters; } |
| 52 | void setValidRegisters(int regs); |
| 53 | [[nodiscard]] const QString* source() const { return symSource; } |
| 54 | void setSource(const QString* name) { symSource = name; } |
| 55 | [[nodiscard]] SymbolStatus status() const { return symStatus; } |
| 56 | void setStatus(SymbolStatus s) { symStatus = s; } |
| 57 | [[nodiscard]] SymbolType type() const { return symType; } |
| 58 | void setType(SymbolType t); |
| 59 | |
| 60 | bool isSlotValid(const MemoryLayout* ml = nullptr) const; |
| 61 | |
| 62 | private: |
| 63 | SymbolTable* table = nullptr; |
| 64 | |
| 65 | QString symText; |
| 66 | int symValue; |
| 67 | uint16_t symSlots = 0xffff; |
| 68 | //QList<uint8_t> symSegments; |
| 69 | int symRegisters; |
| 70 | const QString* symSource = nullptr; |
| 71 | SymbolStatus symStatus = ACTIVE; |
| 72 | SymbolType symType = JUMPLABEL; |
| 73 | |
| 74 | friend class SymbolTable; |
| 75 | }; |
| 76 | |
| 77 | |
| 78 | class SymbolTable : public QObject |
| 79 | { |
| 80 | Q_OBJECT |
| 81 | public: |
| 82 | enum FileType { |
| 83 | DETECT_FILE, |
| 84 | OMDS_FILE, |
| 85 | TNIASM0_FILE, |
| 86 | TNIASM1_FILE, |
| 87 | SJASM_FILE, |
| 88 | ASMSX_FILE, |
| 89 | LINKMAP_FILE, |
| 90 | HTC_FILE, |
| 91 | NOICE_FILE, |
| 92 | PASMO_FILE, |
| 93 | VASM_FILE |
| 94 | }; |
| 95 | |
| 96 | SymbolTable(); |
| 97 | |
| 98 | Symbol* add(std::unique_ptr<Symbol> symbol); |
| 99 | std::unique_ptr<Symbol> removeAt(size_t index); |
| 100 | std::unique_ptr<Symbol> remove(Symbol *symbol); |
| 101 | void clear(); |
| 102 | [[nodiscard]] int size() const; |
| 103 | |
| 104 | // xml session file functions |
| 105 | void saveSymbols(QXmlStreamWriter& xml); |
| 106 | void loadSymbols(QXmlStreamReader& xml); |
| 107 | |
| 108 | // Symbol access functions |
| 109 | [[nodiscard]] Symbol* findFirstAddressSymbol(int addr, MemoryLayout* ml = nullptr); |
| 110 | [[nodiscard]] Symbol* getCurrentAddressSymbol(); |
| 111 | [[nodiscard]] Symbol* findNextAddressSymbol(MemoryLayout* ml = nullptr); |
| 112 | [[nodiscard]] Symbol* getValueSymbol(int val, Symbol::Register reg, MemoryLayout* ml = nullptr); |
| 113 | [[nodiscard]] Symbol* getAddressSymbol(int val, MemoryLayout* ml = nullptr); |
| 114 | [[nodiscard]] Symbol* getAddressSymbol(const QString& label, bool case_sensitive = false); |
| 115 | |
| 116 | [[nodiscard]] QStringList labelList(bool include_vars = false, const MemoryLayout* ml = nullptr) const; |
| 117 | |
| 118 | void symbolTypeChanged(Symbol* symbol); |
| 119 | void symbolValueChanged(Symbol* symbol); |
| 120 | |
| 121 | [[nodiscard]] int symbolFilesSize() const; |
| 122 | [[nodiscard]] const QString& symbolFile(int index) const; |
| 123 | [[nodiscard]] const QDateTime& symbolFileRefresh(int index) const; |
| 124 | |
| 125 | bool readFile(const QString& filename, FileType type = DETECT_FILE); |
| 126 | void reloadFiles(); |
| 127 | void unloadFile(const QString& file, bool keepSymbols = false); |
| 128 | |
| 129 | signals: |
| 130 | void symbolFileChanged(); |
| 131 | |
| 132 | private: |
| 133 | void appendFile(const QString& file, FileType type); |
| 134 | bool readSymbolFile( |
| 135 | const QString& filename, FileType type, const QString& equ); |
| 136 | bool readOMDSFile(const QString& filename); |
| 137 | bool readTNIASM0File(const QString& filename); |
| 138 | bool readTNIASM1File(const QString& filename); |
| 139 | bool readASMSXFile(const QString& filename); |
| 140 | bool readSJASMFile(const QString& filename); |
| 141 | bool readHTCFile(const QString& filename); |
| 142 | bool readNoICEFile(const QString& filename); |
| 143 | bool readLinkMapFile(const QString& filename); |
| 144 | bool readPASMOFile(const QString& filename); |
| 145 | bool readVASMFile(const QString& filename); |
| 146 | |
| 147 | void mapSymbol(Symbol* symbol); |
| 148 | void unmapSymbol(Symbol* symbol); |
| 149 | |
| 150 | void fileChanged(const QString & path); |
| 151 | |
| 152 | private: |
| 153 | std::vector<std::unique_ptr<Symbol>> symbols; |
| 154 | QMultiMap<int, Symbol*> addressSymbols; |
| 155 | QMultiHash<int, Symbol*> valueSymbols; |
| 156 | QMultiMap<int, Symbol*>::iterator currentAddress; |
| 157 | |
| 158 | struct SymbolFileRecord { |
| 159 | QString fileName; |
| 160 | QDateTime refreshTime; |
| 161 | FileType fileType; |
| 162 | }; |
| 163 | QList<SymbolFileRecord> symbolFiles; |
| 164 | QFileSystemWatcher fileWatcher; |
| 165 | }; |
| 166 | |
| 167 | #endif // SYMBOLTABLE_H |