diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/asm2wasm.h | 7 | ||||
-rw-r--r-- | src/binaryen-c.cpp | 12 | ||||
-rw-r--r-- | src/support/file.cpp | 37 | ||||
-rw-r--r-- | src/support/file.h | 19 | ||||
-rw-r--r-- | src/tools/asm2wasm.cpp | 10 | ||||
-rw-r--r-- | src/tools/fuzzing.h | 3 | ||||
-rw-r--r-- | src/tools/wasm-as.cpp | 5 | ||||
-rw-r--r-- | src/tools/wasm-ctor-eval.cpp | 8 | ||||
-rw-r--r-- | src/tools/wasm-dis.cpp | 4 | ||||
-rw-r--r-- | src/tools/wasm-emscripten-finalize.cpp | 6 | ||||
-rw-r--r-- | src/tools/wasm-metadce.cpp | 9 | ||||
-rw-r--r-- | src/tools/wasm-opt.cpp | 9 | ||||
-rw-r--r-- | src/tools/wasm-shell.cpp | 6 | ||||
-rw-r--r-- | src/tools/wasm2js.cpp | 11 | ||||
-rw-r--r-- | src/wasm-binary.h | 130 | ||||
-rw-r--r-- | src/wasm-io.h | 12 | ||||
-rw-r--r-- | src/wasm/wasm-binary.cpp | 580 | ||||
-rw-r--r-- | src/wasm/wasm-io.cpp | 47 | ||||
-rw-r--r-- | src/wasm/wasm-s-parser.cpp | 2 | ||||
-rw-r--r-- | src/wasm2js.h | 2 |
20 files changed, 272 insertions, 647 deletions
diff --git a/src/asm2wasm.h b/src/asm2wasm.h index 0255b2847..637c4ec3b 100644 --- a/src/asm2wasm.h +++ b/src/asm2wasm.h @@ -38,11 +38,14 @@ #include "pass.h" #include "passes/passes.h" #include "shared-constants.h" +#include "support/debug.h" #include "wasm-builder.h" #include "wasm-emscripten.h" #include "wasm-module-building.h" #include "wasm.h" +#define DEBUG_TYPE "asm2wasm" + namespace wasm { using namespace cashew; @@ -1780,9 +1783,7 @@ void Asm2WasmBuilder::processAsm(Ref ast) { Function* Asm2WasmBuilder::processFunction(Ref ast) { auto name = ast[1]->getIString(); - if (debug) { - std::cout << "asm2wasming func: " << ast[1]->getIString().str << '\n'; - } + BYN_TRACE("asm2wasming func: " << ast[1]->getIString().str << '\n'); auto function = new Function; function->name = name; diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index cc543b611..697d9a1e3 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -3796,7 +3796,7 @@ void BinaryenModulePrintAsmjs(BinaryenModuleRef module) { Wasm2JSBuilder wasm2js(flags, globalPassOptions); Ref asmjs = wasm2js.processWasm(wasm); JSPrinter jser(true, true, asmjs); - Output out("", Flags::Text, Flags::Release); // stdout + Output out("", Flags::Text); // stdout Wasm2JSGlue glue(*wasm, out, flags, "asmFunc"); glue.emitPre(); jser.printAst(); @@ -3917,8 +3917,8 @@ static BinaryenBufferSizes writeModule(BinaryenModuleRef module, char* sourceMap, size_t sourceMapSize) { Module* wasm = (Module*)module; - BufferWithRandomAccess buffer(false); - WasmBinaryWriter writer(wasm, buffer, false); + BufferWithRandomAccess buffer; + WasmBinaryWriter writer(wasm, buffer); writer.setNamesSection(globalPassOptions.debugInfo); std::ostringstream os; if (sourceMapUrl) { @@ -3992,8 +3992,8 @@ BinaryenModuleAllocateAndWrite(BinaryenModuleRef module, } Module* wasm = (Module*)module; - BufferWithRandomAccess buffer(false); - WasmBinaryWriter writer(wasm, buffer, false); + BufferWithRandomAccess buffer; + WasmBinaryWriter writer(wasm, buffer); writer.setNamesSection(globalPassOptions.debugInfo); std::ostringstream os; if (sourceMapUrl) { @@ -4036,7 +4036,7 @@ BinaryenModuleRef BinaryenModuleRead(char* input, size_t inputSize) { buffer.resize(inputSize); std::copy_n(input, inputSize, buffer.begin()); try { - WasmBinaryBuilder parser(*wasm, buffer, false); + WasmBinaryBuilder parser(*wasm, buffer); parser.read(); } catch (ParseException& p) { p.dump(std::cerr); diff --git a/src/support/file.cpp b/src/support/file.cpp index 68ffbb5ac..2a8426461 100644 --- a/src/support/file.cpp +++ b/src/support/file.cpp @@ -15,16 +15,17 @@ */ #include "support/file.h" +#include "support/debug.h" #include <cstdint> #include <cstdlib> #include <iostream> #include <limits> -std::vector<char> wasm::read_stdin(Flags::DebugOption debug) { - if (debug == Flags::Debug) { - std::cerr << "Loading stdin..." << std::endl; - } +#define DEBUG_TYPE "file" + +std::vector<char> wasm::read_stdin() { + BYN_TRACE("Loading stdin...\n"); std::vector<char> input; char c; while (std::cin.get(c) && !std::cin.eof()) { @@ -34,12 +35,8 @@ std::vector<char> wasm::read_stdin(Flags::DebugOption debug) { } template<typename T> -T wasm::read_file(const std::string& filename, - Flags::BinaryOption binary, - Flags::DebugOption debug) { - if (debug == Flags::Debug) { - std::cerr << "Loading '" << filename << "'..." << std::endl; - } +T wasm::read_file(const std::string& filename, Flags::BinaryOption binary) { + BYN_TRACE("Loading '" << filename << "'...\n"); std::ifstream infile; std::ios_base::openmode flags = std::ifstream::in; if (binary == Flags::Binary) { @@ -81,28 +78,22 @@ std::string wasm::read_possible_response_file(const std::string& input) { if (input.size() == 0 || input[0] != '@') { return input; } - return wasm::read_file<std::string>( - input.substr(1), Flags::Text, Flags::Release); + return wasm::read_file<std::string>(input.substr(1), Flags::Text); } // Explicit instantiations for the explicit specializations. -template std::string -wasm::read_file<>(const std::string&, Flags::BinaryOption, Flags::DebugOption); -template std::vector<char> -wasm::read_file<>(const std::string&, Flags::BinaryOption, Flags::DebugOption); +template std::string wasm::read_file<>(const std::string&, Flags::BinaryOption); +template std::vector<char> wasm::read_file<>(const std::string&, + Flags::BinaryOption); -wasm::Output::Output(const std::string& filename, - Flags::BinaryOption binary, - Flags::DebugOption debug) - : outfile(), out([this, filename, binary, debug]() { +wasm::Output::Output(const std::string& filename, Flags::BinaryOption binary) + : outfile(), out([this, filename, binary]() { if (filename == "-") { return std::cout.rdbuf(); } std::streambuf* buffer; if (filename.size()) { - if (debug == Flags::Debug) { - std::cerr << "Opening '" << filename << "'" << std::endl; - } + BYN_TRACE("Opening '" << filename << "'\n"); auto flags = std::ofstream::out | std::ofstream::trunc; if (binary == Flags::Binary) { flags |= std::ofstream::binary; diff --git a/src/support/file.h b/src/support/file.h index fb3cad564..ce554acaa 100644 --- a/src/support/file.h +++ b/src/support/file.h @@ -30,21 +30,18 @@ namespace wasm { namespace Flags { enum BinaryOption { Binary, Text }; -enum DebugOption { Debug, Release }; } // namespace Flags -std::vector<char> read_stdin(Flags::DebugOption); +std::vector<char> read_stdin(); template<typename T> -T read_file(const std::string& filename, - Flags::BinaryOption binary, - Flags::DebugOption debug); +T read_file(const std::string& filename, Flags::BinaryOption binary); // Declare the valid explicit specializations. -extern template std::string -read_file<>(const std::string&, Flags::BinaryOption, Flags::DebugOption); -extern template std::vector<char> -read_file<>(const std::string&, Flags::BinaryOption, Flags::DebugOption); +extern template std::string read_file<>(const std::string&, + Flags::BinaryOption); +extern template std::vector<char> read_file<>(const std::string&, + Flags::BinaryOption); // Given a string which may be a response file (i.e., a filename starting // with "@"), if it is a response file read it and return that, or if it @@ -54,9 +51,7 @@ std::string read_possible_response_file(const std::string&); class Output { public: // An empty filename will open stdout instead. - Output(const std::string& filename, - Flags::BinaryOption binary, - Flags::DebugOption debug); + Output(const std::string& filename, Flags::BinaryOption binary); ~Output() = default; template<typename T> std::ostream& operator<<(const T& v) { return out << v; } diff --git a/src/tools/asm2wasm.cpp b/src/tools/asm2wasm.cpp index 083f3386b..b54dd8cd8 100644 --- a/src/tools/asm2wasm.cpp +++ b/src/tools/asm2wasm.cpp @@ -199,10 +199,8 @@ int main(int argc, const char* argv[]) { // debug info is disabled if a map file is not specified with wasm binary pre.debugInfo = options.passOptions.debugInfo && (!emitBinary || sourceMapFilename.size()); - auto input(read_file<std::vector<char>>(options.extra["infile"], - Flags::Text, - options.debug ? Flags::Debug - : Flags::Release)); + auto input( + read_file<std::vector<char>>(options.extra["infile"], Flags::Text)); char* start = pre.process(input.data()); if (options.debug) { @@ -224,8 +222,7 @@ int main(int argc, const char* argv[]) { const auto& memInit = options.extra.find("mem init"); if (memInit != options.extra.end()) { auto filename = memInit->second.c_str(); - auto data(read_file<std::vector<char>>( - filename, Flags::Binary, options.debug ? Flags::Debug : Flags::Release)); + auto data(read_file<std::vector<char>>(filename, Flags::Binary)); // create the memory segment Expression* init; const auto& memBase = options.extra.find("mem base"); @@ -293,7 +290,6 @@ int main(int argc, const char* argv[]) { std::cerr << "emitting..." << std::endl; } ModuleWriter writer; - writer.setDebug(options.debug); writer.setDebugInfo(options.passOptions.debugInfo); writer.setSymbolMap(symbolMap); writer.setBinary(emitBinary); diff --git a/src/tools/fuzzing.h b/src/tools/fuzzing.h index 571dad79e..733029bd7 100644 --- a/src/tools/fuzzing.h +++ b/src/tools/fuzzing.h @@ -65,8 +65,7 @@ class TranslateToFuzzReader { public: TranslateToFuzzReader(Module& wasm, std::string& filename) : wasm(wasm), builder(wasm) { - auto input( - read_file<std::vector<char>>(filename, Flags::Binary, Flags::Release)); + auto input(read_file<std::vector<char>>(filename, Flags::Binary)); readData(input); } diff --git a/src/tools/wasm-as.cpp b/src/tools/wasm-as.cpp index 2203aa83c..cb6a8f226 100644 --- a/src/tools/wasm-as.cpp +++ b/src/tools/wasm-as.cpp @@ -97,10 +97,7 @@ int main(int argc, const char* argv[]) { removeSpecificSuffix(options.extra["infile"], ".wast") + ".wasm"; } - auto input( - read_file<std::string>(options.extra["infile"], - Flags::Text, - options.debug ? Flags::Debug : Flags::Release)); + auto input(read_file<std::string>(options.extra["infile"], Flags::Text)); Module wasm; diff --git a/src/tools/wasm-ctor-eval.cpp b/src/tools/wasm-ctor-eval.cpp index af4d0d0b2..34451b871 100644 --- a/src/tools/wasm-ctor-eval.cpp +++ b/src/tools/wasm-ctor-eval.cpp @@ -433,10 +433,7 @@ int main(int argc, const char* argv[]) { }); options.parse(argc, argv); - auto input( - read_file<std::string>(options.extra["infile"], - Flags::Text, - options.debug ? Flags::Debug : Flags::Release)); + auto input(read_file<std::string>(options.extra["infile"], Flags::Text)); Module wasm; @@ -445,8 +442,6 @@ int main(int argc, const char* argv[]) { std::cerr << "reading...\n"; } ModuleReader reader; - reader.setDebug(options.debug); - try { reader.read(options.extra["infile"], wasm); } catch (ParseException& p) { @@ -488,7 +483,6 @@ int main(int argc, const char* argv[]) { std::cerr << "writing..." << std::endl; } ModuleWriter writer; - writer.setDebug(options.debug); writer.setBinary(emitBinary); writer.setDebugInfo(debugInfo); writer.write(wasm, options.extra["output"]); diff --git a/src/tools/wasm-dis.cpp b/src/tools/wasm-dis.cpp index 6c1ed1524..493a55566 100644 --- a/src/tools/wasm-dis.cpp +++ b/src/tools/wasm-dis.cpp @@ -75,9 +75,7 @@ int main(int argc, const char* argv[]) { if (options.debug) { std::cerr << "Printing..." << std::endl; } - Output output(options.extra["output"], - Flags::Text, - options.debug ? Flags::Debug : Flags::Release); + Output output(options.extra["output"], Flags::Text); WasmPrinter::printModule(&wasm, output.getStream()); output << '\n'; diff --git a/src/tools/wasm-emscripten-finalize.cpp b/src/tools/wasm-emscripten-finalize.cpp index 38904df12..c1d104c6a 100644 --- a/src/tools/wasm-emscripten-finalize.cpp +++ b/src/tools/wasm-emscripten-finalize.cpp @@ -277,7 +277,7 @@ int main(int argc, const char* argv[]) { // Finally, separate out data segments if relevant (they may have been needed // for metadata). if (!dataSegmentFile.empty()) { - Output memInitFile(dataSegmentFile, Flags::Binary, Flags::Release); + Output memInitFile(dataSegmentFile, Flags::Binary); if (globalBase == INVALID_BASE) { Fatal() << "globalBase must be set"; } @@ -296,10 +296,8 @@ int main(int argc, const char* argv[]) { passRunner.run(); } - auto outputBinaryFlag = emitBinary ? Flags::Binary : Flags::Text; - Output output(outfile, outputBinaryFlag, Flags::Release); + Output output(outfile, emitBinary ? Flags::Binary : Flags::Text); ModuleWriter writer; - writer.setDebug(options.debug); writer.setDebugInfo(debugInfo); // writer.setSymbolMap(symbolMap); writer.setBinary(emitBinary); diff --git a/src/tools/wasm-metadce.cpp b/src/tools/wasm-metadce.cpp index 1bdb18db2..6fa0477b3 100644 --- a/src/tools/wasm-metadce.cpp +++ b/src/tools/wasm-metadce.cpp @@ -489,8 +489,7 @@ int main(int argc, const char* argv[]) { Fatal() << "no graph file provided."; } - auto input(read_file<std::string>( - options.extra["infile"], Flags::Text, Flags::Release)); + auto input(read_file<std::string>(options.extra["infile"], Flags::Text)); Module wasm; @@ -499,8 +498,6 @@ int main(int argc, const char* argv[]) { std::cerr << "reading...\n"; } ModuleReader reader; - reader.setDebug(options.debug); - try { reader.read(options.extra["infile"], wasm); } catch (ParseException& p) { @@ -518,8 +515,7 @@ int main(int argc, const char* argv[]) { } } - auto graphInput( - read_file<std::string>(graphFile, Flags::Text, Flags::Release)); + auto graphInput(read_file<std::string>(graphFile, Flags::Text)); auto* copy = strdup(graphInput.c_str()); json::Value outside; outside.parse(copy); @@ -613,7 +609,6 @@ int main(int argc, const char* argv[]) { if (options.extra.count("output") > 0) { ModuleWriter writer; writer.setBinary(emitBinary); - writer.setDebugInfo(debugInfo); writer.write(wasm, options.extra["output"]); } diff --git a/src/tools/wasm-opt.cpp b/src/tools/wasm-opt.cpp index 1964019a5..7b04f5449 100644 --- a/src/tools/wasm-opt.cpp +++ b/src/tools/wasm-opt.cpp @@ -210,7 +210,6 @@ int main(int argc, const char* argv[]) { if (!translateToFuzz) { ModuleReader reader; - reader.setDebug(options.debug); try { reader.read(options.extra["infile"], wasm, inputSourceMapFilename); } catch (ParseException& p) { @@ -285,7 +284,6 @@ int main(int argc, const char* argv[]) { if (extraFuzzCommand.size() > 0 && options.extra.count("output") > 0) { BYN_TRACE("writing binary before opts, for extra fuzz command...\n"); ModuleWriter writer; - writer.setDebug(options.debug); writer.setBinary(emitBinary); writer.setDebugInfo(options.passOptions.debugInfo); writer.write(wasm, options.extra["output"]); @@ -297,13 +295,13 @@ int main(int argc, const char* argv[]) { Module other; if (fuzzExecAfter && fuzzBinary) { - BufferWithRandomAccess buffer(false); + BufferWithRandomAccess buffer; // write the binary - WasmBinaryWriter writer(&wasm, buffer, false); + WasmBinaryWriter writer(&wasm, buffer); writer.write(); // read the binary auto input = buffer.getAsChars(); - WasmBinaryBuilder parser(other, input, false); + WasmBinaryBuilder parser(other, input); parser.read(); options.applyFeatures(other); if (options.passOptions.validate) { @@ -368,7 +366,6 @@ int main(int argc, const char* argv[]) { BYN_TRACE("writing...\n"); ModuleWriter writer; - writer.setDebug(options.debug); writer.setBinary(emitBinary); writer.setDebugInfo(options.passOptions.debugInfo); if (outputSourceMapFilename.size()) { diff --git a/src/tools/wasm-shell.cpp b/src/tools/wasm-shell.cpp index b83989a2d..f9efcd245 100644 --- a/src/tools/wasm-shell.cpp +++ b/src/tools/wasm-shell.cpp @@ -276,10 +276,8 @@ int main(int argc, const char* argv[]) { }); options.parse(argc, argv); - auto input(read_file<std::vector<char>>(options.extra["infile"], - Flags::Text, - options.debug ? Flags::Debug - : Flags::Release)); + auto input( + read_file<std::vector<char>>(options.extra["infile"], Flags::Text)); bool checked = false; diff --git a/src/tools/wasm2js.cpp b/src/tools/wasm2js.cpp index 0eed845c1..b8e2daa39 100644 --- a/src/tools/wasm2js.cpp +++ b/src/tools/wasm2js.cpp @@ -882,13 +882,10 @@ int main(int argc, const char* argv[]) { // is defined. if (binaryInput) { ModuleReader reader; - reader.setDebug(options.debug); reader.read(input, wasm, ""); } else { - auto input(read_file<std::vector<char>>(options.extra["infile"], - Flags::Text, - options.debug ? Flags::Debug - : Flags::Release)); + auto input( + read_file<std::vector<char>>(options.extra["infile"], Flags::Text)); if (options.debug) { std::cerr << "s-parsing..." << std::endl; } @@ -918,9 +915,7 @@ int main(int argc, const char* argv[]) { if (options.debug) { std::cerr << "j-printing..." << std::endl; } - Output output(options.extra["output"], - Flags::Text, - options.debug ? Flags::Debug : Flags::Release); + Output output(options.extra["output"], Flags::Text); if (!binaryInput && options.extra["asserts"] == "1") { AssertionEmitter(*root, *sexprBuilder, output, flags, options).emit(); } else { diff --git a/src/wasm-binary.h b/src/wasm-binary.h index 78fd72e38..a901142b2 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -30,11 +30,14 @@ #include "ir/import-utils.h" #include "ir/module-utils.h" #include "parsing.h" +#include "support/debug.h" #include "wasm-builder.h" #include "wasm-traversal.h" #include "wasm-validator.h" #include "wasm.h" +#define DEBUG_TYPE "binary" + namespace wasm { enum { @@ -156,31 +159,22 @@ typedef LEB<int64_t, int8_t> S64LEB; // is optimized for reading, not writing. // class BufferWithRandomAccess : public std::vector<uint8_t> { - bool debug; - public: - BufferWithRandomAccess(bool debug = false) : debug(debug) {} + BufferWithRandomAccess() = default; BufferWithRandomAccess& operator<<(int8_t x) { - if (debug) { - std::cerr << "writeInt8: " << (int)(uint8_t)x << " (at " << size() << ")" - << std::endl; - } + BYN_TRACE("writeInt8: " << (int)(uint8_t)x << " (at " << size() << ")\n"); push_back(x); return *this; } BufferWithRandomAccess& operator<<(int16_t x) { - if (debug) { - std::cerr << "writeInt16: " << x << " (at " << size() << ")" << std::endl; - } + BYN_TRACE("writeInt16: " << x << " (at " << size() << ")\n"); push_back(x & 0xff); push_back(x >> 8); return *this; } BufferWithRandomAccess& operator<<(int32_t x) { - if (debug) { - std::cerr << "writeInt32: " << x << " (at " << size() << ")" << std::endl; - } + BYN_TRACE("writeInt32: " << x << " (at " << size() << ")\n"); push_back(x & 0xff); x >>= 8; push_back(x & 0xff); @@ -191,9 +185,7 @@ public: return *this; } BufferWithRandomAccess& operator<<(int64_t x) { - if (debug) { - std::cerr << "writeInt64: " << x << " (at " << size() << ")" << std::endl; - } + BYN_TRACE("writeInt64: " << x << " (at " << size() << ")\n"); push_back(x & 0xff); x >>= 8; push_back(x & 0xff); @@ -213,62 +205,46 @@ public: } BufferWithRandomAccess& operator<<(U32LEB x) { size_t before = -1; - if (debug) { - before = size(); - std::cerr << "writeU32LEB: " << x.value << " (at " << before << ")" - << std::endl; - } + BYN_DEBUG(before = size(); std::cerr << "writeU32LEB: " << x.value + << " (at " << before << ")" + << std::endl;); x.write(this); - if (debug) { - for (size_t i = before; i < size(); i++) { - std::cerr << " " << (int)at(i) << " (at " << i << ")\n"; - } - } + BYN_DEBUG(for (size_t i = before; i < size(); i++) { + std::cerr << " " << (int)at(i) << " (at " << i << ")\n"; + }); return *this; } BufferWithRandomAccess& operator<<(U64LEB x) { size_t before = -1; - if (debug) { - before = size(); - std::cerr << "writeU64LEB: " << x.value << " (at " << before << ")" - << std::endl; - } + BYN_DEBUG(before = size(); std::cerr << "writeU64LEB: " << x.value + << " (at " << before << ")" + << std::endl;); x.write(this); - if (debug) { - for (size_t i = before; i < size(); i++) { - std::cerr << " " << (int)at(i) << " (at " << i << ")\n"; - } - } + BYN_DEBUG(for (size_t i = before; i < size(); i++) { + std::cerr << " " << (int)at(i) << " (at " << i << ")\n"; + }); return *this; } BufferWithRandomAccess& operator<<(S32LEB x) { size_t before = -1; - if (debug) { - before = size(); - std::cerr << "writeS32LEB: " << x.value << " (at " << before << ")" - << std::endl; - } + BYN_DEBUG(before = size(); std::cerr << "writeS32LEB: " << x.value + << " (at " << before << ")" + << std::endl;); x.write(this); - if (debug) { - for (size_t i = before; i < size(); i++) { - std::cerr << " " << (int)at(i) << " (at " << i << ")\n"; - } - } + BYN_DEBUG(for (size_t i = before; i < size(); i++) { + std::cerr << " " << (int)at(i) << " (at " << i << ")\n"; + }); return *this; } BufferWithRandomAccess& operator<<(S64LEB x) { size_t before = -1; - if (debug) { - before = size(); - std::cerr << "writeS64LEB: " << x.value << " (at " << before << ")" - << std::endl; - } + BYN_DEBUG(before = size(); std::cerr << "writeS64LEB: " << x.value + << " (at " << before << ")" + << std::endl;); x.write(this); - if (debug) { - for (size_t i = before; i < size(); i++) { - std::cerr << " " << (int)at(i) << " (at " << i << ")\n"; - } - } + BYN_DEBUG(for (size_t i = before; i < size(); i++) { + std::cerr << " " << (int)at(i) << " (at " << i << ")\n"; + }); return *this; } @@ -278,31 +254,21 @@ public: BufferWithRandomAccess& operator<<(uint64_t x) { return *this << (int64_t)x; } BufferWithRandomAccess& operator<<(float x) { - if (debug) { - std::cerr << "writeFloat32: " << x << " (at " << size() << ")" - << std::endl; - } + BYN_TRACE("writeFloat32: " << x << " (at " << size() << ")\n"); return *this << Literal(x).reinterpreti32(); } BufferWithRandomAccess& operator<<(double x) { - if (debug) { - std::cerr << "writeFloat64: " << x << " (at " << size() << ")" - << std::endl; - } + BYN_TRACE("writeFloat64: " << x << " (at " << size() << ")\n"); return *this << Literal(x).reinterpreti64(); } void writeAt(size_t i, uint16_t x) { - if (debug) { - std::cerr << "backpatchInt16: " << x << " (at " << i << ")" << std::endl; - } + BYN_TRACE("backpatchInt16: " << x << " (at " << i << ")\n"); (*this)[i] = x & 0xff; (*this)[i + 1] = x >> 8; } void writeAt(size_t i, uint32_t x) { - if (debug) { - std::cerr << "backpatchInt32: " << x << " (at " << i << ")" << std::endl; - } + BYN_TRACE("backpatchInt32: " << x << " (at " << i << ")\n"); (*this)[i] = x & 0xff; x >>= 8; (*this)[i + 1] = x & 0xff; @@ -315,20 +281,14 @@ public: // writes out an LEB to an arbitrary location. this writes the LEB as a full // 5 bytes, the fixed amount that can easily be set aside ahead of time void writeAtFullFixedSize(size_t i, U32LEB x) { - if (debug) { - std::cerr << "backpatchU32LEB: " << x.value << " (at " << i << ")" - << std::endl; - } + BYN_TRACE("backpatchU32LEB: " << x.value << " (at " << i << ")\n"); // fill all 5 bytes, we have to do this when backpatching x.writeAt(this, i, MaxLEB32Bytes); } // writes out an LEB of normal size // returns how many bytes were written size_t writeAt(size_t i, U32LEB x) { - if (debug) { - std::cerr << "writeAtU32LEB: " << x.value << " (at " << i << ")" - << std::endl; - } + BYN_TRACE("writeAtU32LEB: " << x.value << " (at " << i << ")\n"); return x.writeAt(this, i); } @@ -964,8 +924,8 @@ inline S32LEB binaryType(Type type) { class WasmBinaryWriter { public: - WasmBinaryWriter(Module* input, BufferWithRandomAccess& o, bool debug = false) - : wasm(input), o(o), debug(debug), indexes(*input) { + WasmBinaryWriter(Module* input, BufferWithRandomAccess& o) + : wasm(input), o(o), indexes(*input) { prepare(); } @@ -1058,7 +1018,6 @@ public: private: Module* wasm; BufferWithRandomAccess& o; - bool debug; ModuleUtils::BinaryIndexes indexes; std::unordered_map<Signature, Index> typeIndexes; std::vector<Signature> types; @@ -1086,7 +1045,6 @@ class WasmBinaryBuilder { Module& wasm; MixedArena& allocator; const std::vector<char>& input; - bool debug; std::istream* sourceMap; std::pair<uint32_t, Function::DebugLocation> nextDebugLocation; @@ -1097,9 +1055,9 @@ class WasmBinaryBuilder { std::set<BinaryConsts::Section> seenSections; public: - WasmBinaryBuilder(Module& wasm, const std::vector<char>& input, bool debug) - : wasm(wasm), allocator(wasm.allocator), input(input), debug(debug), - sourceMap(nullptr), nextDebugLocation(0, {0, 0, 0}), debugLocation() {} + WasmBinaryBuilder(Module& wasm, const std::vector<char>& input) + : wasm(wasm), allocator(wasm.allocator), input(input), sourceMap(nullptr), + nextDebugLocation(0, {0, 0, 0}), debugLocation() {} void read(); void readUserSection(size_t payloadLen); @@ -1311,4 +1269,6 @@ public: } // namespace wasm +#undef DEBUG_TYPE + #endif // wasm_wasm_binary_h diff --git a/src/wasm-io.h b/src/wasm-io.h index 9c772f115..ad6b358fd 100644 --- a/src/wasm-io.h +++ b/src/wasm-io.h @@ -27,15 +27,7 @@ namespace wasm { -class ModuleIO { -protected: - bool debug = false; - -public: - void setDebug(bool debug_) { debug = debug_; } -}; - -class ModuleReader : public ModuleIO { +class ModuleReader { public: // read text void readText(std::string filename, Module& wasm); @@ -54,7 +46,7 @@ private: void readStdin(Module& wasm, std::string sourceMapFilename); }; -class ModuleWriter : public ModuleIO { +class ModuleWriter { bool binary = true; bool debugInfo = false; std::string symbolMap; diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 52380041d..bb8b96aa4 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -19,9 +19,12 @@ #include "ir/module-utils.h" #include "support/bits.h" +#include "support/debug.h" #include "wasm-binary.h" #include "wasm-stack.h" +#define DEBUG_TYPE "binary" + namespace wasm { void WasmBinaryWriter::prepare() { @@ -124,9 +127,7 @@ void WasmBinaryWriter::write() { } void WasmBinaryWriter::writeHeader() { - if (debug) { - std::cerr << "== writeHeader" << std::endl; - } + BYN_TRACE("== writeHeader\n"); o << int32_t(BinaryConsts::Magic); // magic number \0asm o << int32_t(BinaryConsts::Version); } @@ -192,9 +193,7 @@ void WasmBinaryWriter::writeStart() { if (!wasm->start.is()) { return; } - if (debug) { - std::cerr << "== writeStart" << std::endl; - } + BYN_TRACE("== writeStart\n"); auto start = startSection(BinaryConsts::Section::Start); o << U32LEB(getFunctionIndex(wasm->start.str)); finishSection(start); @@ -204,9 +203,7 @@ void WasmBinaryWriter::writeMemory() { if (!wasm->memory.exists || wasm->memory.imported()) { return; } - if (debug) { - std::cerr << "== writeMemory" << std::endl; - } + BYN_TRACE("== writeMemory\n"); auto start = startSection(BinaryConsts::Section::Memory); o << U32LEB(1); // Define 1 memory writeResizableLimits(wasm->memory.initial, @@ -220,16 +217,12 @@ void WasmBinaryWriter::writeTypes() { if (types.size() == 0) { return; } - if (debug) { - std::cerr << "== writeTypes" << std::endl; - } + BYN_TRACE("== writeTypes\n"); auto start = startSection(BinaryConsts::Section::Type); o << U32LEB(types.size()); for (Index i = 0; i < types.size(); ++i) { Signature& sig = types[i]; - if (debug) { - std::cerr << "write " << sig.params << " -> " << sig.results << std::endl; - } + BYN_TRACE("write " << sig.params << " -> " << sig.results << std::endl); o << S32LEB(BinaryConsts::EncodedType::Func); for (auto& sigType : {sig.params, sig.results}) { o << U32LEB(sigType.size()); @@ -246,9 +239,7 @@ void WasmBinaryWriter::writeImports() { if (num == 0) { return; } - if (debug) { - std::cerr << "== writeImports" << std::endl; - } + BYN_TRACE("== writeImports\n"); auto start = startSection(BinaryConsts::Section::Import); o << U32LEB(num); auto writeImportHeader = [&](Importable* import) { @@ -256,35 +247,27 @@ void WasmBinaryWriter::writeImports() { writeInlineString(import->base.str); }; ModuleUtils::iterImportedFunctions(*wasm, [&](Function* func) { - if (debug) { - std::cerr << "write one function" << std::endl; - } + BYN_TRACE("write one function\n"); writeImportHeader(func); o << U32LEB(int32_t(ExternalKind::Function)); o << U32LEB(getTypeIndex(Signature(Type(func->params), func->result))); }); ModuleUtils::iterImportedGlobals(*wasm, [&](Global* global) { - if (debug) { - std::cerr << "write one global" << std::endl; - } + BYN_TRACE("write one global\n"); writeImportHeader(global); o << U32LEB(int32_t(ExternalKind::Global)); o << binaryType(global->type); o << U32LEB(global->mutable_); }); ModuleUtils::iterImportedEvents(*wasm, [&](Event* event) { - if (debug) { - std::cerr << "write one event" << std::endl; - } + BYN_TRACE("write one event\n"); writeImportHeader(event); o << U32LEB(int32_t(ExternalKind::Event)); o << U32LEB(event->attribute); o << U32LEB(getTypeIndex(event->sig)); }); if (wasm->memory.imported()) { - if (debug) { - std::cerr << "write one memory" << std::endl; - } + BYN_TRACE("write one memory\n"); writeImportHeader(&wasm->memory); o << U32LEB(int32_t(ExternalKind::Memory)); writeResizableLimits(wasm->memory.initial, @@ -293,9 +276,7 @@ void WasmBinaryWriter::writeImports() { wasm->memory.shared); } if (wasm->table.imported()) { - if (debug) { - std::cerr << "write one table" << std::endl; - } + BYN_TRACE("write one table\n"); writeImportHeader(&wasm->table); o << U32LEB(int32_t(ExternalKind::Table)); o << S32LEB(BinaryConsts::EncodedType::AnyFunc); @@ -311,15 +292,11 @@ void WasmBinaryWriter::writeFunctionSignatures() { if (importInfo->getNumDefinedFunctions() == 0) { return; } - if (debug) { - std::cerr << "== writeFunctionSignatures" << std::endl; - } + BYN_TRACE("== writeFunctionSignatures\n"); auto start = startSection(BinaryConsts::Section::Function); o << U32LEB(importInfo->getNumDefinedFunctions()); ModuleUtils::iterDefinedFunctions(*wasm, [&](Function* func) { - if (debug) { - std::cerr << "write one" << std::endl; - } + BYN_TRACE("write one\n"); o << U32LEB(getTypeIndex(Signature(Type(func->params), func->result))); }); finishSection(start); @@ -333,39 +310,27 @@ void WasmBinaryWriter::writeFunctions() { if (importInfo->getNumDefinedFunctions() == 0) { return; } - if (debug) { - std::cerr << "== writeFunctions" << std::endl; - } + BYN_TRACE("== writeFunctions\n"); auto start = startSection(BinaryConsts::Section::Code); o << U32LEB(importInfo->getNumDefinedFunctions()); ModuleUtils::iterDefinedFunctions(*wasm, [&](Function* func) { size_t sourceMapLocationsSizeAtFunctionStart = sourceMapLocations.size(); - if (debug) { - std::cerr << "write one at" << o.size() << std::endl; - } + BYN_TRACE("write one at" << o.size() << std::endl); size_t sizePos = writeU32LEBPlaceholder(); size_t start = o.size(); - if (debug) { - std::cerr << "writing" << func->name << std::endl; - } + BYN_TRACE("writing" << func->name << std::endl); // Emit Stack IR if present, and if we can if (func->stackIR && !sourceMap) { - if (debug) { - std::cerr << "write Stack IR" << std::endl; - } + BYN_TRACE("write Stack IR\n"); StackIRToBinaryWriter(*this, o, func).write(); } else { - if (debug) { - std::cerr << "write Binaryen IR" << std::endl; - } + BYN_TRACE("write Binaryen IR\n"); BinaryenIRToBinaryWriter(*this, o, func, sourceMap).write(); } size_t size = o.size() - start; assert(size <= std::numeric_limits<uint32_t>::max()); - if (debug) { - std::cerr << "body size: " << size << ", writing at " << sizePos - << ", next starts at " << o.size() << std::endl; - } + BYN_TRACE("body size: " << size << ", writing at " << sizePos + << ", next starts at " << o.size() << "\n"); auto sizeFieldSize = o.writeAt(sizePos, U32LEB(size)); if (sizeFieldSize != MaxLEB32Bytes) { // we can save some room, nice @@ -391,16 +356,12 @@ void WasmBinaryWriter::writeGlobals() { if (importInfo->getNumDefinedGlobals() == 0) { return; } - if (debug) { - std::cerr << "== writeglobals" << std::endl; - } + BYN_TRACE("== writeglobals\n"); auto start = startSection(BinaryConsts::Section::Global); auto num = importInfo->getNumDefinedGlobals(); o << U32LEB(num); ModuleUtils::iterDefinedGlobals(*wasm, [&](Global* global) { - if (debug) { - std::cerr << "write one" << std::endl; - } + BYN_TRACE("write one\n"); o << binaryType(global->type); o << U32LEB(global->mutable_); writeExpression(global->init); @@ -413,15 +374,11 @@ void WasmBinaryWriter::writeExports() { if (wasm->exports.size() == 0) { return; } - if (debug) { - std::cerr << "== writeexports" << std::endl; - } + BYN_TRACE("== writeexports\n"); auto start = startSection(BinaryConsts::Section::Export); o << U32LEB(wasm->exports.size()); for (auto& curr : wasm->exports) { - if (debug) { - std::cerr << "write one" << std::endl; - } + BYN_TRACE("write one\n"); writeInlineString(curr->name.str); o << U32LEB(int32_t(curr->kind)); switch (curr->kind) { @@ -463,7 +420,7 @@ void WasmBinaryWriter::writeDataSegments() { if (wasm->memory.segments.size() > WebLimitations::MaxDataSegments) { std::cerr << "Some VMs may not accept this binary because it has a large " << "number of data segments. Run the limit-segments pass to " - << "merge segments." << std::endl; + << "merge segments.\n"; } auto start = startSection(BinaryConsts::Section::Data); o << U32LEB(wasm->memory.segments.size()); @@ -510,9 +467,7 @@ void WasmBinaryWriter::writeFunctionTableDeclaration() { if (!wasm->table.exists || wasm->table.imported()) { return; } - if (debug) { - std::cerr << "== writeFunctionTableDeclaration" << std::endl; - } + BYN_TRACE("== writeFunctionTableDeclaration\n"); auto start = startSection(BinaryConsts::Section::Table); o << U32LEB(1); // Declare 1 table. o << S32LEB(BinaryConsts::EncodedType::AnyFunc); @@ -527,9 +482,7 @@ void WasmBinaryWriter::writeTableElements() { if (!wasm->table.exists || wasm->table.segments.size() == 0) { return; } - if (debug) { - std::cerr << "== writeTableElements" << std::endl; - } + BYN_TRACE("== writeTableElements\n"); auto start = startSection(BinaryConsts::Section::Element); o << U32LEB(wasm->table.segments.size()); @@ -550,16 +503,12 @@ void WasmBinaryWriter::writeEvents() { if (importInfo->getNumDefinedEvents() == 0) { return; } - if (debug) { - std::cerr << "== writeEvents" << std::endl; - } + BYN_TRACE("== writeEvents\n"); auto start = startSection(BinaryConsts::Section::Event); auto num = importInfo->getNumDefinedEvents(); o << U32LEB(num); ModuleUtils::iterDefinedEvents(*wasm, [&](Event* event) { - if (debug) { - std::cerr << "write one" << std::endl; - } + BYN_TRACE("write one\n"); o << U32LEB(event->attribute); o << U32LEB(getTypeIndex(event->sig)); }); @@ -572,9 +521,7 @@ void WasmBinaryWriter::writeNames() { return; } - if (debug) { - std::cerr << "== writeNames" << std::endl; - } + BYN_TRACE("== writeNames\n"); auto start = startSection(BinaryConsts::Section::User); writeInlineString(BinaryConsts::UserSections::Name); auto substart = @@ -595,9 +542,7 @@ void WasmBinaryWriter::writeNames() { } void WasmBinaryWriter::writeSourceMapUrl() { - if (debug) { - std::cerr << "== writeSourceMapUrl" << std::endl; - } + BYN_TRACE("== writeSourceMapUrl\n"); auto start = startSection(BinaryConsts::Section::User); writeInlineString(BinaryConsts::UserSections::SourceMapUrl); writeInlineString(sourceMapUrl.c_str()); @@ -814,23 +759,18 @@ void WasmBinaryWriter::emitBuffer(const char* data, size_t size) { } void WasmBinaryWriter::emitString(const char* str) { - if (debug) { - std::cerr << "emitString " << str << std::endl; - } + BYN_TRACE("emitString " << str << std::endl); emitBuffer(str, strlen(str) + 1); } void WasmBinaryWriter::finishUp() { - if (debug) { - std::cerr << "finishUp" << std::endl; - } + BYN_TRACE("finishUp\n"); // finish buffers for (const auto& buffer : buffersToWrite) { - if (debug) { - std::cerr << "writing buffer" << (int)buffer.data[0] << "," - << (int)buffer.data[1] << " at " << o.size() - << " and pointer is at " << buffer.pointerLocation << std::endl; - } + BYN_TRACE("writing buffer" + << (int)buffer.data[0] << "," << (int)buffer.data[1] << " at " + << o.size() << " and pointer is at " << buffer.pointerLocation + << "\n"); o.writeAt(buffer.pointerLocation, (uint32_t)o.size()); for (size_t i = 0; i < buffer.size; i++) { o << (uint8_t)buffer.data[i]; @@ -963,151 +903,103 @@ uint8_t WasmBinaryBuilder::getInt8() { if (!more()) { throwError("unexpected end of input"); } - if (debug) { - std::cerr << "getInt8: " << (int)(uint8_t)input[pos] << " (at " << pos - << ")" << std::endl; - } + BYN_TRACE("getInt8: " << (int)(uint8_t)input[pos] << " (at " << pos << ")\n"); return input[pos++]; } uint16_t WasmBinaryBuilder::getInt16() { - if (debug) { - std::cerr << "<==" << std::endl; - } + BYN_TRACE("<==\n"); auto ret = uint16_t(getInt8()); ret |= uint16_t(getInt8()) << 8; - if (debug) { - std::cerr << "getInt16: " << ret << "/0x" << std::hex << ret << std::dec - << " ==>" << std::endl; - } + BYN_TRACE("getInt16: " << ret << "/0x" << std::hex << ret << std::dec + << " ==>\n"); return ret; } uint32_t WasmBinaryBuilder::getInt32() { - if (debug) { - std::cerr << "<==" << std::endl; - } + BYN_TRACE("<==\n"); auto ret = uint32_t(getInt16()); ret |= uint32_t(getInt16()) << 16; - if (debug) { - std::cerr << "getInt32: " << ret << "/0x" << std::hex << ret << std::dec - << " ==>" << std::endl; - } + BYN_TRACE("getInt32: " << ret << "/0x" << std::hex << ret << std::dec + << " ==>\n"); return ret; } uint64_t WasmBinaryBuilder::getInt64() { - if (debug) { - std::cerr << "<==" << std::endl; - } + BYN_TRACE("<==\n"); auto ret = uint64_t(getInt32()); ret |= uint64_t(getInt32()) << 32; - if (debug) { - std::cerr << "getInt64: " << ret << "/0x" << std::hex << ret << std::dec - << " ==>" << std::endl; - } + BYN_TRACE("getInt64: " << ret << "/0x" << std::hex << ret << std::dec + << " ==>\n"); return ret; } uint8_t WasmBinaryBuilder::getLaneIndex(size_t lanes) { - if (debug) { - std::cerr << "<==" << std::endl; - } + BYN_TRACE("<==\n"); auto ret = getInt8(); if (ret >= lanes) { throwError("Illegal lane index"); } - if (debug) { - std::cerr << "getLaneIndex(" << lanes << "): " << ret << " ==>" - << std::endl; - } + BYN_TRACE("getLaneIndex(" << lanes << "): " << ret << " ==>" << std::endl); return ret; } Literal WasmBinaryBuilder::getFloat32Literal() { - if (debug) { - std::cerr << "<==" << std::endl; - } + BYN_TRACE("<==\n"); auto ret = Literal(getInt32()); ret = ret.castToF32(); - if (debug) { - std::cerr << "getFloat32: " << ret << " ==>" << std::endl; - } + BYN_TRACE("getFloat32: " << ret << " ==>\n"); return ret; } Literal WasmBinaryBuilder::getFloat64Literal() { - if (debug) { - std::cerr << "<==" << std::endl; - } + BYN_TRACE("<==\n"); auto ret = Literal(getInt64()); ret = ret.castToF64(); - if (debug) { - std::cerr << "getFloat64: " << ret << " ==>" << std::endl; - } + BYN_TRACE("getFloat64: " << ret << " ==>\n"); return ret; } Literal WasmBinaryBuilder::getVec128Literal() { - if (debug) { - std::cerr << "<==" << std::endl; - } + BYN_TRACE("<==\n"); std::array<uint8_t, 16> bytes; for (auto i = 0; i < 16; ++i) { bytes[i] = getInt8(); } auto ret = Literal(bytes.data()); - if (debug) { - std::cerr << "getVec128: " << ret << " ==>" << std::endl; - } + BYN_TRACE("getVec128: " << ret << " ==>\n"); return ret; } uint32_t WasmBinaryBuilder::getU32LEB() { - if (debug) { - std::cerr << "<==" << std::endl; - } + BYN_TRACE("<==\n"); U32LEB ret; ret.read([&]() { return getInt8(); }); - if (debug) { - std::cerr << "getU32LEB: " << ret.value << " ==>" << std::endl; - } + BYN_TRACE("getU32LEB: " << ret.value << " ==>\n"); return ret.value; } uint64_t WasmBinaryBuilder::getU64LEB() { - if (debug) { - std::cerr << "<==" << std::endl; - } + BYN_TRACE("<==\n"); U64LEB ret; ret.read([&]() { return getInt8(); }); - if (debug) { - std::cerr << "getU64LEB: " << ret.value << " ==>" << std::endl; - } + BYN_TRACE("getU64LEB: " << ret.value << " ==>\n"); return ret.value; } int32_t WasmBinaryBuilder::getS32LEB() { - if (debug) { - std::cerr << "<==" << std::endl; - } + BYN_TRACE("<==\n"); S32LEB ret; ret.read([&]() { return (int8_t)getInt8(); }); - if (debug) { - std::cerr << "getS32LEB: " << ret.value << " ==>" << std::endl; - } + BYN_TRACE("getS32LEB: " << ret.value << " ==>\n"); return ret.value; } int64_t WasmBinaryBuilder::getS64LEB() { - if (debug) { - std::cerr << "<==" << std::endl; - } + BYN_TRACE("<==\n"); S64LEB ret; ret.read([&]() { return (int8_t)getInt8(); }); - if (debug) { - std::cerr << "getS64LEB: " << ret.value << " ==>" << std::endl; - } + BYN_TRACE("getS64LEB: " << ret.value << " ==>\n"); return ret.value; } @@ -1145,9 +1037,7 @@ Type WasmBinaryBuilder::getConcreteType() { } Name WasmBinaryBuilder::getInlineString() { - if (debug) { - std::cerr << "<==" << std::endl; - } + BYN_TRACE("<==\n"); auto len = getU32LEB(); std::string str; for (size_t i = 0; i < len; i++) { @@ -1159,9 +1049,7 @@ Name WasmBinaryBuilder::getInlineString() { } str = str + curr; } - if (debug) { - std::cerr << "getInlineString: " << str << " ==>" << std::endl; - } + BYN_TRACE("getInlineString: " << str << " ==>\n"); return Name(str); } @@ -1195,31 +1083,23 @@ void WasmBinaryBuilder::verifyInt64(int64_t x) { void WasmBinaryBuilder::ungetInt8() { assert(pos > 0); - if (debug) { - std::cerr << "ungetInt8 (at " << pos << ")" << std::endl; - } + BYN_TRACE("ungetInt8 (at " << pos << ")\n"); pos--; } void WasmBinaryBuilder::readHeader() { - if (debug) { - std::cerr << "== readHeader" << std::endl; - } + BYN_TRACE("== readHeader\n"); verifyInt32(BinaryConsts::Magic); verifyInt32(BinaryConsts::Version); } void WasmBinaryBuilder::readStart() { - if (debug) { - std::cerr << "== readStart" << std::endl; - } + BYN_TRACE("== readStart\n"); startIndex = getU32LEB(); } void WasmBinaryBuilder::readMemory() { - if (debug) { - std::cerr << "== readMemory" << std::endl; - } + BYN_TRACE("== readMemory\n"); auto numMemories = getU32LEB(); if (!numMemories) { return; @@ -1238,26 +1118,18 @@ void WasmBinaryBuilder::readMemory() { } void WasmBinaryBuilder::readSignatures() { - if (debug) { - std::cerr << "== readSignatures" << std::endl; - } + BYN_TRACE("== readSignatures\n"); size_t numTypes = getU32LEB(); - if (debug) { - std::cerr << "num: " << numTypes << std::endl; - } + BYN_TRACE("num: " << numTypes << std::endl); for (size_t i = 0; i < numTypes; i++) { - if (debug) { - std::cerr << "read one" << std::endl; - } + BYN_TRACE("read one\n"); auto curr = make_unique<FunctionType>(); auto form = getS32LEB(); if (form != BinaryConsts::EncodedType::Func) { throwError("bad signature form " + std::to_string(form)); } size_t numParams = getU32LEB(); - if (debug) { - std::cerr << "num params: " << numParams << std::endl; - } + BYN_TRACE("num params: " << numParams << std::endl); for (size_t j = 0; j < numParams; j++) { curr->params.push_back(getConcreteType()); } @@ -1316,18 +1188,12 @@ void WasmBinaryBuilder::getResizableLimits(Address& initial, } void WasmBinaryBuilder::readImports() { - if (debug) { - std::cerr << "== readImports" << std::endl; - } + BYN_TRACE("== readImports\n"); size_t num = getU32LEB(); - if (debug) { - std::cerr << "num: " << num << std::endl; - } + BYN_TRACE("num: " << num << std::endl); Builder builder(wasm); for (size_t i = 0; i < num; i++) { - if (debug) { - std::cerr << "read one" << std::endl; - } + BYN_TRACE("read one\n"); auto module = getInlineString(); auto base = getInlineString(); auto kind = (ExternalKind)getU32LEB(); @@ -1429,17 +1295,11 @@ void WasmBinaryBuilder::requireFunctionContext(const char* error) { } void WasmBinaryBuilder::readFunctionSignatures() { - if (debug) { - std::cerr << "== readFunctionSignatures" << std::endl; - } + BYN_TRACE("== readFunctionSignatures\n"); size_t num = getU32LEB(); - if (debug) { - std::cerr << "num: " << num << std::endl; - } + BYN_TRACE("num: " << num << std::endl); for (size_t i = 0; i < num; i++) { - if (debug) { - std::cerr << "read one" << std::endl; - } + BYN_TRACE("read one\n"); auto index = getU32LEB(); if (index >= wasm.functionTypes.size()) { throwError("invalid function type index for function"); @@ -1449,17 +1309,13 @@ void WasmBinaryBuilder::readFunctionSignatures() { } void WasmBinaryBuilder::readFunctions() { - if (debug) { - std::cerr << "== readFunctions" << std::endl; - } + BYN_TRACE("== readFunctions\n"); size_t total = getU32LEB(); if (total != functionTypes.size()) { throwError("invalid function section size, must equal types"); } for (size_t i = 0; i < total; i++) { - if (debug) { - std::cerr << "read one at " << pos << std::endl; - } + BYN_TRACE("read one at " << pos << std::endl); size_t size = getU32LEB(); if (size == 0) { throwError("empty function size"); @@ -1473,9 +1329,7 @@ void WasmBinaryBuilder::readFunctions() { readNextDebugLocation(); auto type = functionTypes[i]; - if (debug) { - std::cerr << "reading " << i << std::endl; - } + BYN_TRACE("reading " << i << std::endl); func->type = type->name; func->result = type->result; for (size_t j = 0; j < type->params.size(); j++) { @@ -1493,9 +1347,7 @@ void WasmBinaryBuilder::readFunctions() { std::swap(func->prologLocation, debugLocation); { // process the function body - if (debug) { - std::cerr << "processing function: " << i << std::endl; - } + BYN_TRACE("processing function: " << i << std::endl); nextLabel = 0; debugLocation.clear(); willBeIgnored = false; @@ -1520,24 +1372,16 @@ void WasmBinaryBuilder::readFunctions() { debugLocation.clear(); functions.push_back(func); } - if (debug) { - std::cerr << " end function bodies" << std::endl; - } + BYN_TRACE(" end function bodies\n"); } void WasmBinaryBuilder::readExports() { - if (debug) { - std::cerr << "== readExports" << std::endl; - } + BYN_TRACE("== readExports\n"); size_t num = getU32LEB(); - if (debug) { - std::cerr << "num: " << num << std::endl; - } + BYN_TRACE("num: " << num << std::endl); std::set<Name> names; for (size_t i = 0; i < num; i++) { - if (debug) { - std::cerr << "read one" << std::endl; - } + BYN_TRACE("read one\n"); auto curr = new Export; curr->name = getInlineString(); if (names.count(curr->name) > 0) { @@ -1743,17 +1587,11 @@ Expression* WasmBinaryBuilder::readExpression() { } void WasmBinaryBuilder::readGlobals() { - if (debug) { - std::cerr << "== readGlobals" << std::endl; - } + BYN_TRACE("== readGlobals\n"); size_t num = getU32LEB(); - if (debug) { - std::cerr << "num: " << num << std::endl; - } + BYN_TRACE("num: " << num << std::endl); for (size_t i = 0; i < num; i++) { - if (debug) { - std::cerr << "read one" << std::endl; - } + BYN_TRACE("read one\n"); auto type = getConcreteType(); auto mutable_ = getU32LEB(); if (mutable_ & ~1) { @@ -1769,18 +1607,14 @@ void WasmBinaryBuilder::readGlobals() { } void WasmBinaryBuilder::processExpressions() { - if (debug) { - std::cerr << "== processExpressions" << std::endl; - } + BYN_TRACE("== processExpressions\n"); unreachableInTheWasmSense = false; while (1) { Expression* curr; auto ret = readExpression(curr); if (!curr) { lastSeparator = ret; - if (debug) { - std::cerr << "== processExpressions finished" << std::endl; - } + BYN_TRACE("== processExpressions finished\n"); return; } expressionStack.push_back(curr); @@ -1799,10 +1633,8 @@ void WasmBinaryBuilder::processExpressions() { auto peek = input[pos]; if (peek == BinaryConsts::End || peek == BinaryConsts::Else || peek == BinaryConsts::Catch) { - if (debug) { - std::cerr << "== processExpressions finished with unreachable" - << std::endl; - } + BYN_TRACE("== processExpressions finished with unreachable" + << std::endl); readNextDebugLocation(); lastSeparator = BinaryConsts::ASTNodes(peek); pos++; @@ -1816,9 +1648,7 @@ void WasmBinaryBuilder::processExpressions() { } void WasmBinaryBuilder::skipUnreachableCode() { - if (debug) { - std::cerr << "== skipUnreachableCode" << std::endl; - } + BYN_TRACE("== skipUnreachableCode\n"); // preserve the stack, and restore it. it contains the instruction that made // us unreachable, and we can ignore anything after it. things after it may // pop, we want to undo that @@ -1838,9 +1668,7 @@ void WasmBinaryBuilder::skipUnreachableCode() { Expression* curr; auto ret = readExpression(curr); if (!curr) { - if (debug) { - std::cerr << "== skipUnreachableCode finished" << std::endl; - } + BYN_TRACE("== skipUnreachableCode finished\n"); lastSeparator = ret; unreachableInTheWasmSense = false; willBeIgnored = before; @@ -1852,17 +1680,12 @@ void WasmBinaryBuilder::skipUnreachableCode() { } Expression* WasmBinaryBuilder::popExpression() { - if (debug) { - std::cerr << "== popExpression" << std::endl; - } + BYN_TRACE("== popExpression\n"); if (expressionStack.empty()) { if (unreachableInTheWasmSense) { // in unreachable code, trying to pop past the polymorphic stack // area results in receiving unreachables - if (debug) { - std::cerr << "== popping unreachable from polymorphic stack" - << std::endl; - } + BYN_TRACE("== popping unreachable from polymorphic stack" << std::endl); return allocator.alloc<Unreachable>(); } throwError( @@ -1975,17 +1798,13 @@ void WasmBinaryBuilder::processFunctions() { } void WasmBinaryBuilder::readDataCount() { - if (debug) { - std::cerr << "== readDataCount" << std::endl; - } + BYN_TRACE("== readDataCount\n"); hasDataCount = true; dataCount = getU32LEB(); } void WasmBinaryBuilder::readDataSegments() { - if (debug) { - std::cerr << "== readDataSegments" << std::endl; - } + BYN_TRACE("== readDataSegments\n"); auto num = getU32LEB(); for (size_t i = 0; i < num; i++) { Memory::Segment curr; @@ -2014,9 +1833,7 @@ void WasmBinaryBuilder::readDataSegments() { } void WasmBinaryBuilder::readFunctionTableDeclaration() { - if (debug) { - std::cerr << "== readFunctionTableDeclaration" << std::endl; - } + BYN_TRACE("== readFunctionTableDeclaration\n"); auto numTables = getU32LEB(); if (numTables != 1) { throwError("Only 1 table definition allowed in MVP"); @@ -2038,9 +1855,7 @@ void WasmBinaryBuilder::readFunctionTableDeclaration() { } void WasmBinaryBuilder::readTableElements() { - if (debug) { - std::cerr << "== readTableElements" << std::endl; - } + BYN_TRACE("== readTableElements\n"); auto numSegments = getU32LEB(); if (numSegments >= Table::kMaxSize) { throwError("Too many segments"); @@ -2061,17 +1876,11 @@ void WasmBinaryBuilder::readTableElements() { } void WasmBinaryBuilder::readEvents() { - if (debug) { - std::cerr << "== readEvents" << std::endl; - } + BYN_TRACE("== readEvents\n"); size_t numEvents = getU32LEB(); - if (debug) { - std::cerr << "num: " << numEvents << std::endl; - } + BYN_TRACE("num: " << numEvents << std::endl); for (size_t i = 0; i < numEvents; i++) { - if (debug) { - std::cerr << "read one" << std::endl; - } + BYN_TRACE("read one\n"); auto attribute = getU32LEB(); auto typeIndex = getU32LEB(); if (typeIndex >= wasm.functionTypes.size()) { @@ -2122,9 +1931,7 @@ Name WasmBinaryBuilder::escape(Name name) { } void WasmBinaryBuilder::readNames(size_t payloadLen) { - if (debug) { - std::cerr << "== readNames" << std::endl; - } + BYN_TRACE("== readNames\n"); auto sectionPos = pos; while (pos < sectionPos + payloadLen) { auto nameType = getU32LEB(); @@ -2223,18 +2030,14 @@ BinaryConsts::ASTNodes WasmBinaryBuilder::readExpression(Expression*& curr) { if (pos == endOfFunction) { throwError("Reached function end without seeing End opcode"); } - if (debug) { - std::cerr << "zz recurse into " << ++depth << " at " << pos << std::endl; - } + BYN_TRACE("zz recurse into " << ++depth << " at " << pos << std::endl); readNextDebugLocation(); std::set<Function::DebugLocation> currDebugLocation; if (debugLocation.size()) { currDebugLocation.insert(*debugLocation.begin()); } uint8_t code = getInt8(); - if (debug) { - std::cerr << "readExpression seeing " << (int)code << std::endl; - } + BYN_TRACE("readExpression seeing " << (int)code << std::endl); switch (code) { case BinaryConsts::Block: visitBlock((curr = allocator.alloc<Block>())->cast<Block>()); @@ -2429,9 +2232,7 @@ BinaryConsts::ASTNodes WasmBinaryBuilder::readExpression(Expression*& curr) { if (curr && currDebugLocation.size()) { currFunction->debugLocations[curr] = *currDebugLocation.begin(); } - if (debug) { - std::cerr << "zz recurse from " << depth-- << " at " << pos << std::endl; - } + BYN_TRACE("zz recurse from " << depth-- << " at " << pos << std::endl); return BinaryConsts::ASTNodes(code); } @@ -2478,9 +2279,7 @@ void WasmBinaryBuilder::pushBlockElements(Block* curr, } void WasmBinaryBuilder::visitBlock(Block* curr) { - if (debug) { - std::cerr << "zz node: Block" << std::endl; - } + BYN_TRACE("zz node: Block\n"); // special-case Block and de-recurse nested blocks in their first position, as // that is a common pattern that can be very highly nested. @@ -2567,9 +2366,7 @@ Expression* WasmBinaryBuilder::getBlockOrSingleton(Type type, } void WasmBinaryBuilder::visitIf(If* curr) { - if (debug) { - std::cerr << "zz node: If" << std::endl; - } + BYN_TRACE("zz node: If\n"); curr->type = getType(); curr->condition = popNonVoidExpression(); curr->ifTrue = getBlockOrSingleton(curr->type); @@ -2583,9 +2380,7 @@ void WasmBinaryBuilder::visitIf(If* curr) { } void WasmBinaryBuilder::visitLoop(Loop* curr) { - if (debug) { - std::cerr << "zz node: Loop" << std::endl; - } + BYN_TRACE("zz node: Loop\n"); curr->type = getType(); curr->name = getNextLabel(); breakStack.push_back({curr->name, 0}); @@ -2615,9 +2410,7 @@ void WasmBinaryBuilder::visitLoop(Loop* curr) { WasmBinaryBuilder::BreakTarget WasmBinaryBuilder::getBreakTarget(int32_t offset) { - if (debug) { - std::cerr << "getBreakTarget " << offset << std::endl; - } + BYN_TRACE("getBreakTarget " << offset << std::endl); if (breakStack.size() < 1 + size_t(offset)) { throwError("bad breakindex (low)"); } @@ -2625,10 +2418,8 @@ WasmBinaryBuilder::getBreakTarget(int32_t offset) { if (index >= breakStack.size()) { throwError("bad breakindex (high)"); } - if (debug) { - std::cerr << "breaktarget " << breakStack[index].name << " arity " - << breakStack[index].arity << std::endl; - } + BYN_TRACE("breaktarget " << breakStack[index].name << " arity " + << breakStack[index].arity << std::endl); auto& ret = breakStack[index]; // if the break is in literally unreachable code, then we will not emit it // anyhow, so do not note that the target has breaks to it @@ -2639,9 +2430,7 @@ WasmBinaryBuilder::getBreakTarget(int32_t offset) { } void WasmBinaryBuilder::visitBreak(Break* curr, uint8_t code) { - if (debug) { - std::cerr << "zz node: Break, code " << int32_t(code) << std::endl; - } + BYN_TRACE("zz node: Break, code " << int32_t(code) << std::endl); BreakTarget target = getBreakTarget(getU32LEB()); curr->name = target.name; if (code == BinaryConsts::BrIf) { @@ -2654,22 +2443,16 @@ void WasmBinaryBuilder::visitBreak(Break* curr, uint8_t code) { } void WasmBinaryBuilder::visitSwitch(Switch* curr) { - if (debug) { - std::cerr << "zz node: Switch" << std::endl; - } + BYN_TRACE("zz node: Switch\n"); curr->condition = popNonVoidExpression(); auto numTargets = getU32LEB(); - if (debug) { - std::cerr << "targets: " << numTargets << std::endl; - } + BYN_TRACE("targets: " << numTargets << std::endl); for (size_t i = 0; i < numTargets; i++) { curr->targets.push_back(getBreakTarget(getU32LEB()).name); } auto defaultTarget = getBreakTarget(getU32LEB()); curr->default_ = defaultTarget.name; - if (debug) { - std::cerr << "default: " << curr->default_ << std::endl; - } + BYN_TRACE("default: " << curr->default_ << "\n"); if (defaultTarget.arity) { curr->value = popNonVoidExpression(); } @@ -2677,9 +2460,7 @@ void WasmBinaryBuilder::visitSwitch(Switch* curr) { } void WasmBinaryBuilder::visitCall(Call* curr) { - if (debug) { - std::cerr << "zz node: Call" << std::endl; - } + BYN_TRACE("zz node: Call\n"); auto index = getU32LEB(); FunctionType* type; if (index < functionImports.size()) { @@ -2704,9 +2485,7 @@ void WasmBinaryBuilder::visitCall(Call* curr) { } void WasmBinaryBuilder::visitCallIndirect(CallIndirect* curr) { - if (debug) { - std::cerr << "zz node: CallIndirect" << std::endl; - } + BYN_TRACE("zz node: CallIndirect\n"); auto index = getU32LEB(); if (index >= wasm.functionTypes.size()) { throwError("bad call_indirect function index"); @@ -2728,9 +2507,8 @@ void WasmBinaryBuilder::visitCallIndirect(CallIndirect* curr) { } void WasmBinaryBuilder::visitLocalGet(LocalGet* curr) { - if (debug) { - std::cerr << "zz node: LocalGet " << pos << std::endl; - } + BYN_TRACE("zz node: LocalGet " << pos << std::endl); + ; requireFunctionContext("local.get"); curr->index = getU32LEB(); if (curr->index >= currFunction->getNumLocals()) { @@ -2741,9 +2519,7 @@ void WasmBinaryBuilder::visitLocalGet(LocalGet* curr) { } void WasmBinaryBuilder::visitLocalSet(LocalSet* curr, uint8_t code) { - if (debug) { - std::cerr << "zz node: Set|LocalTee" << std::endl; - } + BYN_TRACE("zz node: Set|LocalTee\n"); requireFunctionContext("local.set outside of function"); curr->index = getU32LEB(); if (curr->index >= currFunction->getNumLocals()) { @@ -2756,18 +2532,14 @@ void WasmBinaryBuilder::visitLocalSet(LocalSet* curr, uint8_t code) { } void WasmBinaryBuilder::visitGlobalGet(GlobalGet* curr) { - if (debug) { - std::cerr << "zz node: GlobalGet " << pos << std::endl; - } + BYN_TRACE("zz node: GlobalGet " << pos << std::endl); auto index = getU32LEB(); curr->name = getGlobalName(index); curr->type = wasm.getGlobal(curr->name)->type; } void WasmBinaryBuilder::visitGlobalSet(GlobalSet* curr) { - if (debug) { - std::cerr << "zz node: GlobalSet" << std::endl; - } + BYN_TRACE("zz node: GlobalSet\n"); auto index = getU32LEB(); curr->name = getGlobalName(index); curr->value = popNonVoidExpression(); @@ -2872,9 +2644,7 @@ bool WasmBinaryBuilder::maybeVisitLoad(Expression*& out, default: return false; } - if (debug) { - std::cerr << "zz node: Load" << std::endl; - } + BYN_TRACE("zz node: Load\n"); } else { switch (code) { case BinaryConsts::I32AtomicLoad8U: @@ -2916,9 +2686,7 @@ bool WasmBinaryBuilder::maybeVisitLoad(Expression*& out, return false; } curr->signed_ = false; - if (debug) { - std::cerr << "zz node: AtomicLoad" << std::endl; - } + BYN_TRACE("zz node: AtomicLoad\n"); } curr->isAtomic = isAtomic; @@ -3026,9 +2794,7 @@ bool WasmBinaryBuilder::maybeVisitStore(Expression*& out, } curr->isAtomic = isAtomic; - if (debug) { - std::cerr << "zz node: Store" << std::endl; - } + BYN_TRACE("zz node: Store\n"); readMemoryAccess(curr->align, curr->offset); curr->value = popNonVoidExpression(); curr->ptr = popNonVoidExpression(); @@ -3087,9 +2853,7 @@ bool WasmBinaryBuilder::maybeVisitAtomicRMW(Expression*& out, uint8_t code) { #undef SET_FOR_OP #undef SET - if (debug) { - std::cerr << "zz node: AtomicRMW" << std::endl; - } + BYN_TRACE("zz node: AtomicRMW\n"); Address readAlign; readMemoryAccess(readAlign, curr->offset); if (readAlign != curr->bytes) { @@ -3141,9 +2905,7 @@ bool WasmBinaryBuilder::maybeVisitAtomicCmpxchg(Expression*& out, WASM_UNREACHABLE(); } - if (debug) { - std::cerr << "zz node: AtomicCmpxchg" << std::endl; - } + BYN_TRACE("zz node: AtomicCmpxchg\n"); Address readAlign; readMemoryAccess(readAlign, curr->offset); if (readAlign != curr->bytes) { @@ -3175,9 +2937,7 @@ bool WasmBinaryBuilder::maybeVisitAtomicWait(Expression*& out, uint8_t code) { WASM_UNREACHABLE(); } curr->type = i32; - if (debug) { - std::cerr << "zz node: AtomicWait" << std::endl; - } + BYN_TRACE("zz node: AtomicWait\n"); curr->timeout = popNonVoidExpression(); curr->expected = popNonVoidExpression(); curr->ptr = popNonVoidExpression(); @@ -3196,9 +2956,7 @@ bool WasmBinaryBuilder::maybeVisitAtomicNotify(Expression*& out, uint8_t code) { return false; } auto* curr = allocator.alloc<AtomicNotify>(); - if (debug) { - std::cerr << "zz node: AtomicNotify" << std::endl; - } + BYN_TRACE("zz node: AtomicNotify\n"); curr->type = i32; curr->notifyCount = popNonVoidExpression(); @@ -3218,9 +2976,7 @@ bool WasmBinaryBuilder::maybeVisitAtomicFence(Expression*& out, uint8_t code) { return false; } auto* curr = allocator.alloc<AtomicFence>(); - if (debug) { - std::cerr << "zz node: AtomicFence" << std::endl; - } + BYN_TRACE("zz node: AtomicFence\n"); curr->order = getU32LEB(); curr->finalize(); out = curr; @@ -3229,9 +2985,7 @@ bool WasmBinaryBuilder::maybeVisitAtomicFence(Expression*& out, uint8_t code) { bool WasmBinaryBuilder::maybeVisitConst(Expression*& out, uint8_t code) { Const* curr; - if (debug) { - std::cerr << "zz node: Const, code " << code << std::endl; - } + BYN_TRACE("zz node: Const, code " << code << std::endl); switch (code) { case BinaryConsts::I32Const: curr = allocator.alloc<Const>(); @@ -3478,9 +3232,7 @@ bool WasmBinaryBuilder::maybeVisitUnary(Expression*& out, uint8_t code) { default: return false; } - if (debug) { - std::cerr << "zz node: Unary" << std::endl; - } + BYN_TRACE("zz node: Unary\n"); curr->value = popNonVoidExpression(); curr->finalize(); out = curr; @@ -3525,9 +3277,7 @@ bool WasmBinaryBuilder::maybeVisitTruncSat(Expression*& out, uint32_t code) { default: return false; } - if (debug) { - std::cerr << "zz node: Unary (nontrapping float-to-int)" << std::endl; - } + BYN_TRACE("zz node: Unary (nontrapping float-to-int)\n"); curr->value = popNonVoidExpression(); curr->finalize(); out = curr; @@ -3661,9 +3411,7 @@ bool WasmBinaryBuilder::maybeVisitBinary(Expression*& out, uint8_t code) { default: return false; } - if (debug) { - std::cerr << "zz node: Binary" << std::endl; - } + BYN_TRACE("zz node: Binary\n"); curr->right = popNonVoidExpression(); curr->left = popNonVoidExpression(); curr->finalize(); @@ -4060,9 +3808,7 @@ bool WasmBinaryBuilder::maybeVisitSIMDBinary(Expression*& out, uint32_t code) { default: return false; } - if (debug) { - std::cerr << "zz node: Binary" << std::endl; - } + BYN_TRACE("zz node: Binary\n"); curr->right = popNonVoidExpression(); curr->left = popNonVoidExpression(); curr->finalize(); @@ -4542,9 +4288,7 @@ bool WasmBinaryBuilder::maybeVisitSIMDLoad(Expression*& out, uint32_t code) { } void WasmBinaryBuilder::visitSelect(Select* curr) { - if (debug) { - std::cerr << "zz node: Select" << std::endl; - } + BYN_TRACE("zz node: Select\n"); curr->condition = popNonVoidExpression(); curr->ifFalse = popNonVoidExpression(); curr->ifTrue = popNonVoidExpression(); @@ -4552,9 +4296,7 @@ void WasmBinaryBuilder::visitSelect(Select* curr) { } void WasmBinaryBuilder::visitReturn(Return* curr) { - if (debug) { - std::cerr << "zz node: Return" << std::endl; - } + BYN_TRACE("zz node: Return\n"); requireFunctionContext("return"); if (currFunction->result != none) { curr->value = popNonVoidExpression(); @@ -4580,9 +4322,7 @@ bool WasmBinaryBuilder::maybeVisitHost(Expression*& out, uint8_t code) { default: return false; } - if (debug) { - std::cerr << "zz node: Host" << std::endl; - } + BYN_TRACE("zz node: Host\n"); auto reserved = getU32LEB(); if (reserved != 0) { throwError("Invalid reserved field on memory.grow/memory.size"); @@ -4592,30 +4332,20 @@ bool WasmBinaryBuilder::maybeVisitHost(Expression*& out, uint8_t code) { return true; } -void WasmBinaryBuilder::visitNop(Nop* curr) { - if (debug) { - std::cerr << "zz node: Nop" << std::endl; - } -} +void WasmBinaryBuilder::visitNop(Nop* curr) { BYN_TRACE("zz node: Nop\n"); } void WasmBinaryBuilder::visitUnreachable(Unreachable* curr) { - if (debug) { - std::cerr << "zz node: Unreachable" << std::endl; - } + BYN_TRACE("zz node: Unreachable\n"); } void WasmBinaryBuilder::visitDrop(Drop* curr) { - if (debug) { - std::cerr << "zz node: Drop" << std::endl; - } + BYN_TRACE("zz node: Drop\n"); curr->value = popNonVoidExpression(); curr->finalize(); } void WasmBinaryBuilder::visitTry(Try* curr) { - if (debug) { - std::cerr << "zz node: Try" << std::endl; - } + BYN_TRACE("zz node: Try\n"); // For simplicity of implementation, like if scopes, we create a hidden block // within each try-body and catch-body, and let branches target those inner // blocks instead. @@ -4632,9 +4362,7 @@ void WasmBinaryBuilder::visitTry(Try* curr) { } void WasmBinaryBuilder::visitThrow(Throw* curr) { - if (debug) { - std::cerr << "zz node: Throw" << std::endl; - } + BYN_TRACE("zz node: Throw\n"); auto index = getU32LEB(); if (index >= wasm.events.size()) { throwError("bad event index"); @@ -4650,17 +4378,13 @@ void WasmBinaryBuilder::visitThrow(Throw* curr) { } void WasmBinaryBuilder::visitRethrow(Rethrow* curr) { - if (debug) { - std::cerr << "zz node: Rethrow" << std::endl; - } + BYN_TRACE("zz node: Rethrow\n"); curr->exnref = popNonVoidExpression(); curr->finalize(); } void WasmBinaryBuilder::visitBrOnExn(BrOnExn* curr) { - if (debug) { - std::cerr << "zz node: BrOnExn" << std::endl; - } + BYN_TRACE("zz node: BrOnExn\n"); BreakTarget target = getBreakTarget(getU32LEB()); curr->name = target.name; auto index = getU32LEB(); diff --git a/src/wasm/wasm-io.cpp b/src/wasm/wasm-io.cpp index 90df2de12..d874494c0 100644 --- a/src/wasm/wasm-io.cpp +++ b/src/wasm/wasm-io.cpp @@ -25,11 +25,14 @@ // #include "wasm-io.h" +#include "support/debug.h" #include "wasm-binary.h" #include "wasm-s-parser.h" namespace wasm { +#define DEBUG_TYPE "writer" + static void readTextData(std::string& input, Module& wasm) { SExpressionParser parser(const_cast<char*>(input.c_str())); Element& root = *parser.root; @@ -37,20 +40,16 @@ static void readTextData(std::string& input, Module& wasm) { } void ModuleReader::readText(std::string filename, Module& wasm) { - if (debug) { - std::cerr << "reading text from " << filename << "\n"; - } - auto input(read_file<std::string>( - filename, Flags::Text, debug ? Flags::Debug : Flags::Release)); + BYN_TRACE("reading text from " << filename << "\n"); + auto input(read_file<std::string>(filename, Flags::Text)); readTextData(input, wasm); } static void readBinaryData(std::vector<char>& input, Module& wasm, - std::string sourceMapFilename, - bool debug) { + std::string sourceMapFilename) { std::unique_ptr<std::ifstream> sourceMapStream; - WasmBinaryBuilder parser(wasm, input, debug); + WasmBinaryBuilder parser(wasm, input); if (sourceMapFilename.size()) { sourceMapStream = make_unique<std::ifstream>(); sourceMapStream->open(sourceMapFilename); @@ -65,12 +64,9 @@ static void readBinaryData(std::vector<char>& input, void ModuleReader::readBinary(std::string filename, Module& wasm, std::string sourceMapFilename) { - if (debug) { - std::cerr << "reading binary from " << filename << "\n"; - } - auto input(read_file<std::vector<char>>( - filename, Flags::Binary, debug ? Flags::Debug : Flags::Release)); - readBinaryData(input, wasm, sourceMapFilename, debug); + BYN_TRACE("reading binary from " << filename << "\n"); + auto input(read_file<std::vector<char>>(filename, Flags::Binary)); + readBinaryData(input, wasm, sourceMapFilename); } bool ModuleReader::isBinaryFile(std::string filename) { @@ -107,10 +103,10 @@ void ModuleReader::read(std::string filename, // TODO: reading into a vector<char> then copying into a string is unnecessarily // inefficient. It would be better to read just once into a stringstream. void ModuleReader::readStdin(Module& wasm, std::string sourceMapFilename) { - std::vector<char> input = read_stdin(debug ? Flags::Debug : Flags::Release); + std::vector<char> input = read_stdin(); if (input.size() >= 4 && input[0] == '\0' && input[1] == 'a' && input[2] == 's' && input[3] == 'm') { - readBinaryData(input, wasm, sourceMapFilename, debug); + readBinaryData(input, wasm, sourceMapFilename); } else { std::ostringstream s; s.write(input.data(), input.size()); @@ -120,21 +116,22 @@ void ModuleReader::readStdin(Module& wasm, std::string sourceMapFilename) { } } +#undef DEBUG_TYPE +#define DEBUG_TYPE "writer" + void ModuleWriter::writeText(Module& wasm, Output& output) { WasmPrinter::printModule(&wasm, output.getStream()); } void ModuleWriter::writeText(Module& wasm, std::string filename) { - if (debug) { - std::cerr << "writing text to " << filename << "\n"; - } - Output output(filename, Flags::Text, debug ? Flags::Debug : Flags::Release); + BYN_TRACE("writing text to " << filename << "\n"); + Output output(filename, Flags::Text); writeText(wasm, output); } void ModuleWriter::writeBinary(Module& wasm, Output& output) { - BufferWithRandomAccess buffer(debug); - WasmBinaryWriter writer(&wasm, buffer, debug); + BufferWithRandomAccess buffer; + WasmBinaryWriter writer(&wasm, buffer); // if debug info is used, then we want to emit the names section writer.setNamesSection(debugInfo); std::unique_ptr<std::ofstream> sourceMapStream; @@ -154,10 +151,8 @@ void ModuleWriter::writeBinary(Module& wasm, Output& output) { } void ModuleWriter::writeBinary(Module& wasm, std::string filename) { - if (debug) { - std::cerr << "writing binary to " << filename << "\n"; - } - Output output(filename, Flags::Binary, debug ? Flags::Debug : Flags::Release); + BYN_TRACE("writing binary to " << filename << "\n"); + Output output(filename, Flags::Binary); writeBinary(wasm, output); } diff --git a/src/wasm/wasm-s-parser.cpp b/src/wasm/wasm-s-parser.cpp index 3a761fd17..44b08f4a3 100644 --- a/src/wasm/wasm-s-parser.cpp +++ b/src/wasm/wasm-s-parser.cpp @@ -342,7 +342,7 @@ SExpressionWasmBuilder::SExpressionWasmBuilder(Module& wasm, stringToBinary(str, size, data); } } - WasmBinaryBuilder binaryBuilder(wasm, data, false); + WasmBinaryBuilder binaryBuilder(wasm, data); binaryBuilder.read(); return; } diff --git a/src/wasm2js.h b/src/wasm2js.h index 344416b54..5e6e1d8ea 100644 --- a/src/wasm2js.h +++ b/src/wasm2js.h @@ -334,7 +334,7 @@ Ref Wasm2JSBuilder::processWasm(Module* wasm, Name funcName) { } if (flags.symbolsFile.size() > 0) { - Output out(flags.symbolsFile, wasm::Flags::Text, wasm::Flags::Release); + Output out(flags.symbolsFile, wasm::Flags::Text); Index i = 0; for (auto& func : wasm->functions) { out.getStream() << i++ << ':' << func->name.str << '\n'; |