From 82ba6ef67d0bbcf6c375115af473e1f9ee9f3919 Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Thu, 5 Jan 2017 20:38:00 -0500 Subject: [PATCH] variable support. --- zrdz_disassembler.cpp | 36 +++++++++++++++++++++++++++++++----- zrdz_disassembler.h | 1 + 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/zrdz_disassembler.cpp b/zrdz_disassembler.cpp index c0b8cf2..cad725d 100644 --- a/zrdz_disassembler.cpp +++ b/zrdz_disassembler.cpp @@ -31,6 +31,7 @@ zrdz_disassembler::zrdz_disassembler(std::vector
&§ions, std::vecto for (auto &s : _symbols) { if (s.type == S_UND) continue; + if (s.flags & SF_VAR) continue; if (s.section >= _sections.size()) { warnx("invalid section %d for symbol %s", s.section, s.name.c_str()); @@ -81,6 +82,7 @@ void zrdz_disassembler::front_matter(const std::string &module) { emit("", "module", module); putchar('\n'); print_externs(); + print_variables(); // reference-only sections. for (auto &e : _sections) { @@ -175,8 +177,6 @@ void zrdz_disassembler::back_matter(unsigned flags) { void zrdz_disassembler::print_externs() { - - std::vector tmp; for (auto &s : _symbols) { @@ -190,6 +190,34 @@ void zrdz_disassembler::print_externs() { } + +// for sorting symbols by name. +bool operator<(const symbol &a, const symbol &b) { + return a.name < b.name; +} + + + +void zrdz_disassembler::print_variables() { + + std::vector tmp; + + for (auto &s : _symbols) { + if (s.flags & SF_VAR) tmp.push_back(s); + } + + if (tmp.empty()) return; + std::sort(tmp.begin(), tmp.end()); + + for (const auto &s : tmp) { + if (s.flags & SF_GBL) emit("", "public", s.name); + emit(s.name, "var", to_x(s.offset, 4, '$')); + } + + putchar('\n'); + +} + void zrdz_disassembler::print_globals(int section) { std::vector tmp; @@ -213,9 +241,7 @@ void zrdz_disassembler::print_equs(int section) { ); if (tmp.empty()) return; - std::sort(tmp.begin(), tmp.end(), - [](const symbol &a, const symbol &b) { return a.name < b.name; } - ); + std::sort(tmp.begin(), tmp.end()); for (const auto &s : tmp) emit(s.name, "gequ", to_x(s.offset, 4, '$')); putchar('\n'); diff --git a/zrdz_disassembler.h b/zrdz_disassembler.h index 06f11bc..d1ea8b9 100644 --- a/zrdz_disassembler.h +++ b/zrdz_disassembler.h @@ -65,6 +65,7 @@ private: void print_section(const entry &e); void print_externs(); + void print_variables(); void print_globals(int section); void print_equs(int section);