Skip to content

Commit

Permalink
some refactoring, collect labels
Browse files Browse the repository at this point in the history
  • Loading branch information
partouf committed Oct 4, 2020
1 parent 9e9071a commit bc61d8e
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ cmake_minimum_required(VERSION 3.10)
project(asm-parser)

set(CMAKE_CXX_STANDARD 17)
add_compile_options(-Werror -Wall -Wextra)
add_compile_options(-Werror -Wall -Wextra -O3 -flto)

add_subdirectory(src)
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
add_executable(asm-parser
main.cpp
types/state.cpp
utils/utils.cpp
objdump/parser.cpp)
28 changes: 10 additions & 18 deletions src/objdump/parser.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
#include "parser.hpp"
#include "../types/line.hpp"
#include "../utils/utils.hpp"

#include <iostream>

AsmParser::ObjDumpParser::ObjDumpParser() {
}

bool is_whitespace(const char c) {
return ((c == 32) || (c == 7));
}

bool is_hex(const char c) {
return (c >= 'a' && c <= 'f') || (c >= '0' && c <= '9');
}

int8_t hex2int(const char c) {
if (c >= '0' && c <= '9') {
return c - '0';
} else {
return 10 + (c - 'a');
}
}

void AsmParser::ObjDumpParser::eol() {
this->state.currentLine.text = this->state.text;
lines.push_back(this->state.currentLine);
Expand All @@ -31,6 +16,7 @@ void AsmParser::ObjDumpParser::eol() {

void AsmParser::ObjDumpParser::label() {
this->state.previousLabel = this->state.text;
labels.emplace(this->state.previousLabel, lines.size());
this->state.text.clear();
}

Expand Down Expand Up @@ -112,7 +98,7 @@ void AsmParser::ObjDumpParser::fromStream(std::istream &in) {
}

void AsmParser::ObjDumpParser::outputJson(std::ostream &out) {
out << "[";
out << "{[";

for (auto line: this->lines) {
out << "{\n";
Expand All @@ -134,7 +120,13 @@ void AsmParser::ObjDumpParser::outputJson(std::ostream &out) {
out << "},\n";
}

out << "]";
out << "],";

out << "\"labels\": [\n";
for (auto label: this->labels) {
out << "\"" << label.first << "\": " << label.second << ",\n";
}
out << "]}\n";
}

void AsmParser::ObjDumpParser::outputText(std::ostream &out) {
Expand Down
2 changes: 2 additions & 0 deletions src/objdump/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
#include "../types/line.hpp"
#include "../types/state.hpp"
#include <istream>
#include <unordered_map>

namespace AsmParser {

class ObjDumpParser {
private:
ParserState state{};
std::vector<asm_line> lines;
std::unordered_map<std::string, int32_t> labels;

void eol();
void address();
Expand Down
17 changes: 17 additions & 0 deletions src/utils/utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "utils.hpp"

bool AsmParser::is_whitespace(const char c) {
return ((c == 32) || (c == 7));
}

bool AsmParser::is_hex(const char c) {
return (c >= 'a' && c <= 'f') || (c >= '0' && c <= '9');
}

int8_t AsmParser::hex2int(const char c) {
if (c >= '0' && c <= '9') {
return c - '0';
} else {
return 10 + (c - 'a');
}
}
10 changes: 10 additions & 0 deletions src/utils/utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

#include <cstdint>

namespace AsmParser {

bool is_whitespace(const char c);
bool is_hex(const char c);
int8_t hex2int(const char c);

}

0 comments on commit bc61d8e

Please sign in to comment.