Skip to content

Commit

Permalink
Fix binaryen.js's wasm2asm (WebAssembly#1257)
Browse files Browse the repository at this point in the history
* fix wasm2asm in binaryen.js, the function locals may not all have names, so add them as necessary
  • Loading branch information
kripken authored Nov 1, 2017
1 parent 7a4c47e commit d328024
Show file tree
Hide file tree
Showing 9 changed files with 510 additions and 1,495 deletions.
2 changes: 1 addition & 1 deletion auto_update_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
if asm.endswith('.asm.js'):
for precise in [0, 1, 2]:
for opts in [1, 0]:
cmd = ASM2WASM + [os.path.join('test', asm)]
cmd = ASM2WASM + [os.path.join('test', asm), '--enable-threads']
wasm = asm.replace('.asm.js', '.fromasm')
if not precise:
cmd += ['--trap-mode=allow', '--ignore-implicit-traps']
Expand Down
965 changes: 233 additions & 732 deletions bin/binaryen.js

Large diffs are not rendered by default.

873 changes: 164 additions & 709 deletions bin/wasm.js

Large diffs are not rendered by default.

53 changes: 53 additions & 0 deletions src/ir/names.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.
*/

#ifndef wasm_ir_names_h
#define wasm_ir_names_h

#include "wasm.h"

namespace wasm {

namespace Names {

// Add explicit names for function locals not yet named, and do not
// modify existing names
inline void ensureNames(Function* func) {
std::unordered_set<Name> seen;
for (auto& pair : func->localNames) {
seen.insert(pair.second);
}
Index nameIndex = seen.size();
for (Index i = 0; i < func->getNumLocals(); i++) {
if (!func->hasLocalName(i)) {
while (1) {
auto name = Name::fromInt(nameIndex++);
if (seen.count(name) == 0) {
func->localNames[i] = name;
func->localIndices[name] = i;
seen.insert(name);
break;
}
}
}
}
}

} // namespace Names

} // namespace wasm

#endif // wasm_ir_names_h
2 changes: 2 additions & 0 deletions src/passes/I64ToI32Lowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "emscripten-optimizer/istring.h"
#include "support/name.h"
#include "wasm-builder.h"
#include "ir/names.h"


