-
Notifications
You must be signed in to change notification settings - Fork 877
Expand file tree
/
Copy pathPrintBoundary.cpp
More file actions
183 lines (165 loc) · 5.28 KB
/
Copy pathPrintBoundary.cpp
File metadata and controls
183 lines (165 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Copyright 2026 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.
*/
//
// Prints the boundary - the imports and exports - in a convenient JSON format.
// Only enough information for JavaScript is provided (for the full info, parse
// the wat or wasm).
//
// Usage:
//
// wasm-opt --print-boundary=OUTFILE
//
// If OUTFILE is not provided, prints to stdout.
//
// Example:
//
// {
// 'imports': [
// {
// 'module': 'foo', // foo.bar
// 'base': 'bar',
// 'kind': 'func',
// 'type': {
// 'params': ['i32', '(ref func)'],
// 'results': ['f64']
// },
// },
// [..]
// ],
// 'exports': [
// {
// 'name': 'foo',
// 'kind': 'global',
// 'type': 'i32',
// },
// [..]
// ]
// }
//
#include "ir/module-utils.h"
#include "pass.h"
#include "support/file.h"
#include "support/json.h"
#include "wasm.h"
namespace wasm {
struct PrintBoundary : public Pass {
bool modifiesBinaryenIR() override { return false; }
void run(Module* module) override {
std::string target = getArgumentOrDefault("print-boundary", "");
// Imports.
auto imports = json::Value::makeArray();
ModuleUtils::iterImportable(
*module, [&](ExternalKind kind, Importable* import) {
auto item = json::Value::makeObject();
item["module"] = json::Value::make(import->module.view());
item["base"] = json::Value::make(import->base.view());
item["kind"] = getKindName(kind);
item["type"] = getExternalType(kind, import->name, *module);
imports->push_back(item);
});
// Exports.
auto exports = json::Value::makeArray();
for (auto& exp : module->exports) {
auto item = json::Value::makeObject();
item["name"] = json::Value::make(exp->name.view());
item["kind"] = getKindName(exp->kind);
item["type"] =
getExternalType(exp->kind, *exp->getInternalName(), *module);
exports->push_back(item);
}
// Emit the final structure
json::Value root;
root.setObject();
root["imports"] = imports;
root["exports"] = exports;
Output output(target, Flags::BinaryOption::Text);
root.stringify(output.getStream(), true /* pretty */);
}
// Emits an array of multivalue types. For a signature, emits params and
// results.
//
// We emit an array only when needed, unless forceArray is set.
json::Value::Ref
getTypes(Type type, bool forceArray = false, size_t depth = 0) {
// Avoid infinite recursion.
if (type.isRef() && depth == 0) {
auto heapType = type.getHeapType();
if (heapType.isSignature()) {
auto sig = heapType.getSignature();
auto ret = json::Value::makeObject();
// Always emit arrays for params and results.
ret["params"] = getTypes(sig.params, true, depth + 1);
ret["results"] = getTypes(sig.results, true, depth + 1);
return ret;
}
}
// Simplify the output, avoiding an array for a single value.
if (!forceArray && type.size() == 1) {
return json::Value::make(type.toString());
}
auto ret = json::Value::makeArray();
for (auto t : type) {
ret->push_back(json::Value::make(t.toString()));
}
return ret;
}
// For an imported or exported thing (something external), and its name,
// return the type info we report for it.
json::Value::Ref getExternalType(ExternalKind kind, Name name, Module& wasm) {
switch (kind) {
case ExternalKind::Function:
return getTypes(wasm.getFunction(name)->type);
case ExternalKind::Table:
return getTypes(wasm.getTable(name)->type);
case ExternalKind::Memory:
return getTypes(wasm.getMemory(name)->addressType);
case ExternalKind::Global:
return getTypes(wasm.getGlobal(name)->type);
case ExternalKind::Tag:
// Wrap it in a Type so that getTypes can handle it. That will print the
// params and results as we expect.
return getTypes(Type(wasm.getTag(name)->type, NonNullable));
case ExternalKind::Invalid:
break;
}
WASM_UNREACHABLE("invalid ExternalKind");
}
json::Value::Ref getKindName(ExternalKind kind) {
const char* name = nullptr;
switch (kind) {
case ExternalKind::Function:
name = "func";
break;
case ExternalKind::Table:
name = "table";
break;
case ExternalKind::Memory:
name = "memory";
break;
case ExternalKind::Global:
name = "global";
break;
case ExternalKind::Tag:
name = "tag";
break;
case ExternalKind::Invalid:
WASM_UNREACHABLE("invalid ExternalKind");
}
return json::Value::make(name);
}
};
Pass* createPrintBoundaryPass() { return new PrintBoundary(); }
} // namespace wasm