Commit 7c74a9ca authored by Manos Koukoutos's avatar Manos Koukoutos Committed by V8 LUCI CQ

[wasm][test] Represent constant expressions with bytes

Maintaining an AST class just for testing constant exressions does not
seem justified. This CL changes constant expressions in mjsunit tests
to be represented with bytes, like regular expressions.

Change-Id: If5ec5f4d863176952442b1a7e2fec8a61e385971
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3714237Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Commit-Queue: Manos Koukoutos <manoskouk@chromium.org>
Cr-Commit-Position: refs/heads/main@{#81266}
parent 01ccfa44
......@@ -5,7 +5,7 @@
d8.file.execute('test/mjsunit/wasm/wasm-module-builder.js');
const builder = new WasmModuleBuilder();
builder.addGlobal(kWasmI32, true, WasmInitExpr.I32Const(35));
builder.addGlobal(kWasmI32, true, wasmI32Const(35));
builder.addType(makeSig([], [kWasmI32]));
builder.addType(makeSig([kWasmI32, kWasmI32], [kWasmI32]));
// Generate function 1 (out of 3).
......
......@@ -11,7 +11,7 @@ builder.addMemory(16, 32, false, true);
builder.addType(makeSig([kWasmI32, kWasmI32, kWasmI32], [kWasmI32]));
builder.addType(makeSig([], []));
builder.setTableBounds(1, 1);
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(0), [0]);
builder.addActiveElementSegment(0, wasmI32Const(0), [0]);
// Generate function 1 (out of 1).
builder.addFunction(undefined, 0 /* sig */)
.addBodyWithEnd([
......
......@@ -16,9 +16,9 @@ let other = builder.addFunction("other", sig_index).addBody([]);
let table = builder.addTable(kWasmAnyFunc, 1, 1);
builder.addActiveElementSegment(
0, // table
WasmInitExpr.I32Const(0), // offset
[1]); // values
0, // table
wasmI32Const(0), // offset
[1]); // values
main.addBody([
kExprI64Const, 0x33,
......
......@@ -6,7 +6,7 @@ d8.file.execute('test/mjsunit/wasm/wasm-module-builder.js');
const builder = new WasmModuleBuilder();
builder.addMemory(16, 17);
builder.addGlobal(kWasmI32, 1, WasmInitExpr.I32Const(10));
builder.addGlobal(kWasmI32, 1, wasmI32Const(10));
// Generate function 1 (out of 3).
builder.addFunction('load', kSig_i_v)
.addBody([
......
......@@ -17,7 +17,7 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
var builder = new WasmModuleBuilder();
builder.addGlobal(kWasmI32, true, WasmInitExpr.I32Const(1));
builder.addGlobal(kWasmI32, true, wasmI32Const(1));
builder.addFunction("main", kSig_i_i)
.addBody([
......
......@@ -21,10 +21,10 @@ builder.addTable(kWasmFuncRef, 4, 5, undefined)
builder.addTable(kWasmFuncRef, 15, 25, undefined)
builder.addTable(kWasmFuncRef, 1, 1, undefined)
builder.addTable(kWasmFuncRef, 16, 17, undefined)
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(0), [WasmInitExpr.RefFunc(0), WasmInitExpr.RefFunc(1), WasmInitExpr.RefFunc(2), WasmInitExpr.RefFunc(3)], kWasmFuncRef);
builder.addActiveElementSegment(1, WasmInitExpr.I32Const(0), [WasmInitExpr.RefFunc(0), WasmInitExpr.RefFunc(1), WasmInitExpr.RefFunc(2), WasmInitExpr.RefFunc(3), WasmInitExpr.RefFunc(0), WasmInitExpr.RefFunc(1), WasmInitExpr.RefFunc(2), WasmInitExpr.RefFunc(3), WasmInitExpr.RefFunc(0), WasmInitExpr.RefFunc(1), WasmInitExpr.RefFunc(2), WasmInitExpr.RefFunc(3), WasmInitExpr.RefFunc(0), WasmInitExpr.RefFunc(1), WasmInitExpr.RefFunc(2)], kWasmFuncRef);
builder.addActiveElementSegment(2, WasmInitExpr.I32Const(0), [WasmInitExpr.RefFunc(0)], kWasmFuncRef);
builder.addActiveElementSegment(3, WasmInitExpr.I32Const(0), [WasmInitExpr.RefFunc(0), WasmInitExpr.RefFunc(1), WasmInitExpr.RefFunc(2), WasmInitExpr.RefFunc(3), WasmInitExpr.RefFunc(0), WasmInitExpr.RefFunc(1), WasmInitExpr.RefFunc(2), WasmInitExpr.RefFunc(3), WasmInitExpr.RefFunc(0), WasmInitExpr.RefFunc(1), WasmInitExpr.RefFunc(2), WasmInitExpr.RefFunc(3), WasmInitExpr.RefFunc(0), WasmInitExpr.RefFunc(1), WasmInitExpr.RefFunc(2), WasmInitExpr.RefFunc(3)], kWasmFuncRef);
builder.addActiveElementSegment(0, wasmI32Const(0), [[kExprRefFunc, 0], [kExprRefFunc, 1], [kExprRefFunc, 2], [kExprRefFunc, 3]], kWasmFuncRef);
builder.addActiveElementSegment(1, wasmI32Const(0), [[kExprRefFunc, 0], [kExprRefFunc, 1], [kExprRefFunc, 2], [kExprRefFunc, 3], [kExprRefFunc, 0], [kExprRefFunc, 1], [kExprRefFunc, 2], [kExprRefFunc, 3], [kExprRefFunc, 0], [kExprRefFunc, 1], [kExprRefFunc, 2], [kExprRefFunc, 3], [kExprRefFunc, 0], [kExprRefFunc, 1], [kExprRefFunc, 2]], kWasmFuncRef);
builder.addActiveElementSegment(2, wasmI32Const(0), [[kExprRefFunc, 0]], kWasmFuncRef);
builder.addActiveElementSegment(3, wasmI32Const(0), [[kExprRefFunc, 0], [kExprRefFunc, 1], [kExprRefFunc, 2], [kExprRefFunc, 3], [kExprRefFunc, 0], [kExprRefFunc, 1], [kExprRefFunc, 2], [kExprRefFunc, 3], [kExprRefFunc, 0], [kExprRefFunc, 1], [kExprRefFunc, 2], [kExprRefFunc, 3], [kExprRefFunc, 0], [kExprRefFunc, 1], [kExprRefFunc, 2], [kExprRefFunc, 3]], kWasmFuncRef);
builder.addTag(makeSig([], []));
// Generate function 1 (out of 4).
builder.addFunction(undefined, 1 /* sig */)
......
......@@ -24,7 +24,7 @@ let builder1 = new WasmModuleBuilder();
builder1.setName('module_1');
builder1.addFunction('main', kSig_i_v).addBody([kExprUnreachable]);
builder1.addImportedTable('z', 'table');
builder1.addActiveElementSegment(0, WasmInitExpr.I32Const(0), [0]);
builder1.addActiveElementSegment(0, wasmI32Const(0), [0]);
let module1 = new WebAssembly.Module(builder1.toBuffer());
let instance1 =
new WebAssembly.Instance(module1, {z: {table: instance0.exports.table}});
......
......@@ -11,7 +11,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.addActiveElementSegment(0, WasmInitExpr.GlobalGet(q_base), [q_fun]);
builder.addActiveElementSegment(0, [kExprGlobalGet, q_base], [q_fun]);
let module = new WebAssembly.Module(builder.toBuffer());
let table = new WebAssembly.Table({
element: "anyfunc",
......
......@@ -19,7 +19,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.addActiveElementSegment(0, WasmInitExpr.GlobalGet(glob_import),
[mul_import]);
builder2.addActiveElementSegment(0, [kExprGlobalGet, glob_import], [mul_import]);
builder2.instantiate(
{q: {glob: 0, js_div: i => i, wasm_mul: mul, table: table}});
......@@ -33,7 +33,7 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
kExprCallIndirect, 0, kTableZero
])
.exportFunc();
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(0), [0, 1, 1, 0]);
builder.addActiveElementSegment(0, wasmI32Const(0), [0, 1, 1, 0]);
return builder.instantiate({q: {f2: i1.exports.f2, f1: i1.exports.f1}});
})();
......
......@@ -14,8 +14,8 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
let array_type_index = builder.addArray(struct_type, true);
function makeStruct(element) {
return WasmInitExpr.StructNew(
struct_type_index, [WasmInitExpr.I32Const(element)]);
return [...wasmI32Const(element),
kGCPrefix, kExprStructNew, struct_type_index];
}
builder.addTable(kWasmAnyRef, 10, 10);
......@@ -24,11 +24,11 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
let passive_segment = builder.addPassiveElementSegment(
[makeStruct(elems[0]), makeStruct(elems[1]),
WasmInitExpr.RefNull(struct_type_index)],
[kExprRefNull, struct_type_index]],
struct_type);
let active_segment = builder.addActiveElementSegment(
0, WasmInitExpr.I32Const(0), [makeStruct(elems[2]), makeStruct(elems[3])],
0, wasmI32Const(0), [makeStruct(elems[2]), makeStruct(elems[3])],
struct_type);
function generator(name, segment) {
......@@ -87,8 +87,8 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
let array_type = wasmOptRefType(array_type_index);
function makeStruct(element) {
return WasmInitExpr.StructNew(
struct_type_index, [WasmInitExpr.I32Const(element)]);
return [...wasmI32Const(element),
kGCPrefix, kExprStructNew, struct_type_index];
}
builder.addTable(kWasmAnyRef, 10, 10);
......@@ -98,22 +98,21 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
let passive_segment = builder.addPassiveElementSegment(
[makeStruct(elems[0]), makeStruct(elems[1]),
WasmInitExpr.RefNull(struct_type_index)],
[kExprRefNull, struct_type_index]],
struct_type);
let active_segment = builder.addActiveElementSegment(
0, WasmInitExpr.I32Const(0), [makeStruct(elems[2]), makeStruct(elems[3])],
0, wasmI32Const(0), [makeStruct(elems[2]), makeStruct(elems[3])],
struct_type);
let array_segment = builder.addPassiveElementSegment(
[WasmInitExpr.ArrayInitFromElemStatic(
array_type_index, passive_segment,
[WasmInitExpr.I32Const(0), WasmInitExpr.I32Const(3)]),
WasmInitExpr.ArrayInitFromElemStatic(
array_type_index, active_segment,
[WasmInitExpr.I32Const(0), WasmInitExpr.I32Const(0)])],
array_type
);
[[...wasmI32Const(0), ...wasmI32Const(3),
kGCPrefix, kExprArrayInitFromElemStatic,
array_type_index, passive_segment],
[...wasmI32Const(0), ...wasmI32Const(0),
kGCPrefix, kExprArrayInitFromElemStatic,
array_type_index, active_segment]],
array_type);
builder.addFunction("init", kSig_v_v)
.addBody([kExprI32Const, 0, kExprI32Const, 0, kExprI32Const, 2,
......@@ -168,7 +167,7 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
let array_type_index = builder.addArray(struct_type, true);
let passive_segment = builder.addPassiveElementSegment([
WasmInitExpr.RefNull(array_type_index)],
[kExprRefNull, array_type_index]],
wasmOptRefType(array_type_index));
builder.addFunction("mistyped", makeSig([kWasmI32, kWasmI32], [kWasmI32]))
......@@ -196,16 +195,14 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
let array_type_index = builder.addArray(struct_type, true);
let passive_segment = builder.addPassiveElementSegment([
WasmInitExpr.RefNull(struct_type_index)],
[kExprRefNull, struct_type_index]],
struct_type_index);
builder.addGlobal(wasmOptRefType(array_type_index), false,
WasmInitExpr.ArrayInitFromElemStatic(
array_type_index, passive_segment,
[WasmInitExpr.I32Const(0), // offset
WasmInitExpr.I32Const(1) // length
])
);
builder.addGlobal(
wasmOptRefType(array_type_index), false,
[...wasmI32Const(0), ...wasmI32Const(1),
kGCPrefix, kExprArrayInitFromElemStatic,
array_type_index, passive_segment]);
assertThrows(() => builder.instantiate(), WebAssembly.CompileError,
/invalid element segment index/);
......@@ -220,8 +217,8 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
let array_type = wasmOptRefType(array_type_index);
function makeStruct(element) {
return WasmInitExpr.StructNew(
struct_type_index, [WasmInitExpr.I32Const(element)]);
return [...wasmI32Const(element),
kGCPrefix, kExprStructNew, struct_type_index];
}
builder.addTable(kWasmAnyRef, 10, 10);
......@@ -231,13 +228,13 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
let passive_segment = builder.addPassiveElementSegment(
[makeStruct(elems[0]), makeStruct(elems[1]),
WasmInitExpr.RefNull(struct_type_index)],
[kExprRefNull, struct_type_index]],
struct_type);
let array_segment = builder.addPassiveElementSegment(
[WasmInitExpr.ArrayInitFromElemStatic(
array_type_index, passive_segment,
[WasmInitExpr.I32Const(0), WasmInitExpr.I32Const(1 << 30)])],
[[...wasmI32Const(0), ...wasmI32Const(1 << 30),
kGCPrefix, kExprArrayInitFromElemStatic,
array_type_index, passive_segment]],
array_type
);
......@@ -259,8 +256,8 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
let array_type = wasmOptRefType(array_type_index);
function makeStruct(element) {
return WasmInitExpr.StructNew(
struct_type_index, [WasmInitExpr.I32Const(element)]);
return [...wasmI32Const(element),
kGCPrefix, kExprStructNew, struct_type_index];
}
builder.addTable(kWasmAnyRef, 10, 10);
......@@ -270,13 +267,13 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
let passive_segment = builder.addPassiveElementSegment(
[makeStruct(elems[0]), makeStruct(elems[1]),
WasmInitExpr.RefNull(struct_type_index)],
[kExprRefNull, struct_type_index]],
struct_type);
let array_segment = builder.addPassiveElementSegment(
[WasmInitExpr.ArrayInitFromElemStatic(
array_type_index, passive_segment,
[WasmInitExpr.I32Const(0), WasmInitExpr.I32Const(10)])],
[[...wasmI32Const(0), ...wasmI32Const(10),
kGCPrefix, kExprArrayInitFromElemStatic,
array_type_index, passive_segment]],
array_type
);
......@@ -298,8 +295,8 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
let array_type = wasmOptRefType(array_type_index);
function makeStruct(element) {
return WasmInitExpr.StructNew(
struct_type_index, [WasmInitExpr.I32Const(element)]);
return [...wasmI32Const(element),
kGCPrefix, kExprStructNew, struct_type_index];
}
builder.addTable(kWasmAnyRef, 10, 10);
......@@ -308,15 +305,15 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
let elems = [10, -10];
let active_segment = builder.addActiveElementSegment(
table, WasmInitExpr.I32Const(0),
table, wasmI32Const(0),
[makeStruct(elems[0]), makeStruct(elems[1]),
WasmInitExpr.RefNull(struct_type_index)],
[kExprRefNull, struct_type_index]],
struct_type);
let array_segment = builder.addPassiveElementSegment(
[WasmInitExpr.ArrayInitFromElemStatic(
array_type_index, active_segment,
[WasmInitExpr.I32Const(0), WasmInitExpr.I32Const(3)])],
[[...wasmI32Const(0), ...wasmI32Const(3),
kGCPrefix, kExprArrayInitFromElemStatic,
array_type_index, active_segment]],
array_type
);
......
......@@ -158,7 +158,7 @@ function getMemoryFill(mem) {
(function TestElemDropActive() {
const builder = new WasmModuleBuilder();
builder.setTableBounds(5, 5);
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(0), [0, 0, 0]);
builder.addActiveElementSegment(0, wasmI32Const(0), [0, 0, 0]);
builder.addFunction('drop', kSig_v_v)
.addBody([
kNumericPrefix, kExprElemDrop,
......@@ -204,7 +204,7 @@ function getMemoryFill(mem) {
const tableIndex = 0;
builder.addActiveElementSegment(
tableIndex,
WasmInitExpr.I32Const(0),
wasmI32Const(0),
[f.index, f.index]);
builder.addDataSegment(0, [42]);
......
......@@ -220,7 +220,7 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
.exportAs("main");
builder.setTableBounds(kTableSize, kTableSize);
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(0), [f1.index]);
builder.addActiveElementSegment(0, wasmI32Const(0), [f1.index]);
builder.addExportOfKind("table", kExternalTable, 0);
return new WebAssembly.Module(builder.toBuffer());
......@@ -241,7 +241,7 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
.exportAs("main");
builder.addImportedTable("z", "table", kTableSize, kTableSize);
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(1), [f2.index]);
builder.addActiveElementSegment(0, wasmI32Const(1), [f2.index]);
var m2_bytes = builder.toBuffer();
var m2 = new WebAssembly.Module(m2_bytes);
......
......@@ -17,23 +17,17 @@ d8.file.execute('test/mjsunit/wasm/wasm-module-builder.js');
kExprLocalGet, 0, kExprI32Const, 1, kExprI32Add
]);
let global0 =
builder.addGlobal(kWasmFuncRef, false, WasmInitExpr.RefFunc(f0.index));
builder.addGlobal(kWasmFuncRef, false, [kExprRefFunc, f0.index]);
let global1 =
builder.addGlobal(kWasmFuncRef, false, WasmInitExpr.RefFunc(f1.index));
builder.addGlobal(kWasmFuncRef, false, [kExprRefFunc, f1.index]);
// At instantiation, table[0] = global0, table[1] = global1.
builder.addActiveElementSegment(
table.index, WasmInitExpr.I32Const(0),
[
WasmInitExpr.GlobalGet(global0.index),
WasmInitExpr.GlobalGet(global1.index)
],
table.index, wasmI32Const(0),
[[kExprGlobalGet, global0.index], [kExprGlobalGet, global1.index]],
kWasmFuncRef);
let passive = builder.addPassiveElementSegment(
[
WasmInitExpr.GlobalGet(global0.index),
WasmInitExpr.GlobalGet(global1.index)
],
[[kExprGlobalGet, global0.index], [kExprGlobalGet, global1.index]],
kWasmFuncRef);
// table[2] = global0, table[3] = global1.
......@@ -42,8 +36,7 @@ d8.file.execute('test/mjsunit/wasm/wasm-module-builder.js');
kExprI32Const, 2, // table index
kExprI32Const, 0, // element index
kExprI32Const, 2, // length
kNumericPrefix, kExprTableInit, passive, table.index
])
kNumericPrefix, kExprTableInit, passive, table.index])
.exportFunc();
let instance = builder.instantiate({});
......@@ -68,16 +61,16 @@ d8.file.execute('test/mjsunit/wasm/wasm-module-builder.js');
]);
let table =
builder.addTable(wasmRefType(sig), 10, 10, WasmInitExpr.RefFunc(f0.index))
builder.addTable(wasmRefType(sig), 10, 10, [kExprRefFunc, f0.index])
.exportAs('table');
builder.addActiveElementSegment(
table.index, WasmInitExpr.I32Const(0),
[WasmInitExpr.RefFunc(f0.index), WasmInitExpr.RefFunc(f1.index)],
table.index, wasmI32Const(0),
[[kExprRefFunc, f0.index], [kExprRefFunc, f1.index]],
wasmRefType(sig));
let passive = builder.addPassiveElementSegment(
[WasmInitExpr.RefFunc(f0.index), WasmInitExpr.RefFunc(f1.index)],
[[kExprRefFunc, f0.index], [kExprRefFunc, f1.index]],
wasmRefType(sig));
builder.addFunction('init', kSig_v_v)
......@@ -106,8 +99,8 @@ d8.file.execute('test/mjsunit/wasm/wasm-module-builder.js');
let global = builder.addImportedGlobal("m", "g", kWasmFuncRef, true);
let table = builder.addTable(kWasmFuncRef, 10, 10);
builder.addActiveElementSegment(
table.index, WasmInitExpr.I32Const(0),
[WasmInitExpr.GlobalGet(global.index)], kWasmFuncRef);
table.index, wasmI32Const(0),
[[kExprGlobalGet, global]], kWasmFuncRef);
builder.addExportOfKind("table", kExternalTable, table.index);
assertThrows(
......
......@@ -7,20 +7,20 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
(function exportImmutableGlobal() {
var builder = new WasmModuleBuilder();
let globals = [
[kWasmI32, 'i32', WasmInitExpr.I32Const(4711)],
[kWasmF32, 'f32', WasmInitExpr.F32Const(Math.fround(3.14))],
[kWasmF64, 'f64', WasmInitExpr.F64Const(1 / 7)]
[kWasmI32, 'i32', 4711, wasmI32Const(4711)],
[kWasmF32, 'f32', Math.fround(3.14), wasmF32Const(Math.fround(3.14))],
[kWasmF64, 'f64', 1/7, wasmF64Const(1 / 7)]
];
for (let [type, name, value] of globals) {
builder.addGlobal(type, false, value).exportAs(name);
for (let [type, name, value, bytes] of globals) {
builder.addGlobal(type, false, bytes).exportAs(name);
}
var instance = builder.instantiate();
for (let [type, name, value] of globals) {
for (let [type, name, value, bytes] of globals) {
let obj = instance.exports[name];
assertEquals("object", typeof obj, name);
assertTrue(obj instanceof WebAssembly.Global, name);
assertEquals(value.value || 0, obj.value, name);
assertEquals(value || 0, obj.value, name);
assertThrows(() => obj.value = 0);
}
})();
......@@ -44,12 +44,12 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
(function exportMutableGlobal() {
var builder = new WasmModuleBuilder();
let globals = [
[kWasmI32, 'i32', WasmInitExpr.I32Const(4711)], // -
[kWasmF32, 'f32', WasmInitExpr.F32Const(Math.fround(3.14))], // -
[kWasmF64, 'f64', WasmInitExpr.F64Const(1 / 7)] // -
[kWasmI32, 'i32', 4711, wasmI32Const(4711)],
[kWasmF32, 'f32', Math.fround(3.14), wasmF32Const(Math.fround(3.14))],
[kWasmF64, 'f64', 1/7, wasmF64Const(1 / 7)]
];
for (let [index, [type, name, value]] of globals.entries()) {
builder.addGlobal(type, true, value).exportAs(name);
for (let [index, [type, name, value, bytes]] of globals.entries()) {
builder.addGlobal(type, true, bytes).exportAs(name);
builder.addFunction("get " + name, makeSig([], [type]))
.addBody([kExprGlobalGet, index])
.exportFunc();
......@@ -59,10 +59,10 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
}
var instance = builder.instantiate();
for (let [type, name, value] of globals) {
for (let [type, name, value, bytes] of globals) {
let obj = instance.exports[name];
assertEquals(value.value || 0, obj.value, name);
assertEquals(value || 0, obj.value, name);
// Changing the exported global should change the instance's global.
obj.value = 1001;
......
......@@ -16,13 +16,9 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
let defined_global = builder.addGlobal(
kWasmI32, false,
WasmInitExpr.I32Add(
WasmInitExpr.GlobalGet(imported_global_0),
WasmInitExpr.I32Mul(
WasmInitExpr.GlobalGet(imported_global_1),
WasmInitExpr.I32Sub(
WasmInitExpr.GlobalGet(imported_global_0),
WasmInitExpr.I32Const(1)))));
[kExprGlobalGet, imported_global_0, kExprGlobalGet, imported_global_1,
kExprGlobalGet, imported_global_0, ...wasmI32Const(1),
kExprI32Sub, kExprI32Mul, kExprI32Add]);
builder.addExportOfKind("global", kExternalGlobal, defined_global.index);
......@@ -49,13 +45,9 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
let defined_global = builder.addGlobal(
kWasmI64, false,
WasmInitExpr.I64Add(
WasmInitExpr.GlobalGet(imported_global_0),
WasmInitExpr.I64Mul(
WasmInitExpr.GlobalGet(imported_global_1),
WasmInitExpr.I64Sub(
WasmInitExpr.GlobalGet(imported_global_0),
WasmInitExpr.I64Const(1)))));
[kExprGlobalGet, imported_global_0, kExprI64Const, 1, kExprI64Sub,
kExprGlobalGet, imported_global_1, kExprI64Mul,
kExprGlobalGet, imported_global_0, kExprI64Add]);
builder.addExportOfKind("global", kExternalGlobal, defined_global.index);
......
......@@ -549,7 +549,7 @@ function dummy_func() {
const f_func = builder.addFunction('get_anyfunc_global', kSig_a_v)
builder.addDeclarativeElementSegment([f_func.index]);
const g_func = builder.addGlobal(kWasmAnyFunc, true,
WasmInitExpr.RefFunc(f_func.index));
[kExprRefFunc, f_func.index]);
// Doing this here to break the cyclic dependency with g_func.
f_func.addBody([kExprGlobalGet, g_func.index])
.exportAs('get_anyfunc_global');
......@@ -567,9 +567,9 @@ function dummy_func() {
const import_wasm = builder.addImport('m', 'wasm', sig_index);
const import_js = builder.addImport('m', 'js', sig_index);
const g_wasm = builder.addGlobal(kWasmAnyFunc, true,
WasmInitExpr.RefFunc(import_wasm));
[kExprRefFunc, import_wasm]);
const g_js = builder.addGlobal(kWasmAnyFunc, true,
WasmInitExpr.RefFunc(import_js));
[kExprRefFunc, import_js]);
builder.addDeclarativeElementSegment([import_wasm, import_js]);
builder.addFunction('get_global_wasm', kSig_a_v)
.addBody([kExprGlobalGet, g_wasm.index])
......
......@@ -53,7 +53,7 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
print(arguments.callee.name);
const builder = new WasmModuleBuilder();
builder.addDeclarativeElementSegment([WasmInitExpr.RefNull(kWasmFuncRef)],
builder.addDeclarativeElementSegment([[kExprRefNull, kFuncRefCode]],
kWasmFuncRef);
builder.addFunction('drop', kSig_v_v)
.addBody([kNumericPrefix, kExprElemDrop, 0])
......@@ -70,7 +70,7 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
const builder = new WasmModuleBuilder();
const table = builder.addTable(kWasmAnyFunc, 10);
builder.addDeclarativeElementSegment([WasmInitExpr.RefNull(kWasmFuncRef)],
builder.addDeclarativeElementSegment([[kExprRefNull, kFuncRefCode]],
kWasmFuncRef);
builder.addFunction('init', kSig_v_v)
.addBody([
......@@ -158,7 +158,7 @@ function getDummy(val) {
let imported_global = builder.addImportedGlobal('m', 'n', extern_type, false);
let global = builder.addGlobal(kWasmExternRef, true).exportAs('global');
let table = builder.addTable(extern_type, 2, 10,
WasmInitExpr.GlobalGet(imported_global))
[kExprGlobalGet, imported_global])
builder.addFunction(
'setup', makeSig([extern_type, extern_type], []))
.addBody([
......
......@@ -58,6 +58,7 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
print(arguments.callee.name);
let builder = new WasmModuleBuilder();
builder.setNominal();
builder.setEarlyDataCountSection();
let array_type_index = builder.addArray(kWasmI16, true);
let dummy_byte = 0xff;
......@@ -70,9 +71,9 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
let global = builder.addGlobal(
wasmRefType(array_type_index), true,
WasmInitExpr.ArrayInitFromDataStatic(
array_type_index, data_segment,
[WasmInitExpr.I32Const(1), WasmInitExpr.I32Const(2)], builder));
[...wasmI32Const(1), ...wasmI32Const(2),
kGCPrefix, kExprArrayInitFromDataStatic, array_type_index, data_segment],
builder);
builder.addFunction("global_get", kSig_i_i)
.addBody([
......@@ -117,6 +118,7 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
print(arguments.callee.name);
let builder = new WasmModuleBuilder();
builder.setNominal();
builder.setEarlyDataCountSection();
let array_type_index = builder.addArray(kWasmI16, true);
let dummy_byte = 0xff;
......@@ -129,11 +131,10 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
let global = builder.addGlobal(
wasmRefType(array_type_index), true,
WasmInitExpr.ArrayInitFromData(
array_type_index, data_segment,
[WasmInitExpr.I32Const(1), WasmInitExpr.I32Const(2),
WasmInitExpr.RttCanon(array_type_index)],
builder));
[...wasmI32Const(1), ...wasmI32Const(2),
kGCPrefix, kExprRttCanon, array_type_index,
kGCPrefix, kExprArrayInitFromData, array_type_index, data_segment],
builder);
builder.addFunction("global_get", kSig_i_i)
.addBody([
......
......@@ -385,7 +385,7 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
]);
let global = builder.addGlobal(
wasmOptRefType(struct_2), true, WasmInitExpr.RefNull(struct_2));
wasmOptRefType(struct_2), true, [kExprRefNull, struct_2]);
// The three alocations should be folded.
builder.addFunction("main", kSig_i_i)
......
......@@ -101,16 +101,15 @@ function TestExported(type, val, expected) {
assertEquals(expected, instance.exports.foo.value);
}
TestExported(kWasmI32, WasmInitExpr.I32Const(455.5), 455);
TestExported(kWasmF32, WasmInitExpr.F32Const(-999.34343),
TestExported(kWasmI32, wasmI32Const(455.5), 455);
TestExported(kWasmF32, wasmF32Const(-999.34343),
Math.fround(-999.34343));
TestExported(kWasmF64, WasmInitExpr.F64Const(87347.66666), 87347.66666);
TestExported(kWasmF64, wasmF64Const(87347.66666), 87347.66666);
(function TestI64Exported() {
var builder = new WasmModuleBuilder();
builder.addGlobal(kWasmI32); // pad
builder.addGlobal(kWasmI64, false, WasmInitExpr.I64Const(1234))
.exportAs("foo");
builder.addGlobal(kWasmI64, false, wasmI64Const(1234)).exportAs("foo");
builder.addGlobal(kWasmI32); // pad
var instance = builder.instantiate();
......@@ -123,8 +122,7 @@ function TestImportedExported(type, val, expected) {
var builder = new WasmModuleBuilder();
var i = builder.addImportedGlobal("ttt", "foo", type);
builder.addGlobal(kWasmI32); // pad
builder.addGlobal(type, false, WasmInitExpr.GlobalGet(i))
.exportAs("bar");
builder.addGlobal(type, false, [kExprGlobalGet, i]).exportAs("bar");
builder.addGlobal(kWasmI32); // pad
var instance = builder.instantiate({ttt: {foo: val}});
......@@ -140,7 +138,7 @@ function TestGlobalIndexSpace(type, val) {
var builder = new WasmModuleBuilder();
var im = builder.addImportedGlobal("nnn", "foo", type);
assertEquals(0, im);
var def = builder.addGlobal(type, false, WasmInitExpr.GlobalGet(im));
var def = builder.addGlobal(type, false, [kExprGlobalGet, im]);
assertEquals(1, def.index);
var sig = makeSig([], [type]);
......
......@@ -41,8 +41,8 @@ let kTableSize = 50;
let f17 = addConstFunc(builder, 17);
builder.addExport("f15", f15);
builder.addExport("f17", f17);
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(15), [f15]);
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(1), [call.index]);
builder.addActiveElementSegment(0, wasmI32Const(15), [f15]);
builder.addActiveElementSegment(0, wasmI32Const(1), [call.index]);
var mod1 = builder.toModule();
}
......@@ -62,10 +62,10 @@ let kTableSize = 50;
])
.exportAs("call");
let f26 = addConstFunc(builder, 26);
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(17), [f17]);
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(21), [f21]);
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(26), [f26]);
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(5), [call.index]);
builder.addActiveElementSegment(0, wasmI32Const(17), [f17]);
builder.addActiveElementSegment(0, wasmI32Const(21), [f21]);
builder.addActiveElementSegment(0, wasmI32Const(26), [f26]);
builder.addActiveElementSegment(0, wasmI32Const(5), [call.index]);
var mod2 = builder.toModule();
}
......@@ -89,7 +89,7 @@ let kTableSize = 50;
})();
function addConstFuncUsingGlobal(builder, val) {
let g = builder.addGlobal(kWasmI32, false, WasmInitExpr.I32Const(val));
let g = builder.addGlobal(kWasmI32, false, wasmI32Const(val));
return builder.addFunction("global" + val, kSig_i_v)
.addBody([kExprGlobalGet, g.index]).index;
}
......@@ -112,8 +112,8 @@ function addConstFuncUsingGlobal(builder, val) {
let f18 = addConstFuncUsingGlobal(builder, 18);
builder.addExport("f14", f14);
builder.addExport("f18", f18);
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(14), [f14]);
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(1), [call.index]);
builder.addActiveElementSegment(0, wasmI32Const(14), [f14]);
builder.addActiveElementSegment(0, wasmI32Const(1), [call.index]);
var mod1 = builder.toModule();
}
......@@ -133,10 +133,10 @@ function addConstFuncUsingGlobal(builder, val) {
])
.exportAs("call");
let f28 = addConstFuncUsingGlobal(builder, 28);
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(18), [f18]);
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(22), [f22]);
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(28), [f28]);
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(5), [call.index]);
builder.addActiveElementSegment(0, wasmI32Const(18), [f18]);
builder.addActiveElementSegment(0, wasmI32Const(22), [f22]);
builder.addActiveElementSegment(0, wasmI32Const(28), [f28]);
builder.addActiveElementSegment(0, wasmI32Const(5), [call.index]);
var mod2 = builder.toModule();
}
......@@ -192,8 +192,8 @@ function addConstFuncUsingMemory(builder, val) {
let f19 = addConstFuncUsingMemory(builder, 19);
builder.addExport("f13", f13);
builder.addExport("f19", f19);
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(13), [f13]);
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(1), [call.index]);
builder.addActiveElementSegment(0, wasmI32Const(13), [f13]);
builder.addActiveElementSegment(0, wasmI32Const(1), [call.index]);
var mod1 = builder.toModule();
}
......@@ -215,10 +215,10 @@ function addConstFuncUsingMemory(builder, val) {
])
.exportAs("call");
let f29 = addConstFuncUsingMemory(builder, 29);
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(19), [f19]);
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(23), [f23]);
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(29), [f29]);
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(5), [call.index]);
builder.addActiveElementSegment(0, wasmI32Const(19), [f19]);
builder.addActiveElementSegment(0, wasmI32Const(23), [f23]);
builder.addActiveElementSegment(0, wasmI32Const(29), [f29]);
builder.addActiveElementSegment(0, wasmI32Const(5), [call.index]);
var mod2 = builder.toModule();
}
......
......@@ -67,12 +67,12 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
.exportAs('return_call_invalid_sig');
// We want to crash if we call through the table with index 0.
builder.addActiveElementSegment(placeholder, WasmInitExpr.I32Const(0),
builder.addActiveElementSegment(placeholder, wasmI32Const(0),
[f_unreachable, f_unreachable, f_unreachable]);
builder.addActiveElementSegment(table1, WasmInitExpr.I32Const(0),
builder.addActiveElementSegment(table1, wasmI32Const(0),
[f1, f2, f3]);
// Keep one slot in table2 uninitialized. We should trap if we call it.
builder.addActiveElementSegment(table2, WasmInitExpr.I32Const(1),
builder.addActiveElementSegment(table2, wasmI32Const(1),
[f_unreachable, f_unreachable, f4, f5]);
const instance = builder.instantiate();
......@@ -127,7 +127,7 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
kExprCallIndirect, sig_index, t1])
.exportAs('call');
builder.addActiveElementSegment(t1, WasmInitExpr.GlobalGet(g), [f1.index]);
builder.addActiveElementSegment(t1, [kExprGlobalGet, g], [f1.index]);
const base1 = 3;
const base2 = 5;
......@@ -171,8 +171,7 @@ function js_div(a, b) { return (a / b) | 0; }
builder.addFunction("placeholder", sig_index)
.addBody([kExprLocalGet, 0]);
builder.addActiveElementSegment(table_index, WasmInitExpr.GlobalGet(g),
[div]);
builder.addActiveElementSegment(table_index, [kExprGlobalGet, g], [div]);
builder.addFunction("main", kSig_i_ii)
.addBody([
kExprI32Const, 55, // --
......
......@@ -169,7 +169,7 @@ function AddFunctions(builder) {
.exportAs("main");
builder.setTableBounds(length, length);
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(base),
builder.addActiveElementSegment(0, wasmI32Const(base),
[f.add.index, f.sub.index, f.mul.index]);
return builder.instantiate();
......@@ -208,7 +208,7 @@ function AddFunctions(builder) {
builder.setTableBounds(10, 10);
var g = builder.addImportedGlobal("fff", "base", kWasmI32);
builder.addActiveElementSegment(0, WasmInitExpr.GlobalGet(g),
builder.addActiveElementSegment(0, [kExprGlobalGet, g],
[f.mul.index, f.add.index, f.sub.index]);
var module = new WebAssembly.Module(builder.toBuffer());
......
......@@ -50,7 +50,7 @@ function js_div(a, b) { return (a / b) | 0; }
builder.setTableBounds(10, 10);
let g = builder.addImportedGlobal("q", "base", kWasmI32);
builder.addActiveElementSegment(0, WasmInitExpr.GlobalGet(g),
builder.addActiveElementSegment(0, [kExprGlobalGet, g],
[f.mul.index, f.add.index, f.sub.index, d]);
builder.addExportOfKind("table", kExternalTable, 0);
......@@ -109,7 +109,7 @@ function js_div(a, b) { return (a / b) | 0; }
let f = AddFunctions(builder);
builder.setTableBounds(kTableSize, kTableSize);
let g = builder.addImportedGlobal("q", "base", kWasmI32);
builder.addActiveElementSegment(0, WasmInitExpr.GlobalGet(g),
builder.addActiveElementSegment(0, [kExprGlobalGet, g],
[f.mul.index, f.add.index, f.sub.index, d]);
builder.addExportOfKind("table", kExternalTable, 0);
......@@ -179,7 +179,7 @@ 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.addActiveElementSegment(0, WasmInitExpr.GlobalGet(g),
builder.addActiveElementSegment(0, [kExprGlobalGet, g],
[f.mul.index, f.add.index, f.sub.index, d]);
builder.addFunction("main", kSig_i_ii)
.addBody([
......@@ -256,7 +256,7 @@ function js_div(a, b) { return (a / b) | 0; }
kExprLocalGet, 0,
kExprCallIndirect, sig_index, kTableZero]) // --
.exportAs("main");
builder.addActiveElementSegment(0, WasmInitExpr.GlobalGet(g), [f.index]);
builder.addActiveElementSegment(0, [kExprGlobalGet, g], [f.index]);
let module = new WebAssembly.Module(builder.toBuffer());
......@@ -297,7 +297,7 @@ function js_div(a, b) { return (a / b) | 0; }
.exportAs("main");
builder.setTableBounds(kTableSize, kTableSize);
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(0), [f1.index]);
builder.addActiveElementSegment(0, wasmI32Const(0), [f1.index]);
builder.addExportOfKind("table", kExternalTable, 0);
var m1 = new WebAssembly.Module(builder.toBuffer());
......@@ -316,7 +316,7 @@ function js_div(a, b) { return (a / b) | 0; }
.exportAs("main");
builder.addImportedTable("z", "table", kTableSize, kTableSize);
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(1), [f2.index]);
builder.addActiveElementSegment(0, wasmI32Const(1), [f2.index]);
var m2 = new WebAssembly.Module(builder.toBuffer());
......@@ -411,7 +411,7 @@ function js_div(a, b) { return (a / b) | 0; }
kExprLocalGet, 0,
kExprCallIndirect, sig_index, kTableZero]) // --
.exportAs("main");
builder.addActiveElementSegment(0, WasmInitExpr.GlobalGet(g), [g]);
builder.addActiveElementSegment(0, [kExprGlobalGet, g], [g]);
let module = new WebAssembly.Module(builder.toBuffer());
var instances = [];
......@@ -542,7 +542,7 @@ function js_div(a, b) { return (a / b) | 0; }
let offset = i + 1;
let len = i + 2;
let index = indexes[i];
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(offset),
builder.addActiveElementSegment(0, wasmI32Const(offset),
new Array(len).fill(index));
}
......@@ -587,7 +587,7 @@ function js_div(a, b) { return (a / b) | 0; }
builder1.setName('module_1');
builder1.addFunction('f', kSig_i_i).addBody([kExprLocalGet, 0]);
builder1.addImportedTable('z', 'table');
builder1.addActiveElementSegment(0, WasmInitExpr.I32Const(0), [0]);
builder1.addActiveElementSegment(0, wasmI32Const(0), [0]);
let module1 = new WebAssembly.Module(builder1.toBuffer());
let instance1 =
new WebAssembly.Instance(module1, {z: {table: instance0.exports.table}});
......@@ -626,7 +626,7 @@ function js_div(a, b) { return (a / b) | 0; }
builder.addFunction('main', kSig_i_v)
.addBody([kExprI32Const, 0, kExprI32LoadMem, 0, 0]);
builder.addImportedTable('', 'table');
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(0), [0]);
builder.addActiveElementSegment(0, wasmI32Const(0), [0]);
builder.addImportedMemory('', 'memory', 1);
......@@ -734,7 +734,7 @@ function js_div(a, b) { return (a / b) | 0; }
builder.setTableBounds(10, 10);
let g = builder.addImportedGlobal("q", "base", kWasmI32);
builder.addActiveElementSegment(0, WasmInitExpr.GlobalGet(g), [j, w]);
builder.addActiveElementSegment(0, [kExprGlobalGet, g], [j, w]);
let module = new WebAssembly.Module(builder.toBuffer());
for (var i = 0; i < 5; i++) {
......@@ -783,7 +783,7 @@ function js_div(a, b) { return (a / b) | 0; }
.exportAs("main");
let g = builder.addImportedGlobal("q", "base", kWasmI32);
builder.addActiveElementSegment(0, WasmInitExpr.GlobalGet(g), [j, w]);
builder.addActiveElementSegment(0, [kExprGlobalGet, g], [j, w]);
let module = new WebAssembly.Module(builder.toBuffer());
for (var i = 0; i < 5; i++) {
......@@ -824,7 +824,7 @@ function js_div(a, b) { return (a / b) | 0; }
kExprCallIndirect, 0, kTableZero
])
.exportFunc();
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(0), [0, 1, 1, 0]);
builder.addActiveElementSegment(0, wasmI32Const(0), [0, 1, 1, 0]);
return builder.instantiate({q: {f2: i1.exports.f2, f1: i1.exports.f1}});
})();
......@@ -887,7 +887,7 @@ function js_div(a, b) { return (a / b) | 0; }
])
.exportFunc();
builder.exportMemoryAs("memory");
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(0), [0, 1, 2, 3]);
builder.addActiveElementSegment(0, wasmI32Const(0), [0, 1, 2, 3]);
var instance = builder.instantiate({q: {f1: f100, f2: f200, f3: f300}});
setMemI32(instance, 0, 5000000);
setMemI32(instance, 4, 6000000);
......@@ -905,11 +905,11 @@ function js_div(a, b) { return (a / b) | 0; }
print(arguments.callee.name);
let builder = new WasmModuleBuilder();
let global = builder.addGlobal(kWasmFuncRef, false,
WasmInitExpr.RefNull(kWasmFuncRef));
[kExprRefNull, kFuncRefCode]);
let table = builder.addTable(kWasmFuncRef, 10, 10);
builder.addActiveElementSegment(
table.index, WasmInitExpr.I32Const(0),
[WasmInitExpr.GlobalGet(global.index)], kWasmFuncRef);
table.index, wasmI32Const(0),
[[kExprGlobalGet, global.index]], kWasmFuncRef);
builder.addExportOfKind("table", kExternalTable, table.index);
assertThrows(
......
......@@ -32,7 +32,7 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
const f2 = builder_for_import.addFunction('f2', kSig_i_v)
.addBody([kExprI32Const, value2]).index;
builder_for_import.addActiveElementSegment(t1, WasmInitExpr.I32Const(offset1),
builder_for_import.addActiveElementSegment(t1, wasmI32Const(offset1),
[f1, f2]);
const instance_for_import = builder_for_import.instantiate();
const table1 = instance_for_import.exports.table;
......@@ -56,11 +56,11 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
.addBody([kExprI32Const, value5]).index;
builder.addActiveElementSegment(t2, WasmInitExpr.I32Const(offset2), [f3, f4]);
builder.addActiveElementSegment(t3, WasmInitExpr.I32Const(offset3), [f5, f4]);
builder.addActiveElementSegment(t4, WasmInitExpr.I32Const(offset4), [f3, f5]);
builder.addActiveElementSegment(t2, wasmI32Const(offset2), [f3, f4]);
builder.addActiveElementSegment(t3, wasmI32Const(offset3), [f5, f4]);
builder.addActiveElementSegment(t4, wasmI32Const(offset4), [f3, f5]);
// Add one more overlapping offset
builder.addActiveElementSegment(t4, WasmInitExpr.I32Const(offset4 + 1),
builder.addActiveElementSegment(t4, wasmI32Const(offset4 + 1),
[f4, f3]);
const instance = builder.instantiate(instance_for_import);
......
......@@ -20,7 +20,7 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
.exportFunc();
builder.addGlobal(wasmRefType(sig_index), false,
WasmInitExpr.RefFunc(addition_index))
[kExprRefFunc, addition_index.index])
.exportAs("global");
builder.addGlobal(wasmOptRefType(wrong_sig_index), false)
.exportAs("mistyped_global");
......@@ -122,25 +122,21 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
var global0 = builder.addGlobal(
wasmRefType(struct_index), false,
WasmInitExpr.StructNewWithRtt(
struct_index,
[WasmInitExpr.I32Const(field2_value),
WasmInitExpr.RttCanon(struct_index)]));
[...wasmI32Const(field2_value),
kGCPrefix, kExprRttCanon, struct_index,
kGCPrefix, kExprStructNewWithRtt, struct_index]);
var global = builder.addGlobal(
wasmRefType(composite_struct_index), false,
WasmInitExpr.StructNewWithRtt(
composite_struct_index,
[WasmInitExpr.I32Const(field1_value),
WasmInitExpr.GlobalGet(global0.index),
WasmInitExpr.I32Const(field3_value),
WasmInitExpr.RttCanon(composite_struct_index)]));
[...wasmI32Const(field1_value), kExprGlobalGet, global0.index,
...wasmI32Const(field3_value),
kGCPrefix, kExprRttCanon, composite_struct_index,
kGCPrefix, kExprStructNewWithRtt, composite_struct_index]);
var global_default = builder.addGlobal(
wasmRefType(composite_struct_index), false,
WasmInitExpr.StructNewDefaultWithRtt(
composite_struct_index,
WasmInitExpr.RttCanon(composite_struct_index)));
[kGCPrefix, kExprRttCanon, composite_struct_index,
kGCPrefix, kExprStructNewDefaultWithRtt, composite_struct_index]);
builder.addFunction("field_1", kSig_i_v)
.addBody([
......@@ -202,15 +198,13 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
var global0 = builder.addGlobal(
kWasmI32, false,
WasmInitExpr.I32Const(element0_value));
wasmI32Const(element0_value));
var global = builder.addGlobal(
wasmRefType(array_index), false,
WasmInitExpr.ArrayInit(
array_index,
[WasmInitExpr.GlobalGet(global0.index),
WasmInitExpr.I32Const(element1_value),
WasmInitExpr.RttCanon(array_index)]));
[kExprGlobalGet, global0.index, ...wasmI32Const(element1_value),
kGCPrefix, kExprRttCanon, array_index,
kGCPrefix, kExprArrayInit, array_index, 2]);
builder.addFunction("get_element", kSig_i_i)
.addBody([
......@@ -237,22 +231,17 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
var global0 = builder.addGlobal(
wasmRefType(struct_index), false,
WasmInitExpr.StructNewWithRtt(
struct_index,
[WasmInitExpr.I32Const(element0_value),
WasmInitExpr.RttCanon(struct_index)]));
[...wasmI32Const(element0_value), kGCPrefix, kExprRttCanon, struct_index,
kGCPrefix, kExprStructNewWithRtt, struct_index]);
var global = builder.addGlobal(
wasmRefType(array_index), false,
WasmInitExpr.ArrayInit(
array_index,
[WasmInitExpr.GlobalGet(global0.index),
WasmInitExpr.RefNull(struct_index),
WasmInitExpr.StructNewWithRtt(
struct_index,
[WasmInitExpr.I32Const(element2_value),
WasmInitExpr.RttCanon(struct_index)]),
WasmInitExpr.RttCanon(array_index)]));
[kExprGlobalGet, global0.index, kExprRefNull, struct_index,
...wasmI32Const(element2_value),
kGCPrefix, kExprRttCanon, struct_index,
kGCPrefix, kExprStructNewWithRtt, struct_index,
kGCPrefix, kExprRttCanon, array_index,
kGCPrefix, kExprArrayInit, array_index, 3]);
builder.addFunction("element0", kSig_i_v)
.addBody([
......@@ -294,8 +283,9 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
let global = builder.addGlobal(
wasmRefType(array_index), true,
WasmInitExpr.ArrayInitStatic(array_index, values.map(
value => WasmInitExpr.I31New(WasmInitExpr.I32Const(value)))));
[...values.flatMap(
value => [...wasmI32Const(value), kGCPrefix, kExprI31New]),
kGCPrefix, kExprArrayInitStatic, array_index, 5]);
for (signed of [true, false]) {
builder.addFunction(`get_${signed ? "s" : "u"}`, kSig_i_i)
......@@ -325,7 +315,7 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
let builder = new WasmModuleBuilder();
builder.addGlobal(kWasmI31Ref, false,
WasmInitExpr.I31New(WasmInitExpr.I64Const(0)));
[...wasmI64Const(0), kGCPrefix, kExprI31New]);
assertThrows(() => builder.instantiate(), WebAssembly.CompileError,
/i31.new\[0\] expected type i32, found i64.const of type i64/);
......
......@@ -117,7 +117,7 @@ d8.file.execute('test/mjsunit/wasm/wasm-module-builder.js');
.exportFunc();
var table = builder.addTable(wasmRefType(binary_type), 3, 3,
WasmInitExpr.RefFunc(addition.index));
[kExprRefFunc, addition.index]);
builder.addFunction('init', kSig_v_v)
.addBody([
......@@ -159,11 +159,11 @@ d8.file.execute('test/mjsunit/wasm/wasm-module-builder.js');
let table = builder.addTable(kWasmAnyRef, 4, 4);
builder.addActiveElementSegment(
table, WasmInitExpr.I32Const(0),
[WasmInitExpr.RefFunc(successor.index),
WasmInitExpr.RefFunc(subtraction.index),
WasmInitExpr.StructNew(struct_type, [WasmInitExpr.I32Const(10)]),
WasmInitExpr.RefNull(kWasmEqRef)],
table, wasmI32Const(0),
[[kExprRefFunc, successor.index],
[kExprRefFunc, subtraction.index],
[...wasmI32Const(10), kGCPrefix, kExprStructNew, struct_type],
[kExprRefNull, kEqRefCode]],
kWasmAnyRef);
// return static_cast<i->i>(table[0])(local_0)
......
......@@ -140,7 +140,7 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
// Arbitrary location in the table.
const tableIndex = 3;
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(tableIndex),[pick]);
builder.addActiveElementSegment(0, wasmI32Const(tableIndex),[pick]);
let main = builder.addFunction("main", kSig_i_iii)
.addBody([
......
......@@ -12,8 +12,9 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
(function TestS128GlobalInitialization() {
var builder = new WasmModuleBuilder();
var g = builder.addGlobal(
kWasmS128, false, WasmInitExpr.S128Const(
[1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]));
kWasmS128, false,
[kSimdPrefix, kExprS128Const,
1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]);
// Check that all lanes have the right values by creating 4 functions that
// extract each lane.
......
......@@ -23,7 +23,7 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
.addBody([kExprLocalGet, 0, kExprI32Const, 1, kExprI32Sub]);
let global = builder.addGlobal(wasmRefType(0), false,
WasmInitExpr.RefFunc(callee.index));
[kExprRefFunc, callee.index]);
// g(x) = f(5) + x
builder.addFunction("main", kSig_i_i)
......@@ -55,9 +55,9 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
.addBody([kExprLocalGet, 0, kExprI32Const, 2, kExprI32Sub]);
let global0 = builder.addGlobal(wasmRefType(sig_index), false,
WasmInitExpr.RefFunc(callee0.index));
[kExprRefFunc, callee0.index]);
let global1 = builder.addGlobal(wasmRefType(sig_index), false,
WasmInitExpr.RefFunc(callee1.index));
[kExprRefFunc, callee1.index]);
// g(x, y) = if (y) { h(5) + x } else { f(7) + x }
builder.addFunction("main", kSig_i_ii)
......@@ -95,7 +95,7 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
.addBody([kExprLocalGet, 0, kExprI32Const, 1, kExprI32Sub]);
let global = builder.addGlobal(wasmRefType(0), false,
WasmInitExpr.RefFunc(callee.index));
[kExprRefFunc, callee.index]);
// g(x) = f(5 + x)
builder.addFunction("main", kSig_i_i)
......@@ -127,9 +127,9 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
.addBody([kExprLocalGet, 0, kExprI32Const, 2, kExprI32Sub]);
let global0 = builder.addGlobal(wasmRefType(sig_index), false,
WasmInitExpr.RefFunc(callee0.index));
[kExprRefFunc, callee0.index]);
let global1 = builder.addGlobal(wasmRefType(sig_index), false,
WasmInitExpr.RefFunc(callee1.index));
[kExprRefFunc, callee1.index]);
// g(x, y) = if (y) { h(x) } else { f(x) }
builder.addFunction("main", kSig_i_ii)
......
......@@ -154,11 +154,10 @@ function makeWtf16TestDataSegment() {
builder.addFunction("string_const" + index, kSig_w_v)
.exportFunc()
.addBody([
kGCPrefix, kExprStringConst, index
]);
.addBody([kGCPrefix, kExprStringConst, index]);
builder.addGlobal(kWasmStringRef, false, WasmInitExpr.StringConst(index))
builder.addGlobal(kWasmStringRef, false,
[kGCPrefix, kExprStringConst, index])
.exportAs("global" + index);
}
......
......@@ -17,12 +17,12 @@ let enableMessage = 'enable with --experimental-wasm-stringref'
assertInvalid(builder => builder.addLiteralStringRef("foo"),
`unexpected section <StringRef> (${enableMessage}) @+10`);
for (let [name, code] of [['string', kWasmStringRef],
['stringview_wtf8', kWasmStringViewWtf8],
['stringview_wtf16', kWasmStringViewWtf16],
['stringview_iter', kWasmStringViewIter]]) {
for (let [name, code] of [['string', kStringRefCode],
['stringview_wtf8', kStringViewWtf8Code],
['stringview_wtf16', kStringViewWtf16Code],
['stringview_iter', kStringViewIterCode]]) {
let message = `invalid value type '${name}ref', ${enableMessage}`;
let default_init = WasmInitExpr.RefNull(code);
let default_init = [kExprRefNull, code];
assertInvalid(b => b.addType(makeSig([code], [])),
`${message} @+13`);
......
......@@ -23,11 +23,11 @@ function assertInvalid(fn, message) {
assertValid(builder => builder.addLiteralStringRef("foo"));
for (let [name, code] of [['string', kWasmStringRef],
['stringview_wtf8', kWasmStringViewWtf8],
['stringview_wtf16', kWasmStringViewWtf16],
['stringview_iter', kWasmStringViewIter]]) {
let default_init = WasmInitExpr.RefNull(code);
for (let [name, code] of [['string', kStringRefCode],
['stringview_wtf8', kStringViewWtf8Code],
['stringview_wtf16', kStringViewWtf16Code],
['stringview_iter', kStringViewIterCode]]) {
let default_init = [kExprRefNull, code];
assertValid(b => b.addType(makeSig([code], [])));
assertValid(b => b.addStruct([makeField(code, true)]));
......
......@@ -117,9 +117,9 @@ const dummy_func = exports.set_table_func1;
const offset1 = 3;
const offset2 = 9;
builder.addActiveElementSegment(t1, WasmInitExpr.I32Const(offset1),
builder.addActiveElementSegment(t1, wasmI32Const(offset1),
[f1.index, f2.index]);
builder.addActiveElementSegment(t2, WasmInitExpr.I32Const(offset2),
builder.addActiveElementSegment(t2, wasmI32Const(offset2),
[f3.index, f1.index]);
const instance = builder.instantiate();
......
......@@ -11,7 +11,7 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
const f1 = builder.addFunction('f', kSig_i_v).addBody([kExprI32Const, 11]);
const f2 = builder.addFunction('f', kSig_i_v).addBody([kExprI32Const, 22]);
const offset = 3;
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(offset),
builder.addActiveElementSegment(0, wasmI32Const(offset),
[f1.index, f2.index]);
const instance = builder.instantiate();
......@@ -28,7 +28,7 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
const f2 = builder.addFunction('f', kSig_i_v).addBody([kExprI32Const, 22])
.exportAs("f2");
const offset = 3;
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(offset),
builder.addActiveElementSegment(0, wasmI32Const(offset),
[f1.index, f2.index]);
const instance = builder.instantiate();
......@@ -44,9 +44,9 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
const f1 = builder.addFunction('f', kSig_i_v).addBody([kExprI32Const, 11]);
const f2 = builder.addFunction('f', kSig_i_v).addBody([kExprI32Const, 22]);
const offset = 3;
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(offset),
builder.addActiveElementSegment(0, wasmI32Const(offset),
[f1.index, f2.index]);
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(offset + 1),
builder.addActiveElementSegment(0, wasmI32Const(offset + 1),
[f1.index, f2.index]);
const instance = builder.instantiate();
......@@ -63,7 +63,7 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
.addBody([kExprI32Const, 11]).exportAs("f1");
const f2 = builder.addFunction('f', kSig_i_v).addBody([kExprI32Const, 22]);
const offset = 3;
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(offset),
builder.addActiveElementSegment(0, wasmI32Const(offset),
[f1.index, f1.index, f1.index]);
const instance = builder.instantiate();
......@@ -84,7 +84,7 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
const f1 = builder.addFunction('f', kSig_i_v).addBody([kExprI32Const, 11]);
const f2 = builder.addFunction('f', kSig_i_v).addBody([kExprI32Const, 22]);
const offset = 3;
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(offset),
builder.addActiveElementSegment(0, wasmI32Const(offset),
[f1.index, f1.index, f1.index]);
const instance = builder.instantiate();
......@@ -104,7 +104,7 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
const f1 = builder.addFunction('f', kSig_i_v).addBody([kExprI32Const, 11]);
const f2 = builder.addFunction('f', kSig_i_v).addBody([kExprI32Const, 22]);
const offset = 3;
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(offset),
builder.addActiveElementSegment(0, wasmI32Const(offset),
[f1.index, f1.index, f1.index]);
const instance = builder.instantiate();
......@@ -119,7 +119,7 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
const f1 = builder.addFunction('f', kSig_i_v).addBody([kExprI32Const, 11]);
const f2 = builder.addFunction('f', kSig_i_v).addBody([kExprI32Const, 22]);
const offset = 3;
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(offset),
builder.addActiveElementSegment(0, wasmI32Const(offset),
[f1.index, f1.index, f1.index]);
const instance = builder.instantiate();
......@@ -134,7 +134,7 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
const import1 = builder.addImport("q", "fun", kSig_i_ii);
const f1 = builder.addFunction('f', kSig_i_v).addBody([kExprI32Const, 11]);
const offset = 3;
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(offset),
builder.addActiveElementSegment(0, wasmI32Const(offset),
[f1.index, import1]);
const instance = builder.instantiate({q: {fun: () => 33}});
......
......@@ -132,7 +132,7 @@ let id = (() => { // identity exported function
builder.addImportedTable("q", "table", 5, 32);
let g = builder.addImportedGlobal("q", "base", kWasmI32);
builder.addActiveElementSegment(0, WasmInitExpr.GlobalGet(g),
builder.addActiveElementSegment(0, [kExprGlobalGet, g],
[funcs.mul.index, funcs.add.index, funcs.sub.index]);
builder.addExportOfKind("table", kExternalTable, 0);
let module = new WebAssembly.Module(builder.toBuffer());
......@@ -179,7 +179,7 @@ let id = (() => { // identity exported function
let t = builder.addImport("q", "exp_ten", sig_i_v);
builder.setTableBounds(7, 35);
builder.addActiveElementSegment(0, WasmInitExpr.GlobalGet(g1), [a, i, t]);
builder.addActiveElementSegment(0, [kExprGlobalGet, g1], [a, i, t]);
builder.addExportOfKind("table", kExternalTable, 0);
let module = new WebAssembly.Module(builder.toBuffer());
......@@ -213,7 +213,7 @@ let id = (() => { // identity exported function
builder1.addImportedTable("q", "table", 6, 36);
builder1.addActiveElementSegment(
0, WasmInitExpr.GlobalGet(g),
0, [kExprGlobalGet, g],
[funcs.mul.index, funcs.add.index, funcs.sub.index]);
let module1 = new WebAssembly.Module(builder1.toBuffer());
......@@ -265,7 +265,7 @@ let id = (() => { // identity exported function
kExprLocalGet, 0,
kExprCallIndirect, index_i_ii, kTableZero])
.exportAs("main");
builder.addActiveElementSegment(0, WasmInitExpr.I32Const(0), [0]);
builder.addActiveElementSegment(0, wasmI32Const(0), [0]);
return new WebAssembly.Module(builder.toBuffer());
}
......
......@@ -195,7 +195,8 @@ function instantiate(buffer, ffi) {
builder.addType(kSig_i_i);
builder.addFunction(undefined, i)
.addBody([kExprLocalGet, 0]);
builder.addGlobal(wasmRefType(i), false, WasmInitExpr.RefFunc(i));
builder.addGlobal(wasmRefType(i), false,
[kExprRefFunc, ...wasmSignedLeb(i)]);
}
for (let i of indices) {
builder.addFunction('f_' + i, makeSig([], [wasmRefType(i)]))
......
......@@ -1036,117 +1036,11 @@ class Binary {
}
}
emit_init_expr_recursive(expr) {
switch (expr.kind) {
case kExprGlobalGet:
this.emit_u8(kExprGlobalGet);
this.emit_u32v(expr.value);
break;
case kExprI32Const:
this.emit_bytes(wasmI32Const(expr.value));
break;
case kExprI64Const:
this.emit_bytes(wasmI64Const(expr.value));
break;
case kExprF32Const:
this.emit_bytes(wasmF32Const(expr.value));
break;
case kExprF64Const:
this.emit_bytes(wasmF64Const(expr.value));
break;
case kSimdPrefix:
this.emit_bytes(wasmS128Const(expr.value));
break;
case kExprI32Add:
case kExprI32Sub:
case kExprI32Mul:
case kExprI64Add:
case kExprI64Sub:
case kExprI64Mul:
this.emit_init_expr_recursive(expr.operands[0]);
this.emit_init_expr_recursive(expr.operands[1]);
this.emit_u8(expr.kind);
break;
case kExprRefFunc:
this.emit_u8(kExprRefFunc);
this.emit_u32v(expr.value);
break;
case kExprRefNull:
this.emit_u8(kExprRefNull);
this.emit_heap_type(expr.value);
break;
case kExprStructNew:
case kExprStructNewWithRtt:
case kExprStructNewDefault:
case kExprStructNewDefaultWithRtt:
for (let operand of expr.operands) {
this.emit_init_expr_recursive(operand);
}
this.emit_u8(kGCPrefix);
this.emit_u8(expr.kind);
this.emit_u32v(expr.value);
break;
case kExprArrayInit:
for (let operand of expr.operands) {
this.emit_init_expr_recursive(operand);
}
this.emit_u8(kGCPrefix);
this.emit_u8(expr.kind);
this.emit_u32v(expr.value);
this.emit_u32v(expr.operands.length - 1);
break;
case kExprArrayInitStatic:
for (let operand of expr.operands) {
this.emit_init_expr_recursive(operand);
}
this.emit_u8(kGCPrefix);
this.emit_u8(expr.kind);
this.emit_u32v(expr.value);
this.emit_u32v(expr.operands.length);
break;
case kExprArrayInitFromData:
case kExprArrayInitFromDataStatic:
case kExprArrayInitFromElemStatic:
for (let operand of expr.operands) {
this.emit_init_expr_recursive(operand);
}
this.emit_u8(kGCPrefix);
this.emit_u8(expr.kind);
this.emit_u32v(expr.array_index);
this.emit_u32v(expr.segment_index);
break;
case kExprI31New:
this.emit_init_expr_recursive(expr.operand);
this.emit_u8(kGCPrefix);
this.emit_u8(expr.kind);
break;
case kExprRttCanon:
this.emit_u8(kGCPrefix);
this.emit_u8(kExprRttCanon);
this.emit_u32v(expr.value);
break;
case kExprRttSub:
this.emit_init_expr_recursive(expr.parent);
this.emit_u8(kGCPrefix);
this.emit_u8(kExprRttSub);
this.emit_u32v(expr.value);
break;
case kExprRttFreshSub:
this.emit_init_expr_recursive(expr.parent);
this.emit_u8(kGCPrefix);
this.emit_u8(kExprRttFreshSub);
this.emit_u32v(expr.value);
break;
case kExprStringConst:
this.emit_u8(kGCPrefix);
this.emit_u8(kExprStringConst);
this.emit_u32v(expr.index);
break;
}
}
emit_init_expr(expr) {
this.emit_init_expr_recursive(expr);
// TODO(manoskouk): This is redundant, remove it once we are confident we
// check everything.
checkExpr(expr);
this.emit_bytes(expr);
this.emit_u8(kExprEnd);
}
......@@ -1207,12 +1101,7 @@ class WasmFunctionBuilder {
}
addBody(body) {
for (let b of body) {
if (typeof b !== 'number' || (b & (~0xFF)) !== 0) {
throw new Error(
'invalid body (entries must be 8 bit numbers): ' + body);
}
}
checkExpr(body);
this.body = body.slice();
// Automatically add the end for the function block to the body.
this.body.push(kExprEnd);
......@@ -1246,125 +1135,7 @@ class WasmFunctionBuilder {
}
}
class WasmInitExpr {
static I32Const(value) {
return {kind: kExprI32Const, value: value};
}
static I64Const(value) {
return {kind: kExprI64Const, value: value};
}
static F32Const(value) {
return {kind: kExprF32Const, value: value};
}
static F64Const(value) {
return {kind: kExprF64Const, value: value};
}
static S128Const(value) {
return {kind: kSimdPrefix, value: value};
}
static I32Add(lhs, rhs) {
return {kind: kExprI32Add, operands: [lhs, rhs]};
}
static I32Sub(lhs, rhs) {
return {kind: kExprI32Sub, operands: [lhs, rhs]};
}
static I32Mul(lhs, rhs) {
return {kind: kExprI32Mul, operands: [lhs, rhs]};
}
static I64Add(lhs, rhs) {
return {kind: kExprI64Add, operands: [lhs, rhs]};
}
static I64Sub(lhs, rhs) {
return {kind: kExprI64Sub, operands: [lhs, rhs]};
}
static I64Mul(lhs, rhs) {
return {kind: kExprI64Mul, operands: [lhs, rhs]};
}
static GlobalGet(index) {
return {kind: kExprGlobalGet, value: index};
}
static RefFunc(index) {
return {kind: kExprRefFunc, value: index};
}
static RefNull(type) {
return {kind: kExprRefNull, value: type};
}
static StructNewWithRtt(type, args) {
return {kind: kExprStructNewWithRtt, value: type, operands: args};
}
static StructNew(type, args) {
return {kind: kExprStructNew, value: type, operands: args};
}
static StructNewDefaultWithRtt(type, rtt) {
return {kind: kExprStructNewDefaultWithRtt, value: type, operands: [rtt]};
}
static StructNewDefault(type) {
return {kind: kExprStructNewDefault, value: type, operands: []};
}
static ArrayInit(type, args) {
return {kind: kExprArrayInit, value: type, operands: args};
}
static ArrayInitStatic(type, args) {
return {kind: kExprArrayInitStatic, value: type, operands: args};
}
static ArrayInitFromData(array_index, segment_index, args, builder) {
// array.init_from_data means we need to pull the data count section before
// any section that may include init. expressions.
builder.early_data_count_section = true;
return {kind: kExprArrayInitFromData, array_index: array_index,
segment_index: segment_index, operands: args};
}
static ArrayInitFromDataStatic(array_index, segment_index, args, builder) {
// array.init_from_data means we need to pull the data count section before
// any section that may include init. expressions.
builder.early_data_count_section = true;
return {kind: kExprArrayInitFromDataStatic, array_index: array_index,
segment_index: segment_index, operands: args};
}
static ArrayInitFromElemStatic(array_index, segment_index, args) {
return {kind: kExprArrayInitFromElemStatic, array_index: array_index,
segment_index: segment_index, operands: args};
}
static I31New(value) {
return {kind: kExprI31New, operand: value};
}
static RttCanon(type) {
return {kind: kExprRttCanon, value: type};
}
static RttSub(type, parent) {
return {kind: kExprRttSub, value: type, parent: parent};
}
static RttFreshSub(type, parent) {
return {kind: kExprRttFreshSub, value: type, parent: parent};
}
static StringConst(index) {
return {kind: kExprStringConst, index};
}
static defaultFor(type) {
switch (type) {
case kWasmI32:
return this.I32Const(0);
case kWasmI64:
return this.I64Const(0);
case kWasmF32:
return this.F32Const(0);
case kWasmF64:
return this.F64Const(0);
case kWasmS128:
return this.S128Const(new Array(16).fill(0));
default:
if ((typeof type) != 'number' && type.opcode != kWasmOptRef) {
throw new Error("Non-defaultable type");
}
let heap_type = (typeof type) == 'number' ? type : type.heap_type;
return this.RefNull(heap_type);
}
}
}
class WasmGlobalBuilder {
// {init} should be constructed with WasmInitExpr.
constructor(module, type, mutable, init) {
this.module = module;
this.type = type;
......@@ -1379,6 +1150,15 @@ class WasmGlobalBuilder {
}
}
function checkExpr(expr) {
for (let b of expr) {
if (typeof b !== 'number' || (b & (~0xFF)) !== 0) {
throw new Error(
'invalid body (entries must be 8 bit numbers): ' + expr);
}
}
}
class WasmTableBuilder {
constructor(module, type, initial_size, max_size, init_expr) {
// TODO(manoskouk): Add the table index.
......@@ -1563,8 +1343,30 @@ class WasmModuleBuilder {
return this.types.length - 1;
}
static defaultFor(type) {
switch (type) {
case kWasmI32:
return wasmI32Const(0);
case kWasmI64:
return wasmI64Const(0);
case kWasmF32:
return wasmF32Const(0.0);
case kWasmF64:
return wasmF64Const(0.0);
case kWasmS128:
return [kSimdPrefix, kExprSimdConst, ...(new Array(16).fill(0))];
default:
if ((typeof type) != 'number' && type.opcode != kWasmOptRef) {
throw new Error("Non-defaultable type");
}
let heap_type = (typeof type) == 'number' ? type : type.heap_type;
return [kExprRefNull, ...wasmSignedLeb(heap_type, kMaxVarInt32Size)];
}
}
addGlobal(type, mutable, init) {
if (init === undefined) init = WasmInitExpr.defaultFor(type);
if (init === undefined) init = WasmModuleBuilder.defaultFor(type);
checkExpr(init);
let glob = new WasmGlobalBuilder(this, type, mutable, init);
glob.index = this.globals.length + this.num_imported_globals;
this.globals.push(glob);
......@@ -1577,6 +1379,7 @@ class WasmModuleBuilder {
type == kWasmF64 || type == kWasmS128 || type == kWasmVoid) {
throw new Error('Tables must be of a reference type');
}
if (init_expr != undefined) checkExpr(init_expr);
let table = new WasmTableBuilder(
this, type, initial_size, max_size, init_expr);
table.index = this.tables.length + this.num_imported_tables;
......@@ -1724,6 +1527,10 @@ class WasmModuleBuilder {
// If {type} is undefined, then {elements} are function indices. Otherwise,
// they are initializer expressions.
addActiveElementSegment(table, offset, elements, type) {
checkExpr(offset);
if (type != undefined) {
for (let element of elements) checkExpr(element);
}
this.element_segments.push(
new WasmElemSegment(table, offset, type, elements, false));
return this.element_segments.length - 1;
......@@ -1732,6 +1539,9 @@ class WasmModuleBuilder {
// If {type} is undefined, then {elements} are function indices. Otherwise,
// they are initializer expressions.
addPassiveElementSegment(elements, type) {
if (type != undefined) {
for (let element of elements) checkExpr(element);
}
this.element_segments.push(
new WasmElemSegment(undefined, undefined, type, elements, false));
return this.element_segments.length - 1;
......@@ -1740,6 +1550,9 @@ class WasmModuleBuilder {
// If {type} is undefined, then {elements} are function indices. Otherwise,
// they are initializer expressions.
addDeclarativeElementSegment(elements, type) {
if (type != undefined) {
for (let element of elements) checkExpr(element);
}
this.element_segments.push(
new WasmElemSegment(undefined, undefined, type, elements, true));
return this.element_segments.length - 1;
......@@ -1761,7 +1574,7 @@ class WasmModuleBuilder {
if (table.has_max && table_size > table.max_size) {
table.max_size = table_size;
}
return this.addActiveElementSegment(0, WasmInitExpr.I32Const(base), array);
return this.addActiveElementSegment(0, wasmI32Const(base), array);
}
setTableBounds(min, max = undefined) {
......@@ -1776,6 +1589,10 @@ class WasmModuleBuilder {
this.nominal = true;
}
setEarlyDataCountSection() {
this.early_data_count_section = true;
}
setName(name) {
this.name = name;
return this;
......
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