Finishing the WlaDx label's parser

This commit is contained in:
Zoe Roux
2021-02-17 17:10:35 +01:00
parent f33593c51f
commit a53568bf5d
6 changed files with 94 additions and 21 deletions
+2
View File
@@ -7,6 +7,8 @@ project(ComSquare)
# show compilation warnings # show compilation warnings
add_compile_options(-W -Wall -Wextra -Wshadow) add_compile_options(-W -Wall -Wextra -Wshadow)
include_directories(sources)
# make unit tests # make unit tests
add_executable(unit_tests add_executable(unit_tests
tests/CPU/testAddressingMode.cpp tests/CPU/testAddressingMode.cpp
+4 -3
View File
@@ -3,8 +3,9 @@
// //
#include "CPUDebug.hpp" #include "CPUDebug.hpp"
#include "../../Utility/Utility.hpp" #include "Utility/Utility.hpp"
#include "../../Exceptions/InvalidOpcode.hpp" #include "Exceptions/InvalidOpcode.hpp"
#include "SymbolLoaders/WlaDx.hpp"
#include <QtEvents> #include <QtEvents>
#include <QPainter> #include <QPainter>
#include <iostream> #include <iostream>
@@ -316,7 +317,7 @@ namespace ComSquare::Debugger
std::ifstream sym(symbolPath); std::ifstream sym(symbolPath);
if (sym) { if (sym) {
std::vector<Label> symLabels = WlaDx.parse(sym); std::vector<Label> symLabels = WlaDx::parse(sym);
labels.insert(labels.end(), labels.insert(labels.end(),
std::make_move_iterator(symLabels.begin()), std::make_move_iterator(symLabels.begin()),
std::make_move_iterator(symLabels.end())); std::make_move_iterator(symLabels.end()));
+64 -8
View File
@@ -3,23 +3,79 @@
// //
#include <regex> #include <regex>
#include "Utility/Utility.hpp"
#include "WlaDx.hpp" #include "WlaDx.hpp"
namespace ComSquare::Debugger namespace ComSquare::Debugger
{ {
std::vector<Label> WlaDx::parse(std::fstream symbolFile) std::vector<Label> WlaDx::parse(std::ifstream &symbolFile)
{ {
std::vector<Label> labels; std::vector<Label> ret;
std::map<std::string, Label> labels;
std::string line; std::string line;
std::smatch match;
std::regex re(R"(\[(\S+)\])");
while (std::getline(symbolFile, line)) { while (symbolFile) {
line = line.substr(0, line.rfind(';')); if (line.empty()) {
std::smatch match; std::getline(symbolFile, line);
std::regex re("\\[\\S+\\]"); line = WlaDx::_cleanLine(line);
}
if (!std::regex_match(line, match, re))
continue;
if (!std::regex_search(line, match, re) if (match[1] == "labels")
line = WlaDx::_parseLabels(labels, symbolFile);
else if (match[1] == "definitions")
line = WlaDx::_parseDefinitions(labels, symbolFile);
} }
return labels; std::transform(labels.begin(), labels.end(), std::back_inserter(ret), [](auto &i){return i.second;});
return ret;
}
std::string WlaDx::_parseLabels(std::map<std::string, Label> &labels, std::ifstream &f)
{
std::string line;
std::smatch match;
std::regex re(R"(([0-9a-fA-F]{2}):([0-9a-fA-F]{4}) (.*))");
while (std::getline(f, line)) {
line = WlaDx::_cleanLine(line);
if (!std::regex_match(line, match, re))
return line;
labels[match[3]].name = match[3];
labels[match[3]].address = std::stoi(match[1], nullptr, 16) << 16
| std::stoi(match[2], nullptr, 16);
}
return "";
}
std::string WlaDx::_parseDefinitions(std::map<std::string, Label> &labels, std::ifstream &f)
{
std::string line;
std::smatch match;
std::regex re(R"(([0-9a-fA-F]{8}) (.*))");
std::string name;
while (std::getline(f, line)) {
line = WlaDx::_cleanLine(line);
if (!std::regex_match(line, match, re))
return line;
// TODO support other definitions.
if (!match[2].str().starts_with("_sizeof_"))
continue;
name = match[2].str().substr(std::string("_sizeof_").size());
labels[name].size = std::stoi(match[1], nullptr, 16);
}
return "";
}
std::string WlaDx::_cleanLine(std::string line)
{
line = line.substr(0, line.rfind(';'));
line.erase(0, line.find_first_not_of(Utility::WHITESPACES));
line.erase(line.find_last_not_of(Utility::WHITESPACES) + 1);
return line;
} }
} }
+18 -1
View File
@@ -12,6 +12,23 @@ namespace ComSquare::Debugger
//! @brief Class to parse WLA-DX symbol files. //! @brief Class to parse WLA-DX symbol files.
class WlaDx { class WlaDx {
public: public:
static std::vector<Label> parse(std::fstream symbolFile); //! @brief Parse the whole file
//! @param symbolFile The file to parse (opened but not seeked)
//! @return The list of symbol found.
static std::vector<Label> parse(std::ifstream &symbolFile);
//! @brief Parse labels name & addresses (the [labels] section)
//! @param labels An list of labels. If one is already defined, the address will be set.
//! @param symbolFile The file to parse, opened and seeked to the line just after the [labels] line.
//! @return The line after the label section (the first line that doesn't match the section's regex).
static std::string _parseLabels(std::map<std::string, Label> &labels, std::ifstream &symbolFile);
//! @brief Parse definitions (and size of labels) (the [definitions] section)
//! @param labels An list of labels. If one is already defined, the size will be set.
//! @param symbolFile The file to parse, opened and seeked to the line just after the [definitions] line.
//! @return The line after the definitions section (the first line that doesn't match the section's regex).
static std::string _parseDefinitions(std::map<std::string, Label> &labels, std::ifstream &symbolFile);
//! @brief Remove comments, indentation & trailing spaces from a line.
//! @param line The line to clean
//! @return The line cleaned up.
static std::string _cleanLine(std::string line);
}; };
} }
+2
View File
@@ -55,4 +55,6 @@ namespace ComSquare::Utility
{ {
return std::bitset<24>(i).to_string(); return std::bitset<24>(i).to_string();
} }
const std::string WHITESPACES = " \t\n\r\f\v";
} }
+4 -9
View File
@@ -2,13 +2,12 @@
// Created by anonymus-raccoon on 2/16/20. // Created by anonymus-raccoon on 2/16/20.
// //
#ifndef COMSQUARE_UTILITY_HPP #pragma once
#define COMSQUARE_UTILITY_HPP
#include <string> #include <string>
#include <ios> #include <ios>
#include <sstream> #include <sstream>
#include "../Models/Int24.hpp" #include "Models/Int24.hpp"
namespace ComSquare::Utility namespace ComSquare::Utility
{ {
@@ -19,16 +18,12 @@ namespace ComSquare::Utility
}; };
std::string to_hex(uint8_t i, HexString prefix = AsmPrefix); std::string to_hex(uint8_t i, HexString prefix = AsmPrefix);
std::string to_hex(uint16_t i, HexString prefix = AsmPrefix); std::string to_hex(uint16_t i, HexString prefix = AsmPrefix);
std::string to_hex(uint24_t i, HexString prefix = AsmPrefix); std::string to_hex(uint24_t i, HexString prefix = AsmPrefix);
std::string to_binary(uint8_t i); std::string to_binary(uint8_t i);
std::string to_binary(uint16_t i); std::string to_binary(uint16_t i);
std::string to_binary(uint24_t i); std::string to_binary(uint24_t i);
}
#endif //COMSQUARE_UTILITY_HPP extern const std::string WHITESPACES;
}