Verified Commit 7d57b57f authored by nagayama15's avatar nagayama15

feat(ordering): implement extractor

parent 6923124d
......@@ -94,7 +94,7 @@ namespace kyut {
for (std::size_t i = 0; i < count; i++) {
const auto it = std::begin(chunk) + i;
// Find the position of `it`.
// Find the position of `begin + i`.
const auto found = std::find(it, std::end(chunk), begin + i);
assert(found != std::end(chunk));
......
......@@ -29,6 +29,25 @@ namespace kyut::methods::function_ordering {
return size_bits;
}
std::size_t extract(BitStreamWriter& w, wasm::Module& module, std::size_t chunk_size) {
const auto begin = std::begin(module.functions);
const auto end = std::end(module.functions);
const auto start = std::partition(begin, end, [](const auto& f) {
return f->body == nullptr;
});
const auto size_bits = extract_by_ordering(
w,
chunk_size,
start,
end,
[](const auto& a, const auto& b) {
return a->name < b->name;
});
return size_bits;
}
} // namespace kyut::methods::function_ordering
#endif // INCLUDE_kyut_methods_FunctionOrdering_hpp
#include <fmt/printf.h>
#include "cmdline.h"
#include "kyut/methods/FunctionOrdering.hpp"
#include "wasm-io.h"
namespace {
......@@ -15,6 +16,7 @@ int main(int argc, char* argv[]) {
options.add<std::string>("method", 'm', "Embedding method (function-ordering)", true, "", cmdline::oneof<std::string>("function-ordering"));
options.add<std::size_t>("chunk-size", 'c', "Chunk size [2~20]", false, 20, cmdline::range<std::size_t>(2, 20));
options.add<std::string>("dump", 0, "Output format (ascii, hex)", false, "ascii", cmdline::oneof<std::string>("ascii", "hex"));
options.set_program_name(program_name);
options.footer("filename");
......@@ -38,11 +40,33 @@ int main(int argc, char* argv[]) {
const auto input = options.rest()[0];
const auto method = options.get<std::string>("method");
[[maybe_unused]] const auto chunk_size = options.get<std::size_t>("chunk-size");
const auto chunk_size = options.get<std::size_t>("chunk-size");
const auto dump_format = options.get<std::string>("dump");
try {
wasm::Module module{};
wasm::ModuleReader{}.read(input, module);
kyut::BitStreamWriter w{};
std::size_t size_bits;
if (method == "function-ordering") {
size_bits = kyut::methods::function_ordering::extract(w, module, chunk_size);
} else {
WASM_UNREACHABLE(("unknown method: " + method).c_str());
}
fmt::print("{} bits\n", size_bits);
if (dump_format == "ascii") {
fmt::print("{}", w.data_as_str());
} else if (dump_format == "hex") {
for (const auto& byte : w.data()) {
fmt::print("{:02X}", byte);
}
} else {
WASM_UNREACHABLE(("unknown dump format: " + dump_format).c_str());
}
} catch (const std::exception& e) {
fmt::print(std::cerr, "error: {}\n", e.what());
std::exit(EXIT_FAILURE);
......
#include <fmt/printf.h>
#include "cmdline.h"
#include "kyut/methods/FunctionOrdering.hpp"
#include "support/colors.h"
#include "wasm-io.h"
namespace {
......@@ -72,7 +73,8 @@ int main(int argc, char* argv[]) {
WASM_UNREACHABLE(("unknown method: " + method).c_str());
}
wasm::ModuleWriter{}.write(module, output);
Colors::setEnabled(false);
wasm::ModuleWriter{}.writeText(module, output);
fmt::print("{} bits\n", size_bits);
} catch (const std::exception& e) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment