Commit 3f207617 authored by titzer's avatar titzer Committed by Commit bot

[wasm] Binary 0xD: update encoding of opcodes, types, and add immediates.

R=ahaas@chromium.org,rossberg@chromium.org,binji@chromium.org,bradnelson@chromium.org
BUG=chromium:575167, chromium:659591

Review-Url: https://codereview.chromium.org/2440953002
Cr-Commit-Position: refs/heads/master@{#40600}
parent ff5194e3
......@@ -1394,6 +1394,7 @@ class AsmWasmBuilderImpl final : public AstVisitor<AsmWasmBuilderImpl> {
current_function_builder_->AddAsmWasmOffset(expr->position());
current_function_builder_->Emit(kExprCallIndirect);
current_function_builder_->EmitVarInt(indices->signature_index);
current_function_builder_->EmitVarInt(0); // table index
returns_value =
builder_->GetSignature(indices->signature_index)->return_count() >
0;
......
......@@ -317,6 +317,11 @@ class WasmDecoder : public Decoder {
ImmI64Operand operand(this, pc);
return 1 + operand.length;
}
case kExprGrowMemory:
case kExprMemorySize: {
MemoryIndexOperand operand(this, pc);
return 1 + operand.length;
}
case kExprI8Const:
return 2;
case kExprF32Const:
......@@ -1102,17 +1107,23 @@ class WasmFullDecoder : public WasmDecoder {
case kExprF64StoreMem:
len = DecodeStoreMem(kAstF64, MachineType::Float64());
break;
case kExprGrowMemory:
case kExprGrowMemory: {
MemoryIndexOperand operand(this, pc_);
if (module_->origin != kAsmJsOrigin) {
Value val = Pop(0, kAstI32);
Push(kAstI32, BUILD(GrowMemory, val.node));
} else {
error("grow_memory is not supported for asmjs modules");
}
len = 1 + operand.length;
break;
case kExprMemorySize:
}
case kExprMemorySize: {
MemoryIndexOperand operand(this, pc_);
Push(kAstI32, BUILD(CurrentMemoryPages));
len = 1 + operand.length;
break;
}
case kExprCallFunction: {
CallFunctionOperand operand(this, pc_);
if (Validate(pc_, operand)) {
......
......@@ -186,14 +186,19 @@ struct BreakDepthOperand {
};
struct CallIndirectOperand {
uint32_t table_index;
uint32_t index;
FunctionSig* sig;
unsigned length;
inline CallIndirectOperand(Decoder* decoder, const byte* pc) {
unsigned len1 = 0;
unsigned len2 = 0;
index = decoder->checked_read_u32v(pc, 1 + len1, &len2, "signature index");
length = len1 + len2;
unsigned len = 0;
index = decoder->checked_read_u32v(pc, 1, &len, "signature index");
table_index = decoder->checked_read_u8(pc, 1 + len, "table index");
if (table_index != 0) {
decoder->error(pc, pc + 1 + len, "expected table index 0, found %u",
table_index);
}
length = 1 + len;
sig = nullptr;
}
};
......@@ -211,6 +216,18 @@ struct CallFunctionOperand {
}
};
struct MemoryIndexOperand {
uint32_t index;
unsigned length;
inline MemoryIndexOperand(Decoder* decoder, const byte* pc) {
index = decoder->checked_read_u8(pc, 1, "memory index");
if (index != 0) {
decoder->error(pc, pc + 1, "expected memory index 0, found %u", index);
}
length = 1;
}
};
struct BranchTableOperand {
uint32_t table_count;
const byte* start;
......
......@@ -313,7 +313,7 @@ class ModuleDecoder : public Decoder {
static_cast<uint32_t>(module->function_tables.size());
module->function_tables.push_back(
{0, 0, std::vector<int32_t>(), true, false, SignatureMap()});
expect_u8("element type", 0x20);
expect_u8("element type", kWasmAnyFunctionTypeForm);
WasmIndirectFunctionTable* table = &module->function_tables.back();
consume_resizable_limits("element count", "elements", kMaxTableSize,
&table->size, &table->max_size);
......
......@@ -1592,13 +1592,17 @@ class ThreadImpl : public WasmInterpreter::Thread {
ASMJS_STORE_CASE(F64AsmjsStoreMem, double, double);
#undef ASMJS_STORE_CASE
case kExprGrowMemory: {
MemoryIndexOperand operand(&decoder, code->at(pc));
uint32_t delta_pages = Pop().to<uint32_t>();
Push(pc, WasmVal(ExecuteGrowMemory(delta_pages, instance())));
len = 1 + operand.length;
break;
}
case kExprMemorySize: {
MemoryIndexOperand operand(&decoder, code->at(pc));
Push(pc, WasmVal(static_cast<uint32_t>(instance()->mem_size /
WasmModule::kPageSize)));
len = 1 + operand.length;
break;
}
#define EXECUTE_SIMPLE_BINOP(name, ctype, op) \
......
......@@ -420,21 +420,23 @@ class LocalDeclEncoder {
#define WASM_CALL_FUNCTION(index, ...) \
__VA_ARGS__, kExprCallFunction, static_cast<byte>(index)
#define TABLE_ZERO 0
// TODO(titzer): change usages of these macros to put func last.
#define WASM_CALL_INDIRECT0(index, func) \
func, kExprCallIndirect, static_cast<byte>(index)
func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
#define WASM_CALL_INDIRECT1(index, func, a) \
a, func, kExprCallIndirect, static_cast<byte>(index)
a, func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
#define WASM_CALL_INDIRECT2(index, func, a, b) \
a, b, func, kExprCallIndirect, static_cast<byte>(index)
a, b, func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
#define WASM_CALL_INDIRECT3(index, func, a, b, c) \
a, b, c, func, kExprCallIndirect, static_cast<byte>(index)
a, b, c, func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
#define WASM_CALL_INDIRECT4(index, func, a, b, c, d) \
a, b, c, d, func, kExprCallIndirect, static_cast<byte>(index)
a, b, c, d, func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
#define WASM_CALL_INDIRECT5(index, func, a, b, c, d, e) \
a, b, c, d, e, func, kExprCallIndirect, static_cast<byte>(index)
a, b, c, d, e, func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
#define WASM_CALL_INDIRECTN(arity, index, func, ...) \
__VA_ARGS__, func, kExprCallIndirect, static_cast<byte>(index)
__VA_ARGS__, func, kExprCallIndirect, static_cast<byte>(index), TABLE_ZERO
#define WASM_NOT(x) x, kExprI32Eqz
#define WASM_SEQ(...) __VA_ARGS__
......@@ -605,8 +607,8 @@ class LocalDeclEncoder {
//------------------------------------------------------------------------------
// Memory Operations.
//------------------------------------------------------------------------------
#define WASM_GROW_MEMORY(x) x, kExprGrowMemory
#define WASM_MEMORY_SIZE kExprMemorySize
#define WASM_GROW_MEMORY(x) x, kExprGrowMemory, 0
#define WASM_MEMORY_SIZE kExprMemorySize, 0
//------------------------------------------------------------------------------
// Simd Operations.
......
......@@ -31,10 +31,10 @@ const size_t kMaxModuleSize = 1024 * 1024 * 1024;
const size_t kMaxFunctionSize = 128 * 1024;
const size_t kMaxStringSize = 256;
const uint32_t kWasmMagic = 0x6d736100;
const uint32_t kWasmVersion = 0x0c;
const uint32_t kWasmVersion = 0x0d;
const uint8_t kWasmFunctionTypeForm = 0x40;
const uint8_t kWasmAnyFunctionTypeForm = 0x20;
const uint8_t kWasmFunctionTypeForm = 0x60;
const uint8_t kWasmAnyFunctionTypeForm = 0x70;
enum WasmSectionCode {
kUnknownSectionCode = 0, // code for unknown sections
......
This diff is collapsed.
......@@ -1570,7 +1570,7 @@ static void CompileCallIndirectMany(LocalType param) {
ADD_CODE(code, kExprGetLocal, p);
}
ADD_CODE(code, kExprI8Const, 0);
ADD_CODE(code, kExprCallIndirect, 1);
ADD_CODE(code, kExprCallIndirect, 1, TABLE_ZERO);
t.Build(&code[0], &code[0] + code.size());
t.Compile();
......
......@@ -2920,7 +2920,7 @@ static void CompileCallIndirectMany(LocalType param) {
ADD_CODE(code, kExprGetLocal, p);
}
ADD_CODE(code, kExprI8Const, 0);
ADD_CODE(code, kExprCallIndirect, 1);
ADD_CODE(code, kExprCallIndirect, 1, TABLE_ZERO);
t.Build(&code[0], &code[0] + code.size());
t.Compile();
......
......@@ -12,9 +12,9 @@ load("test/mjsunit/wasm/wasm-module-builder.js");
builder.addMemory(1, 1, false);
builder.addFunction("foo", kSig_i_v)
.addBody([
kExprMemorySize,
kExprMemorySize, kMemoryZero,
kExprI32Const, 0x10,
kExprGrowMemory,
kExprGrowMemory, kMemoryZero,
kExprI32Mul,
])
.exportFunc();
......
......@@ -20,7 +20,7 @@ load("test/mjsunit/wasm/wasm-module-builder.js");
kExprGetLocal, 0,
kExprI32LoadMem, 0, 0,
kExprI32Const, 1,
kExprCallIndirect, signature,
kExprCallIndirect, signature, kTableZero,
kExprGetLocal,0,
kExprI32LoadMem,0, 0,
kExprCallFunction, 0,
......
......@@ -12,7 +12,7 @@ var kPageSize = 0x10000;
function genGrowMemoryBuilder() {
var builder = new WasmModuleBuilder();
builder.addFunction("grow_memory", kSig_i_i)
.addBody([kExprGetLocal, 0, kExprGrowMemory])
.addBody([kExprGetLocal, 0, kExprGrowMemory, kMemoryZero])
.exportFunc();
builder.addFunction("load", kSig_i_i)
.addBody([kExprGetLocal, 0, kExprI32LoadMem, 0, 0])
......@@ -325,7 +325,7 @@ function testGrowMemoryCurrentMemory() {
var builder = genGrowMemoryBuilder();
builder.addMemory(1, 1, false);
builder.addFunction("memory_size", kSig_i_v)
.addBody([kExprMemorySize])
.addBody([kExprMemorySize, kMemoryZero])
.exportFunc();
var module = builder.instantiate();
function growMem(pages) { return module.exports.grow_memory(pages); }
......@@ -449,7 +449,7 @@ function testGrowMemoryOutOfBoundsOffset2() {
.addBody([
kExprI32Const, 20,
kExprI32Const, 29,
kExprGrowMemory,
kExprGrowMemory, kMemoryZero,
kExprI32StoreMem, 0, 0xFF, 0xFF, 0xFF, 0x3a
])
.exportAs("main");
......
......@@ -188,7 +188,7 @@ load("test/mjsunit/wasm/wasm-module-builder.js");
assertEquals(2*kPageSize, memory.buffer.byteLength);
let builder = new WasmModuleBuilder();
builder.addFunction("grow", kSig_i_i)
.addBody([kExprGetLocal, 0, kExprGrowMemory])
.addBody([kExprGetLocal, 0, kExprGrowMemory, kMemoryZero])
.exportFunc();
builder.addImportedMemory("mine");
let instance = builder.instantiate({mine: memory});
......
......@@ -27,7 +27,7 @@ var module = (function () {
kExprGetLocal, 1,
kExprGetLocal, 2,
kExprGetLocal, 0,
kExprCallIndirect, sig_index
kExprCallIndirect, sig_index, kTableZero
])
.exportFunc()
builder.appendToTable([1, 2, 3]);
......@@ -71,7 +71,7 @@ module = (function () {
kExprGetLocal, 1,
kExprGetLocal, 2,
kExprGetLocal, 0,
kExprCallIndirect, sig_i_ii
kExprCallIndirect, sig_i_ii, kTableZero
])
.exportFunc();
builder.appendToTable([mul.index, add.index, popcnt.index, main.index]);
......@@ -117,7 +117,7 @@ module = (function () {
kExprI32Const, 33, // --
kExprGetLocal, 0, // --
kExprGetLocal, 1, // --
kExprCallIndirect, 0]) // --
kExprCallIndirect, 0, kTableZero]) // --
.exportAs("main");
builder.appendToTable([f.mul.index, f.add.index, f.sub.index]);
......@@ -144,7 +144,7 @@ assertTraps(kTrapFuncInvalid, "module.exports.main(12, 3)");
kExprI32Const, 33, // --
kExprGetLocal, 0, // --
kExprGetLocal, 1, // --
kExprCallIndirect, 0]) // --
kExprCallIndirect, 0, kTableZero]) // --
.exportAs("main");
builder.setFunctionTableLength(length);
......@@ -181,7 +181,7 @@ assertTraps(kTrapFuncInvalid, "module.exports.main(12, 3)");
kExprI32Const, 33, // --
kExprGetLocal, 0, // --
kExprGetLocal, 1, // --
kExprCallIndirect, 0]) // --
kExprCallIndirect, 0, kTableZero]) // --
.exportAs("main");
builder.setFunctionTableLength(10);
......
......@@ -42,7 +42,7 @@ function AddFunctions(builder) {
kExprI32Const, 33, // --
kExprGetLocal, 0, // --
kExprGetLocal, 1, // --
kExprCallIndirect, 0]) // --
kExprCallIndirect, 0, kTableZero]) // --
.exportAs("main");
f.add.exportAs("blarg");
......
......@@ -124,7 +124,7 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88)));
kExprGetLocal, 0,
kExprI32LoadMem, 0, 0,
kExprI32Const, 1,
kExprCallIndirect, signature,
kExprCallIndirect, signature, kTableZero,
kExprGetLocal,0,
kExprI32LoadMem,0, 0,
kExprCallFunction, 0,
......
......@@ -11,7 +11,7 @@ load("test/mjsunit/wasm/wasm-module-builder.js");
print("testMemorySizeZero()");
var builder = new WasmModuleBuilder();
builder.addFunction("memory_size", kSig_i_v)
.addBody([kExprMemorySize])
.addBody([kExprMemorySize, kMemoryZero])
.exportFunc();
var module = builder.instantiate();
assertEquals(0, module.exports.memory_size());
......@@ -23,7 +23,7 @@ load("test/mjsunit/wasm/wasm-module-builder.js");
var size = 11;
builder.addMemory(size, size, false);
builder.addFunction("memory_size", kSig_i_v)
.addBody([kExprMemorySize])
.addBody([kExprMemorySize, kMemoryZero])
.exportFunc();
var module = builder.instantiate();
assertEquals(size, module.exports.memory_size());
......
......@@ -135,7 +135,7 @@ Error.prepareStackTrace = function(error, frames) {
builder.addFunction("recursion", sig_index)
.addBody([
kExprI32Const, 0,
kExprCallIndirect, sig_index
kExprCallIndirect, sig_index, kTableZero
])
.exportFunc()
builder.appendToTable([0]);
......
......@@ -89,7 +89,7 @@ var debug = true;
.addBody([kExprGetLocal, 0, kExprGetLocal, 1, kExprI32Add]);
module.addFunction("main", kSig_i_iii)
.addBody([kExprGetLocal,
1, kExprGetLocal, 2, kExprGetLocal, 0, kExprCallIndirect, 0])
1, kExprGetLocal, 2, kExprGetLocal, 0, kExprCallIndirect, 0, kTableZero])
.exportAs("main");
module.appendToTable([0]);
......
......@@ -52,7 +52,7 @@ builder.addFunction("main", kSig_i_i)
kExprEnd,
// offset 30
kExprGetLocal, 0,
kExprCallIndirect, sig_index,
kExprCallIndirect, sig_index, kTableZero,
kExprEnd,
])
.exportAs("main");
......
This diff is collapsed.
......@@ -1149,7 +1149,7 @@ TEST_F(AstDecoderTest, AllSimpleExpressions) {
}
TEST_F(AstDecoderTest, MemorySize) {
byte code[] = {kExprMemorySize};
byte code[] = {kExprMemorySize, 0};
EXPECT_VERIFIES_C(i_i, code);
EXPECT_FAILURE_C(f_ff, code);
}
......@@ -1632,7 +1632,7 @@ TEST_F(AstDecoderTest, WasmGrowMemory) {
module = &module_env;
module->origin = kWasmOrigin;
byte code[] = {WASM_UNOP(kExprGrowMemory, WASM_GET_LOCAL(0))};
byte code[] = {WASM_GET_LOCAL(0), kExprGrowMemory, 0};
EXPECT_VERIFIES_C(i_i, code);
EXPECT_FAILURE_C(i_d, code);
}
......@@ -1642,7 +1642,7 @@ TEST_F(AstDecoderTest, AsmJsGrowMemory) {
module = &module_env;
module->origin = kAsmJsOrigin;
byte code[] = {WASM_UNOP(kExprGrowMemory, WASM_GET_LOCAL(0))};
byte code[] = {WASM_GET_LOCAL(0), kExprGrowMemory, 0};
EXPECT_FAILURE_C(i_i, code);
}
......@@ -2316,7 +2316,7 @@ TEST_F(WasmOpcodeLengthTest, MiscExpressions) {
EXPECT_LENGTH(2, kExprGetGlobal);
EXPECT_LENGTH(2, kExprSetGlobal);
EXPECT_LENGTH(2, kExprCallFunction);
EXPECT_LENGTH(2, kExprCallIndirect);
EXPECT_LENGTH(3, kExprCallIndirect);
}
TEST_F(WasmOpcodeLengthTest, I32Const) {
......@@ -2375,8 +2375,8 @@ TEST_F(WasmOpcodeLengthTest, LoadsAndStores) {
}
TEST_F(WasmOpcodeLengthTest, MiscMemExpressions) {
EXPECT_LENGTH(1, kExprMemorySize);
EXPECT_LENGTH(1, kExprGrowMemory);
EXPECT_LENGTH(2, kExprMemorySize);
EXPECT_LENGTH(2, kExprGrowMemory);
}
TEST_F(WasmOpcodeLengthTest, SimpleExpressions) {
......
......@@ -131,12 +131,12 @@ TEST_F(WasmMacroGenTest, CallFunction) {
}
TEST_F(WasmMacroGenTest, CallIndirect) {
EXPECT_SIZE(4, WASM_CALL_INDIRECT0(0, WASM_ZERO));
EXPECT_SIZE(4, WASM_CALL_INDIRECT0(1, WASM_ZERO));
EXPECT_SIZE(4, WASM_CALL_INDIRECT0(11, WASM_ZERO));
EXPECT_SIZE(5, WASM_CALL_INDIRECT0(0, WASM_ZERO));
EXPECT_SIZE(5, WASM_CALL_INDIRECT0(1, WASM_ZERO));
EXPECT_SIZE(5, WASM_CALL_INDIRECT0(11, WASM_ZERO));
EXPECT_SIZE(6, WASM_CALL_INDIRECT1(0, WASM_ZERO, WASM_ZERO));
EXPECT_SIZE(8, WASM_CALL_INDIRECT2(1, WASM_ZERO, WASM_ZERO, WASM_ZERO));
EXPECT_SIZE(7, WASM_CALL_INDIRECT1(0, WASM_ZERO, WASM_ZERO));
EXPECT_SIZE(9, WASM_CALL_INDIRECT2(1, WASM_ZERO, WASM_ZERO, WASM_ZERO));
}
TEST_F(WasmMacroGenTest, Int32Ops) {
......
......@@ -12,30 +12,30 @@ cd ${TOOLS_DIR}/..
rm -rf test/fuzzer/wasm
rm -rf test/fuzzer/wasm_asmjs
make x64.debug -j
make x64.release -j
mkdir -p test/fuzzer/wasm
mkdir -p test/fuzzer/wasm_asmjs
# asm.js
./tools/run-tests.py -j8 --variants=default --timeout=10 --arch=x64 \
--mode=debug --no-presubmit --extra-flags="--dump-wasm-module \
--mode=release --no-presubmit --extra-flags="--dump-wasm-module \
--dump-wasm-module-path=./test/fuzzer/wasm_asmjs/" mjsunit/wasm/asm*
./tools/run-tests.py -j8 --variants=default --timeout=10 --arch=x64 \
--mode=debug --no-presubmit --extra-flags="--dump-wasm-module \
--mode=release --no-presubmit --extra-flags="--dump-wasm-module \
--dump-wasm-module-path=./test/fuzzer/wasm_asmjs/" mjsunit/asm/*
./tools/run-tests.py -j8 --variants=default --timeout=10 --arch=x64 \
--mode=debug --no-presubmit --extra-flags="--dump-wasm-module \
--mode=release --no-presubmit --extra-flags="--dump-wasm-module \
--dump-wasm-module-path=./test/fuzzer/wasm_asmjs/" mjsunit/regress/asm/*
# WASM
./tools/run-tests.py -j8 --variants=default --timeout=10 --arch=x64 \
--mode=debug --no-presubmit --extra-flags="--dump-wasm-module \
--mode=release --no-presubmit --extra-flags="--dump-wasm-module \
--dump-wasm-module-path=./test/fuzzer/wasm/" unittests
./tools/run-tests.py -j8 --variants=default --timeout=10 --arch=x64 \
--mode=debug --no-presubmit --extra-flags="--dump-wasm-module \
--mode=release --no-presubmit --extra-flags="--dump-wasm-module \
--dump-wasm-module-path=./test/fuzzer/wasm/" mjsunit/wasm/*
./tools/run-tests.py -j8 --variants=default --timeout=10 --arch=x64 \
--mode=debug --no-presubmit --extra-flags="--dump-wasm-module \
--mode=release --no-presubmit --extra-flags="--dump-wasm-module \
--dump-wasm-module-path=./test/fuzzer/wasm/" \
$(cd test/; ls cctest/wasm/test-*.cc | \
sed -es/wasm\\///g | sed -es/[.]cc/\\/\\*/g)
......
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