From dbb5f32bafcf3c8f51eefb95e6c298ce6b9ac8cc Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 17 Feb 2016 12:44:14 -0800 Subject: move printing to a pass --- src/binaryen-shell.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/binaryen-shell.cpp') diff --git a/src/binaryen-shell.cpp b/src/binaryen-shell.cpp index d450e4db7..1b0bf8f78 100644 --- a/src/binaryen-shell.cpp +++ b/src/binaryen-shell.cpp @@ -281,7 +281,7 @@ static void run_asserts(size_t* i, bool* checked, AllocatingModule* wasm, Colors::bold(std::cout); std::cerr << "printing in module invalidity test:\n"; Colors::normal(std::cout); - std::cout << wasm; + printWasm(&wasm, std::cout); } bool invalid = false; std::unique_ptr builder; @@ -411,7 +411,7 @@ int main(int argc, const char* argv[]) { Colors::bold(std::cout); std::cerr << "printing before:\n"; Colors::normal(std::cout); - std::cout << wasm; + printWasm(&wasm, std::cout); } MixedArena moreModuleAllocations; @@ -429,7 +429,7 @@ int main(int argc, const char* argv[]) { Colors::bold(std::cout); std::cerr << "printing after:\n"; Colors::normal(std::cout); - std::cout << wasm; + printWasm(&wasm, std::cout); } run_asserts(&i, &checked, &wasm, &root, &builder, print_before, print_after, entry); -- cgit v1.2.3 From 51a0ffcc0a6bdd205e3979cc147adcf0e1186af4 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 17 Feb 2016 14:37:06 -0800 Subject: add wasm-printing.h --- check.py | 6 +++++- src/asm2wasm-main.cpp | 1 + src/binaryen-shell.cpp | 1 + src/pass.h | 8 -------- src/passes/Print.cpp | 10 +++++++++- src/s2wasm-main.cpp | 1 + src/s2wasm.h | 1 + src/wasm-dis.cpp | 2 +- src/wasm-printing.h | 37 +++++++++++++++++++++++++++++++++++++ test/example/find_div0s.cpp | 6 ++++-- 10 files changed, 60 insertions(+), 13 deletions(-) create mode 100644 src/wasm-printing.h (limited to 'src/binaryen-shell.cpp') diff --git a/check.py b/check.py index 00ab1c52e..ba9d34871 100755 --- a/check.py +++ b/check.py @@ -505,7 +505,11 @@ if has_vanilla_emcc and has_vanilla_llvm: print '\n[ checking example testcases... ]\n' -cmd = [os.environ.get('CXX') or 'g++', '-std=c++11', os.path.join('test', 'example', 'find_div0s.cpp'), '-Isrc', '-g', '-lsupport', '-Llib/.'] +cmd = [os.environ.get('CXX') or 'g++', '-std=c++11', + os.path.join('test', 'example', 'find_div0s.cpp'), + os.path.join('src', 'pass.cpp'), + os.path.join('src', 'passes', 'Print.cpp'), + '-Isrc', '-g', '-lsupport', '-Llib/.'] if os.environ.get('COMPILER_FLAGS'): for f in os.environ.get('COMPILER_FLAGS').split(' '): cmd.append(f) diff --git a/src/asm2wasm-main.cpp b/src/asm2wasm-main.cpp index 3c1377c08..aa914c168 100644 --- a/src/asm2wasm-main.cpp +++ b/src/asm2wasm-main.cpp @@ -21,6 +21,7 @@ #include "support/colors.h" #include "support/command-line.h" #include "support/file.h" +#include "wasm-printing.h" #include "asm2wasm.h" diff --git a/src/binaryen-shell.cpp b/src/binaryen-shell.cpp index 1b0bf8f78..a9a9ea1da 100644 --- a/src/binaryen-shell.cpp +++ b/src/binaryen-shell.cpp @@ -26,6 +26,7 @@ #include "support/command-line.h" #include "support/file.h" #include "wasm-interpreter.h" +#include "wasm-printing.h" #include "wasm-s-parser.h" #include "wasm-validator.h" diff --git a/src/pass.h b/src/pass.h index 250ba59b2..53b486e41 100644 --- a/src/pass.h +++ b/src/pass.h @@ -165,14 +165,6 @@ public: void run(PassRunner* runner, Module* module) override; }; -// Standard pass utilities - -inline void printWasm(Module* module, std::ostream& o) { - PassRunner passRunner(nullptr); - passRunner.add(o); - passRunner.run(module); -} - } // namespace wasm #endif // wasm_pass_h diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp index 14cb929c5..a5014d034 100644 --- a/src/passes/Print.cpp +++ b/src/passes/Print.cpp @@ -470,7 +470,7 @@ struct PrintSExpression : public WasmVisitor { } }; -// Main entry point. Eventually this will direct printing to one of various options. +// Pass entry point. Eventually this will direct printing to one of various options. void Printer::run(PassRunner* runner, Module* module) { PrintSExpression print(o); @@ -479,4 +479,12 @@ void Printer::run(PassRunner* runner, Module* module) { static RegisterPass registerPass("print", "print in s-expression format"); +// Print individual expressions + +std::ostream& printWasm(Expression* expression, std::ostream& o) { + PrintSExpression print(o); + print.visit(expression); + return o; +} + } // namespace wasm diff --git a/src/s2wasm-main.cpp b/src/s2wasm-main.cpp index 377d44c52..d7a18f547 100644 --- a/src/s2wasm-main.cpp +++ b/src/s2wasm-main.cpp @@ -22,6 +22,7 @@ #include "support/command-line.h" #include "support/file.h" #include "s2wasm.h" +#include "wasm-printing.h" using namespace cashew; using namespace wasm; diff --git a/src/s2wasm.h b/src/s2wasm.h index 9c875ea9e..0520d5c3b 100644 --- a/src/s2wasm.h +++ b/src/s2wasm.h @@ -27,6 +27,7 @@ #include "parsing.h" #include "pass.h" #include "asm_v_wasm.h" +#include "wasm-printing.h" namespace wasm { diff --git a/src/wasm-dis.cpp b/src/wasm-dis.cpp index c7ebf8861..7486e0bbd 100644 --- a/src/wasm-dis.cpp +++ b/src/wasm-dis.cpp @@ -18,11 +18,11 @@ // wasm2asm console tool // -#include "pass.h" #include "support/colors.h" #include "support/command-line.h" #include "support/file.h" #include "wasm-binary.h" +#include "wasm-printing.h" using namespace cashew; using namespace wasm; diff --git a/src/wasm-printing.h b/src/wasm-printing.h new file mode 100644 index 000000000..d7868d52c --- /dev/null +++ b/src/wasm-printing.h @@ -0,0 +1,37 @@ +/* + * Copyright 2016 WebAssembly Community Group participants + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __wasm_printing_h__ +#define __wasm_printing_h__ + +#include "wasm.h" +#include "pass.h" + +namespace wasm { + +inline std::ostream& printWasm(Module* module, std::ostream& o) { + PassRunner passRunner(nullptr); + passRunner.add(o); + passRunner.run(module); + return o; +} + +extern std::ostream& printWasm(Expression* expression, std::ostream& o); + +} + +#endif + diff --git a/test/example/find_div0s.cpp b/test/example/find_div0s.cpp index cb29043a7..e74d4781f 100644 --- a/test/example/find_div0s.cpp +++ b/test/example/find_div0s.cpp @@ -8,6 +8,7 @@ #include #include +#include #include using namespace wasm; @@ -34,7 +35,7 @@ int main() { SExpressionWasmBuilder builder(module, *root[0], [&]() { abort(); }); // Print it out - std::cout << module; + printWasm(&module, std::cout); // Search it for divisions by zero: Walk the module, looking for // that operation. @@ -45,7 +46,8 @@ int main() { // Check if the right operand is a constant, and if it is 0 auto right = curr->right->dyn_cast(); if (right && right->value.getInteger() == 0) { - std::cout << "We found that " << curr->left << " is divided by zero\n"; + std::cout << "We found that "; + printWasm(curr->left, std::cout) << " is divided by zero\n"; } } } -- cgit v1.2.3 From 29370f0fc7ad44f506e1bd54441f19111fe9ff5f Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 17 Feb 2016 16:46:39 -0800 Subject: remove --print-before and --print-after flags, since we now have --print --- auto_update_tests.py | 2 +- check.py | 6 +++--- src/binaryen-shell.cpp | 36 ++---------------------------------- 3 files changed, 6 insertions(+), 38 deletions(-) (limited to 'src/binaryen-shell.cpp') diff --git a/auto_update_tests.py b/auto_update_tests.py index 66557c0cf..86c4ebbeb 100755 --- a/auto_update_tests.py +++ b/auto_update_tests.py @@ -48,7 +48,7 @@ for t in sorted(os.listdir(os.path.join('test', 'passes'))): if t.endswith('.wast'): print '..', t passname = os.path.basename(t).replace('.wast', '') - cmd = [os.path.join('bin', 'binaryen-shell'), '--print-after', ('--' + passname if passname != 'O' else '-O'), os.path.join('test', 'passes', t)] + cmd = [os.path.join('bin', 'binaryen-shell'), ('--' + passname if passname != 'O' else '-O'), os.path.join('test', 'passes', t), '--print'] print ' ', ' '.join(cmd) actual, err = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() open(os.path.join('test', 'passes', passname + '.txt'), 'w').write(actual) diff --git a/check.py b/check.py index ba9d34871..7b1ec6835 100755 --- a/check.py +++ b/check.py @@ -259,7 +259,7 @@ for asm in tests: # verify in wasm if interpreter: # remove imports, spec interpreter doesn't know what to do with them - subprocess.check_call([os.path.join('bin', 'binaryen-shell'), '--remove-imports', '--print-after', os.path.join('test', wasm)], stdout=open('ztemp.wast', 'w'), stderr=subprocess.PIPE) + subprocess.check_call([os.path.join('bin', 'binaryen-shell'), '--remove-imports', '--print', os.path.join('test', wasm)], stdout=open('ztemp.wast', 'w'), stderr=subprocess.PIPE) proc = subprocess.Popen([interpreter, 'ztemp.wast'], stderr=subprocess.PIPE) out, err = proc.communicate() if proc.returncode != 0: @@ -286,7 +286,7 @@ for t in sorted(os.listdir(os.path.join('test', 'passes'))): print '..', t passname = os.path.basename(t).replace('.wast', '') opt = '-O' if passname == 'O' else '--' + passname - cmd = [os.path.join('bin', 'binaryen-shell'), '--print-after', opt, os.path.join('test', 'passes', t)] + cmd = [os.path.join('bin', 'binaryen-shell'), opt, os.path.join('test', 'passes', t), '--print'] print ' ', ' '.join(cmd) actual, err = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() fail_if_not_identical(actual, open(os.path.join('test', 'passes', passname + '.txt')).read()) @@ -297,7 +297,7 @@ for t in tests: if t.endswith('.wast') and not t.startswith('spec'): print '..', t t = os.path.join('test', t) - cmd = [os.path.join('bin', 'binaryen-shell'), t, '--print-before'] + cmd = [os.path.join('bin', 'binaryen-shell'), t, '--print'] print ' ', ' '.join(cmd) actual, err = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() assert err.replace('printing before:', '').strip() == '', 'bad err:' + err diff --git a/src/binaryen-shell.cpp b/src/binaryen-shell.cpp index a9a9ea1da..6b27333cc 100644 --- a/src/binaryen-shell.cpp +++ b/src/binaryen-shell.cpp @@ -241,7 +241,6 @@ static void verify_result(Literal a, Literal b) { static void run_asserts(size_t* i, bool* checked, AllocatingModule* wasm, Element* root, std::unique_ptr* builder, - bool print_before, bool print_after, Name entry) { ShellExternalInterface* interface = nullptr; ModuleInstance* instance = nullptr; @@ -278,12 +277,6 @@ static void run_asserts(size_t* i, bool* checked, AllocatingModule* wasm, if (id == ASSERT_INVALID) { // a module invalidity test AllocatingModule wasm; - if (print_before || print_after) { - Colors::bold(std::cout); - std::cerr << "printing in module invalidity test:\n"; - Colors::normal(std::cout); - printWasm(&wasm, std::cout); - } bool invalid = false; std::unique_ptr builder; try { @@ -342,8 +335,6 @@ static void run_asserts(size_t* i, bool* checked, AllocatingModule* wasm, // int main(int argc, const char* argv[]) { - bool print_before = false; - bool print_after = false; Name entry; std::vector passes; @@ -368,14 +359,6 @@ int main(int argc, const char* argv[]) { [&passes](Options*, const std::string&) { for (const auto* p : default_passes) passes.push_back(p); }) - .add("--print-before", "", "Print modules before processing them", - Options::Arguments::Zero, - [&print_before](Options*, const std::string&) { - print_before = true; - }) - .add("--print-after", "", "Print modules after processing them", - Options::Arguments::Zero, - [&print_after](Options*, const std::string&) { print_after = true; }) .add_positional("INFILE", Options::Arguments::One, [](Options* o, const std::string& argument) { o->extra["infile"] = argument; @@ -408,13 +391,6 @@ int main(int argc, const char* argv[]) { new SExpressionWasmBuilder(wasm, *root[i], [&]() { abort(); }, options.debug)); i++; - if (print_before) { - Colors::bold(std::cout); - std::cerr << "printing before:\n"; - Colors::normal(std::cout); - printWasm(&wasm, std::cout); - } - MixedArena moreModuleAllocations; if (passes.size() > 0) { @@ -426,17 +402,9 @@ int main(int argc, const char* argv[]) { passRunner.run(&wasm); } - if (print_after) { - Colors::bold(std::cout); - std::cerr << "printing after:\n"; - Colors::normal(std::cout); - printWasm(&wasm, std::cout); - } - run_asserts(&i, &checked, &wasm, &root, &builder, print_before, - print_after, entry); + run_asserts(&i, &checked, &wasm, &root, &builder, entry); } else { - run_asserts(&i, &checked, nullptr, &root, nullptr, print_before, - print_after, entry); + run_asserts(&i, &checked, nullptr, &root, nullptr, entry); } } -- cgit v1.2.3