From b9bcd885dd27918e5b4f05d38a116c2745871723 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Thu, 13 Aug 2020 18:58:02 -0700 Subject: [PATCH 1/4] update changelog --- CHANGELOG.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8d925546be..b8bddc6245d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,15 @@ full changeset diff at the end of each section. Current Trunk ------------- +v96 +--- +- Fuzzing: Compare wasm2js to the interpreter (#3026) +- Fix CountLeadingZeroes on MSVC, which lead to bad optimizations (#3028) +- Asyncify verbose option (#3022) +- wasm2js: Add an "Export" scope for name resolution, avoids annoying + warnings (#2998) +- Extend the C- and JS-APIs (#2586) + v95 --- From acfad42aaf4a5c24c101e1d13ce6eaab60d376ea Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Fri, 14 Aug 2020 05:47:25 -0700 Subject: [PATCH 2/4] cleanup --- CMakeLists.txt | 1 - auto_update_tests.py | 46 - check.py | 3 - scripts/test/asm2wasm.py | 149 - src/asm2wasm.h | 3290 -- src/tools/asm2wasm.cpp | 296 - test/bad_params.fromasm | 10 - test/bad_params.fromasm.clamp | 10 - test/bad_params.fromasm.clamp.no-opts | 46 - test/bad_params.fromasm.imprecise | 7 - test/bad_params.fromasm.imprecise.no-opts | 46 - test/bad_params.fromasm.no-opts | 46 - test/debugInfo.fromasm | 192 - test/debugInfo.fromasm.clamp | 192 - test/debugInfo.fromasm.clamp.map | 1 - test/debugInfo.fromasm.clamp.no-opts | 329 - test/debugInfo.fromasm.clamp.no-opts.map | 1 - test/debugInfo.fromasm.imprecise | 184 - test/debugInfo.fromasm.imprecise.map | 1 - test/debugInfo.fromasm.imprecise.no-opts | 317 - test/debugInfo.fromasm.imprecise.no-opts.map | 1 - test/debugInfo.fromasm.map | 1 - test/debugInfo.fromasm.no-opts | 329 - test/debugInfo.fromasm.no-opts.map | 1 - test/debugInfo.fromasm.read-written | 196 - test/dynamicLibrary.fromasm | 69 - test/dynamicLibrary.fromasm.clamp | 69 - test/dynamicLibrary.fromasm.clamp.no-opts | 198 - test/dynamicLibrary.fromasm.imprecise | 66 - test/dynamicLibrary.fromasm.imprecise.no-opts | 198 - test/dynamicLibrary.fromasm.no-opts | 198 - test/emcc_O2_hello_world.fromasm | 9047 ----- test/emcc_O2_hello_world.fromasm.clamp | 9047 ----- .../emcc_O2_hello_world.fromasm.clamp.no-opts | 11943 ------ test/emcc_O2_hello_world.fromasm.imprecise | 9021 ----- ...c_O2_hello_world.fromasm.imprecise.no-opts | 11931 ------ test/emcc_O2_hello_world.fromasm.no-opts | 11943 ------ test/emcc_hello_world.fromasm | 14944 -------- test/emcc_hello_world.fromasm.clamp | 14995 -------- test/emcc_hello_world.fromasm.clamp.no-opts | 31685 ---------------- test/emcc_hello_world.fromasm.imprecise | 14839 -------- ...emcc_hello_world.fromasm.imprecise.no-opts | 31571 --------------- test/emcc_hello_world.fromasm.no-opts | 31634 --------------- test/empty.fromasm | 4 - test/empty.fromasm.clamp | 4 - test/empty.fromasm.clamp.no-opts | 6 - test/empty.fromasm.imprecise | 2 - test/empty.fromasm.imprecise.no-opts | 6 - test/empty.fromasm.no-opts | 6 - test/empty_4GB.fromasm | 4 - test/empty_4GB.fromasm.clamp | 4 - test/empty_4GB.fromasm.clamp.no-opts | 6 - test/empty_4GB.fromasm.imprecise | 2 - test/empty_4GB.fromasm.imprecise.no-opts | 6 - test/empty_4GB.fromasm.no-opts | 6 - test/hello_world.fromasm | 13 - test/hello_world.fromasm.clamp | 13 - test/hello_world.fromasm.clamp.no-opts | 16 - test/hello_world.fromasm.imprecise | 10 - test/hello_world.fromasm.imprecise.no-opts | 16 - test/hello_world.fromasm.no-opts | 16 - test/i64-setTempRet0.fromasm | 33 - test/i64-setTempRet0.fromasm.clamp | 33 - test/i64-setTempRet0.fromasm.clamp.no-opts | 57 - test/i64-setTempRet0.fromasm.imprecise | 30 - .../i64-setTempRet0.fromasm.imprecise.no-opts | 57 - test/i64-setTempRet0.fromasm.no-opts | 57 - test/importedSignCast.fromasm | 19 - test/importedSignCast.fromasm.clamp | 19 - test/importedSignCast.fromasm.clamp.no-opts | 24 - test/importedSignCast.fromasm.imprecise | 16 - ...importedSignCast.fromasm.imprecise.no-opts | 24 - test/importedSignCast.fromasm.no-opts | 24 - test/memorygrowth-minimal.fromasm | 12 - test/memorygrowth-minimal.fromasm.clamp | 12 - ...memorygrowth-minimal.fromasm.clamp.no-opts | 13 - test/memorygrowth-minimal.fromasm.imprecise | 10 - ...rygrowth-minimal.fromasm.imprecise.no-opts | 13 - test/memorygrowth-minimal.fromasm.no-opts | 13 - test/memorygrowth.fromasm | 9084 ----- test/memorygrowth.fromasm.clamp | 9084 ----- test/memorygrowth.fromasm.clamp.no-opts | 12006 ------ test/memorygrowth.fromasm.imprecise | 9054 ----- test/memorygrowth.fromasm.imprecise.no-opts | 11994 ------ test/memorygrowth.fromasm.no-opts | 12006 ------ test/min.fromasm | 43 - test/min.fromasm.clamp | 43 - test/min.fromasm.clamp.no-opts | 80 - test/min.fromasm.imprecise | 41 - test/min.fromasm.imprecise.no-opts | 80 - test/min.fromasm.no-opts | 80 - test/noffi_f32.fromasm | 24 - test/noffi_f32.fromasm.clamp | 24 - test/noffi_f32.fromasm.clamp.no-opts | 29 - test/noffi_f32.fromasm.imprecise | 21 - test/noffi_f32.fromasm.imprecise.no-opts | 29 - test/noffi_f32.fromasm.no-opts | 29 - test/noffi_i64.fromasm | 25 - test/noffi_i64.fromasm.clamp | 25 - test/noffi_i64.fromasm.clamp.no-opts | 34 - test/noffi_i64.fromasm.imprecise | 22 - test/noffi_i64.fromasm.imprecise.no-opts | 34 - test/noffi_i64.fromasm.no-opts | 34 - test/threads.fromasm | 74 - test/threads.fromasm.clamp | 74 - test/threads.fromasm.clamp.no-opts | 137 - test/threads.fromasm.imprecise | 72 - test/threads.fromasm.imprecise.no-opts | 137 - test/threads.fromasm.no-opts | 137 - test/threads.wasm-only.fromasm | 70 - test/threads.wasm-only.fromasm.clamp | 70 - test/threads.wasm-only.fromasm.clamp.no-opts | 93 - test/threads.wasm-only.fromasm.imprecise | 68 - ...hreads.wasm-only.fromasm.imprecise.no-opts | 93 - test/threads.wasm-only.fromasm.no-opts | 93 - test/two_sides.fromasm | 61 - test/two_sides.fromasm.clamp | 86 - test/two_sides.fromasm.clamp.no-opts | 114 - test/two_sides.fromasm.imprecise | 56 - test/two_sides.fromasm.imprecise.no-opts | 87 - test/two_sides.fromasm.no-opts | 89 - test/unit.fromasm | 1177 - test/unit.fromasm.clamp | 1221 - test/unit.fromasm.clamp.no-opts | 2252 -- test/unit.fromasm.imprecise | 1145 - test/unit.fromasm.imprecise.no-opts | 2136 -- test/unit.fromasm.no-opts | 2176 -- test/unreachable-import_wasm-only.fromasm | 13 - ...unreachable-import_wasm-only.fromasm.clamp | 13 - ...ble-import_wasm-only.fromasm.clamp.no-opts | 88 - ...achable-import_wasm-only.fromasm.imprecise | 11 - ...import_wasm-only.fromasm.imprecise.no-opts | 88 - ...reachable-import_wasm-only.fromasm.no-opts | 88 - test/use-import-and-drop.fromasm | 5 - test/use-import-and-drop.fromasm.clamp | 5 - .../use-import-and-drop.fromasm.clamp.no-opts | 51 - test/use-import-and-drop.fromasm.imprecise | 2 - ...-import-and-drop.fromasm.imprecise.no-opts | 51 - test/use-import-and-drop.fromasm.no-opts | 51 - test/wasm-only.fromasm | 642 - test/wasm-only.fromasm.clamp | 642 - test/wasm-only.fromasm.clamp.no-opts | 1928 - test/wasm-only.fromasm.imprecise | 326 - test/wasm-only.fromasm.imprecise.no-opts | 1761 - test/wasm-only.fromasm.no-opts | 1928 - 145 files changed, 293035 deletions(-) delete mode 100644 scripts/test/asm2wasm.py delete mode 100644 src/asm2wasm.h delete mode 100644 src/tools/asm2wasm.cpp delete mode 100644 test/bad_params.fromasm delete mode 100644 test/bad_params.fromasm.clamp delete mode 100644 test/bad_params.fromasm.clamp.no-opts delete mode 100644 test/bad_params.fromasm.imprecise delete mode 100644 test/bad_params.fromasm.imprecise.no-opts delete mode 100644 test/bad_params.fromasm.no-opts delete mode 100644 test/debugInfo.fromasm delete mode 100644 test/debugInfo.fromasm.clamp delete mode 100644 test/debugInfo.fromasm.clamp.map delete mode 100644 test/debugInfo.fromasm.clamp.no-opts delete mode 100644 test/debugInfo.fromasm.clamp.no-opts.map delete mode 100644 test/debugInfo.fromasm.imprecise delete mode 100644 test/debugInfo.fromasm.imprecise.map delete mode 100644 test/debugInfo.fromasm.imprecise.no-opts delete mode 100644 test/debugInfo.fromasm.imprecise.no-opts.map delete mode 100644 test/debugInfo.fromasm.map delete mode 100644 test/debugInfo.fromasm.no-opts delete mode 100644 test/debugInfo.fromasm.no-opts.map delete mode 100644 test/debugInfo.fromasm.read-written delete mode 100644 test/dynamicLibrary.fromasm delete mode 100644 test/dynamicLibrary.fromasm.clamp delete mode 100644 test/dynamicLibrary.fromasm.clamp.no-opts delete mode 100644 test/dynamicLibrary.fromasm.imprecise delete mode 100644 test/dynamicLibrary.fromasm.imprecise.no-opts delete mode 100644 test/dynamicLibrary.fromasm.no-opts delete mode 100644 test/emcc_O2_hello_world.fromasm delete mode 100644 test/emcc_O2_hello_world.fromasm.clamp delete mode 100644 test/emcc_O2_hello_world.fromasm.clamp.no-opts delete mode 100644 test/emcc_O2_hello_world.fromasm.imprecise delete mode 100644 test/emcc_O2_hello_world.fromasm.imprecise.no-opts delete mode 100644 test/emcc_O2_hello_world.fromasm.no-opts delete mode 100644 test/emcc_hello_world.fromasm delete mode 100644 test/emcc_hello_world.fromasm.clamp delete mode 100644 test/emcc_hello_world.fromasm.clamp.no-opts delete mode 100644 test/emcc_hello_world.fromasm.imprecise delete mode 100644 test/emcc_hello_world.fromasm.imprecise.no-opts delete mode 100644 test/emcc_hello_world.fromasm.no-opts delete mode 100644 test/empty.fromasm delete mode 100644 test/empty.fromasm.clamp delete mode 100644 test/empty.fromasm.clamp.no-opts delete mode 100644 test/empty.fromasm.imprecise delete mode 100644 test/empty.fromasm.imprecise.no-opts delete mode 100644 test/empty.fromasm.no-opts delete mode 100644 test/empty_4GB.fromasm delete mode 100644 test/empty_4GB.fromasm.clamp delete mode 100644 test/empty_4GB.fromasm.clamp.no-opts delete mode 100644 test/empty_4GB.fromasm.imprecise delete mode 100644 test/empty_4GB.fromasm.imprecise.no-opts delete mode 100644 test/empty_4GB.fromasm.no-opts delete mode 100644 test/hello_world.fromasm delete mode 100644 test/hello_world.fromasm.clamp delete mode 100644 test/hello_world.fromasm.clamp.no-opts delete mode 100644 test/hello_world.fromasm.imprecise delete mode 100644 test/hello_world.fromasm.imprecise.no-opts delete mode 100644 test/hello_world.fromasm.no-opts delete mode 100644 test/i64-setTempRet0.fromasm delete mode 100644 test/i64-setTempRet0.fromasm.clamp delete mode 100644 test/i64-setTempRet0.fromasm.clamp.no-opts delete mode 100644 test/i64-setTempRet0.fromasm.imprecise delete mode 100644 test/i64-setTempRet0.fromasm.imprecise.no-opts delete mode 100644 test/i64-setTempRet0.fromasm.no-opts delete mode 100644 test/importedSignCast.fromasm delete mode 100644 test/importedSignCast.fromasm.clamp delete mode 100644 test/importedSignCast.fromasm.clamp.no-opts delete mode 100644 test/importedSignCast.fromasm.imprecise delete mode 100644 test/importedSignCast.fromasm.imprecise.no-opts delete mode 100644 test/importedSignCast.fromasm.no-opts delete mode 100644 test/memorygrowth-minimal.fromasm delete mode 100644 test/memorygrowth-minimal.fromasm.clamp delete mode 100644 test/memorygrowth-minimal.fromasm.clamp.no-opts delete mode 100644 test/memorygrowth-minimal.fromasm.imprecise delete mode 100644 test/memorygrowth-minimal.fromasm.imprecise.no-opts delete mode 100644 test/memorygrowth-minimal.fromasm.no-opts delete mode 100644 test/memorygrowth.fromasm delete mode 100644 test/memorygrowth.fromasm.clamp delete mode 100644 test/memorygrowth.fromasm.clamp.no-opts delete mode 100644 test/memorygrowth.fromasm.imprecise delete mode 100644 test/memorygrowth.fromasm.imprecise.no-opts delete mode 100644 test/memorygrowth.fromasm.no-opts delete mode 100644 test/min.fromasm delete mode 100644 test/min.fromasm.clamp delete mode 100644 test/min.fromasm.clamp.no-opts delete mode 100644 test/min.fromasm.imprecise delete mode 100644 test/min.fromasm.imprecise.no-opts delete mode 100644 test/min.fromasm.no-opts delete mode 100644 test/noffi_f32.fromasm delete mode 100644 test/noffi_f32.fromasm.clamp delete mode 100644 test/noffi_f32.fromasm.clamp.no-opts delete mode 100644 test/noffi_f32.fromasm.imprecise delete mode 100644 test/noffi_f32.fromasm.imprecise.no-opts delete mode 100644 test/noffi_f32.fromasm.no-opts delete mode 100644 test/noffi_i64.fromasm delete mode 100644 test/noffi_i64.fromasm.clamp delete mode 100644 test/noffi_i64.fromasm.clamp.no-opts delete mode 100644 test/noffi_i64.fromasm.imprecise delete mode 100644 test/noffi_i64.fromasm.imprecise.no-opts delete mode 100644 test/noffi_i64.fromasm.no-opts delete mode 100644 test/threads.fromasm delete mode 100644 test/threads.fromasm.clamp delete mode 100644 test/threads.fromasm.clamp.no-opts delete mode 100644 test/threads.fromasm.imprecise delete mode 100644 test/threads.fromasm.imprecise.no-opts delete mode 100644 test/threads.fromasm.no-opts delete mode 100644 test/threads.wasm-only.fromasm delete mode 100644 test/threads.wasm-only.fromasm.clamp delete mode 100644 test/threads.wasm-only.fromasm.clamp.no-opts delete mode 100644 test/threads.wasm-only.fromasm.imprecise delete mode 100644 test/threads.wasm-only.fromasm.imprecise.no-opts delete mode 100644 test/threads.wasm-only.fromasm.no-opts delete mode 100644 test/two_sides.fromasm delete mode 100644 test/two_sides.fromasm.clamp delete mode 100644 test/two_sides.fromasm.clamp.no-opts delete mode 100644 test/two_sides.fromasm.imprecise delete mode 100644 test/two_sides.fromasm.imprecise.no-opts delete mode 100644 test/two_sides.fromasm.no-opts delete mode 100644 test/unit.fromasm delete mode 100644 test/unit.fromasm.clamp delete mode 100644 test/unit.fromasm.clamp.no-opts delete mode 100644 test/unit.fromasm.imprecise delete mode 100644 test/unit.fromasm.imprecise.no-opts delete mode 100644 test/unit.fromasm.no-opts delete mode 100644 test/unreachable-import_wasm-only.fromasm delete mode 100644 test/unreachable-import_wasm-only.fromasm.clamp delete mode 100644 test/unreachable-import_wasm-only.fromasm.clamp.no-opts delete mode 100644 test/unreachable-import_wasm-only.fromasm.imprecise delete mode 100644 test/unreachable-import_wasm-only.fromasm.imprecise.no-opts delete mode 100644 test/unreachable-import_wasm-only.fromasm.no-opts delete mode 100644 test/use-import-and-drop.fromasm delete mode 100644 test/use-import-and-drop.fromasm.clamp delete mode 100644 test/use-import-and-drop.fromasm.clamp.no-opts delete mode 100644 test/use-import-and-drop.fromasm.imprecise delete mode 100644 test/use-import-and-drop.fromasm.imprecise.no-opts delete mode 100644 test/use-import-and-drop.fromasm.no-opts delete mode 100644 test/wasm-only.fromasm delete mode 100644 test/wasm-only.fromasm.clamp delete mode 100644 test/wasm-only.fromasm.clamp.no-opts delete mode 100644 test/wasm-only.fromasm.imprecise delete mode 100644 test/wasm-only.fromasm.imprecise.no-opts delete mode 100644 test/wasm-only.fromasm.no-opts diff --git a/CMakeLists.txt b/CMakeLists.txt index a529cd1b618..180bdc92984 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -321,7 +321,6 @@ endfunction() binaryen_add_executable(wasm-opt src/tools/wasm-opt.cpp) binaryen_add_executable(wasm-shell src/tools/wasm-shell.cpp) binaryen_add_executable(wasm-metadce src/tools/wasm-metadce.cpp) -binaryen_add_executable(asm2wasm src/tools/asm2wasm.cpp) binaryen_add_executable(wasm2js src/tools/wasm2js.cpp) binaryen_add_executable(wasm-emscripten-finalize src/tools/wasm-emscripten-finalize.cpp) binaryen_add_executable(wasm-as src/tools/wasm-as.cpp) diff --git a/auto_update_tests.py b/auto_update_tests.py index 2f81387497b..65b825393d7 100755 --- a/auto_update_tests.py +++ b/auto_update_tests.py @@ -27,51 +27,6 @@ from scripts.test import wasm_opt -def update_asm_js_tests(): - print('[ processing and updating testcases... ]\n') - for asm in shared.get_tests(shared.options.binaryen_test, ['.asm.js']): - basename = os.path.basename(asm) - for precise in [0, 1, 2]: - for opts in [1, 0]: - cmd = shared.ASM2WASM + [asm] - if 'threads' in basename: - cmd += ['--enable-threads'] - wasm = asm.replace('.asm.js', '.fromasm') - if not precise: - cmd += ['--trap-mode=allow', '--ignore-implicit-traps'] - wasm += '.imprecise' - elif precise == 2: - cmd += ['--trap-mode=clamp'] - wasm += '.clamp' - if not opts: - wasm += '.no-opts' - if precise: - cmd += ['-O0'] # test that -O0 does nothing - else: - cmd += ['-O'] - if 'debugInfo' in basename: - cmd += ['-g'] - if 'noffi' in basename: - cmd += ['--no-legalize-javascript-ffi'] - if precise and opts: - # test mem init importing - open('a.mem', 'wb').write(bytes(basename, 'utf-8')) - cmd += ['--mem-init=a.mem'] - if basename[0] == 'e': - cmd += ['--mem-base=1024'] - if '4GB' in basename: - cmd += ['--mem-max=4294967296'] - if 'i64' in basename or 'wasm-only' in basename or 'noffi' in basename: - cmd += ['--wasm-only'] - print(' '.join(cmd)) - actual = support.run_command(cmd) - with open(os.path.join(shared.options.binaryen_test, wasm), 'w') as o: - o.write(actual) - if 'debugInfo' in basename: - cmd += ['--source-map', os.path.join(shared.options.binaryen_test, wasm + '.map'), '-o', 'a.wasm'] - support.run_command(cmd) - - def update_bin_fmt_tests(): print('\n[ checking binary format testcases... ]\n') for wast in shared.get_tests(shared.options.binaryen_test, ['.wast']): @@ -224,7 +179,6 @@ def update_spec_tests(): TEST_SUITES = OrderedDict([ ('wasm-opt', wasm_opt.update_wasm_opt_tests), - ('asm2wasm', update_asm_js_tests), ('wasm-dis', update_wasm_dis_tests), ('example', update_example_tests), ('ctor-eval', update_ctor_eval_tests), diff --git a/check.py b/check.py index a47d4ef7e7a..e91dfda85f5 100755 --- a/check.py +++ b/check.py @@ -21,7 +21,6 @@ import unittest from collections import OrderedDict -from scripts.test import asm2wasm from scripts.test import binaryenjs from scripts.test import lld from scripts.test import shared @@ -354,8 +353,6 @@ def run_unittest(): TEST_SUITES = OrderedDict([ ('help-messages', run_help_tests), ('wasm-opt', wasm_opt.test_wasm_opt), - ('asm2wasm', asm2wasm.test_asm2wasm), - ('asm2wasm-binary', asm2wasm.test_asm2wasm_binary), ('wasm-dis', run_wasm_dis_tests), ('crash', run_crash_tests), ('dylink', run_dylink_tests), diff --git a/scripts/test/asm2wasm.py b/scripts/test/asm2wasm.py deleted file mode 100644 index bdc75c9f92b..00000000000 --- a/scripts/test/asm2wasm.py +++ /dev/null @@ -1,149 +0,0 @@ -# Copyright 2017 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. - -import os -import subprocess - -from scripts.test import shared -from scripts.test import support - - -def test_asm2wasm(): - print('[ checking asm2wasm testcases... ]\n') - - for asm in shared.get_tests(shared.options.binaryen_test, ['.asm.js']): - basename = os.path.basename(asm) - for precise in [0, 1, 2]: - for opts in [1, 0]: - cmd = shared.ASM2WASM + [asm] - if 'threads' in asm: - cmd += ['--enable-threads'] - wasm = asm.replace('.asm.js', '.fromasm') - if not precise: - cmd += ['--trap-mode=allow', '--ignore-implicit-traps'] - wasm += '.imprecise' - elif precise == 2: - cmd += ['--trap-mode=clamp'] - wasm += '.clamp' - if not opts: - wasm += '.no-opts' - if precise: - cmd += ['-O0'] # test that -O0 does nothing - else: - cmd += ['-O'] - if 'debugInfo' in basename: - cmd += ['-g'] - if 'noffi' in basename: - cmd += ['--no-legalize-javascript-ffi'] - if precise and opts: - # test mem init importing - open('a.mem', 'w').write(basename) - cmd += ['--mem-init=a.mem'] - if basename[0] == 'e': - cmd += ['--mem-base=1024'] - if '4GB' in basename: - cmd += ['--mem-max=4294967296'] - if 'i64' in basename or 'wasm-only' in basename or 'noffi' in basename: - cmd += ['--wasm-only'] - print('..', basename, os.path.basename(wasm)) - - def do_asm2wasm_test(): - actual = support.run_command(cmd) - - # verify output - if not os.path.exists(wasm): - shared.fail_with_error('output .wast file %s does not exist' % wasm) - shared.fail_if_not_identical_to_file(actual, wasm) - - shared.binary_format_check(wasm, verify_final_result=False) - - # test both normally and with pass debug (so each inter-pass state - # is validated) - old_pass_debug = os.environ.get('BINARYEN_PASS_DEBUG') - try: - os.environ['BINARYEN_PASS_DEBUG'] = '1' - print("With BINARYEN_PASS_DEBUG=1:") - do_asm2wasm_test() - del os.environ['BINARYEN_PASS_DEBUG'] - print("With BINARYEN_PASS_DEBUG disabled:") - do_asm2wasm_test() - finally: - if old_pass_debug is not None: - os.environ['BINARYEN_PASS_DEBUG'] = old_pass_debug - else: - if 'BINARYEN_PASS_DEBUG' in os.environ: - del os.environ['BINARYEN_PASS_DEBUG'] - - # verify in wasm - if shared.options.interpreter: - # remove imports, spec interpreter doesn't know what to do with them - subprocess.check_call(shared.WASM_OPT + ['--remove-imports', wasm], - stdout=open('ztemp.wast', 'w'), - stderr=subprocess.PIPE) - proc = subprocess.Popen([shared.options.interpreter, 'ztemp.wast'], - stderr=subprocess.PIPE) - out, err = proc.communicate() - if proc.returncode != 0: - try: # to parse the error - reported = err.split(':')[1] - start, end = reported.split('-') - start_line, start_col = map(int, start.split('.')) - lines = open('ztemp.wast').read().split('\n') - print() - print('=' * 80) - print(lines[start_line - 1]) - print((' ' * (start_col - 1)) + '^') - print((' ' * (start_col - 2)) + '/_\\') - print('=' * 80) - print(err) - except Exception: - # failed to pretty-print - shared.fail_with_error('wasm interpreter error: ' + err) - shared.fail_with_error('wasm interpreter error') - - # verify debug info - if 'debugInfo' in asm: - jsmap = 'a.wasm.map' - cmd += ['--source-map', jsmap, - '--source-map-url', 'http://example.org/' + jsmap, - '-o', 'a.wasm'] - support.run_command(cmd) - if not os.path.isfile(jsmap): - shared.fail_with_error('Debug info map not created: %s' % jsmap) - with open(jsmap, 'rb') as actual: - shared.fail_if_not_identical_to_file(actual.read(), wasm + '.map') - with open('a.wasm', 'rb') as binary: - url_section_name = bytes([16]) + bytes('sourceMappingURL', 'utf-8') - url = 'http://example.org/' + jsmap - assert len(url) < 256, 'name too long' - url_section_contents = bytes([len(url)]) + bytes(url, 'utf-8') - print(url_section_name) - binary_contents = binary.read() - if url_section_name not in binary_contents: - shared.fail_with_error('source map url section not found in binary') - url_section_index = binary_contents.index(url_section_name) - if url_section_contents not in binary_contents[url_section_index:]: - shared.fail_with_error('source map url not found in url section') - - -def test_asm2wasm_binary(): - print('\n[ checking asm2wasm binary reading/writing... ]\n') - - asmjs = os.path.join(shared.options.binaryen_test, 'hello_world.asm.js') - shared.delete_from_orbit('a.wasm') - shared.delete_from_orbit('b.wast') - support.run_command(shared.ASM2WASM + [asmjs, '-o', 'a.wasm']) - assert open('a.wasm', 'rb').read()[0] == 0, 'we emit binary by default' - support.run_command(shared.ASM2WASM + [asmjs, '-o', 'b.wast', '-S']) - assert open('b.wast', 'rb').read()[0] != 0, 'we emit text with -S' diff --git a/src/asm2wasm.h b/src/asm2wasm.h deleted file mode 100644 index ef19c32ffff..00000000000 --- a/src/asm2wasm.h +++ /dev/null @@ -1,3290 +0,0 @@ -/* - * Copyright 2015 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. - */ - -// -// asm.js-to-WebAssembly translator. Uses the Emscripten optimizer -// infrastructure. -// - -#ifndef wasm_asm2wasm_h -#define wasm_asm2wasm_h - -#include "abi/js.h" -#include "asm_v_wasm.h" -#include "asmjs/shared-constants.h" -#include "emscripten-optimizer/optimizer.h" -#include "ir/bits.h" -#include "ir/branch-utils.h" -#include "ir/literal-utils.h" -#include "ir/module-utils.h" -#include "ir/trapping.h" -#include "ir/utils.h" -#include "mixed_arena.h" -#include "parsing.h" -#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; - -// Names - -Name I32_CTTZ("i32_cttz"); -Name I32_CTPOP("i32_ctpop"); -Name I32_BC2F("i32_bc2f"); -Name I32_BC2I("i32_bc2i"); -Name I64("i64"); -Name I64_CONST("i64_const"); -Name I64_ADD("i64_add"); -Name I64_SUB("i64_sub"); -Name I64_MUL("i64_mul"); -Name I64_UDIV("i64_udiv"); -Name I64_SDIV("i64_sdiv"); -Name I64_UREM("i64_urem"); -Name I64_SREM("i64_srem"); -Name I64_AND("i64_and"); -Name I64_OR("i64_or"); -Name I64_XOR("i64_xor"); -Name I64_SHL("i64_shl"); -Name I64_ASHR("i64_ashr"); -Name I64_LSHR("i64_lshr"); -Name I64_EQ("i64_eq"); -Name I64_NE("i64_ne"); -Name I64_ULE("i64_ule"); -Name I64_SLE("i64_sle"); -Name I64_UGE("i64_uge"); -Name I64_SGE("i64_sge"); -Name I64_ULT("i64_ult"); -Name I64_SLT("i64_slt"); -Name I64_UGT("i64_ugt"); -Name I64_SGT("i64_sgt"); -Name I64_TRUNC("i64_trunc"); -Name I64_SEXT("i64_sext"); -Name I64_ZEXT("i64_zext"); -Name I64_S2F("i64_s2f"); -Name I64_S2D("i64_s2d"); -Name I64_U2F("i64_u2f"); -Name I64_U2D("i64_u2d"); -Name I64_F2S("i64_f2s"); -Name I64_D2S("i64_d2s"); -Name I64_F2U("i64_f2u"); -Name I64_D2U("i64_d2u"); -Name I64_BC2D("i64_bc2d"); -Name I64_BC2I("i64_bc2i"); -Name I64_CTTZ("i64_cttz"); -Name I64_CTLZ("i64_ctlz"); -Name I64_CTPOP("i64_ctpop"); -Name F32_COPYSIGN("f32_copysign"); -Name F64_COPYSIGN("f64_copysign"); -Name LOAD1("load1"); -Name LOAD2("load2"); -Name LOAD4("load4"); -Name LOAD8("load8"); -Name LOADF("loadf"); -Name LOADD("loadd"); -Name STORE1("store1"); -Name STORE2("store2"); -Name STORE4("store4"); -Name STORE8("store8"); -Name STOREF("storef"); -Name STORED("stored"); -Name FTCALL("ftCall_"); -Name MFTCALL("mftCall_"); -Name MAX_("max"); -Name MIN_("min"); -Name ATOMICS("Atomics"); -Name ATOMICS_LOAD("load"); -Name ATOMICS_STORE("store"); -Name ATOMICS_EXCHANGE("exchange"); -Name ATOMICS_COMPARE_EXCHANGE("compareExchange"); -Name ATOMICS_ADD("add"); -Name ATOMICS_SUB("sub"); -Name ATOMICS_AND("and"); -Name ATOMICS_OR("or"); -Name ATOMICS_XOR("xor"); -Name I64_ATOMICS_LOAD("i64_atomics_load"); -Name I64_ATOMICS_STORE("i64_atomics_store"); -Name I64_ATOMICS_AND("i64_atomics_and"); -Name I64_ATOMICS_OR("i64_atomics_or"); -Name I64_ATOMICS_XOR("i64_atomics_xor"); -Name I64_ATOMICS_ADD("i64_atomics_add"); -Name I64_ATOMICS_SUB("i64_atomics_sub"); -Name I64_ATOMICS_EXCHANGE("i64_atomics_exchange"); -Name I64_ATOMICS_COMPAREEXCHANGE("i64_atomics_compareExchange"); -Name TEMP_DOUBLE_PTR("tempDoublePtr"); -Name EMSCRIPTEN_DEBUGINFO("emscripten_debuginfo"); - -// Utilities - -static WASM_NORETURN void abort_on(std::string why, Ref element) { - std::cerr << why << ' '; - element->stringify(std::cerr); - std::cerr << '\n'; - abort(); -} -static WASM_NORETURN void abort_on(std::string why, IString element) { - std::cerr << why << ' ' << element.str << '\n'; - abort(); -} - -Index indexOr(Index x, Index y) { return x ? x : y; } - -// useful when we need to see our parent, in an asm.js expression stack -struct AstStackHelper { - static std::vector astStack; - AstStackHelper(Ref curr) { astStack.push_back(curr); } - ~AstStackHelper() { astStack.pop_back(); } - Ref getParent() { - if (astStack.size() >= 2) { - return astStack[astStack.size() - 2]; - } else { - return Ref(); - } - } -}; - -std::vector AstStackHelper::astStack; - -static bool startsWith(const char* string, const char* prefix) { - while (1) { - if (*prefix == 0) { - return true; - } - if (*string == 0) { - return false; - } - if (*string++ != *prefix++) { - return false; - } - } -}; - -// -// Asm2WasmPreProcessor - does some initial parsing/processing -// of asm.js code. -// - -struct Asm2WasmPreProcessor { - bool memoryGrowth = false; - bool debugInfo = false; - - std::vector debugInfoFileNames; - std::unordered_map debugInfoFileIndices; - - char* allocatedCopy = nullptr; - - ~Asm2WasmPreProcessor() { - if (allocatedCopy) { - free(allocatedCopy); - } - } - - char* process(char* input) { - // emcc --separate-asm modules can look like - // - // Module["asm"] = (function(global, env, buffer) { - // .. - // }); - // - // we need to clean that up. - if (*input == 'M') { - size_t num = strlen(input); - while (*input != 'f') { - input++; - num--; - } - char* end = input + num - 1; - while (*end != '}') { - *end = 0; - end--; - } - } - - // asm.js memory growth uses a quite elaborate pattern. Instead of parsing - // and matching it, we do a simpler detection on emscripten's asm.js output - // format - const char* START_FUNCS = "// EMSCRIPTEN_START_FUNCS"; - char* marker = strstr(input, START_FUNCS); - if (marker) { - // look for memory growth code just up to here, as an optimization - *marker = 0; - } - // this can only show up in growth code, as normal asm.js lacks "true" - char* growthSign = strstr(input, "return true;"); - if (growthSign) { - memoryGrowth = true; - // clean out this function, we don't need it. first where it starts - char* growthFuncStart = growthSign; - while (*growthFuncStart != '{') { - growthFuncStart--; // skip body - } - while (*growthFuncStart != '(') { - growthFuncStart--; // skip params - } - while (*growthFuncStart != ' ') { - growthFuncStart--; // skip function name - } - while (*growthFuncStart != 'f') { - growthFuncStart--; // skip 'function' - } - assert(strstr(growthFuncStart, "function ") == growthFuncStart); - char* growthFuncEnd = strchr(growthSign, '}'); - assert(growthFuncEnd > growthFuncStart + 5); - growthFuncStart[0] = '/'; - growthFuncStart[1] = '*'; - growthFuncEnd--; - growthFuncEnd[0] = '*'; - growthFuncEnd[1] = '/'; - } - if (marker) { - *marker = START_FUNCS[0]; - } - - // handle debug info, if this build wants that. - if (debugInfo) { - // asm.js debug info comments look like - // ..command..; //@line 4 "tests/hello_world.c" - // we convert those into emscripten_debuginfo(file, line) - // calls, where the params are indices into a mapping. then - // the compiler and optimizer can operate on them. after - // that, we can apply the debug info to the wasm node right - // before it - this is guaranteed to be correct without opts, - // and is usually decently accurate with them. - - // an upper bound on how much more space we need as a multiple of the - // original - const auto SCALE_FACTOR = 1.25; - // an upper bound on how much we write for each debug info element itself - const auto ADD_FACTOR = 100; - auto size = strlen(input); - auto upperBound = Index(size * SCALE_FACTOR) + ADD_FACTOR; - char* copy = allocatedCopy = (char*)malloc(upperBound); - char* end = copy + upperBound; - char* out = copy; - std::string DEBUGINFO_INTRINSIC = EMSCRIPTEN_DEBUGINFO.str; - auto DEBUGINFO_INTRINSIC_SIZE = DEBUGINFO_INTRINSIC.size(); - const char* UNKNOWN_FILE = "(unknown)"; - bool seenUseAsm = false; - while (input[0]) { - if (out + ADD_FACTOR >= end) { - Fatal() << "error in handling debug info"; - } - if (startsWith(input, "//@line")) { - char* linePos = input + 8; - char* lineEnd = strpbrk(input + 8, " \n"); - if (!lineEnd) { - // comment goes to end of input - break; - } - input = lineEnd + 1; - std::string file; - if (*lineEnd == ' ') { - // we have a file - char* filePos = strpbrk(input, "\"\n"); - if (!filePos) { - // goes to end of input - break; - } - if (*filePos == '"') { - char* fileEnd = strpbrk(filePos + 1, "\"\n"); - input = fileEnd + 1; - *fileEnd = 0; - file = filePos + 1; - } else { - file = UNKNOWN_FILE; - input = filePos + 1; - } - } else { - // no file, we found \n - file = UNKNOWN_FILE; - } - *lineEnd = 0; - std::string line = linePos; - auto iter = debugInfoFileIndices.find(file); - if (iter == debugInfoFileIndices.end()) { - Index index = debugInfoFileNames.size(); - debugInfoFileNames.push_back(file); - debugInfoFileIndices[file] = index; - } - std::string fileIndex = std::to_string(debugInfoFileIndices[file]); - // write out the intrinsic - strcpy(out, DEBUGINFO_INTRINSIC.c_str()); - out += DEBUGINFO_INTRINSIC_SIZE; - *out++ = '('; - strcpy(out, fileIndex.c_str()); - out += fileIndex.size(); - *out++ = ','; - strcpy(out, line.c_str()); - out += line.size(); - *out++ = ')'; - *out++ = ';'; - } else if (!seenUseAsm && - (startsWith(input, "asm'") || startsWith(input, "asm\""))) { - // end of "use asm" or "almost asm" - // skip the end of "use asm"; (5 chars, a,s,m," or ',;) - const auto SKIP = 5; - seenUseAsm = true; - memcpy(out, input, SKIP); - out += SKIP; - input += SKIP; - // add a fake import for the intrinsic, so the module validates - std::string import = - "\n var emscripten_debuginfo = env.emscripten_debuginfo;"; - strcpy(out, import.c_str()); - out += import.size(); - } else { - *out++ = *input++; - } - } - if (out >= end) { - Fatal() << "error in handling debug info"; - } - *out = 0; - input = copy; - } - - return input; - } -}; - -static Call* checkDebugInfo(Expression* curr) { - if (auto* call = curr->dynCast()) { - if (call->target == EMSCRIPTEN_DEBUGINFO) { - return call; - } - } - return nullptr; -} - -// Debug info appears in the ast as calls to the debug intrinsic. These are -// usually after the relevant node. We adjust them to a position that is not -// dce-able, so that they are not trivially removed when optimizing. -struct AdjustDebugInfo - : public WalkerPass>> { - bool isFunctionParallel() override { return true; } - - Pass* create() override { return new AdjustDebugInfo(); } - - AdjustDebugInfo() { name = "adjust-debug-info"; } - - void visitBlock(Block* curr) { - // look for a debug info call that is unreachable - if (curr->list.size() == 0) { - return; - } - auto* back = curr->list.back(); - for (Index i = 1; i < curr->list.size(); i++) { - if (checkDebugInfo(curr->list[i]) && !checkDebugInfo(curr->list[i - 1])) { - // swap them - std::swap(curr->list[i - 1], curr->list[i]); - } - } - if (curr->list.back() != back) { - // we changed the last element, update the type - curr->finalize(); - } - } -}; - -// -// Asm2WasmBuilder - converts an asm.js module into WebAssembly -// - -class Asm2WasmBuilder { -public: - Module& wasm; - - MixedArena& allocator; - - Builder builder; - - std::unique_ptr optimizingBuilder; - - // globals - - struct MappedGlobal { - Type type; - // if true, this is an import - we should read the value, not just set a - // zero - bool import; - IString module, base; - MappedGlobal() : type(Type::none), import(false) {} - MappedGlobal(Type type) : type(type), import(false) {} - MappedGlobal(Type type, bool import, IString module, IString base) - : type(type), import(import), module(module), base(base) {} - }; - - // function table - // each asm function table gets a range in the one wasm table, starting at a - // location - std::map functionTableStarts; - - Asm2WasmPreProcessor& preprocessor; - bool debug; - TrapMode trapMode; - TrappingFunctionContainer trappingFunctions; - PassOptions passOptions; - bool legalizeJavaScriptFFI; - bool runOptimizationPasses; - bool wasmOnly; - -public: - std::map mappedGlobals; - -private: - void allocateGlobal(IString name, Type type, Literal value = Literal()) { - assert(mappedGlobals.find(name) == mappedGlobals.end()); - if (value.type == Type::none) { - value = Literal::makeSingleZero(type); - } - mappedGlobals.emplace(name, MappedGlobal(type)); - wasm.addGlobal(builder.makeGlobal( - name, type, builder.makeConst(value), Builder::Mutable)); - } - - struct View { - unsigned bytes; - bool integer, signed_; - AsmType type; - View() : bytes(0) {} - View(unsigned bytes, bool integer, bool signed_, AsmType type) - : bytes(bytes), integer(integer), signed_(signed_), type(type) {} - }; - - std::map views; // name (e.g. HEAP8) => view info - - // Imported names of Math.* - IString Math_imul; - IString Math_clz32; - IString Math_fround; - IString Math_abs; - IString Math_floor; - IString Math_ceil; - IString Math_sqrt; - IString Math_max; - IString Math_min; - - // Imported names of Atomics.* - IString Atomics_load; - IString Atomics_store; - IString Atomics_exchange; - IString Atomics_compareExchange; - IString Atomics_add; - IString Atomics_sub; - IString Atomics_and; - IString Atomics_or; - IString Atomics_xor; - - IString llvm_cttz_i32; - - IString tempDoublePtr; // imported name of tempDoublePtr - - // possibly-minified names, detected via their exports - IString udivmoddi4; - IString getTempRet0; - - // function types. we fill in this information as we see - // uses, in the first pass - - std::map importedSignatures; - - void noteImportedFunctionCall(Ref ast, Type resultType, Call* call) { - assert(ast[0] == CALL && ast[1]->isString()); - IString importName = ast[1]->getIString(); - std::vector params; - for (auto* operand : call->operands) { - params.push_back(operand->type); - } - Signature sig = Signature(Type(params), resultType); - // if we already saw this signature, verify it's the same (or else handle - // that) - if (importedSignatures.find(importName) != importedSignatures.end()) { - Signature& previous = importedSignatures[importName]; - if (sig != previous) { - std::vector mergedParams = previous.params.expand(); - // merge it in. we'll add on extra 0 parameters for ones not actually - // used, and upgrade types to double where there is a conflict (which is - // ok since in JS, double can contain everything i32 and f32 can). - for (size_t i = 0; i < params.size(); i++) { - if (mergedParams.size() > i) { - if (mergedParams[i] != params[i]) { - mergedParams[i] = Type::f64; // overloaded type, make it a double - } - } else { - mergedParams.push_back(params[i]); // add a new param - } - } - previous.params = Type(mergedParams); - // we accept none and a concrete type, but two concrete types mean we - // need to use an f64 to contain anything - if (previous.results == Type::none) { - previous.results = sig.results; // use a more concrete type - } else if (previous.results != sig.results && - sig.results != Type::none) { - // overloaded return type, make it a double - previous.results = Type::f64; - } - } - } else { - importedSignatures[importName] = sig; - } - } - - Type getResultTypeOfCallUsingParent(Ref parent, AsmData* data) { - Type result = Type::none; - if (!!parent) { - // if the parent is a seq, we cannot be the last element in it (we would - // have a coercion, which would be the parent), so we must be (us, - // somethingElse), and so our return is void - if (parent[0] != SEQ) { - result = detectWasmType(parent, data); - } - } - return result; - } - - Signature getSignature(Ref parent, ExpressionList& operands, AsmData* data) { - Type results = getResultTypeOfCallUsingParent(parent, data); - std::vector paramTypes; - for (auto& op : operands) { - assert(op->type != Type::unreachable); - paramTypes.push_back(op->type); - } - return Signature(Type(paramTypes), results); - } - -public: - Asm2WasmBuilder(Module& wasm, - Asm2WasmPreProcessor& preprocessor, - bool debug, - TrapMode trapMode, - PassOptions passOptions, - bool legalizeJavaScriptFFI, - bool runOptimizationPasses, - bool wasmOnly) - : wasm(wasm), allocator(wasm.allocator), builder(wasm), - preprocessor(preprocessor), debug(debug), trapMode(trapMode), - trappingFunctions(trapMode, wasm, /* immediate = */ true), - passOptions(passOptions), legalizeJavaScriptFFI(legalizeJavaScriptFFI), - runOptimizationPasses(runOptimizationPasses), wasmOnly(wasmOnly) {} - - void processAsm(Ref ast); - -private: - AsmType detectAsmType(Ref ast, AsmData* data) { - if (ast->isString()) { - IString name = ast->getIString(); - if (!data->isLocal(name)) { - // must be global - assert(mappedGlobals.find(name) != mappedGlobals.end()); - return wasmToAsmType(mappedGlobals[name].type); - } - } else if (ast->isArray(SUB) && ast[1]->isString()) { - // could be a heap access, use view info - auto view = views.find(ast[1]->getIString()); - if (view != views.end()) { - return view->second.type; - } - } - return detectType(ast, data, false, Math_fround, wasmOnly); - } - - Type detectWasmType(Ref ast, AsmData* data) { - return asmToWasmType(detectAsmType(ast, data)); - } - - bool isUnsignedCoercion(Ref ast) { - return detectSign(ast, Math_fround) == ASM_UNSIGNED; - } - - bool isParentUnsignedCoercion(Ref parent) { - // parent may not exist, or may be a non-relevant node - if (!!parent && parent->isArray() && parent[0] == BINARY && - isUnsignedCoercion(parent)) { - return true; - } - return false; - } - - BinaryOp parseAsmBinaryOp(IString op, - Ref left, - Ref right, - Expression* leftWasm, - Expression* rightWasm) { - Type leftType = leftWasm->type; - bool isInteger = leftType == Type::i32; - - if (op == PLUS) { - return isInteger ? BinaryOp::AddInt32 - : (leftType == Type::f32 ? BinaryOp::AddFloat32 - : BinaryOp::AddFloat64); - } - if (op == MINUS) { - return isInteger ? BinaryOp::SubInt32 - : (leftType == Type::f32 ? BinaryOp::SubFloat32 - : BinaryOp::SubFloat64); - } - if (op == MUL) { - return isInteger ? BinaryOp::MulInt32 - : (leftType == Type::f32 ? BinaryOp::MulFloat32 - : BinaryOp::MulFloat64); - } - if (op == AND) { - return BinaryOp::AndInt32; - } - if (op == OR) { - return BinaryOp::OrInt32; - } - if (op == XOR) { - return BinaryOp::XorInt32; - } - if (op == LSHIFT) { - return BinaryOp::ShlInt32; - } - if (op == RSHIFT) { - return BinaryOp::ShrSInt32; - } - if (op == TRSHIFT) { - return BinaryOp::ShrUInt32; - } - if (op == EQ) { - return isInteger ? BinaryOp::EqInt32 - : (leftType == Type::f32 ? BinaryOp::EqFloat32 - : BinaryOp::EqFloat64); - } - if (op == NE) { - return isInteger ? BinaryOp::NeInt32 - : (leftType == Type::f32 ? BinaryOp::NeFloat32 - : BinaryOp::NeFloat64); - } - - bool isUnsigned = isUnsignedCoercion(left) || isUnsignedCoercion(right); - - if (op == DIV) { - if (isInteger) { - return isUnsigned ? BinaryOp::DivUInt32 : BinaryOp::DivSInt32; - } - return leftType == Type::f32 ? BinaryOp::DivFloat32 - : BinaryOp::DivFloat64; - } - if (op == MOD) { - if (isInteger) { - return isUnsigned ? BinaryOp::RemUInt32 : BinaryOp::RemSInt32; - } - return BinaryOp::RemSInt32; // XXX no floating-point remainder op, this - // must be handled by the caller - } - if (op == GE) { - if (isInteger) { - return isUnsigned ? BinaryOp::GeUInt32 : BinaryOp::GeSInt32; - } - return leftType == Type::f32 ? BinaryOp::GeFloat32 : BinaryOp::GeFloat64; - } - if (op == GT) { - if (isInteger) { - return isUnsigned ? BinaryOp::GtUInt32 : BinaryOp::GtSInt32; - } - return leftType == Type::f32 ? BinaryOp::GtFloat32 : BinaryOp::GtFloat64; - } - if (op == LE) { - if (isInteger) { - return isUnsigned ? BinaryOp::LeUInt32 : BinaryOp::LeSInt32; - } - return leftType == Type::f32 ? BinaryOp::LeFloat32 : BinaryOp::LeFloat64; - } - if (op == LT) { - if (isInteger) { - return isUnsigned ? BinaryOp::LtUInt32 : BinaryOp::LtSInt32; - } - return leftType == Type::f32 ? BinaryOp::LtFloat32 : BinaryOp::LtFloat64; - } - abort_on("bad wasm binary op", op); - abort(); // avoid warning - } - - int32_t bytesToShift(unsigned bytes) { - switch (bytes) { - case 1: - return 0; - case 2: - return 1; - case 4: - return 2; - case 8: - return 3; - default: {} - } - abort(); - return -1; // avoid warning - } - - std::map tempNums; - - Literal checkLiteral(Ref ast, bool rawIsInteger = true) { - if (ast->isNumber()) { - if (rawIsInteger) { - return Literal((int32_t)ast->getInteger()); - } else { - return Literal(ast->getNumber()); - } - } else if (ast->isArray(UNARY_PREFIX)) { - if (ast[1] == PLUS && ast[2]->isNumber()) { - return Literal((double)ast[2]->getNumber()); - } - if (ast[1] == MINUS && ast[2]->isNumber()) { - double num = -ast[2]->getNumber(); - if (isSInteger32(num)) { - return Literal((int32_t)num); - } - if (isUInteger32(num)) { - return Literal((uint32_t)num); - } - assert(false && "expected signed or unsigned int32"); - } - if (ast[1] == PLUS && ast[2]->isArray(UNARY_PREFIX) && - ast[2][1] == MINUS && ast[2][2]->isNumber()) { - return Literal((double)-ast[2][2]->getNumber()); - } - if (ast[1] == MINUS && ast[2]->isArray(UNARY_PREFIX) && - ast[2][1] == PLUS && ast[2][2]->isNumber()) { - return Literal((double)-ast[2][2]->getNumber()); - } - } else if (wasmOnly && ast->isArray(CALL) && ast[1]->isString() && - ast[1] == I64_CONST) { - uint64_t low = ast[2][0]->getNumber(); - uint64_t high = ast[2][1]->getNumber(); - return Literal(uint64_t(low + (high << 32))); - } - return Literal(); - } - - Literal getLiteral(Ref ast) { - Literal ret = checkLiteral(ast); - assert(ret.type != Type::none); - return ret; - } - - void fixCallType(Expression* call, Type type) { - if (call->is()) { - call->cast()->type = type; - } else if (call->is()) { - call->cast()->type = type; - } - } - - bool getBuiltinSignature(Signature& sig, - Name module, - Name base, - ExpressionList* operands = nullptr) { - if (module == GLOBAL_MATH) { - if (base == ABS) { - assert(operands && operands->size() == 1); - Type type = (*operands)[0]->type; - if (type == Type::i32) { - sig = Signature(Type::i32, Type::i32); - return true; - } - if (type == Type::f32) { - sig = Signature(Type::f32, Type::f32); - return true; - } - if (type == Type::f64) { - sig = Signature(Type::f64, Type::f64); - return true; - } - } - } - return false; - } - - // ensure a nameless block - Block* blockify(Expression* expression) { - if (expression->is() && !expression->cast()->name.is()) { - return expression->dynCast(); - } - auto ret = allocator.alloc(); - ret->list.push_back(expression); - ret->finalize(); - return ret; - } - - Expression* ensureDouble(Expression* expr) { - return wasm::ensureDouble(expr, allocator); - } - - Expression* truncateToInt32(Expression* value) { - if (value->type == Type::i64) { - return builder.makeUnary(UnaryOp::WrapInt64, value); - } - // either i32, or a call_import whose type we don't know yet (but would be - // legalized to i32 anyhow) - return value; - } - - Function* processFunction(Ref ast); -}; - -void Asm2WasmBuilder::processAsm(Ref ast) { - assert(ast[0] == TOPLEVEL); - if (ast[1]->size() == 0) { - Fatal() << "empty input"; - } - Ref asmFunction = ast[1][0]; - assert(asmFunction[0] == DEFUN); - Ref body = asmFunction[3]; - assert(body[0][0] == STRING && - (body[0][1]->getIString() == IString("use asm") || - body[0][1]->getIString() == IString("almost asm"))); - - // extra functions that we add, that are not from the compiled code. we need - // to make sure to optimize them normally (OptimizingIncrementalModuleBuilder - // does that on the fly for compiled code) - std::vector extraSupportFunctions; - - // first, add the memory elements. we do this before the main compile+optimize - // since the optimizer should see the memory - - // apply memory growth, if relevant - if (preprocessor.memoryGrowth) { - EmscriptenGlueGenerator generator(wasm); - auto* func = generator.generateMemoryGrowthFunction(); - extraSupportFunctions.push_back(func); - wasm.memory.max = Memory::kUnlimitedSize; - } - - // import memory - wasm.memory.name = MEMORY; - wasm.memory.module = ENV; - wasm.memory.base = MEMORY; - wasm.memory.exists = true; - - // import table - wasm.table.name = TABLE; - wasm.table.module = ENV; - wasm.table.base = TABLE; - wasm.table.exists = true; - - // Import memory offset, if not already there - { - auto* import = new Global; - import->name = MEMORY_BASE; - import->module = "env"; - import->base = MEMORY_BASE; - import->type = Type::i32; - wasm.addGlobal(import); - } - - // Import table offset, if not already there - { - auto* import = new Global; - import->name = TABLE_BASE; - import->module = "env"; - import->base = TABLE_BASE; - import->type = Type::i32; - wasm.addGlobal(import); - } - - auto addImport = [&](IString name, Ref imported, Type type) { - assert(imported[0] == DOT); - Ref module = imported[1]; - IString moduleName; - if (module->isArray(DOT)) { - // we can have (global.Math).floor; skip the 'Math' - assert(module[1]->isString()); - if (module[2] == MATH) { - if (imported[2] == IMUL) { - assert(Math_imul.isNull()); - Math_imul = name; - return; - } else if (imported[2] == CLZ32) { - assert(Math_clz32.isNull()); - Math_clz32 = name; - return; - } else if (imported[2] == FROUND) { - assert(Math_fround.isNull()); - Math_fround = name; - return; - } else if (imported[2] == ABS) { - assert(Math_abs.isNull()); - Math_abs = name; - return; - } else if (imported[2] == FLOOR) { - assert(Math_floor.isNull()); - Math_floor = name; - return; - } else if (imported[2] == CEIL) { - assert(Math_ceil.isNull()); - Math_ceil = name; - return; - } else if (imported[2] == SQRT) { - assert(Math_sqrt.isNull()); - Math_sqrt = name; - return; - } else if (imported[2] == MAX_) { - assert(Math_max.isNull()); - Math_max = name; - return; - } else if (imported[2] == MIN_) { - assert(Math_min.isNull()); - Math_min = name; - return; - } - } else if (module[2] == ATOMICS) { - if (imported[2] == ATOMICS_LOAD) { - assert(Atomics_load.isNull()); - Atomics_load = name; - return; - } else if (imported[2] == ATOMICS_STORE) { - assert(Atomics_store.isNull()); - Atomics_store = name; - return; - } else if (imported[2] == ATOMICS_EXCHANGE) { - assert(Atomics_exchange.isNull()); - Atomics_exchange = name; - return; - } else if (imported[2] == ATOMICS_COMPARE_EXCHANGE) { - assert(Atomics_compareExchange.isNull()); - Atomics_compareExchange = name; - return; - } else if (imported[2] == ATOMICS_ADD) { - assert(Atomics_add.isNull()); - Atomics_add = name; - return; - } else if (imported[2] == ATOMICS_SUB) { - assert(Atomics_sub.isNull()); - Atomics_sub = name; - return; - } else if (imported[2] == ATOMICS_AND) { - assert(Atomics_and.isNull()); - Atomics_and = name; - return; - } else if (imported[2] == ATOMICS_OR) { - assert(Atomics_or.isNull()); - Atomics_or = name; - return; - } else if (imported[2] == ATOMICS_XOR) { - assert(Atomics_xor.isNull()); - Atomics_xor = name; - return; - } - } - std::string fullName = module[1]->getCString(); - fullName += '.'; - fullName += +module[2]->getCString(); - moduleName = IString(fullName.c_str(), false); - } else { - assert(module->isString()); - moduleName = module->getIString(); - if (moduleName == ENV) { - auto base = imported[2]->getIString(); - if (base == TEMP_DOUBLE_PTR) { - assert(tempDoublePtr.isNull()); - tempDoublePtr = name; - // we don't return here, as we can only optimize out some uses of tDP. - // So it remains imported - } else if (base == LLVM_CTTZ_I32) { - assert(llvm_cttz_i32.isNull()); - llvm_cttz_i32 = name; - return; - } - } - } - auto base = imported[2]->getIString(); - // special-case some asm builtins - if (module == GLOBAL && (base == NAN_ || base == INFINITY_)) { - type = Type::f64; - } - if (type != Type::none) { - // this is a global - auto* import = new Global; - import->name = name; - import->module = moduleName; - import->base = base; - import->type = type; - mappedGlobals.emplace(name, type); - // __table_base and __memory_base are used as segment/element offsets, and - // must be constant; otherwise, an asm.js import of a constant is mutable, - // e.g. STACKTOP - if (name != TABLE_BASE && name != MEMORY_BASE) { - // we need imported globals to be mutable, but wasm doesn't support that - // yet, so we must import an immutable and create a mutable global - // initialized to its value - import->name = Name(std::string(import->name.str) + "$asm2wasm$import"); - { - wasm.addGlobal( - builder.makeGlobal(name, - type, - builder.makeGlobalGet(import->name, type), - Builder::Mutable)); - } - } - if ((name == TABLE_BASE || name == MEMORY_BASE) && - wasm.getGlobalOrNull(import->base)) { - return; - } - wasm.addGlobal(import); - } else { - // this is a function - auto* import = new Function; - import->name = name; - import->module = moduleName; - import->base = base; - import->sig = Signature(Type::none, Type::none); - wasm.addFunction(import); - } - }; - - IString Int8Array, Int16Array, Int32Array, UInt8Array, UInt16Array, - UInt32Array, Float32Array, Float64Array; - - // set up optimization - - if (runOptimizationPasses) { - Index numFunctions = 0; - for (unsigned i = 1; i < body->size(); i++) { - if (body[i][0] == DEFUN) { - numFunctions++; - } - } - optimizingBuilder = make_unique( - &wasm, - numFunctions, - passOptions, - [&](PassRunner& passRunner) { - // addPrePasses - passRunner.options.lowMemoryUnused = true; - if (debug) { - passRunner.setDebug(true); - passRunner.setValidateGlobally(false); - } - // run autodrop first, before optimizations - passRunner.add(make_unique()); - if (preprocessor.debugInfo) { - // fix up debug info to better survive optimization - passRunner.add(make_unique()); - } - // optimize relooper label variable usage at the wasm level, where it is - // easy - passRunner.add("relooper-jump-threading"); - }, - debug, - false /* do not validate globally yet */); - } - - // if we see no function tables in the processing below, then the table still - // exists and has size 0 - - wasm.table.initial = wasm.table.max = 0; - - // first pass - do all global things, aside from function bodies (second pass) - // and function imports and indirect calls (last pass) - - for (unsigned i = 1; i < body->size(); i++) { - Ref curr = body[i]; - if (curr[0] == VAR) { - // import, global, or table - for (unsigned j = 0; j < curr[1]->size(); j++) { - Ref pair = curr[1][j]; - IString name = pair[0]->getIString(); - Ref value = pair[1]; - if (value->isNumber()) { - // global int - allocateGlobal( - name, Type::i32, Literal(int32_t(value->getInteger()))); - } else if (value[0] == BINARY) { - // int import - assert(value[1] == OR && value[3]->isNumber() && - value[3]->getNumber() == 0); - Ref import = value[2]; // env.what - addImport(name, import, Type::i32); - } else if (value[0] == UNARY_PREFIX) { - // double import or global - assert(value[1] == PLUS); - Ref import = value[2]; - if (import->isNumber()) { - // global - assert(import->getNumber() == 0); - allocateGlobal(name, Type::f64); - } else { - // import - addImport(name, import, Type::f64); - } - } else if (value[0] == CALL) { - assert(value[1]->isString() && value[1] == Math_fround && - value[2][0]->isNumber() && value[2][0]->getNumber() == 0); - allocateGlobal(name, Type::f32); - } else if (value[0] == DOT) { - // simple module.base import. can be a view, or a function. - if (value[1]->isString()) { - IString module = value[1]->getIString(); - IString base = value[2]->getIString(); - if (module == GLOBAL) { - if (base == INT8ARRAY) { - Int8Array = name; - } else if (base == INT16ARRAY) { - Int16Array = name; - } else if (base == INT32ARRAY) { - Int32Array = name; - } else if (base == UINT8ARRAY) { - UInt8Array = name; - } else if (base == UINT16ARRAY) { - UInt16Array = name; - } else if (base == UINT32ARRAY) { - UInt32Array = name; - } else if (base == FLOAT32ARRAY) { - Float32Array = name; - } else if (base == FLOAT64ARRAY) { - Float64Array = name; - } - } - } - // function import - addImport(name, value, Type::none); - } else if (value[0] == NEW) { - // ignore imports of typed arrays, but note the names of the arrays - value = value[1]; - assert(value[0] == CALL); - unsigned bytes; - bool integer, signed_; - AsmType asmType; - Ref constructor = value[1]; - if (constructor->isArray(DOT)) { // global.*Array - IString heap = constructor[2]->getIString(); - if (heap == INT8ARRAY) { - bytes = 1; - integer = true; - signed_ = true; - asmType = ASM_INT; - } else if (heap == INT16ARRAY) { - bytes = 2; - integer = true; - signed_ = true; - asmType = ASM_INT; - } else if (heap == INT32ARRAY) { - bytes = 4; - integer = true; - signed_ = true; - asmType = ASM_INT; - } else if (heap == UINT8ARRAY) { - bytes = 1; - integer = true; - signed_ = false; - asmType = ASM_INT; - } else if (heap == UINT16ARRAY) { - bytes = 2; - integer = true; - signed_ = false; - asmType = ASM_INT; - } else if (heap == UINT32ARRAY) { - bytes = 4; - integer = true; - signed_ = false; - asmType = ASM_INT; - } else if (heap == FLOAT32ARRAY) { - bytes = 4; - integer = false; - signed_ = true; - asmType = ASM_FLOAT; - } else if (heap == FLOAT64ARRAY) { - bytes = 8; - integer = false; - signed_ = true; - asmType = ASM_DOUBLE; - } else { - abort_on("invalid view import", heap); - } - } else { // *ArrayView that was previously imported - assert(constructor->isString()); - IString viewName = constructor->getIString(); - if (viewName == Int8Array) { - bytes = 1; - integer = true; - signed_ = true; - asmType = ASM_INT; - } else if (viewName == Int16Array) { - bytes = 2; - integer = true; - signed_ = true; - asmType = ASM_INT; - } else if (viewName == Int32Array) { - bytes = 4; - integer = true; - signed_ = true; - asmType = ASM_INT; - } else if (viewName == UInt8Array) { - bytes = 1; - integer = true; - signed_ = false; - asmType = ASM_INT; - } else if (viewName == UInt16Array) { - bytes = 2; - integer = true; - signed_ = false; - asmType = ASM_INT; - } else if (viewName == UInt32Array) { - bytes = 4; - integer = true; - signed_ = false; - asmType = ASM_INT; - } else if (viewName == Float32Array) { - bytes = 4; - integer = false; - signed_ = true; - asmType = ASM_FLOAT; - } else if (viewName == Float64Array) { - bytes = 8; - integer = false; - signed_ = true; - asmType = ASM_DOUBLE; - } else { - abort_on("invalid short view import", viewName); - } - } - assert(views.find(name) == views.end()); - views.emplace(name, View(bytes, integer, signed_, asmType)); - } else if (value[0] == ARRAY) { - // function table. we merge them into one big table, so e.g. [foo, - // b1] , [b2, bar] => [foo, b1, b2, bar] - // TODO: when not using aliasing function pointers, we could merge - // them by noticing that - // index 0 in each table is the null func, and each other index - // should only have one non-null func. However, that breaks down - // when function pointer casts are emulated. - if (wasm.table.segments.size() == 0) { - wasm.table.segments.emplace_back( - builder.makeGlobalGet(Name(TABLE_BASE), Type::i32)); - } - auto& segment = wasm.table.segments[0]; - functionTableStarts[name] = - segment.data.size(); // this table starts here - Ref contents = value[1]; - for (unsigned k = 0; k < contents->size(); k++) { - IString curr = contents[k]->getIString(); - segment.data.push_back(curr); - } - wasm.table.initial = wasm.table.max = segment.data.size(); - } else { - abort_on("invalid var element", pair); - } - } - } else if (curr[0] == RETURN) { - // exports - Ref object = curr[1]; - Ref contents = object[1]; - std::map exported; - for (unsigned k = 0; k < contents->size(); k++) { - Ref pair = contents[k]; - IString key = pair[0]->getIString(); - if (pair[1]->isString()) { - // exporting a function - IString value = pair[1]->getIString(); - if (key == Name("_emscripten_replace_memory")) { - // asm.js memory growth provides this special non-asm function, - // which we don't need (we use memory.grow) - assert(!wasm.getFunctionOrNull(value)); - continue; - } else if (key == UDIVMODDI4) { - udivmoddi4 = value; - } else if (key == GET_TEMP_RET0) { - getTempRet0 = value; - } - if (exported.count(key) > 0) { - // asm.js allows duplicate exports, but not wasm. use the last, like - // asm.js - exported[key]->value = value; - } else { - auto* export_ = new Export; - export_->name = key; - export_->value = value; - export_->kind = ExternalKind::Function; - wasm.addExport(export_); - exported[key] = export_; - } - } else { - // export a number. create a global and export it - assert(pair[1]->isNumber()); - assert(exported.count(key) == 0); - auto value = pair[1]->getInteger(); - auto* global = - builder.makeGlobal(key, - Type::i32, - builder.makeConst(Literal(int32_t(value))), - Builder::Immutable); - wasm.addGlobal(global); - auto* export_ = new Export; - export_->name = key; - export_->value = global->name; - export_->kind = ExternalKind::Global; - wasm.addExport(export_); - exported[key] = export_; - } - } - } - } - - // second pass: function bodies - for (unsigned i = 1; i < body->size(); i++) { - Ref curr = body[i]; - if (curr[0] == DEFUN) { - // function - auto* func = processFunction(curr); - if (wasm.getFunctionOrNull(func->name)) { - Fatal() << "duplicate function: " << func->name; - } - if (runOptimizationPasses) { - optimizingBuilder->addFunction(func); - } else { - wasm.addFunction(func); - } - } - } - - if (runOptimizationPasses) { - optimizingBuilder->finish(); - // Now that we have a full module, do memory packing optimizations - { - PassRunner passRunner(&wasm, passOptions); - passRunner.options.lowMemoryUnused = true; - passRunner.add("memory-packing"); - passRunner.run(); - } - // if we added any helper functions (like non-trapping i32-div, etc.), then - // those have not been optimized (the optimizing builder has just been fed - // the asm.js functions). Optimize those now. Typically there are very few, - // just do it sequentially. - PassRunner passRunner(&wasm, passOptions); - passRunner.options.lowMemoryUnused = true; - passRunner.addDefaultFunctionOptimizationPasses(); - for (auto& pair : trappingFunctions.getFunctions()) { - auto* func = pair.second; - passRunner.runOnFunction(func); - } - for (auto* func : extraSupportFunctions) { - passRunner.runOnFunction(func); - } - } - wasm.debugInfoFileNames = std::move(preprocessor.debugInfoFileNames); - - // third pass. first, function imports - - std::vector toErase; - - ModuleUtils::iterImportedFunctions(wasm, [&](Function* import) { - IString name = import->name; - if (importedSignatures.find(name) != importedSignatures.end()) { - // special math builtins - Signature builtin; - if (getBuiltinSignature(builtin, import->module, import->base)) { - import->sig = builtin; - } else { - import->sig = importedSignatures[name]; - } - } else if (import->module != ASM2WASM) { // special-case the special module - // never actually used, which means we don't know the function type since - // the usage tells us, so illegal for it to remain - toErase.push_back(name); - } - }); - - for (auto curr : toErase) { - wasm.removeFunction(curr); - } - - // Finalize calls now that everything is known and generated - - struct FinalizeCalls : public WalkerPass> { - bool isFunctionParallel() override { return true; } - - Pass* create() override { return new FinalizeCalls(parent); } - - Asm2WasmBuilder* parent; - - FinalizeCalls(Asm2WasmBuilder* parent) : parent(parent) { - name = "finalize-calls"; - } - - void notifyAboutWrongOperands(std::string why, Function* calledFunc) { - // use a mutex as this may be shown from multiple threads - static std::mutex mutex; - std::unique_lock lock(mutex); - static const int MAX_SHOWN = 20; - static std::unique_ptr> numShown; - if (!numShown) { - numShown = make_unique>(); - numShown->store(0); - } - if (numShown->load() >= MAX_SHOWN) { - return; - } - std::cerr << why << " in call from " << getFunction()->name << " to " - << calledFunc->name - << " (this is likely due to undefined behavior in C, like " - "defining a function one way and calling it in another, " - "which is important to fix)\n"; - (*numShown)++; - if (numShown->load() >= MAX_SHOWN) { - std::cerr << "(" << numShown->load() - << " such warnings shown; not showing any more)\n"; - } - } - - void visitCall(Call* curr) { - // The call target may not exist if it is one of our special fake imports - // for callIndirect fixups - auto* calledFunc = getModule()->getFunctionOrNull(curr->target); - if (calledFunc && !calledFunc->imported()) { - // The result type of the function being called is now known, and can be - // applied. - auto results = calledFunc->sig.results; - if (curr->type != results) { - curr->type = results; - } - // Handle mismatched numbers of arguments. In clang, if a function is - // declared one way but called in another, it inserts bitcasts to make - // things work. Those end up working since it is "ok" to drop or add - // parameters in native platforms, even though it's undefined behavior. - // We warn about it here, but tolerate it, if there is a simple - // solution. - const std::vector& params = calledFunc->sig.params.expand(); - if (curr->operands.size() < params.size()) { - notifyAboutWrongOperands("warning: asm2wasm adding operands", - calledFunc); - while (curr->operands.size() < params.size()) { - // Add params as necessary, with zeros. - curr->operands.push_back(LiteralUtils::makeZero( - params[curr->operands.size()], *getModule())); - } - } - if (curr->operands.size() > params.size()) { - notifyAboutWrongOperands("warning: asm2wasm dropping operands", - calledFunc); - curr->operands.resize(params.size()); - } - // If the types are wrong, validation will fail later anyhow, but add a - // warning here, it may help people. - for (Index i = 0; i < curr->operands.size(); i++) { - auto sent = curr->operands[i]->type; - if (sent != Type::unreachable && sent != params[i]) { - notifyAboutWrongOperands( - "error: asm2wasm seeing an invalid argument type at index " + - std::to_string(i) + " (this will not validate)", - calledFunc); - } - } - } else { - // A call to an import - // fill things out: add extra params as needed, etc. asm tolerates ffi - // overloading, wasm does not - auto iter = parent->importedSignatures.find(curr->target); - if (iter == parent->importedSignatures.end()) { - return; // one of our fake imports for callIndirect fixups - } - const std::vector& params = iter->second.params.expand(); - for (size_t i = 0; i < params.size(); i++) { - if (i >= curr->operands.size()) { - // add a new param - auto val = parent->allocator.alloc(); - val->type = val->value.type = params[i]; - curr->operands.push_back(val); - } else if (curr->operands[i]->type != params[i]) { - // if the param is used, then we have overloading here and the - // combined type must be f64; if this is an unreachable param, then - // it doesn't matter. - assert(params[i] == Type::f64 || - curr->operands[i]->type == Type::unreachable); - // overloaded, upgrade to f64 - switch (curr->operands[i]->type.getSingle()) { - case Type::i32: - curr->operands[i] = parent->builder.makeUnary( - ConvertSInt32ToFloat64, curr->operands[i]); - break; - case Type::f32: - curr->operands[i] = - parent->builder.makeUnary(PromoteFloat32, curr->operands[i]); - break; - default: {} // f64, unreachable, etc., are all good - } - } - } - Module* wasm = getModule(); - Type importResults = wasm->getFunction(curr->target)->sig.results; - if (curr->type != importResults) { - auto old = curr->type; - curr->type = importResults; - if (importResults == Type::f64) { - // we use a JS f64 value which is the most general, and convert to - // it - switch (old.getSingle()) { - case Type::i32: { - Unary* trunc = - parent->builder.makeUnary(TruncSFloat64ToInt32, curr); - replaceCurrent( - makeTrappingUnary(trunc, parent->trappingFunctions)); - break; - } - case Type::f32: { - replaceCurrent(parent->builder.makeUnary(DemoteFloat64, curr)); - break; - } - case Type::none: { - // this function returns a value, but we are not using it, so it - // must be dropped. autodrop will do that for us. - break; - } - default: - WASM_UNREACHABLE("unexpected type"); - } - } else { - assert(old == Type::none); - // we don't want a return value here, but the import does provide - // one autodrop will do that for us. - } - } - } - } - - void visitCallIndirect(CallIndirect* curr) { - // we already call into target = something + offset, where offset is a - // callImport with the name of the table. replace that with the table - // offset note that for an ftCall or mftCall, we have no asm.js mask, so - // have nothing to do here - auto* target = curr->target; - // might be a block with a fallthrough - if (auto* block = target->dynCast()) { - target = block->list.back(); - } - // the something might have been optimized out, leaving only the call - if (auto* call = target->dynCast()) { - auto tableName = call->target; - if (parent->functionTableStarts.find(tableName) == - parent->functionTableStarts.end()) { - return; - } - curr->target = parent->builder.makeConst( - Literal((int32_t)parent->functionTableStarts[tableName])); - return; - } - auto* add = target->dynCast(); - if (!add) { - return; - } - if (add->right->is()) { - auto* offset = add->right->cast(); - auto tableName = offset->target; - if (parent->functionTableStarts.find(tableName) == - parent->functionTableStarts.end()) { - return; - } - add->right = parent->builder.makeConst( - Literal((int32_t)parent->functionTableStarts[tableName])); - } else { - auto* offset = add->left->dynCast(); - if (!offset) { - return; - } - auto tableName = offset->target; - if (parent->functionTableStarts.find(tableName) == - parent->functionTableStarts.end()) { - return; - } - add->left = parent->builder.makeConst( - Literal((int32_t)parent->functionTableStarts[tableName])); - } - } - - void visitFunction(Function* curr) { - // changing call types requires we percolate types, and drop stuff. - // we do this in this pass so that we don't look broken between passes - AutoDrop().walkFunctionInModule(curr, getModule()); - } - }; - - // apply debug info, reducing intrinsic calls into annotations on the ast - // nodes - struct ApplyDebugInfo - : public WalkerPass< - ExpressionStackWalker>> { - bool isFunctionParallel() override { return true; } - - Pass* create() override { return new ApplyDebugInfo(); } - - ApplyDebugInfo() { name = "apply-debug-info"; } - - Call* lastDebugInfo = nullptr; - - void visitExpression(Expression* curr) { - if (auto* call = checkDebugInfo(curr)) { - lastDebugInfo = call; - replaceCurrent(getModule()->allocator.alloc()); - } else { - if (lastDebugInfo) { - auto& debugLocations = getFunction()->debugLocations; - uint32_t fileIndex = - lastDebugInfo->operands[0]->cast()->value.geti32(); - assert(getModule()->debugInfoFileNames.size() > fileIndex); - uint32_t lineNumber = - lastDebugInfo->operands[1]->cast()->value.geti32(); - // look up the stack, apply to the root expression - Index i = expressionStack.size() - 1; - while (1) { - auto* exp = expressionStack[i]; - bool parentIsStructure = - i > 0 && (expressionStack[i - 1]->is() || - expressionStack[i - 1]->is() || - expressionStack[i - 1]->is()); - if (i == 0 || parentIsStructure || exp->type == Type::none || - exp->type == Type::unreachable) { - if (debugLocations.count(exp) > 0) { - // already present, so look back up - i++; - while (i < expressionStack.size()) { - exp = expressionStack[i]; - if (debugLocations.count(exp) == 0) { - debugLocations[exp] = {fileIndex, lineNumber, 0}; - break; - } - i++; - } - } else { - debugLocations[exp] = {fileIndex, lineNumber, 0}; - } - break; - } - i--; - } - lastDebugInfo = nullptr; - } - } - } - }; - - PassRunner passRunner(&wasm, passOptions); - passRunner.options.lowMemoryUnused = true; - if (debug) { - passRunner.setDebug(true); - passRunner.setValidateGlobally(false); - } - // finalizeCalls also does autoDrop, which is crucial for the non-optimizing - // case, so that the output of the first pass is valid - passRunner.add(make_unique(this)); - passRunner.add(ABI::getLegalizationPass(legalizeJavaScriptFFI - ? ABI::LegalizationLevel::Full - : ABI::LegalizationLevel::Minimal)); - if (runOptimizationPasses) { - // autodrop can add some garbage - passRunner.add("vacuum"); - passRunner.add("remove-unused-brs"); - passRunner.add("vacuum"); - passRunner.add("remove-unused-names"); - passRunner.add("merge-blocks"); - passRunner.add("optimize-instructions"); - passRunner.add("post-emscripten"); - } else { - if (preprocessor.debugInfo) { - // we would have run this before if optimizing, do it now otherwise. must - // precede ApplyDebugInfo - passRunner.add(make_unique()); - } - } - if (preprocessor.debugInfo) { - passRunner.add(make_unique()); - // FIXME maybe just remove the nops that were debuginfo nodes, if not - // optimizing? - passRunner.add("vacuum"); - } - if (runOptimizationPasses) { - // do final global optimizations after all function work is done - // (e.g. duplicate funcs may appear thanks to that work) - passRunner.addDefaultGlobalOptimizationPostPasses(); - } - passRunner.run(); - - // remove the debug info intrinsic - if (preprocessor.debugInfo) { - wasm.removeFunction(EMSCRIPTEN_DEBUGINFO); - } - - if (udivmoddi4.is() && getTempRet0.is()) { - // generate a wasm-optimized __udivmoddi4 method, which we can do much more - // efficiently in wasm we can only do this if we know getTempRet0 as well - // since we use it to figure out which minified global is tempRet0 - // (getTempRet0 might be an import, if this is a shared module, so we can't - // optimize that case) - Name tempRet0; - { - Expression* curr = wasm.getFunction(getTempRet0)->body; - if (curr->is()) { - curr = curr->cast()->list.back(); - } - if (curr->is()) { - curr = curr->cast()->value; - } - auto* get = curr->cast(); - tempRet0 = get->name; - } - // udivmoddi4 receives xl, xh, yl, yl, r, and - // if r then *r = x % y - // returns x / y - auto* func = wasm.getFunction(udivmoddi4); - Builder::clearLocals(func); - Index xl = Builder::addParam(func, "xl", Type::i32), - xh = Builder::addParam(func, "xh", Type::i32), - yl = Builder::addParam(func, "yl", Type::i32), - yh = Builder::addParam(func, "yh", Type::i32), - r = Builder::addParam(func, "r", Type::i32), - x64 = Builder::addVar(func, "x64", Type::i64), - y64 = Builder::addVar(func, "y64", Type::i64); - auto* body = allocator.alloc(); - body->list.push_back( - builder.makeLocalSet(x64, I64Utilities::recreateI64(builder, xl, xh))); - body->list.push_back( - builder.makeLocalSet(y64, I64Utilities::recreateI64(builder, yl, yh))); - body->list.push_back( - builder.makeIf(builder.makeLocalGet(r, Type::i32), - builder.makeStore( - 8, - 0, - 8, - builder.makeLocalGet(r, Type::i32), - builder.makeBinary(RemUInt64, - builder.makeLocalGet(x64, Type::i64), - builder.makeLocalGet(y64, Type::i64)), - Type::i64))); - body->list.push_back(builder.makeLocalSet( - x64, - builder.makeBinary(DivUInt64, - builder.makeLocalGet(x64, Type::i64), - builder.makeLocalGet(y64, Type::i64)))); - body->list.push_back( - builder.makeGlobalSet(tempRet0, I64Utilities::getI64High(builder, x64))); - body->list.push_back(I64Utilities::getI64Low(builder, x64)); - body->finalize(); - func->body = body; - } -} - -Function* Asm2WasmBuilder::processFunction(Ref ast) { - auto name = ast[1]->getIString(); - - BYN_TRACE("asm2wasming func: " << ast[1]->getIString().str << '\n'); - - auto function = new Function; - function->sig = Signature(Type::none, Type::none); - function->name = name; - Ref params = ast[2]; - Ref body = ast[3]; - - UniqueNameMapper nameMapper; - - // given an asm.js label, returns the wasm label for breaks or continues - auto getBreakLabelName = [](IString label) { - return Name(std::string("label$break$") + label.str); - }; - auto getContinueLabelName = [](IString label) { - return Name(std::string("label$continue$") + label.str); - }; - - IStringSet functionVariables; // params or vars - - IString parentLabel; // set in LABEL, then read in WHILE/DO/SWITCH - std::vector breakStack; // where a break will go - std::vector continueStack; // where a continue will go - - AsmData asmData; // need to know var and param types, for asm type detection - - for (unsigned i = 0; i < params->size(); i++) { - Ref curr = body[i]; - auto* assign = curr->asAssignName(); - IString name = assign->target(); - AsmType asmType = - detectType(assign->value(), nullptr, false, Math_fround, wasmOnly); - Builder::addParam(function, name, asmToWasmType(asmType)); - functionVariables.insert(name); - asmData.addParam(name, asmType); - } - unsigned start = params->size(); - while (start < body->size() && body[start]->isArray(VAR)) { - Ref curr = body[start]; - for (unsigned j = 0; j < curr[1]->size(); j++) { - Ref pair = curr[1][j]; - IString name = pair[0]->getIString(); - AsmType asmType = - detectType(pair[1], nullptr, true, Math_fround, wasmOnly); - Builder::addVar(function, name, asmToWasmType(asmType)); - functionVariables.insert(name); - asmData.addVar(name, asmType); - } - start++; - } - - bool addedI32Temp = false; - auto ensureI32Temp = [&]() { - if (addedI32Temp) { - return; - } - addedI32Temp = true; - Builder::addVar(function, I32_TEMP, Type::i32); - functionVariables.insert(I32_TEMP); - asmData.addVar(I32_TEMP, ASM_INT); - }; - - bool seenReturn = false; // function->result is updated if we see a return - // processors - std::function processStatements; - std::function processUnshifted; - std::function processIgnoringShift; - - std::function process = [&](Ref ast) -> Expression* { - // TODO: only create one when we need it? - AstStackHelper astStackHelper(ast); - if (ast->isString()) { - IString name = ast->getIString(); - if (functionVariables.has(name)) { - // var in scope - auto ret = allocator.alloc(); - ret->index = function->getLocalIndex(name); - ret->type = asmToWasmType(asmData.getType(name)); - return ret; - } - if (name == DEBUGGER) { - Call* call = allocator.alloc(); - call->target = DEBUGGER; - call->type = Type::none; - static bool addedImport = false; - if (!addedImport) { - addedImport = true; - auto import = new Function; // debugger = asm2wasm.debugger; - import->name = DEBUGGER; - import->module = ASM2WASM; - import->base = DEBUGGER; - import->sig = Signature(Type::none, Type::none); - wasm.addFunction(import); - } - return call; - } - // global var - assert(mappedGlobals.find(name) != mappedGlobals.end() - ? true - : (std::cerr << name.str << '\n', false)); - MappedGlobal& global = mappedGlobals[name]; - return builder.makeGlobalGet(name, global.type); - } - if (ast->isNumber()) { - auto ret = allocator.alloc(); - double num = ast->getNumber(); - if (isSInteger32(num)) { - ret->value = Literal(int32_t(toSInteger32(num))); - } else if (isUInteger32(num)) { - ret->value = Literal(uint32_t(toUInteger32(num))); - } else { - ret->value = Literal(num); - } - ret->type = ret->value.type; - return ret; - } - if (ast->isAssignName()) { - auto* assign = ast->asAssignName(); - IString name = assign->target(); - if (functionVariables.has(name)) { - auto ret = allocator.alloc(); - ret->index = function->getLocalIndex(assign->target()); - ret->value = process(assign->value()); - ret->makeSet(); - ret->finalize(); - return ret; - } - // global var - if (mappedGlobals.find(name) == mappedGlobals.end()) { - Fatal() << "error: access of a non-existent global var " << name.str; - } - auto* ret = builder.makeGlobalSet(name, process(assign->value())); - // global.set does not return; if our value is trivially not used, don't - // emit a load (if nontrivially not used, opts get it later) - auto parent = astStackHelper.getParent(); - if (!parent || parent->isArray(BLOCK) || parent->isArray(IF)) { - return ret; - } - return builder.makeSequence( - ret, builder.makeGlobalGet(name, ret->value->type)); - } - if (ast->isAssign()) { - auto* assign = ast->asAssign(); - assert(assign->target()->isArray(SUB)); - Ref target = assign->target(); - assert(target[1]->isString()); - IString heap = target[1]->getIString(); - assert(views.find(heap) != views.end()); - View& view = views[heap]; - auto ret = allocator.alloc(); - ret->isAtomic = false; - ret->bytes = view.bytes; - ret->offset = 0; - ret->align = view.bytes; - ret->ptr = processUnshifted(target[2], view.bytes); - ret->value = process(assign->value()); - ret->valueType = asmToWasmType(view.type); - ret->finalize(); - if (ret->valueType != ret->value->type) { - // in asm.js we have some implicit coercions that we must do explicitly - // here - if (ret->valueType == Type::f32 && ret->value->type == Type::f64) { - auto conv = allocator.alloc(); - conv->op = DemoteFloat64; - conv->value = ret->value; - conv->type = Type::f32; - ret->value = conv; - } else if (ret->valueType == Type::f64 && - ret->value->type == Type::f32) { - ret->value = ensureDouble(ret->value); - } else { - abort_on("bad sub[] types", ast); - } - } - return ret; - } - IString what = ast[0]->getIString(); - if (what == BINARY) { - if ((ast[1] == OR || ast[1] == TRSHIFT) && ast[3]->isNumber() && - ast[3]->getNumber() == 0) { - // just look through the ()|0 or ()>>>0 coercion - auto ret = process(ast[2]); - fixCallType(ret, Type::i32); - return ret; - } - auto ret = allocator.alloc(); - ret->left = process(ast[2]); - ret->right = process(ast[3]); - ret->op = parseAsmBinaryOp( - ast[1]->getIString(), ast[2], ast[3], ret->left, ret->right); - ret->finalize(); - if (ret->op == BinaryOp::RemSInt32 && ret->type.isFloat()) { - // WebAssembly does not have floating-point remainder, we have to emit a - // call to a special import of ours - Call* call = allocator.alloc(); - call->target = F64_REM; - call->operands.push_back(ensureDouble(ret->left)); - call->operands.push_back(ensureDouble(ret->right)); - call->type = Type::f64; - static bool addedImport = false; - if (!addedImport) { - addedImport = true; - auto import = new Function; // f64-rem = asm2wasm.f64-rem; - import->name = F64_REM; - import->module = ASM2WASM; - import->base = F64_REM; - import->sig = Signature({Type::f64, Type::f64}, Type::f64); - wasm.addFunction(import); - } - return call; - } - return makeTrappingBinary(ret, trappingFunctions); - } else if (what == SUB) { - Ref target = ast[1]; - assert(target->isString()); - IString heap = target->getIString(); - assert(views.find(heap) != views.end()); - View& view = views[heap]; - auto ret = allocator.alloc(); - ret->isAtomic = false; - ret->bytes = view.bytes; - ret->signed_ = view.signed_; - ret->offset = 0; - ret->align = view.bytes; - ret->ptr = processUnshifted(ast[2], view.bytes); - ret->type = Type::get(view.bytes, !view.integer); - return ret; - } else if (what == UNARY_PREFIX) { - if (ast[1] == PLUS) { - Literal literal = checkLiteral(ast); - if (literal.type != Type::none) { - return builder.makeConst(literal); - } - auto ret = process(ast[2]); // we are a +() coercion - if (ret->type == Type::i32) { - auto conv = allocator.alloc(); - conv->op = isUnsignedCoercion(ast[2]) ? ConvertUInt32ToFloat64 - : ConvertSInt32ToFloat64; - conv->value = ret; - conv->type = Type::f64; - return conv; - } - if (ret->type == Type::f32) { - return ensureDouble(ret); - } - fixCallType(ret, Type::f64); - return ret; - } else if (ast[1] == MINUS) { - if (ast[2]->isNumber() || - (ast[2]->isArray(UNARY_PREFIX) && ast[2][1] == PLUS && - ast[2][2]->isNumber())) { - auto ret = allocator.alloc(); - ret->value = getLiteral(ast); - ret->type = ret->value.type; - return ret; - } - AsmType asmType = detectAsmType(ast[2], &asmData); - if (asmType == ASM_INT) { - // wasm has no unary negation for int, so do 0- - auto ret = allocator.alloc(); - ret->op = SubInt32; - ret->left = builder.makeConst(Literal((int32_t)0)); - ret->right = process(ast[2]); - ret->type = Type::i32; - return ret; - } - auto ret = allocator.alloc(); - ret->value = process(ast[2]); - if (asmType == ASM_DOUBLE) { - ret->op = NegFloat64; - ret->type = Type::f64; - } else if (asmType == ASM_FLOAT) { - ret->op = NegFloat32; - ret->type = Type::f32; - } else { - WASM_UNREACHABLE("unexpected asm type"); - } - return ret; - } else if (ast[1] == B_NOT) { - // ~, might be ~~ as a coercion or just a not - if (ast[2]->isArray(UNARY_PREFIX) && ast[2][1] == B_NOT) { - // if we have an unsigned coercion on us, it is an unsigned op - Expression* expr = process(ast[2][2]); - bool isSigned = !isParentUnsignedCoercion(astStackHelper.getParent()); - bool isF64 = expr->type == Type::f64; - UnaryOp op; - if (isSigned && isF64) { - op = UnaryOp::TruncSFloat64ToInt32; - } else if (isSigned && !isF64) { - op = UnaryOp::TruncSFloat32ToInt32; - } else if (!isSigned && isF64) { - op = UnaryOp::TruncUFloat64ToInt32; - } else { // !isSigned && !isF64 - op = UnaryOp::TruncUFloat32ToInt32; - } - return makeTrappingUnary(builder.makeUnary(op, expr), - trappingFunctions); - } - // no bitwise unary not, so do xor with -1 - auto ret = allocator.alloc(); - ret->op = XorInt32; - ret->left = process(ast[2]); - ret->right = builder.makeConst(Literal(int32_t(-1))); - ret->type = Type::i32; - return ret; - } else if (ast[1] == L_NOT) { - auto ret = allocator.alloc(); - ret->op = EqZInt32; - ret->value = process(ast[2]); - ret->type = Type::i32; - return ret; - } - abort_on("bad unary", ast); - } else if (what == IF) { - auto* condition = process(ast[1]); - auto* ifTrue = process(ast[2]); - return builder.makeIf(truncateToInt32(condition), - ifTrue, - !!ast[3] ? process(ast[3]) : nullptr); - } else if (what == CALL) { - if (ast[1]->isString()) { - IString name = ast[1]->getIString(); - if (name == Math_imul) { - assert(ast[2]->size() == 2); - auto ret = allocator.alloc(); - ret->op = MulInt32; - ret->left = process(ast[2][0]); - ret->right = process(ast[2][1]); - ret->type = Type::i32; - return ret; - } - if (name == Math_clz32 || name == llvm_cttz_i32) { - assert(ast[2]->size() == 1); - auto ret = allocator.alloc(); - ret->op = name == Math_clz32 ? ClzInt32 : CtzInt32; - ret->value = process(ast[2][0]); - ret->type = Type::i32; - return ret; - } - if (name == Math_fround) { - assert(ast[2]->size() == 1); - Literal lit = checkLiteral(ast[2][0], false /* raw is float */); - if (lit.type == Type::f64) { - return builder.makeConst(Literal((float)lit.getf64())); - } - auto ret = allocator.alloc(); - ret->value = process(ast[2][0]); - if (ret->value->type == Type::f64) { - ret->op = DemoteFloat64; - } else if (ret->value->type == Type::i32) { - if (isUnsignedCoercion(ast[2][0])) { - ret->op = ConvertUInt32ToFloat32; - } else { - ret->op = ConvertSInt32ToFloat32; - } - } else if (ret->value->type == Type::f32) { - return ret->value; - } else if (ret->value->type == Type::none) { // call, etc. - ret->value->type = Type::f32; - return ret->value; - } else { - abort_on("confusing fround target", ast[2][0]); - } - ret->type = Type::f32; - return ret; - } - if (name == Math_abs) { - // overloaded on type: i32, f32 or f64 - Expression* value = process(ast[2][0]); - if (value->type == Type::i32) { - // No wasm support, so use a temp local - ensureI32Temp(); - auto set = allocator.alloc(); - set->index = function->getLocalIndex(I32_TEMP); - set->value = value; - set->makeSet(); - set->finalize(); - auto get = [&]() { - auto ret = allocator.alloc(); - ret->index = function->getLocalIndex(I32_TEMP); - ret->type = Type::i32; - return ret; - }; - auto isNegative = allocator.alloc(); - isNegative->op = LtSInt32; - isNegative->left = get(); - isNegative->right = builder.makeConst(Literal(0)); - isNegative->finalize(); - auto block = allocator.alloc(); - block->list.push_back(set); - auto flip = allocator.alloc(); - flip->op = SubInt32; - flip->left = builder.makeConst(Literal(0)); - flip->right = get(); - flip->type = Type::i32; - auto select = allocator.alloc