Skip to content

Commit 1005b12

Browse files
authored
Emit binary function index in comment in text format, for convenience (#1232)
1 parent 71dc079 commit 1005b12

354 files changed

Lines changed: 4133 additions & 4079 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

scripts/test/s2wasm.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,15 @@ def test_linker():
119119
for name, extra in expected_funcs:
120120
space = ' ' if extra else ''
121121
fail_if_not_contained(output, '(export "{0}" (func ${0}))'.format(name))
122-
fail_if_not_contained(output, '(func ${0}'.format(name + space + extra))
122+
for line in output.split('\n'):
123+
if '(func ${0}'.format(name + space) in line:
124+
# we found the relevant line for the function definition. remove
125+
# a (; X ;) comment with its index
126+
start = line.find('(; ')
127+
if start >= 0:
128+
end = line.find(' ;)')
129+
line = line[:start] + line[end + 4:]
130+
fail_if_not_contained(line, '(func ${0}'.format(name + space + extra))
123131

124132

125133
if __name__ == '__main__':

src/ast/module-utils.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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_ast_module_h
18+
#define wasm_ast_module_h
19+
20+
#include "wasm.h"
21+
22+
namespace wasm {
23+
24+
namespace ModuleUtils {
25+
26+
// Computes the indexes in a wasm binary, i.e., with function imports
27+
// and function implementations sharing a single index space, etc.
28+
struct BinaryIndexes {
29+
std::unordered_map<Name, Index> functionIndexes;
30+
std::unordered_map<Name, Index> globalIndexes;
31+
32+
BinaryIndexes(Module& wasm) {
33+
for (Index i = 0; i < wasm.imports.size(); i++) {
34+
auto& import = wasm.imports[i];
35+
if (import->kind == ExternalKind::Function) {
36+
auto index = functionIndexes.size();
37+
functionIndexes[import->name] = index;
38+
} else if (import->kind == ExternalKind::Global) {
39+
auto index = globalIndexes.size();
40+
globalIndexes[import->name] = index;
41+
}
42+
}
43+
for (Index i = 0; i < wasm.functions.size(); i++) {
44+
auto index = functionIndexes.size();
45+
functionIndexes[wasm.functions[i]->name] = index;
46+
}
47+
for (Index i = 0; i < wasm.globals.size(); i++) {
48+
auto index = globalIndexes.size();
49+
globalIndexes[wasm.globals[i]->name] = index;
50+
}
51+
}
52+
};
53+
54+
} // namespace ModuleUtils
55+
56+
} // namespace wasm
57+
58+
#endif // wasm_ast_module_h
59+

src/passes/Print.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <wasm-printing.h>
2323
#include <pass.h>
2424
#include <pretty_printing.h>
25+
#include <ast/module-utils.h>
2526

2627
namespace wasm {
2728

@@ -47,6 +48,8 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
4748
Function* currFunction = nullptr;
4849
Function::DebugLocation lastPrintedLocation;
4950

51+
std::unordered_map<Name, Index> functionIndexes;
52+
5053
PrintSExpression(std::ostream& o) : o(o) {
5154
setMinify(false);
5255
if (!full) full = isFullForced();
@@ -686,6 +689,14 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
686689
lastPrintedLocation = { 0, 0, 0 };
687690
printOpening(o, "func ", true);
688691
printName(curr->name);
692+
if (currModule && !minify) {
693+
// emit the function index in a comment
694+
if (functionIndexes.empty()) {
695+
ModuleUtils::BinaryIndexes indexes(*currModule);
696+
functionIndexes = std::move(indexes.functionIndexes);
697+
}
698+
o << " (; " << functionIndexes[curr->name] << " ;)";
699+
}
689700
if (curr->type.is()) {
690701
o << maybeSpace << "(type " << curr->type << ')';
691702
}

src/wasm-binary.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -696,8 +696,8 @@ class WasmBinaryWriter : public Visitor<WasmBinaryWriter, void> {
696696
void writeExports();
697697
void writeDataSegments();
698698

699-
std::map<Name, Index> mappedFunctions; // name of the Function => index. first imports, then internals
700-
std::map<Name, uint32_t> mappedGlobals; // name of the Global => index. first imported globals, then internal globals
699+
std::unordered_map<Name, Index> mappedFunctions; // name of the Function => index. first imports, then internals
700+
std::unordered_map<Name, uint32_t> mappedGlobals; // name of the Global => index. first imported globals, then internal globals
701701
uint32_t getFunctionIndex(Name name);
702702
uint32_t getGlobalIndex(Name name);
703703

src/wasm/wasm-binary.cpp

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "support/bits.h"
2121
#include "wasm-binary.h"
2222
#include "ast/branch-utils.h"
23+
#include "ast/module-utils.h"
2324

2425
namespace wasm {
2526

@@ -31,6 +32,9 @@ void WasmBinaryWriter::prepare() {
3132
}
3233
// TODO: depending on upstream flux https://github.com/WebAssembly/spec/pull/301 might want this: assert(!func->type.isNull());
3334
}
35+
ModuleUtils::BinaryIndexes indexes(*wasm);
36+
mappedFunctions = std::move(indexes.functionIndexes);
37+
mappedGlobals = std::move(indexes.globalIndexes);
3438
}
3539

3640
void WasmBinaryWriter::write() {
@@ -343,39 +347,11 @@ void WasmBinaryWriter::writeDataSegments() {
343347
}
344348

345349
uint32_t WasmBinaryWriter::getFunctionIndex(Name name) {
346-
if (!mappedFunctions.size()) {
347-
// Create name => index mapping.
348-
for (auto& import : wasm->imports) {
349-
if (import->kind != ExternalKind::Function) continue;
350-
assert(mappedFunctions.count(import->name) == 0);
351-
auto index = mappedFunctions.size();
352-
mappedFunctions[import->name] = index;
353-
}
354-
for (size_t i = 0; i < wasm->functions.size(); i++) {
355-
assert(mappedFunctions.count(wasm->functions[i]->name) == 0);
356-
auto index = mappedFunctions.size();
357-
mappedFunctions[wasm->functions[i]->name] = index;
358-
}
359-
}
360350
assert(mappedFunctions.count(name));
361351
return mappedFunctions[name];
362352
}
363353

364354
uint32_t WasmBinaryWriter::getGlobalIndex(Name name) {
365-
if (!mappedGlobals.size()) {
366-
// Create name => index mapping.
367-
for (auto& import : wasm->imports) {
368-
if (import->kind != ExternalKind::Global) continue;
369-
assert(mappedGlobals.count(import->name) == 0);
370-
auto index = mappedGlobals.size();
371-
mappedGlobals[import->name] = index;
372-
}
373-
for (size_t i = 0; i < wasm->globals.size(); i++) {
374-
assert(mappedGlobals.count(wasm->globals[i]->name) == 0);
375-
auto index = mappedGlobals.size();
376-
mappedGlobals[wasm->globals[i]->name] = index;
377-
}
378-
}
379355
assert(mappedGlobals.count(name));
380356
return mappedGlobals[name];
381357
}

test/atomics.wast.from-wast

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(module
22
(type $0 (func))
33
(memory $0 (shared 23 256))
4-
(func $atomic-loadstore (type $0)
4+
(func $atomic-loadstore (; 0 ;) (type $0)
55
(local $0 i32)
66
(local $1 i64)
77
(drop
@@ -68,7 +68,7 @@
6868
(get_local $1)
6969
)
7070
)
71-
(func $atomic-rmw (type $0)
71+
(func $atomic-rmw (; 1 ;) (type $0)
7272
(local $0 i32)
7373
(local $1 i64)
7474
(drop
@@ -102,7 +102,7 @@
102102
)
103103
)
104104
)
105-
(func $atomic-cmpxchg (type $0)
105+
(func $atomic-cmpxchg (; 2 ;) (type $0)
106106
(local $0 i32)
107107
(local $1 i64)
108108
(drop
@@ -134,7 +134,7 @@
134134
)
135135
)
136136
)
137-
(func $atomic-wait-wake (type $0)
137+
(func $atomic-wait-wake (; 3 ;) (type $0)
138138
(local $0 i32)
139139
(local $1 i64)
140140
(drop

test/atomics.wast.fromBinary

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(module
22
(type $0 (func))
33
(memory $0 (shared 23 256))
4-
(func $atomic-loadstore (type $0)
4+
(func $atomic-loadstore (; 0 ;) (type $0)
55
(local $var$0 i32)
66
(local $var$1 i64)
77
(drop
@@ -68,7 +68,7 @@
6868
(get_local $var$1)
6969
)
7070
)
71-
(func $atomic-rmw (type $0)
71+
(func $atomic-rmw (; 1 ;) (type $0)
7272
(local $var$0 i32)
7373
(local $var$1 i64)
7474
(drop
@@ -102,7 +102,7 @@
102102
)
103103
)
104104
)
105-
(func $atomic-cmpxchg (type $0)
105+
(func $atomic-cmpxchg (; 2 ;) (type $0)
106106
(local $var$0 i32)
107107
(local $var$1 i64)
108108
(drop
@@ -134,7 +134,7 @@
134134
)
135135
)
136136
)
137-
(func $atomic-wait-wake (type $0)
137+
(func $atomic-wait-wake (; 3 ;) (type $0)
138138
(local $var$0 i32)
139139
(local $var$1 i64)
140140
(drop

test/atomics.wast.fromBinary.noDebugInfo

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(module
22
(type $0 (func))
33
(memory $0 (shared 23 256))
4-
(func $0 (type $0)
4+
(func $0 (; 0 ;) (type $0)
55
(local $var$0 i32)
66
(local $var$1 i64)
77
(drop
@@ -68,7 +68,7 @@
6868
(get_local $var$1)
6969
)
7070
)
71-
(func $1 (type $0)
71+
(func $1 (; 1 ;) (type $0)
7272
(local $var$0 i32)
7373
(local $var$1 i64)
7474
(drop
@@ -102,7 +102,7 @@
102102
)
103103
)
104104
)
105-
(func $2 (type $0)
105+
(func $2 (; 2 ;) (type $0)
106106
(local $var$0 i32)
107107
(local $var$1 i64)
108108
(drop
@@ -134,7 +134,7 @@
134134
)
135135
)
136136
)
137-
(func $3 (type $0)
137+
(func $3 (; 3 ;) (type $0)
138138
(local $var$0 i32)
139139
(local $var$1 i64)
140140
(drop

test/break-to-return.wasm.fromBinary

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
(type $0 (func (param i32 i32) (result i32)))
33
(memory $0 256 256)
44
(export "add" (func $0))
5-
(func $0 (type $0) (param $var$0 i32) (param $var$1 i32) (result i32)
5+
(func $0 (; 0 ;) (type $0) (param $var$0 i32) (param $var$1 i32) (result i32)
66
(block $label$0 (result i32)
77
(br $label$0
88
(i32.add

test/ctor-eval/bad-indirect-call.wast.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
(memory $0 256 256)
66
(data (i32.const 10) "waka waka waka waka waka")
77
(export "test1" (func $test1))
8-
(func $test1 (type $v)
8+
(func $test1 (; 0 ;) (type $v)
99
(call_indirect $v
1010
(i32.const 1)
1111
)
@@ -14,7 +14,7 @@
1414
(i32.const 120)
1515
)
1616
)
17-
(func $call-indirect (type $v)
17+
(func $call-indirect (; 1 ;) (type $v)
1818
(i32.store8
1919
(i32.const 40)
2020
(i32.const 67)

0 commit comments

Comments
 (0)