Commit bf3c8b8f authored by Ben Smith's avatar Ben Smith Committed by Commit Bot

[wasm] Cleanup wasm-module-builder.js

Use naming similar to the spec: "table" instead of "function table",
"element segment" instead of "function table init".

Change-Id: Ib1b6cdfa566f8bd00017ccedf9440084204f10ff
Reviewed-on: https://chromium-review.googlesource.com/c/1273612
Commit-Queue: Ben Smith <binji@chromium.org>
Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#56545}
parent 0c968742
......@@ -16,7 +16,7 @@ builder0.addFunction('main', kSig_i_i)
kExprCallIndirect, sig_index, kTableZero
]) // --
.exportAs('main');
builder0.setFunctionTableBounds(3, 3);
builder0.setTableBounds(3, 3);
builder0.addExportOfKind('table', kExternalTable);
let module0 = new WebAssembly.Module(builder0.toBuffer());
let instance0 = new WebAssembly.Instance(module0);
......@@ -25,7 +25,7 @@ let builder1 = new WasmModuleBuilder();
builder1.setName('module_1');
builder1.addFunction('main', kSig_i_v).addBody([kExprUnreachable]);
builder1.addImportedTable('z', 'table');
builder1.addFunctionTableInit(0, false, [0], true);
builder1.addElementSegment(0, false, [0], true);
let module1 = new WebAssembly.Module(builder1.toBuffer());
let instance1 =
new WebAssembly.Instance(module1, {z: {table: instance0.exports.table}});
......
......@@ -12,7 +12,7 @@ let q_table = builder.addImportedTable("q", "table")
let q_base = builder.addImportedGlobal("q", "base", kWasmI32);
let q_fun = builder.addImport("q", "fun", kSig_v_v);
builder.addType(kSig_i_ii);
builder.addFunctionTableInit(q_base, true, [ q_fun ])
builder.addElementSegment(q_base, true, [ q_fun ])
let module = new WebAssembly.Module(builder.toBuffer());
let table = new WebAssembly.Table({
element: "anyfunc",
......
......@@ -17,7 +17,7 @@ builder.addFunction('main', kSig_i_ii).addBody([
sig_index1,
kTableZero
]).exportAs('main');
builder.setFunctionTableBounds(kTableSize, kTableSize);
builder.setTableBounds(kTableSize, kTableSize);
var m1_bytes = builder.toBuffer();
var m1 = new WebAssembly.Module(m1_bytes);
......
......@@ -20,6 +20,6 @@ const builder2 = new WasmModuleBuilder();
const mul_import = builder2.addImport('q', 'wasm_mul', kSig_i_ii);
builder2.addImportedTable('q', 'table');
const glob_import = builder2.addImportedGlobal('q', 'glob', kWasmI32);
builder2.addFunctionTableInit(glob_import, true, [mul_import]);
builder2.addElementSegment(glob_import, true, [mul_import]);
builder2.instantiate(
{q: {glob: 0, js_div: i => i, wasm_mul: mul, table: table}});
......@@ -33,7 +33,7 @@ load("test/mjsunit/wasm/wasm-module-builder.js");
kExprCallIndirect, 0, kTableZero
])
.exportFunc();
builder.addFunctionTableInit(0, false, [0, 1, 1, 0]);
builder.addElementSegment(0, false, [0, 1, 1, 0]);
return builder.instantiate({q: {f2: i1.exports.f2, f1: i1.exports.f1}});
})();
......
......@@ -217,8 +217,8 @@ load("test/mjsunit/wasm/wasm-module-builder.js");
kExprCallIndirect, sig_index1, kTableZero]) // --
.exportAs("main");
builder.setFunctionTableBounds(kTableSize, kTableSize);
builder.addFunctionTableInit(0, false, [f1.index]);
builder.setTableBounds(kTableSize, kTableSize);
builder.addElementSegment(0, false, [f1.index]);
builder.addExportOfKind("table", kExternalTable, 0);
return new WebAssembly.Module(builder.toBuffer());
......@@ -239,7 +239,7 @@ load("test/mjsunit/wasm/wasm-module-builder.js");
.exportAs("main");
builder.addImportedTable("z", "table", kTableSize, kTableSize);
builder.addFunctionTableInit(1, false, [f2.index], true);
builder.addElementSegment(1, false, [f2.index], true);
var m2_bytes = builder.toBuffer();
var m2 = new WebAssembly.Module(m2_bytes);
......
......@@ -42,8 +42,8 @@ let kTableSize = 50;
let f17 = addConstFunc(builder, 17);
builder.addExport("f15", f15);
builder.addExport("f17", f17);
builder.addFunctionTableInit(15, false, [f15], true);
builder.addFunctionTableInit(1, false, [call.index], true);
builder.addElementSegment(15, false, [f15], true);
builder.addElementSegment(1, false, [call.index], true);
var mod1 = builder.toModule();
}
......@@ -63,10 +63,10 @@ let kTableSize = 50;
])
.exportAs("call");
let f26 = addConstFunc(builder, 26);
builder.addFunctionTableInit(17, false, [f17], true);
builder.addFunctionTableInit(21, false, [f21], true);
builder.addFunctionTableInit(26, false, [f26], true);
builder.addFunctionTableInit(5, false, [call.index], true);
builder.addElementSegment(17, false, [f17], true);
builder.addElementSegment(21, false, [f21], true);
builder.addElementSegment(26, false, [f26], true);
builder.addElementSegment(5, false, [call.index], true);
var mod2 = builder.toModule();
}
......@@ -114,8 +114,8 @@ function addConstFuncUsingGlobal(builder, val) {
let f18 = addConstFuncUsingGlobal(builder, 18);
builder.addExport("f14", f14);
builder.addExport("f18", f18);
builder.addFunctionTableInit(14, false, [f14], true);
builder.addFunctionTableInit(1, false, [call.index], true);
builder.addElementSegment(14, false, [f14], true);
builder.addElementSegment(1, false, [call.index], true);
var mod1 = builder.toModule();
}
......@@ -135,10 +135,10 @@ function addConstFuncUsingGlobal(builder, val) {
])
.exportAs("call");
let f28 = addConstFuncUsingGlobal(builder, 28);
builder.addFunctionTableInit(18, false, [f18], true);
builder.addFunctionTableInit(22, false, [f22], true);
builder.addFunctionTableInit(28, false, [f28], true);
builder.addFunctionTableInit(5, false, [call.index], true);
builder.addElementSegment(18, false, [f18], true);
builder.addElementSegment(22, false, [f22], true);
builder.addElementSegment(28, false, [f28], true);
builder.addElementSegment(5, false, [call.index], true);
var mod2 = builder.toModule();
}
......@@ -194,8 +194,8 @@ function addConstFuncUsingMemory(builder, val) {
let f19 = addConstFuncUsingMemory(builder, 19);
builder.addExport("f13", f13);
builder.addExport("f19", f19);
builder.addFunctionTableInit(13, false, [f13], true);
builder.addFunctionTableInit(1, false, [call.index], true);
builder.addElementSegment(13, false, [f13], true);
builder.addElementSegment(1, false, [call.index], true);
var mod1 = builder.toModule();
}
......@@ -217,10 +217,10 @@ function addConstFuncUsingMemory(builder, val) {
])
.exportAs("call");
let f29 = addConstFuncUsingMemory(builder, 29);
builder.addFunctionTableInit(19, false, [f19], true);
builder.addFunctionTableInit(23, false, [f23], true);
builder.addFunctionTableInit(29, false, [f29], true);
builder.addFunctionTableInit(5, false, [call.index], true);
builder.addElementSegment(19, false, [f19], true);
builder.addElementSegment(23, false, [f23], true);
builder.addElementSegment(29, false, [f29], true);
builder.addElementSegment(5, false, [call.index], true);
var mod2 = builder.toModule();
}
......
......@@ -169,8 +169,8 @@ function AddFunctions(builder) {
kExprCallIndirect, 0, kTableZero]) // --
.exportAs("main");
builder.setFunctionTableBounds(length, length);
builder.addFunctionTableInit(base, false, [f.add.index, f.sub.index, f.mul.index]);
builder.setTableBounds(length, length);
builder.addElementSegment(base, false, [f.add.index, f.sub.index, f.mul.index]);
return builder.instantiate();
}
......@@ -206,9 +206,9 @@ function AddFunctions(builder) {
kExprCallIndirect, 0, kTableZero]) // --
.exportAs("main");
builder.setFunctionTableBounds(10, 10);
builder.setTableBounds(10, 10);
var g = builder.addImportedGlobal("fff", "base", kWasmI32);
builder.addFunctionTableInit(g, true, [f.mul.index, f.add.index, f.sub.index]);
builder.addElementSegment(g, true, [f.mul.index, f.add.index, f.sub.index]);
var module = new WebAssembly.Module(builder.toBuffer());
......
......@@ -49,11 +49,10 @@ function js_div(a, b) { return (a / b) | 0; }
f.add.exportAs("blarg");
builder.setFunctionTableBounds(10, 10);
builder.setTableBounds(10, 10);
let g = builder.addImportedGlobal("q", "base", kWasmI32);
builder.addFunctionTableInit(g, true, [f.mul.index, f.add.index,
f.sub.index,
d]);
builder.addElementSegment(
g, true, [f.mul.index, f.add.index, f.sub.index, d]);
builder.addExportOfKind("table", kExternalTable, 0);
let module = new WebAssembly.Module(builder.toBuffer());
......@@ -109,11 +108,10 @@ function js_div(a, b) { return (a / b) | 0; }
let d = builder.addImport("q", "js_div", kSig_i_ii);
let f = AddFunctions(builder);
builder.setFunctionTableBounds(kTableSize, kTableSize);
builder.setTableBounds(kTableSize, kTableSize);
let g = builder.addImportedGlobal("q", "base", kWasmI32);
builder.addFunctionTableInit(g, true, [f.mul.index, f.add.index,
f.sub.index,
d]);
builder.addElementSegment(
g, true, [f.mul.index, f.add.index, f.sub.index, d]);
builder.addExportOfKind("table", kExternalTable, 0);
let m1 = new WebAssembly.Module(builder.toBuffer());
......@@ -182,9 +180,8 @@ function js_div(a, b) { return (a / b) | 0; }
builder.addImportedTable("q", "table", kTableSize, kTableSize);
let g = builder.addImportedGlobal("q", "base", kWasmI32);
let f = AddFunctions(builder);
builder.addFunctionTableInit(g, true, [f.mul.index, f.add.index,
f.sub.index,
d]);
builder.addElementSegment(
g, true, [f.mul.index, f.add.index, f.sub.index, d]);
builder.addFunction("main", kSig_i_ii)
.addBody([
kExprI32Const, 55, // --
......@@ -260,7 +257,7 @@ function js_div(a, b) { return (a / b) | 0; }
kExprGetLocal, 0,
kExprCallIndirect, sig_index, kTableZero]) // --
.exportAs("main");
builder.addFunctionTableInit(g, true, [f.index]);
builder.addElementSegment(g, true, [f.index]);
let module = new WebAssembly.Module(builder.toBuffer());
......@@ -300,8 +297,8 @@ function js_div(a, b) { return (a / b) | 0; }
kExprCallIndirect, sig_index1, kTableZero]) // --
.exportAs("main");
builder.setFunctionTableBounds(kTableSize, kTableSize);
builder.addFunctionTableInit(0, false, [f1.index]);
builder.setTableBounds(kTableSize, kTableSize);
builder.addElementSegment(0, false, [f1.index]);
builder.addExportOfKind("table", kExternalTable, 0);
var m1 = new WebAssembly.Module(builder.toBuffer());
......@@ -320,7 +317,7 @@ function js_div(a, b) { return (a / b) | 0; }
.exportAs("main");
builder.addImportedTable("z", "table", kTableSize, kTableSize);
builder.addFunctionTableInit(1, false, [f2.index], true);
builder.addElementSegment(1, false, [f2.index], true);
var m2 = new WebAssembly.Module(builder.toBuffer());
......@@ -349,7 +346,7 @@ function js_div(a, b) { return (a / b) | 0; }
for (var impsize = 1; impsize < 4; impsize++) {
print(" expsize = " + expsize + ", impsize = " + impsize);
var builder = new WasmModuleBuilder();
builder.setFunctionTableBounds(expsize, expsize);
builder.setTableBounds(expsize, expsize);
builder.addExportOfKind("expfoo", kExternalTable, 0);
let m1 = new WebAssembly.Module(builder.toBuffer());
......@@ -415,7 +412,7 @@ function js_div(a, b) { return (a / b) | 0; }
kExprGetLocal, 0,
kExprCallIndirect, sig_index, kTableZero]) // --
.exportAs("main");
builder.addFunctionTableInit(g, true, [g]);
builder.addElementSegment(g, true, [g]);
let module = new WebAssembly.Module(builder.toBuffer());
var instances = [];
......@@ -538,7 +535,7 @@ function js_div(a, b) { return (a / b) | 0; }
for (let num_segments = 1; num_segments < 4; ++num_segments) {
var builder = new WasmModuleBuilder();
builder.setFunctionTableBounds(kTableSize, kTableSize);
builder.setTableBounds(kTableSize, kTableSize);
builder.addExportOfKind("table", kExternalTable, 0);
let f = AddFunctions(builder);
let indexes = [f.mul.index, f.add.index, f.sub.index];
......@@ -546,7 +543,7 @@ function js_div(a, b) { return (a / b) | 0; }
let offset = i + 1;
let len = i + 2;
let index = indexes[i];
builder.addFunctionTableInit(offset, false, new Array(len).fill(index));
builder.addElementSegment(offset, false, new Array(len).fill(index));
}
let instance = builder.instantiate();
......@@ -580,7 +577,7 @@ function js_div(a, b) { return (a / b) | 0; }
kExprCallIndirect, sig_index, kTableZero
])
.exportAs('main');
builder0.setFunctionTableBounds(3, 3);
builder0.setTableBounds(3, 3);
builder0.addExportOfKind('table', kExternalTable);
let module0 = new WebAssembly.Module(builder0.toBuffer());
let instance0 = new WebAssembly.Instance(module0);
......@@ -590,7 +587,7 @@ function js_div(a, b) { return (a / b) | 0; }
builder1.setName('module_1');
builder1.addFunction('f', kSig_i_i).addBody([kExprGetLocal, 0]);
builder1.addImportedTable('z', 'table');
builder1.addFunctionTableInit(0, false, [0], true);
builder1.addElementSegment(0, false, [0], true);
let module1 = new WebAssembly.Module(builder1.toBuffer());
let instance1 =
new WebAssembly.Instance(module1, {z: {table: instance0.exports.table}});
......@@ -619,7 +616,7 @@ function js_div(a, b) { return (a / b) | 0; }
.exportAs('main');
builder.addImportedMemory('', 'memory', 1);
builder.setFunctionTableBounds(1, 1);
builder.setTableBounds(1, 1);
builder.addExportOfKind('table', kExternalTable);
let module1 = new WebAssembly.Module(builder.toBuffer());
......@@ -628,7 +625,7 @@ function js_div(a, b) { return (a / b) | 0; }
builder = new WasmModuleBuilder();
builder.addFunction('main', kSig_i_v).addBody([kExprI32Const, 0, kExprI32LoadMem, 0, 0]);
builder.addImportedTable('', 'table');
builder.addFunctionTableInit(0, false, [0], true);
builder.addElementSegment(0, false, [0], true);
builder.addImportedMemory('', 'memory', 1);
......@@ -734,9 +731,9 @@ function js_div(a, b) { return (a / b) | 0; }
kExprCallIndirect, 0, kTableZero]) // --
.exportAs("main");
builder.setFunctionTableBounds(10, 10);
builder.setTableBounds(10, 10);
let g = builder.addImportedGlobal("q", "base", kWasmI32);
builder.addFunctionTableInit(g, true, [j, w]);
builder.addElementSegment(g, true, [j, w]);
let module = new WebAssembly.Module(builder.toBuffer());
for (var i = 0; i < 5; i++) {
......@@ -785,7 +782,7 @@ function js_div(a, b) { return (a / b) | 0; }
.exportAs("main");
let g = builder.addImportedGlobal("q", "base", kWasmI32);
builder.addFunctionTableInit(g, true, [j, w]);
builder.addElementSegment(g, true, [j, w]);
let module = new WebAssembly.Module(builder.toBuffer());
for (var i = 0; i < 5; i++) {
......@@ -824,7 +821,7 @@ function js_div(a, b) { return (a / b) | 0; }
kExprCallIndirect, 0, kTableZero
])
.exportFunc();
builder.addFunctionTableInit(0, false, [0, 1, 1, 0]);
builder.addElementSegment(0, false, [0, 1, 1, 0]);
return builder.instantiate({q: {f2: i1.exports.f2, f1: i1.exports.f1}});
})();
......@@ -886,7 +883,7 @@ function js_div(a, b) { return (a / b) | 0; }
])
.exportFunc();
builder.exportMemoryAs("memory");
builder.addFunctionTableInit(0, false, [0, 1, 2, 3]);
builder.addElementSegment(0, false, [0, 1, 2, 3]);
var instance = builder.instantiate({q: {f1: f100, f2: f200, f3: f300}});
setMemI32(instance, 0, 5000000);
setMemI32(instance, 4, 6000000);
......
......@@ -530,7 +530,7 @@ function checkStack(stack, expected_lines) {
kExprCallIndirect, sig_index, kTableZero
]) // --
.exportAs('main');
builder0.setFunctionTableBounds(3, 3);
builder0.setTableBounds(3, 3);
builder0.addExportOfKind('table', kExternalTable);
const module0 = new WebAssembly.Module(builder0.toBuffer());
const instance0 = new WebAssembly.Instance(module0);
......@@ -538,7 +538,7 @@ function checkStack(stack, expected_lines) {
const builder1 = new WasmModuleBuilder();
builder1.addFunction('main', kSig_i_v).addBody([kExprUnreachable]);
builder1.addImportedTable('z', 'table');
builder1.addFunctionTableInit(0, false, [0], true);
builder1.addElementSegment(0, false, [0], true);
const module1 = new WebAssembly.Module(builder1.toBuffer());
const instance1 =
new WebAssembly.Instance(module1, {z: {table: instance0.exports.table}});
......
......@@ -265,7 +265,7 @@ let exportingModuleBinary2 = (() => {
builder.addFunction('foo', kSig_v_v).addBody([]).exportAs('a');
builder.addMemory(1, 1, false);
builder.exportMemoryAs('b');
builder.setFunctionTableBounds(1, 1);
builder.setTableBounds(1, 1);
builder.addExportOfKind('c', kExternalTable, 0);
var o = builder.addGlobal(kWasmI32, false).exportAs('x');
return new Int8Array(builder.toBuffer());
......
......@@ -133,7 +133,7 @@ let id = (() => { // identity exported function
builder.addImportedTable("q", "table", 5, 32);
let g = builder.addImportedGlobal("q", "base", kWasmI32);
builder.addFunctionTableInit(g, true,
builder.addElementSegment(g, true,
[funcs.mul.index, funcs.add.index, funcs.sub.index]);
builder.addExportOfKind("table", kExternalTable, 0);
let module = new WebAssembly.Module(builder.toBuffer());
......@@ -179,10 +179,10 @@ let id = (() => { // identity exported function
let i = builder.addImport("q", "exp_inc", sig_i_i);
let t = builder.addImport("q", "exp_ten", sig_i_v);
builder.setFunctionTableBounds(7, 35);
// builder.addFunctionTableInit(g1, true,
builder.setTableBounds(7, 35);
// builder.addElementSegment(g1, true,
// [funcs.mul.index, funcs.add.index, funcs.sub.index]);
builder.addFunctionTableInit(g1, true, [a, i, t]);
builder.addElementSegment(g1, true, [a, i, t]);
builder.addExportOfKind("table", kExternalTable, 0);
let module = new WebAssembly.Module(builder.toBuffer());
......@@ -215,7 +215,7 @@ let id = (() => { // identity exported function
let funcs = addFunctions(builder1);
builder1.addImportedTable("q", "table", 6, 36);
builder1.addFunctionTableInit(g, true,
builder1.addElementSegment(g, true,
[funcs.mul.index, funcs.add.index, funcs.sub.index]);
let module1 = new WebAssembly.Module(builder1.toBuffer());
......@@ -267,7 +267,7 @@ let id = (() => { // identity exported function
kExprGetLocal, 0,
kExprCallIndirect, index_i_ii, kTableZero])
.exportAs("main");
builder.addFunctionTableInit(0, false, [0], true);
builder.addElementSegment(0, false, [0], true);
return new WebAssembly.Module(builder.toBuffer());
}
......
......@@ -171,11 +171,10 @@ class WasmModuleBuilder {
this.globals = [];
this.exceptions = [];
this.functions = [];
this.function_table = [];
this.function_table_length_min = 0;
this.function_table_length_max = 0;
this.function_table_inits = [];
this.segments = [];
this.table_length_min = 0;
this.table_length_max = undefined;
this.element_segments = [];
this.data_segments = [];
this.explicit = [];
this.num_imported_funcs = 0;
this.num_imported_globals = 0;
......@@ -301,24 +300,24 @@ class WasmModuleBuilder {
}
addDataSegment(addr, data, is_global = false) {
this.segments.push({addr: addr, data: data, is_global: is_global});
return this.segments.length - 1;
this.data_segments.push({addr: addr, data: data, is_global: is_global});
return this.data_segments.length - 1;
}
exportMemoryAs(name) {
this.exports.push({name: name, kind: kExternalMemory, index: 0});
}
addFunctionTableInit(base, is_global, array, is_import = false) {
this.function_table_inits.push({base: base, is_global: is_global,
addElementSegment(base, is_global, array, is_import = false) {
this.element_segments.push({base: base, is_global: is_global,
array: array});
if (!is_global) {
var length = base + array.length;
if (length > this.function_table_length_min && !is_import) {
this.function_table_length_min = length;
if (length > this.table_length_min && !is_import) {
this.table_length_min = length;
}
if (length > this.function_table_length_max && !is_import) {
this.function_table_length_max = length;
if (length > this.table_length_max && !is_import) {
this.table_length_max = length;
}
}
return this;
......@@ -329,12 +328,18 @@ class WasmModuleBuilder {
if (typeof n != 'number')
throw new Error('invalid table (entries have to be numbers): ' + array);
}
return this.addFunctionTableInit(this.function_table.length, false, array);
return this.addElementSegment(this.table_length_min, false, array);
}
setFunctionTableBounds(min, max) {
this.function_table_length_min = min;
this.function_table_length_max = max;
setTableBounds(min, max) {
this.table_length_min = min;
this.table_length_max = max;
return this;
}
setTableLength(length) {
this.table_length_min = length;
this.table_length_max = length;
return this;
}
......@@ -422,17 +427,17 @@ class WasmModuleBuilder {
});
}
// Add function_table.
if (wasm.function_table_length_min > 0) {
// Add table section
if (wasm.table_length_min > 0) {
if (debug) print("emitting table @ " + binary.length);
binary.emit_section(kTableSectionCode, section => {
section.emit_u8(1); // one table entry
section.emit_u8(kWasmAnyFunctionTypeForm);
// TODO(gdeepti): Cleanup to use optional max flag,
// fix up tests to set correct initial/maximum values
section.emit_u32v(1);
section.emit_u32v(wasm.function_table_length_min);
section.emit_u32v(wasm.function_table_length_max);
const max = wasm.table_length_max;
const has_max = max !== undefined;
section.emit_u8(has_max ? kHasMaximumFlag : 0);
section.emit_u32v(wasm.table_length_min);
if (has_max) section.emit_u32v(max);
});
}
......@@ -546,11 +551,11 @@ class WasmModuleBuilder {
});
}
// Add table elements.
if (wasm.function_table_inits.length > 0) {
if (debug) print("emitting table @ " + binary.length);
// Add element segments
if (wasm.element_segments.length > 0) {
if (debug) print("emitting element segments @ " + binary.length);
binary.emit_section(kElementSectionCode, section => {
var inits = wasm.function_table_inits;
var inits = wasm.element_segments;
section.emit_u32v(inits.length);
for (let init of inits) {
......@@ -618,11 +623,11 @@ class WasmModuleBuilder {
}
// Add data segments.
if (wasm.segments.length > 0) {
if (wasm.data_segments.length > 0) {
if (debug) print("emitting data segments @ " + binary.length);
binary.emit_section(kDataSectionCode, section => {
section.emit_u32v(wasm.segments.length);
for (let seg of wasm.segments) {
section.emit_u32v(wasm.data_segments.length);
for (let seg of wasm.data_segments) {
section.emit_u8(0); // linear memory index 0
if (seg.is_global) {
// initializer is a global variable
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment