Add source mappings to Program
MODIFY
src/assembler/assembler.h
src/assembler/assembler.h
@@ -82,7 +82,8 @@ public:
AssemblerTypes(Reg_T);
Assembler(const ISAInfoBase* isa) : m_isa(isa) {}
- AssembleResult assemble(const QStringList& programLines, const SymbolMap* symbols = nullptr) const override {
+ AssembleResult assemble(const QStringList& programLines, const SymbolMap* symbols = nullptr,
+ QString sourceHash = QString()) const override {
AssembleResult result;
/// by default, emit to .text until otherwise specified
@@ -109,6 +110,7 @@ public:
Q_UNUSED(unused);
result.program = program;
+ result.program.sourceHash = sourceHash;
result.program.entryPoint = m_sectionBasePointers.at(".text");
return result;
}
@@ -368,6 +370,7 @@ protected:
std::shared_ptr<_Instruction> assembledWith;
runOperation(machineCode, _InstrRes, assembleInstruction, line, assembledWith);
assert(assembledWith && "Expected the assembler instruction to be set");
+ program.sourceMapping[addr_offset] = line.sourceLine;
if (!machineCode.linksWithSymbol.symbol.isEmpty()) {
LinkRequest req;

MODIFY
src/assembler/assemblerbase.cpp
src/assembler/assemblerbase.cpp
@@ -28,7 +28,7 @@ void AssemblerBase::setSegmentBase(Section seg, AInt base) {
AssembleResult AssemblerBase::assembleRaw(const QString& program, const SymbolMap* symbols) const {
const auto programLines = program.split(QRegExp("[\r\n]"));
- return assemble(programLines, symbols);
+ return assemble(programLines, symbols, Program::calculateHash(program.toUtf8()));
}
/// Adds a symbol to the current symbol mapping of this assembler defined at the 'line' in the input program.

MODIFY
src/assembler/assemblerbase.h
src/assembler/assemblerbase.h
@@ -32,7 +32,10 @@ public:
/// Assembles an input program (represented as a list of strings). Optionally, a set of predefined symbols may be
/// provided to the assemble call.
- virtual AssembleResult assemble(const QStringList& programLines, const SymbolMap* symbols = nullptr) const = 0;
+ /// If programLines does not represent the source program directly (possibly due to conversion of newline/cr/..., an
+ /// explicit hash of the source program can be provided for later identification.
+ virtual AssembleResult assemble(const QStringList& programLines, const SymbolMap* symbols = nullptr,
+ QString sourceHash = QString()) const = 0;
AssembleResult assembleRaw(const QString& program, const SymbolMap* symbols = nullptr) const;
/// Disassembles an input program relative to the provided base address.

MODIFY
src/assembler/program.cpp
src/assembler/program.cpp
@@ -94,4 +94,16 @@ const DisassembledProgram& Program::getDisassembled() const {
return disassembled;
}
+QString Program::calculateHash(const QByteArray& data) {
+ return QCryptographicHash::hash(data, QCryptographicHash::Sha1);
+}
+
+bool Program::isSameSource(const QByteArray& data) const {
+ /// We consider no source program to be equal to this program if no source hash has been set.
+ if (sourceHash.isEmpty())
+ return false;
+
+ return sourceHash == calculateHash(data);
+}
+
} // namespace Ripes

MODIFY
src/assembler/program.h
src/assembler/program.h
#pragma once
#include <QByteArray>
+#include <QHash>
#include <QMap>
#include <QMetaType>
#include <QString>
@@ -99,9 +100,18 @@ private:
*/
class Program {
public:
+ // A source mapping is a mapping from {instruction address : source code line}
+ using SourceMapping = std::map<VInt, unsigned>;
+
AInt entryPoint = 0;
std::map<QString, ProgramSection> sections;
ReverseSymbolMap symbols;
+ SourceMapping sourceMapping;
+
+ // Hash of the source code which this program resulted from. Expected to be a SHA-1 hash (fastest).
+ QString sourceHash;
+ // Returns true if data is equal to the sourceHash of this program.
+ bool isSameSource(const QByteArray& data) const;
/// Returns the program section corresponding to the provided name. Return nullptr if no section was found with the
/// given name.
@@ -110,6 +120,10 @@ public:
/// Returns the disassembled version of this program. The result is an ordered map of
/// [instruction address : disassembled instruction]
const DisassembledProgram& getDisassembled() const;
+ const SourceMapping& getSourceMapping() const;
+
+ /// Calculates a hash used for source identification.
+ static QString calculateHash(const QByteArray& data);
private:
/// A caching of the disassembled version of this program.

Add source mappings to Program #185