Skip to content

Commit d328024

Browse files
authored
Fix binaryen.js's wasm2asm (#1257)
* fix wasm2asm in binaryen.js, the function locals may not all have names, so add them as necessary
1 parent 7a4c47e commit d328024

File tree

9 files changed

+510
-1495
lines changed

9 files changed

+510
-1495
lines changed

auto_update_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
if asm.endswith('.asm.js'):
1717
for precise in [0, 1, 2]:
1818
for opts in [1, 0]:
19-
cmd = ASM2WASM + [os.path.join('test', asm)]
19+
cmd = ASM2WASM + [os.path.join('test', asm), '--enable-threads']
2020
wasm = asm.replace('.asm.js', '.fromasm')
2121
if not precise:
2222
cmd += ['--trap-mode=allow', '--ignore-implicit-traps']

bin/binaryen.js

Lines changed: 233 additions & 732 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/wasm.js

Lines changed: 164 additions & 709 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ir/names.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2017 WebAssembly Community Group participants
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef wasm_ir_names_h
18+
#define wasm_ir_names_h
19+
20+
#include "wasm.h"
21+
22+
namespace wasm {
23+
24+
namespace Names {
25+
26+
// Add explicit names for function locals not yet named, and do not
27+
// modify existing names
28+
inline void ensureNames(Function* func) {
29+
std::unordered_set<Name> seen;
30+
for (auto& pair : func->localNames) {
31+
seen.insert(pair.second);
32+
}
33+
Index nameIndex = seen.size();
34+
for (Index i = 0; i < func->getNumLocals(); i++) {
35+
if (!func->hasLocalName(i)) {
36+
while (1) {
37+
auto name = Name::fromInt(nameIndex++);
38+
if (seen.count(name) == 0) {
39+
func->localNames[i] = name;
40+
func->localIndices[name] = i;
41+
seen.insert(name);
42+
break;
43+
}
44+
}
45+
}
46+
}
47+
}
48+
49+
} // namespace Names
50+
51+
} // namespace wasm
52+
53+
#endif // wasm_ir_names_h

src/passes/I64ToI32Lowering.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "emscripten-optimizer/istring.h"
2828
#include "support/name.h"
2929
#include "wasm-builder.h"
30+
#include "ir/names.h"
3031

3132

3233
namespace wasm {
@@ -140,6 +141,7 @@ struct I64ToI32Lowering : public WalkerPass<PostWalker<I64ToI32Lowering>> {
140141
func->localNames.clear();
141142
func->localIndices.clear();
142143
Index newIdx = 0;
144+
Names::ensureNames(&oldFunc);
143145
for (Index i = 0; i < oldFunc.getNumLocals(); ++i) {
144146
assert(oldFunc.hasLocalName(i));
145147
Name lowName = oldFunc.getLocalName(i);

test/binaryen.js/call_import_error.js.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
(import "env" "fn" (func $fn))
44
(memory $0 0)
55
(export "main" (func $main))
6-
(func $main (type $v)
6+
(func $main (; 1 ;) (type $v)
77
(call $fn)
88
)
99
)
@@ -16,7 +16,7 @@
1616
(import "env" "fn" (func $fn))
1717
(memory $0 0)
1818
(export "main" (func $main))
19-
(func $main (type $v)
19+
(func $main (; 1 ;) (type $v)
2020
(call $fn)
2121
)
2222
)

test/binaryen.js/emit_asmjs.js.txt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,49 @@ function asmFunc(global, env, buffer) {
1414
var Math_clz32 = global.Math.clz32;
1515
function main($$0) {
1616
$$0 = $$0 | 0;
17+
var $$1 = 0, wasm2asm_i32$0 = 0;
1718
return $$0 | 0;
19+
return wasm2asm_i32$0 | 0;
1820
}
1921

2022
function __wasm_ctz_i32(x) {
2123
x = x | 0;
22-
var wasm2asm_i32$0 = 0;
23-
if ((x | 0) == (0 | 0)) wasm2asm_i32$0 = 32; else wasm2asm_i32$0 = 31 - Math_clz32(x ^ (x - 1 | 0) | 0) | 0;
24-
return wasm2asm_i32$0 | 0;
24+
var $$1 = 0, $$2 = 0, $$3 = 0, $$4 = 0, $$5 = 0, $$6 = 0, $$7 = 0, $$8 = 0, $$9 = 0, $$10 = 0;
25+
if ((x | 0) == (0 | 0)) $$9 = 32; else $$9 = 31 - Math_clz32(x ^ (x - 1 | 0) | 0) | 0;
26+
return $$9 | 0;
2527
}
2628

2729
function __wasm_popcnt_i32(x) {
2830
x = x | 0;
29-
var count = 0, wasm2asm_i32$0 = 0;
31+
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;
3032
count = 0;
3133
b : {
3234
l : do {
33-
if ((x | 0) == (0 | 0)) {
34-
wasm2asm_i32$0 = count;
35-
break b;
36-
}
35+
$$5 = count;
36+
if ((x | 0) == (0 | 0)) break b;
3737
x = x & (x - 1 | 0) | 0;
3838
count = count + 1 | 0;
3939
continue l;
4040
break l;
4141
} while (1);
4242
};
43-
return wasm2asm_i32$0 | 0;
43+
return $$5 | 0;
4444
}
4545

4646
function __wasm_rotl_i32(x, k) {
4747
x = x | 0;
4848
k = k | 0;
49+
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;
4950
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;
51+
return wasm2asm_i32$0 | 0;
5052
}
5153

5254
function __wasm_rotr_i32(x, k) {
5355
x = x | 0;
5456
k = k | 0;
57+
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;
5558
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;
59+
return wasm2asm_i32$0 | 0;
5660
}
5761

5862
return {

test/binaryen.js/hello-world.js.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
(type $iii (func (param i32 i32) (result i32)))
33
(memory $0 0)
44
(export "adder" (func $adder))
5-
(func $adder (type $iii) (param $0 i32) (param $1 i32) (result i32)
5+
(func $adder (; 0 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
66
(return
77
(i32.add
88
(get_local $0)
@@ -18,7 +18,7 @@ optimized:
1818
(type $iii (func (param i32 i32) (result i32)))
1919
(memory $0 0)
2020
(export "adder" (func $adder))
21-
(func $adder (type $iii) (param $0 i32) (param $1 i32) (result i32)
21+
(func $adder (; 0 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
2222
(i32.add
2323
(get_local $0)
2424
(get_local $1)

0 commit comments

Comments
 (0)