Commit 20655a20 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm][test] Add method to create signed LEB values

This method will be used for a test with multiple code spaces, to
encode large function indexes. The current implementation in
{wasmI32Const} just always uses 5 bytes for encoding the LEB value.
This CL adds a {wasmSignedLeb} function which properly encodes the
value, and adds tests for that.

Drive-by: Clean up the rest of {test-wasm-module-builder.js}.

R=mstarzinger@chromium.org

Bug: v8:9477
Change-Id: Ide2d90eed9d40aa28df680fbb413275346d9c0b6
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1725623Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#62990}
parent 32afdd6c
......@@ -13,12 +13,12 @@ function instantiate(buffer, ffi) {
}
(function BasicTest() {
print("BasicTest");
print(arguments.callee.name);
let builder = new WasmModuleBuilder();
builder.addMemory(1, 2, false);
builder.addFunction("foo", kSig_i_v)
builder.addFunction('foo', kSig_i_v)
.addBody([kExprI32Const, 11])
.exportAs("blarg");
.exportAs('blarg');
var buffer = builder.toBuffer(debug);
var instance = instantiate(buffer);
......@@ -26,26 +26,26 @@ function instantiate(buffer, ffi) {
})();
(function ImportTest() {
print("ImportTest");
print(arguments.callee.name);
let builder = new WasmModuleBuilder();
var index = builder.addImport("", "print", makeSig_v_x(kWasmI32));
builder.addFunction("foo", kSig_v_v)
var index = builder.addImport('', 'print', makeSig_v_x(kWasmI32));
builder.addFunction('foo', kSig_v_v)
.addBody([kExprI32Const, 13, kExprCallFunction, index])
.exportAs("main");
.exportAs('main');
var buffer = builder.toBuffer(debug);
var instance = instantiate(buffer, {"": {print: print}});
print("should print 13! ");
var instance = instantiate(buffer, {'': {print: print}});
print('should print 13! ');
instance.exports.main();
})();
(function LocalsTest() {
print("LocalsTest");
print(arguments.callee.name);
let builder = new WasmModuleBuilder();
builder.addFunction(undefined, kSig_i_i)
.addLocals({i32_count: 1})
.addBody([kExprGetLocal, 0, kExprSetLocal, 1, kExprGetLocal, 1])
.exportAs("main");
.exportAs('main');
var buffer = builder.toBuffer(debug);
var instance = instantiate(buffer);
......@@ -54,11 +54,11 @@ function instantiate(buffer, ffi) {
})();
(function LocalsTest2() {
print("LocalsTest2");
print(arguments.callee.name);
// TODO(titzer): i64 only works on 64-bit platforms.
var types = [
{locals: {i32_count: 1}, type: kWasmI32},
// {locals: {i64_count: 1}, type: kWasmI64},
// {locals: {i64_count: 1}, type: kWasmI64},
{locals: {f32_count: 1}, type: kWasmF32},
{locals: {f64_count: 1}, type: kWasmF64},
];
......@@ -68,7 +68,7 @@ function instantiate(buffer, ffi) {
builder.addFunction(undefined, makeSig_r_x(p.type, p.type))
.addLocals(p.locals)
.addBody([kExprGetLocal, 0, kExprSetLocal, 1, kExprGetLocal, 1])
.exportAs("main");
.exportAs('main');
var buffer = builder.toBuffer(debug);
var instance = instantiate(buffer);
......@@ -78,13 +78,14 @@ function instantiate(buffer, ffi) {
})();
(function CallTest() {
print("CallTest");
print(arguments.callee.name);
let builder = new WasmModuleBuilder();
builder.addFunction("add", kSig_i_ii)
.addBody([kExprGetLocal, 0, kExprGetLocal, 1, kExprI32Add]);
builder.addFunction("main", kSig_i_ii)
builder.addFunction('add', kSig_i_ii).addBody([
kExprGetLocal, 0, kExprGetLocal, 1, kExprI32Add
]);
builder.addFunction('main', kSig_i_ii)
.addBody([kExprGetLocal, 0, kExprGetLocal, 1, kExprCallFunction, 0])
.exportAs("main");
.exportAs('main');
var instance = builder.instantiate();
assertEquals(44, instance.exports.main(11, 33));
......@@ -92,29 +93,32 @@ function instantiate(buffer, ffi) {
})();
(function IndirectCallTest() {
print("IndirectCallTest");
print(arguments.callee.name);
let builder = new WasmModuleBuilder();
builder.addFunction("add", kSig_i_ii)
.addBody([kExprGetLocal, 0, kExprGetLocal, 1, kExprI32Add]);
builder.addFunction("main", kSig_i_iii)
.addBody([kExprGetLocal,
1, kExprGetLocal, 2, kExprGetLocal, 0, kExprCallIndirect, 0, kTableZero])
.exportAs("main");
builder.addFunction('add', kSig_i_ii).addBody([
kExprGetLocal, 0, kExprGetLocal, 1, kExprI32Add
]);
builder.addFunction('main', kSig_i_iii)
.addBody([
kExprGetLocal, 1, kExprGetLocal, 2, kExprGetLocal, 0, kExprCallIndirect,
0, kTableZero
])
.exportAs('main');
builder.appendToTable([0]);
var instance = builder.instantiate();
assertEquals(44, instance.exports.main(0, 11, 33));
assertEquals(7777, instance.exports.main(0, 2222, 5555));
assertThrows(function() { instance.exports.main(1, 1, 1); });
assertThrows(() => instance.exports.main(1, 1, 1));
})();
(function DataSegmentTest() {
print("DataSegmentTest");
print(arguments.callee.name);
let builder = new WasmModuleBuilder();
builder.addMemory(1, 1, false);
builder.addFunction("load", kSig_i_i)
builder.addFunction('load', kSig_i_i)
.addBody([kExprGetLocal, 0, kExprI32LoadMem, 0, 0])
.exportAs("load");
.exportAs('load');
builder.addDataSegment(0, [9, 9, 9, 9]);
var buffer = builder.toBuffer(debug);
......@@ -122,14 +126,13 @@ function instantiate(buffer, ffi) {
assertEquals(151587081, instance.exports.load(0));
})();
(function BasicTestWithUint8Array() {
print("BasicTestWithUint8Array");
print(arguments.callee.name);
let builder = new WasmModuleBuilder();
builder.addMemory(1, 2, false);
builder.addFunction("foo", kSig_i_v)
builder.addFunction('foo', kSig_i_v)
.addBody([kExprI32Const, 17])
.exportAs("blarg");
.exportAs('blarg');
var buffer = builder.toBuffer(debug);
var array = new Uint8Array(buffer);
......@@ -151,15 +154,33 @@ function instantiate(buffer, ffi) {
})();
(function ImportTestTwoLevel() {
print("ImportTestTwoLevel");
print(arguments.callee.name);
let builder = new WasmModuleBuilder();
var index = builder.addImport("mod", "print", makeSig_v_x(kWasmI32));
builder.addFunction("foo", kSig_v_v)
var index = builder.addImport('mod', 'print', makeSig_v_x(kWasmI32));
builder.addFunction('foo', kSig_v_v)
.addBody([kExprI32Const, 19, kExprCallFunction, index])
.exportAs("main");
.exportAs('main');
var buffer = builder.toBuffer(debug);
var instance = instantiate(buffer, {mod: {print: print}});
print("should print 19! ");
print('should print 19! ');
instance.exports.main();
})();
(function TestI32Const() {
print(arguments.callee.name);
let ints = [
// A few negative number of different length.
-3 << 28, -20000, -400, -200, -100, -50, -10, -1,
// And a few positive number of different length.
0, 1, 2, 20, 120, 130, 260, 500, 5000000, 3 << 28
];
for (let i of ints) {
let builder = new WasmModuleBuilder();
builder.addFunction('main', kSig_i_v)
.addBody([...wasmI32Const(i)])
.exportAs('main');
let instance = builder.instantiate();
assertEquals(i, instance.exports.main());
}
})();
......@@ -552,7 +552,7 @@ class Binary {
this.buffer[this.length++] = val >> 24;
}
emit_leb(val, max_len) {
emit_leb_u(val, max_len) {
this.ensure_space(max_len);
for (let i = 0; i < max_len; ++i) {
let v = val & 0xff;
......@@ -567,11 +567,11 @@ class Binary {
}
emit_u32v(val) {
this.emit_leb(val, kMaxVarInt32Size);
this.emit_leb_u(val, kMaxVarInt32Size);
}
emit_u64v(val) {
this.emit_leb(val, kMaxVarInt64Size);
this.emit_leb_u(val, kMaxVarInt64Size);
}
emit_bytes(data) {
......@@ -1384,13 +1384,24 @@ class WasmModuleBuilder {
}
}
function wasmI32Const(val) {
let bytes = [kExprI32Const];
for (let i = 0; i < 4; ++i) {
bytes.push(0x80 | ((val >> (7 * i)) & 0x7f));
function wasmSignedLeb(val, max_len = 5) {
let res = [];
for (let i = 0; i < max_len; ++i) {
let v = val & 0x7f;
// If {v} sign-extended from 7 to 32 bits is equal to val, we are done.
if (((v << 25) >> 25) == val) {
res.push(v);
return res;
}
res.push(v | 0x80);
val = val >> 7;
}
bytes.push((val >> (7 * 4)) & 0x7f);
return bytes;
throw new Error(
'Leb value <' + val + '> exceeds maximum length of ' + max_len);
}
function wasmI32Const(val) {
return [kExprI32Const, ...wasmSignedLeb(val, 5)];
}
function wasmF32Const(f) {
......
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