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
...@@ -552,7 +552,7 @@ class Binary { ...@@ -552,7 +552,7 @@ class Binary {
this.buffer[this.length++] = val >> 24; this.buffer[this.length++] = val >> 24;
} }
emit_leb(val, max_len) { emit_leb_u(val, max_len) {
this.ensure_space(max_len); this.ensure_space(max_len);
for (let i = 0; i < max_len; ++i) { for (let i = 0; i < max_len; ++i) {
let v = val & 0xff; let v = val & 0xff;
...@@ -567,11 +567,11 @@ class Binary { ...@@ -567,11 +567,11 @@ class Binary {
} }
emit_u32v(val) { emit_u32v(val) {
this.emit_leb(val, kMaxVarInt32Size); this.emit_leb_u(val, kMaxVarInt32Size);
} }
emit_u64v(val) { emit_u64v(val) {
this.emit_leb(val, kMaxVarInt64Size); this.emit_leb_u(val, kMaxVarInt64Size);
} }
emit_bytes(data) { emit_bytes(data) {
...@@ -1384,13 +1384,24 @@ class WasmModuleBuilder { ...@@ -1384,13 +1384,24 @@ class WasmModuleBuilder {
} }
} }
function wasmI32Const(val) { function wasmSignedLeb(val, max_len = 5) {
let bytes = [kExprI32Const]; let res = [];
for (let i = 0; i < 4; ++i) { for (let i = 0; i < max_len; ++i) {
bytes.push(0x80 | ((val >> (7 * i)) & 0x7f)); 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); throw new Error(
return bytes; 'Leb value <' + val + '> exceeds maximum length of ' + max_len);
}
function wasmI32Const(val) {
return [kExprI32Const, ...wasmSignedLeb(val, 5)];
} }
function wasmF32Const(f) { 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