Commit 3c3233e6 authored by Sven Sauleau's avatar Sven Sauleau Committed by Commit Bot

[wasm] js module-builder and constants improvements

Changes made to the Wasm module builder:
- Create emit_u64v for emitting a LEB128 that conceptually represent
an 64-bits integer.
- Differentiate toBuffer and toUint8Array for returning respectively
ArrayBuffer and a Uint8Array.
- Moved wasmF32Const and wasmF64Const functions because we can take
advantage of the existing cached conversion buffers. Also, declaring
them in the constants file will collide with the module-builder since
they are loaded in the same scope.
- The byte_view buffer (used by some conversions to bytes) switched from
Int8Array to Uint8Array.

Changes made to the Wasm contants:
- Add a new kSharedHasMaximumFlag flag for shared memory with a maximum
value.

Bug: v8:8726
Change-Id: If16c59896cfa1d42008da413e2e144b73a0fa5ce
Reviewed-on: https://chromium-review.googlesource.com/c/1443062Reviewed-by: 's avatarBen Titzer <titzer@chromium.org>
Commit-Queue: Sven Sauleau <ssauleau@igalia.com>
Cr-Commit-Position: refs/heads/master@{#59213}
parent 866d43e6
......@@ -76,6 +76,7 @@ let kWasmFunctionTypeForm = 0x60;
let kWasmAnyFunctionTypeForm = 0x70;
let kHasMaximumFlag = 1;
let kSharedHasMaximumFlag = 3;
// Segment flags
let kActiveNoIndex = 0;
......@@ -493,20 +494,3 @@ function assertTraps(trap, code) {
}
throw new MjsUnitAssertionError('Did not trap, expected: ' + kTrapMsgs[trap]);
}
function wasmI32Const(val) {
let bytes = [kExprI32Const];
for (let i = 0; i < 4; ++i) {
bytes.push(0x80 | ((val >> (7 * i)) & 0x7f));
}
bytes.push((val >> (7 * 4)) & 0x7f);
return bytes;
}
function wasmF32Const(f) {
return [kExprF32Const].concat(Array.from(new Uint8Array((new Float32Array([f])).buffer)));
}
function wasmF64Const(f) {
return [kExprF64Const].concat(Array.from(new Uint8Array((new Float64Array([f])).buffer)));
}
......@@ -4,7 +4,7 @@
// Used for encoding f32 and double constants to bits.
let __buffer = new ArrayBuffer(8);
let byte_view = new Int8Array(__buffer);
let byte_view = new Uint8Array(__buffer);
let f32_view = new Float32Array(__buffer);
let f64_view = new Float64Array(__buffer);
......@@ -37,6 +37,18 @@ class Binary extends Array {
}
}
emit_u64v(val) {
while (true) {
let v = val & 0xff;
val = val >>> 7;
if (val == 0) {
this.push(v);
break;
}
this.push(v | 0x80);
}
}
emit_bytes(data) {
for (let i = 0; i < data.length; i++) {
this.push(data[i] & 0xff);
......@@ -452,9 +464,9 @@ class WasmModuleBuilder {
const is_shared = wasm.memory.shared !== undefined;
// Emit flags (bit 0: reszeable max, bit 1: shared memory)
if (is_shared) {
section.emit_u8(has_max ? 3 : 2);
section.emit_u8(has_max ? kSharedHasMaximumFlag : 2);
} else {
section.emit_u8(has_max ? 1 : 0);
section.emit_u8(has_max ? kHasMaximumFlag : 0);
}
section.emit_u32v(wasm.memory.min);
if (has_max) section.emit_u32v(wasm.memory.max);
......@@ -478,7 +490,7 @@ class WasmModuleBuilder {
break;
case kWasmI64:
section.emit_u8(kExprI64Const);
section.emit_u32v(global.init);
section.emit_u64v(global.init);
break;
case kWasmF32:
section.emit_u8(kExprF32Const);
......@@ -735,6 +747,10 @@ class WasmModuleBuilder {
return buffer;
}
toUint8Array(debug = false) {
return new Uint8Array(this.toBuffer(debug));
}
instantiate(ffi) {
let module = new WebAssembly.Module(this.toBuffer());
let instance = new WebAssembly.Instance(module, ffi);
......@@ -750,3 +766,23 @@ class WasmModuleBuilder {
return new WebAssembly.Module(this.toBuffer(debug));
}
}
function wasmI32Const(val) {
let bytes = [kExprI32Const];
for (let i = 0; i < 4; ++i) {
bytes.push(0x80 | ((val >> (7 * i)) & 0x7f));
}
bytes.push((val >> (7 * 4)) & 0x7f);
return bytes;
}
function wasmF32Const(f) {
f32_view[0] = f;
return [kExprF32Const, byte_view[0], byte_view[1], byte_view[2], byte_view[3]];
}
function wasmF64Const(f) {
f64_view[0] = f;
return [kExprF64Const, byte_view[0], byte_view[1], byte_view[2], byte_view[3],
byte_view[4], byte_view[5], byte_view[6], byte_view[7]];
}
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