diff options
author | Alon Zakai <alonzakai@gmail.com> | 2016-10-11 14:40:23 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2016-10-11 15:01:04 -0700 |
commit | 1e783f3e96f904b88b692522de04f786feaf41a0 (patch) | |
tree | 1733305ab97e113b137107d7711c56c0a3652310 | |
parent | a3dc8bf8cba7cd1b5ece8abf0489a9dcacfa3f2e (diff) | |
download | binaryen-1e783f3e96f904b88b692522de04f786feaf41a0.tar.gz binaryen-1e783f3e96f904b88b692522de04f786feaf41a0.tar.bz2 binaryen-1e783f3e96f904b88b692522de04f786feaf41a0.zip |
allow a maximum 0 size for a table
-rw-r--r-- | src/passes/Print.cpp | 2 | ||||
-rw-r--r-- | src/wasm-binary.h | 36 | ||||
-rw-r--r-- | test/empty_table.wast | 4 | ||||
-rw-r--r-- | test/empty_table.wast.fromBinary | 5 | ||||
-rw-r--r-- | test/empty_table.wast.fromBinary.noDebugInfo | 5 |
5 files changed, 33 insertions, 19 deletions
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp index 33b5f5d16..075d013e5 100644 --- a/src/passes/Print.cpp +++ b/src/passes/Print.cpp @@ -615,7 +615,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> { void printTableHeader(Table* curr) { printOpening(o, "table") << ' '; o << curr->initial; - if (curr->max && curr->max != Table::kMaxSize) o << ' ' << curr->max; + if (curr->max != Table::kMaxSize) o << ' ' << curr->max; o << " anyfunc)"; } void visitTable(Table *curr) { diff --git a/src/wasm-binary.h b/src/wasm-binary.h index eef315fda..43a43b5c4 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -558,11 +558,11 @@ public: return ret; } - void writeResizableLimits(Address initial, Address maximum) { - uint32_t flags = maximum ? 1 : 0; + void writeResizableLimits(Address initial, Address maximum, bool hasMaximum) { + uint32_t flags = hasMaximum ? 1 : 0; o << U32LEB(flags); o << U32LEB(initial); - if (flags) { + if (hasMaximum) { o << U32LEB(maximum); } } @@ -590,8 +590,7 @@ public: if (debug) std::cerr << "== writeMemory" << std::endl; auto start = startSection(BinaryConsts::Section::Memory); o << U32LEB(1); // Define 1 memory - Address max = wasm->memory.max == Memory::kMaxSize ? Address(0) : wasm->memory.max; - writeResizableLimits(wasm->memory.initial, max); + writeResizableLimits(wasm->memory.initial, wasm->memory.max, wasm->memory.max != Memory::kMaxSize); finishSection(start); } @@ -639,13 +638,12 @@ public: case ExternalKind::Function: o << U32LEB(getFunctionTypeIndex(import->functionType->name)); break; case ExternalKind::Table: { o << U32LEB(BinaryConsts::ElementType::AnyFunc); - auto max = wasm->table.max == Table::kMaxSize ? Address(0) : wasm->table.max; - writeResizableLimits(wasm->table.initial, max); + writeResizableLimits(wasm->table.initial, wasm->table.max, wasm->table.max != Table::kMaxSize); break; } case ExternalKind::Memory: { - auto max = wasm->memory.max == Memory::kMaxSize ? Address(0) : wasm->memory.max; - writeResizableLimits(wasm->memory.initial, max); break; + writeResizableLimits(wasm->memory.initial, wasm->memory.max, wasm->memory.max != Memory::kMaxSize); + break; } case ExternalKind::Global: o << binaryWasmType(import->globalType); @@ -850,8 +848,7 @@ public: auto start = startSection(BinaryConsts::Section::Table); o << U32LEB(1); // Declare 1 table. o << U32LEB(BinaryConsts::ElementType::AnyFunc); - Address max = wasm->table.max == Table::kMaxSize ? Address(0) : wasm->table.max; - writeResizableLimits(wasm->table.initial, max); + writeResizableLimits(wasm->table.initial, wasm->table.max, wasm->table.max != Table::kMaxSize); finishSection(start); } @@ -1562,7 +1559,7 @@ public: auto numMemories = getU32LEB(); if (!numMemories) return; assert(numMemories == 1); - getResizableLimits(wasm.memory.initial, &wasm.memory.max); + getResizableLimits(wasm.memory.initial, wasm.memory.max, Memory::kMaxSize); } void readSignatures() { @@ -1606,12 +1603,12 @@ public: } } - void getResizableLimits(Address& initial, Address* max) { + void getResizableLimits(Address& initial, Address& max, Address defaultIfNoMax) { auto flags = getU32LEB(); initial = getU32LEB(); bool hasMax = flags & 0x1; - assert(max || !hasMax); - if (hasMax) *max = getU32LEB(); + if (hasMax) max = getU32LEB(); + else max = defaultIfNoMax; } void readImports() { @@ -1640,10 +1637,13 @@ public: if (elementType != BinaryConsts::ElementType::AnyFunc) throw ParseException("Imported table type is not AnyFunc"); wasm.table.exists = true; wasm.table.imported = true; - getResizableLimits(wasm.table.initial, &wasm.table.max); + getResizableLimits(wasm.table.initial, wasm.table.max, Table::kMaxSize); + break; + } + case ExternalKind::Memory: { + getResizableLimits(wasm.memory.initial, wasm.memory.max, Memory::kMaxSize); break; } - case ExternalKind::Memory: getResizableLimits(wasm.memory.initial, &wasm.memory.max); break; case ExternalKind::Global: { curr->globalType = getWasmType(); auto globalMutable = getU32LEB(); @@ -1897,7 +1897,7 @@ public: wasm.table.exists = true; auto elemType = getU32LEB(); if (elemType != BinaryConsts::ElementType::AnyFunc) throw ParseException("ElementType must be AnyFunc in MVP"); - getResizableLimits(wasm.table.initial, &wasm.table.max); + getResizableLimits(wasm.table.initial, wasm.table.max, Table::kMaxSize); } void readTableElements() { diff --git a/test/empty_table.wast b/test/empty_table.wast new file mode 100644 index 000000000..17ce9d6b2 --- /dev/null +++ b/test/empty_table.wast @@ -0,0 +1,4 @@ +(module + (table 0 0 anyfunc) + (memory $0 0) +) diff --git a/test/empty_table.wast.fromBinary b/test/empty_table.wast.fromBinary new file mode 100644 index 000000000..219d2388c --- /dev/null +++ b/test/empty_table.wast.fromBinary @@ -0,0 +1,5 @@ +(module + (table 0 0 anyfunc) + (memory $0 0) +) + diff --git a/test/empty_table.wast.fromBinary.noDebugInfo b/test/empty_table.wast.fromBinary.noDebugInfo new file mode 100644 index 000000000..219d2388c --- /dev/null +++ b/test/empty_table.wast.fromBinary.noDebugInfo @@ -0,0 +1,5 @@ +(module + (table 0 0 anyfunc) + (memory $0 0) +) + |