Overhaul debugh annotation system. Multiple annotations can be had, at the moment, FunctionName and Comments
Some checks reported errors
continuous-integration/drone/push Build was killed

This commit is contained in:
DataHoarder 2021-08-03 01:37:14 +02:00
parent 835c84c944
commit 5e7360b844
6 changed files with 56 additions and 9 deletions

View file

@ -205,6 +205,9 @@ uint32_t Assembler::placeFunction(uint32_t base, const Function &function) {
}
}
std::unordered_map<uint32_t, std::string> knownComments;
for (auto &d : declarations) {
auto instr = Instruction::Instruction::getCommandForDeclaration(d);
if (instr == nullptr) {
@ -217,6 +220,16 @@ uint32_t Assembler::placeFunction(uint32_t base, const Function &function) {
instr->setAddress(offset);
auto comment = instr->getComment();
if(!comment.empty()){
d.comment += "\n" + comment;
}
if(!d.comment.empty()){
knownComments[offset] = d.comment;
}
instr->setEndAddress(endAddresses[offset]); //TODO: this might not be needed anymore!
auto data = instr->toBytes();
@ -230,22 +243,38 @@ uint32_t Assembler::placeFunction(uint32_t base, const Function &function) {
}
if (placeSymbols) {
enum class SymbolKind : uint8_t {
FunctionName = 0,
Comment = 1,
Reserved_2 = 2,
Reserved_3 = 3
};
uint32_t endOffset = offset;
{
auto endInstruction = Instruction::End();
endInstruction.reserved = base; // Tag start of function
endInstruction.reserved = endOffset; // Tag end of function, start of debug data
auto data = endInstruction.toBytes();
std::copy(data.begin(), data.end(), bytes.begin() + offset);
offset += data.size();
}
{
std::vector<std::pair<std::pair<SymbolKind, uint32_t>, std::string>> debugEntries;
debugEntries.push_back({{SymbolKind::FunctionName, base}, function.label}); //Tag start of function, and data
for (auto& comment : knownComments) {
debugEntries.push_back({{SymbolKind::Comment, comment.first}, comment.second});
}
for(auto i = 0; i < debugEntries.size(); ++i){
auto& entry = debugEntries[i];
std::vector<uint32_t> debugData;
uint8_t off = 4;
uint32_t v = 0;
for(uint8_t c : function.label){
for(uint8_t c : entry.second){
--off;
v |= (uint32_t)c << (off * 8);
if(off == 0){
@ -258,14 +287,12 @@ uint32_t Assembler::placeFunction(uint32_t base, const Function &function) {
debugData.emplace_back(v);
}
auto loadInstruction = Instruction::Load({endOffset, 0 /*TODO: can place 2 bits of data here!*/}, 0 /*TODO: can place 1 bit of data here!*/, debugData); //Tag end of function, and data
auto loadInstruction = Instruction::Load({entry.first.second, (uint8_t)entry.first.first}, (i + 1) < debugEntries.size(), debugData);
auto data = loadInstruction.toBytes();
std::copy(data.begin(), data.end(), bytes.begin() + offset);
offset += data.size();
}
}
entries[function.label] = {base, needsReLabeling};

View file

@ -33,10 +33,9 @@
class Declaration {
public:
Declaration() {
}
Declaration() = default;
std::string label;
std::vector<Token> tokens;
std::string comment;
};

View file

@ -62,6 +62,7 @@ void Parser::parse() {
Function currentFunction;
Declaration currentDeclaration;
std::string currentComment;
while (true) {
try{
@ -217,6 +218,9 @@ void Parser::parse() {
}
if (!currentDeclaration.tokens.empty()) {
currentDeclaration.comment = currentComment;
currentComment.clear();
if (currentDeclaration.tokens.size() >= 3 &&
currentDeclaration.tokens.at(0).getType() == Token::Type::Directive) {
@ -245,6 +249,8 @@ void Parser::parse() {
currentFunction.declarations.emplace_back(std::move(currentDeclaration));
currentDeclaration = Declaration();
}
}else if (!currentComment.empty()){
currentComment += "\n";
}
if (c == '\0') {
@ -259,6 +265,8 @@ void Parser::parse() {
}
} else if (!isComment) {
currentValue += c;
} else {
currentComment += c;
}
}catch (const std::exception& e){
std::cerr << "Exception: " << e.what() << " on " << currentFunction.label << " (line# " << std::dec << lineNumber << ")\n";

View file

@ -83,6 +83,10 @@ namespace Instruction {
virtual CommandOp getCommand() const = 0;
virtual std::string getComment() const {
return "";
};
void setAddress(uint32_t address){
_address = address;
}

View file

@ -48,12 +48,16 @@ namespace Instruction{
initData(data);
instructions.push_back(std::make_unique<Call>(address));
takedownData(data);
comment = "FastCall framing start, " + std::to_string(data.size()) + " arguments";
}
FastCall::FastCall(const AddressWithOffset &address, const std::vector<Token> &data) {
initData(data);
instructions.push_back(std::make_unique<Call>(address));
takedownData(data);
comment = "FastCall framing start, " + std::to_string(data.size()) + " arguments";
}
void FastCall::takedownData(const std::vector<Token> &data) {

View file

@ -33,6 +33,7 @@ namespace Instruction {
//Akin to __fastcall, but the caller cleans up the stack
class FastCall : public Instruction {
private:
std::string comment;
std::vector<std::unique_ptr<Instruction>> instructions;
void initData(const std::vector<Token>& data);
void takedownData(const std::vector<Token>& data);
@ -47,6 +48,10 @@ namespace Instruction {
std::vector<uint8_t> toBytes() const override;
std::string getComment() const override {
return comment;
};
size_t getByteSize() const override{
size_t s = 0;
for(auto& i : instructions){