namespace wasm {
Expand Down Expand Up @@ -140,6 +141,7 @@ struct I64ToI32Lowering : public WalkerPass<PostWalker<I64ToI32Lowering>> {
func->localNames.clear();
func->localIndices.clear();
Index newIdx = 0;
Names::ensureNames(&oldFunc);
for (Index i = 0; i < oldFunc.getNumLocals(); ++i) {
assert(oldFunc.hasLocalName(i));
Name lowName = oldFunc.getLocalName(i);
Expand Down
4 changes: 2 additions & 2 deletions test/binaryen.js/call_import_error.js.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(import "env" "fn" (func $fn))
(memory $0 0)
(export "main" (func $main))
(func $main (type $v)
(func $main (; 1 ;) (type $v)
(call $fn)
)
)
Expand All @@ -16,7 +16,7 @@
(import "env" "fn" (func $fn))
(memory $0 0)
(export "main" (func $main))
(func $main (type $v)
(func $main (; 1 ;) (type $v)
(call $fn)
)
)
22 changes: 13 additions & 9 deletions test/binaryen.js/emit_asmjs.js.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,49 @@ function asmFunc(global, env, buffer) {
var Math_clz32 = global.Math.clz32;
function main($$0) {
$$0 = $$0 | 0;
var $$1 = 0, wasm2asm_i32$0 = 0;
return $$0 | 0;
return wasm2asm_i32$0 | 0;
}

function __wasm_ctz_i32(x) {
x = x | 0;
var wasm2asm_i32$0 = 0;
if ((x | 0) == (0 | 0)) wasm2asm_i32$0 = 32; else wasm2asm_i32$0 = 31 - Math_clz32(x ^ (x - 1 | 0) | 0) | 0;
return wasm2asm_i32$0 | 0;
var $$1 = 0, $$2 = 0, $$3 = 0, $$4 = 0, $$5 = 0, $$6 = 0, $$7 = 0, $$8 = 0, $$9 = 0, $$10 = 0;
if ((x | 0) == (0 | 0)) $$9 = 32; else $$9 = 31 - Math_clz32(x ^ (x - 1 | 0) | 0) | 0;
return $$9 | 0;
}

function __wasm_popcnt_i32(x) {
x = x | 0;
var count = 0, wasm2asm_i32$0 = 0;
var count = 0, $$2 = 0, $$3 = 0, $$4 = 0, $$5 = 0, $$6 = 0, $$7 = 0, $$8 = 0, $$9 = 0, $$10 = 0, $$11 = 0, $$12 = 0, $$13 = 0, $$14 = 0, $$15 = 0;
count = 0;
b : {
l : do {
if ((x | 0) == (0 | 0)) {
wasm2asm_i32$0 = count;
break b;
}
$$5 = count;
if ((x | 0) == (0 | 0)) break b;
x = x & (x - 1 | 0) | 0;
count = count + 1 | 0;
continue l;
break l;
} while (1);
};
return wasm2asm_i32$0 | 0;
return $$5 | 0;
}

function __wasm_rotl_i32(x, k) {
x = x | 0;
k = k | 0;
var $$2 = 0, $$3 = 0, $$4 = 0, $$5 = 0, $$6 = 0, $$7 = 0, $$8 = 0, $$9 = 0, $$10 = 0, $$11 = 0, $$12 = 0, $$13 = 0, $$14 = 0, $$15 = 0, $$16 = 0, $$17 = 0, $$18 = 0, $$19 = 0, $$20 = 0, wasm2asm_i32$0 = 0;
return ((4294967295 >>> (k & 31 | 0) | 0) & x | 0) << (k & 31 | 0) | 0 | (((4294967295 << (32 - (k & 31 | 0) | 0) | 0) & x | 0) >>> (32 - (k & 31 | 0) | 0) | 0) | 0 | 0;
return wasm2asm_i32$0 | 0;
}

function __wasm_rotr_i32(x, k) {
x = x | 0;
k = k | 0;
var $$2 = 0, $$3 = 0, $$4 = 0, $$5 = 0, $$6 = 0, $$7 = 0, $$8 = 0, $$9 = 0, $$10 = 0, $$11 = 0, $$12 = 0, $$13 = 0, $$14 = 0, $$15 = 0, $$16 = 0, $$17 = 0, $$18 = 0, $$19 = 0, $$20 = 0, wasm2asm_i32$0 = 0;
return ((4294967295 << (k & 31 | 0) | 0) & x | 0) >>> (k & 31 | 0) | 0 | (((4294967295 >>> (32 - (k & 31 | 0) | 0) | 0) & x | 0) << (32 - (k & 31 | 0) | 0) | 0) | 0 | 0;
return wasm2asm_i32$0 | 0;
}

return {
Expand Down
4 changes: 2 additions & 2 deletions test/binaryen.js/hello-world.js.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(type $iii (func (param i32 i32) (result i32)))
(memory $0 0)
(export "adder" (func $adder))
(func $adder (type $iii) (param $0 i32) (param $1 i32) (result i32)
(func $adder (; 0 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
(return
(i32.add
(get_local $0)
Expand All @@ -18,7 +18,7 @@ optimized:
(type $iii (func (param i32 i32) (result i32)))
(memory $0 0)
(export "adder" (func $adder))
(func $adder (type $iii) (param $0 i32) (param $1 i32) (result i32)
(func $adder (; 0 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
(i32.add
(get_local $0)
(get_local $1)
Expand Down
Loading

0 comments on commit d328024

Please sign in to comment.