forked from titzer/wizard-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstantiator.v3
356 lines (345 loc) · 11.7 KB
/
Instantiator.v3
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright 2020 Ben L. Titzer. All rights reserved.
// See LICENSE for details of Apache 2.0 license.
// Extension point for import processing. An {ImportProcessor} lookups or materializes
// an {Exportable} for a given name and import kind. It can use the {binder} as a
// callback to access parts of the module that need polymorphic substitution.
class ImportProcessor(name: string) {
def preprocess(error: ErrorGen, module: Module, imports: Array<Exportable>) { }
def processFunction(error: ErrorGen, name: string, decl: FuncDecl) -> Exportable { return null; }
def processTable(error: ErrorGen, name: string, decl: TableDecl) -> Exportable { return null; }
def processMemory(error: ErrorGen, name: string, decl: MemoryDecl) -> Exportable { return null; }
def processGlobal(error: ErrorGen, name: string, decl: GlobalDecl) -> Exportable { return null; }
def processTag(error: ErrorGen, name: string, decl: TagDecl) -> Exportable { return null; }
def postprocess(error: ErrorGen, instance: Instance) { }
}
// Creates {Instance} objects, given a module and a list of imports.
class Instantiator(extensions: Extension.set, module: Module, var imports: Array<Exportable>, error: ErrorGen) { // TODO: s/error/err
def instance = Instance.new(module, imports);
def processors = Vector<ImportProcessor>.new();
var tiering: ExecutionStrategy;
var binder: (Decl, Exportable) -> Exportable; // last binding step
var trap_reason: TrapReason;
var import_pos: int;
var import_info: ImportInfo;
def run() -> Instance {
if (tiering != null) tiering.onInstantiateStart(this);
if (module.imports.length > 0) {
if (imports == null) imports = Array.new(module.imports.length);
else if (imports.length < module.imports.length) {
return fail(Strings.format2("expected %d imports, got %d", module.imports.length, imports.length));
}
}
for (i < processors.length) {
processors[i].preprocess(error, module, imports);
}
var needSigIds = false;
for (j < module.decls.length) {
if (error.error()) return null;
match (module.decls[j]) {
Sig(index) => {
var decl = SigDecl.!(module.heaptypes[index]);
instance.heaptypes[index] = mapSig(decl); // TODO: respect recursion group
}
Struct(index) => {
var decl = StructDecl.!(module.heaptypes[index]);
// TODO: substitute type declarations in struct
}
Array(index) => {
var decl = ArrayDecl.!(module.heaptypes[index]);
}
Func(index, sig_index) => {
var decl = module.functions[index];
if ((import_info = decl.imp) != null) {
var r = imports[decl.imp.import_index];
instance.functions[index] = importFunction(decl, r);
} else {
var f = WasmFunction.new(instance, mapFuncDecl(index));
instance.functions[index] = f;
}
}
Table(index) => {
var decl = module.tables[index];
if ((import_info = decl.imp) != null) {
var r = imports[decl.imp.import_index];
var t = importTable(decl, r);
instance.tables[index] = t;
if (t != null && t.funcs != null) needSigIds = true;
} else {
var t = Table.new(mapType(decl.elemtype), decl);
if (t.oom) return fail("out of memory allocating table");
instance.tables[index] = t;
if (t.funcs != null) needSigIds = true;
}
}
Memory(index) => {
var decl = module.memories[index];
if ((import_info = decl.imp) != null) {
var r = imports[decl.imp.import_index];
instance.memories[index] = importMemory(decl, r);
} else {
var m = Target.newMemory(decl);
if (m.oom) return fail("out of memory allocating memory");
instance.memories[index] = m;
}
}
Global(index) => {
var decl = module.globals[index];
if ((import_info = decl.imp) != null) {
var r = imports[decl.imp.import_index];
instance.globals[index] = importGlobal(decl, r);
} else {
var g = Global.new(mapType(decl.valtype), decl);
instance.globals[index] = g;
g.value = instance.evalInitExpr(decl.init);
}
}
Tag(index) => {
var decl = module.tags[index];
if ((import_info = decl.imp) != null) {
var r = imports[decl.imp.import_index];
instance.tags[index] = importTag(decl, r);
} else {
instance.tags[index] = Tag.new(getSigDecl(decl.sig_index), decl);
}
}
Continuation(index) => {
// TODO-SS: implement
}
}
}
if (error.error()) return null;
import_info = null;
if (needSigIds) {
for (i < instance.heaptypes.length) {
match (instance.heaptypes[i]) {
x: SigDecl => instance.sig_ids[i] = Canon.sigId(x);
_ => instance.sig_ids[i] = -1;
}
}
}
// Initialize tables that have default element values.
for (t in instance.tables) {
if (!t.decl.has_default_elem) continue;
var val = instance.evalInitExpr(t.decl.default_elem);
t.fill(0, val, u32.view(t.elems.length));
}
// Load element segments.
for (i < module.elems.length) {
var e = module.elems[i];
match (e.mode) {
Passive => ;
Active(index, offset) => {
loadElems(index, offset, e);
instance.dropped_elems[i] = true;
if (error.error()) return null;
}
Declarative => {
instance.dropped_elems[i] = true;
}
}
}
// Load data segments.
for (i < module.data.length) {
var d = module.data[i];
match (d.mode) {
Passive => ;
Active(index, offset) => {
loadData(index, offset, d);
instance.dropped_data[i] = true;
if (error.error()) return null;
}
Declarative => {
instance.dropped_data[i] = true;
}
}
}
// Organize exports
var exports = instance.exports;
for (i < module.exports.length) {
exports[i] = getDecl(module.exports[i].1);
}
for (i < processors.length) {
processors[i].postprocess(error, instance);
}
if (tiering != null) tiering.onInstantiateFinish(this, error);
return instance;
}
def importFunction(decl: FuncDecl, r: Exportable) -> Function {
if (r == null) r = processImport(decl, ImportProcessor.processFunction(_, error, _, decl));
if (binder != null) r = binder(decl, r);
match (r) {
func: Function => {
var expected_sig = getSigDecl(decl.sig_index);
if (func.sig != expected_sig) {
var buf = StringBuilder.new().puts("expected function with signature ");
if (expected_sig == null) buf.puts("(null)");
else expected_sig.render(buf);
buf.puts(", got ");
func.sig.render(buf);
if (func.sig.isAssignableSig(expected_sig)) {
// structurally equivalent, but not allowed in Wasm's canonicalization--add context
buf.put2(", structurally equivalent (canon #%d, #%d)", expected_sig.canon_id, func.sig.canon_id);
}
fail(buf.toString());
}
return func;
}
null => fail("function import not found");
_ => fail("expected function import");
}
return null;
}
def importTable(decl: TableDecl, r: Exportable) -> Table {
if (r == null) r = processImport(decl, ImportProcessor.processTable(_, error, _, decl));
if (binder != null) r = binder(decl, r);
match (r) {
table: Table => {
if (!checkLimits(table.decl.initial, table.decl.maximum, decl.initial, decl.maximum)) {
fail("table limits mismatch");
}
var expected_type = mapType(decl.elemtype);
var got_type = table.decl.elemtype;
if (got_type != expected_type) { // TODO: proper equality check
fail("table element type mismatch");
}
return table;
}
}
fail("expected table import");
return null;
}
def importMemory(decl: MemoryDecl, r: Exportable) -> Memory {
if (r == null) r = processImport(decl, ImportProcessor.processMemory(_, error, _, decl));
if (binder != null) r = binder(decl, r);
match (r) {
memory: Memory => {
if (!checkLimits(memory.size(), memory.decl.maximum, decl.initial, decl.maximum)) {
fail("memory limits mismatch");
}
if (memory.decl.shared != decl.shared) {
fail("memory sharing mismatch");
}
return memory;
}
}
fail("expected memory import");
return null;
}
def importTag(decl: TagDecl, r: Exportable) -> Tag {
if (r == null) r = processImport(decl, ImportProcessor.processTag(_, error, _, decl));
if (binder != null) r = binder(decl, r);
match (r) {
tag: Tag => {
var expected_sig = getSigDecl(decl.sig_index);
if (!tag.sig.isAssignableSig(expected_sig)) {
var expected = if(expected_sig == null, StringBuilder.puts(_, "(null)"), expected_sig.render);
fail(Strings.format2("expected tag with signature %q, got %q", expected, tag.sig.render));
}
return tag;
}
null => fail("tag import not found");
_ => fail("expected tag import");
}
return null;
}
def importGlobal(decl: GlobalDecl, r: Exportable) -> Global {
if (r == null) r = processImport(decl, ImportProcessor.processGlobal(_, error, _, decl));
if (binder != null) r = binder(decl, r);
match (r) {
global: Global => {
if (decl.mutable != global.decl.mutable) {
fail("global immutability mismatch");
}
var expected_type = mapType(decl.valtype);
var got_type = global.valtype;
if (decl.mutable) {
if (got_type != expected_type) fail("global type mismatch");
} else {
if (!ValueTypes.isAssignable(got_type, expected_type)) fail("global type mismatch");
}
return global;
}
}
fail("expected global import");
return null;
}
def processImport(decl: Decl, f: (ImportProcessor, string) -> Exportable) -> Exportable {
var i = decl.imp;
import_pos = i.import_index;
var modname = i.module_name, fieldname = i.field_name;
for (i < processors.length) {
var p = processors[i];
if (Strings.equal(modname, p.name)) {
var r = f(p, fieldname);
if (r != null) {
imports[decl.imp.import_index] = r;
return r;
}
}
}
return null;
}
def getDecl(d: Decl) -> Exportable {
match (d) {
x: FuncDecl => return instance.functions[x.func_index];
x: TableDecl => return instance.tables[x.table_index];
x: MemoryDecl => return instance.memories[x.memory_index];
x: GlobalDecl => return instance.globals[x.global_index];
x: TagDecl => return instance.tags[x.tag_index];
}
return null;
}
def getSigDecl(sig_index: int) -> SigDecl {
if (u32.view(sig_index) >= instance.heaptypes.length) return null;
return SigDecl.!(instance.heaptypes[sig_index]);
}
// Substitution utilities.
def mapFuncDecl(func_index: int) -> FuncDecl {
var orig = module.functions[func_index];
return orig;
}
def mapSig(sig: SigDecl) -> SigDecl {
return sig;
}
def mapType(t: ValueType) -> ValueType {
return t;
}
private def fail(msg: string) -> Instance {
if (import_info != null) {
var buf = StringBuilder.new()
.put3("import #%d(\"%s\".\"%s\"): ", import_pos, import_info.module_name, import_info.field_name)
.puts(msg);
msg = buf.toString();
}
error.abs(import_pos).set(msg);
return null;
}
private def checkLimits(fmin: u64, fmax: Max, tmin: u64, tmax: Max) -> bool {
if (fmin < tmin) return false;
match (tmax) {
None => return true;
Set(tmax_val) => match (fmax) {
None => return false;
Set(fmax_val) => return fmax_val <= tmax_val;
}
}
}
private def loadElems(index: int, offset: InitExpr, elems: ElemDecl) {
var dst_offset = Values.v_u(instance.evalInitExpr(offset));
var table = instance.tables[index];
var r = table.copyE(instance, dst_offset, elems, 0, u32.view(elems.details.length()));
if (r != TrapReason.NONE) {
fail("out of bounds in initialization");
trap_reason = r;
}
}
private def loadData(index: int, offset: InitExpr, ddecl: DataDecl) {
var dst_offset = Values.v_u(instance.evalInitExpr(offset));
var memory = instance.memories[index];
var r = memory.copyIn(dst_offset, if(ddecl != null, ddecl.data), 0, u32.view(ddecl.data.length));
if (r != TrapReason.NONE) {
fail("out of bounds in initialization");
trap_reason = r;
}
}
}