diff options
Diffstat (limited to 'src/wasm-binary.h')
-rw-r--r-- | src/wasm-binary.h | 56 |
1 files changed, 39 insertions, 17 deletions
diff --git a/src/wasm-binary.h b/src/wasm-binary.h index aad142d44..04bebddc5 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -706,7 +706,8 @@ public: o << U32LEB(num); for (auto& segment : wasm->memory.segments) { if (segment.data.size() == 0) continue; - o << U32LEB(segment.offset); + writeExpression(segment.offset); + o << int8_t(BinaryConsts::End); writeInlineBuffer(&segment.data[0], segment.data.size()); } finishSection(start); @@ -739,12 +740,19 @@ public: } void writeFunctionTable() { - if (wasm->table.names.size() == 0) return; + if (wasm->table.segments.size() == 0) return; if (debug) std::cerr << "== writeFunctionTable" << std::endl; auto start = startSection(BinaryConsts::Section::FunctionTable); - o << U32LEB(wasm->table.names.size()); - for (auto name : wasm->table.names) { - o << U32LEB(getFunctionIndex(name)); + o << U32LEB(wasm->table.initial); + o << U32LEB(wasm->table.max); + o << U32LEB(wasm->table.segments.size()); + for (auto& segment : wasm->table.segments) { + writeExpression(segment.offset); + o << int8_t(BinaryConsts::End); + o << U32LEB(segment.data.size()); + for (auto name : segment.data) { + o << U32LEB(getFunctionIndex(name)); + } } finishSection(start); } @@ -1572,6 +1580,15 @@ public: } } + Expression* readExpression() { + assert(depth == 0); + processExpressions(); + assert(expressionStack.size() == 1); + auto* ret = popExpression(); + assert(depth == 0); + return ret; + } + void readGlobals() { if (debug) std::cerr << "== readGlobals" << std::endl; size_t num = getU32LEB(); @@ -1580,11 +1597,7 @@ public: if (debug) std::cerr << "read one" << std::endl; auto curr = new Global; curr->type = getWasmType(); - assert(depth == 0); - processExpressions(); - assert(expressionStack.size() == 1); - curr->init = popExpression(); - assert(depth == 0); + curr->init = readExpression(); wasm.addGlobal(curr); } } @@ -1638,9 +1651,12 @@ public: } } - for (size_t index : functionTable) { - assert(index < wasm.functions.size()); - wasm.table.names.push_back(wasm.functions[index]->name); + for (auto& pair : functionTable) { + auto i = pair.first; + auto& indexes = pair.second; + for (auto j : indexes) { + wasm.table.segments[i].data.push_back(wasm.functions[j]->name); + } } } @@ -1649,7 +1665,7 @@ public: auto num = getU32LEB(); for (size_t i = 0; i < num; i++) { Memory::Segment curr; - auto offset = getU32LEB(); + auto offset = readExpression(); auto size = getU32LEB(); std::vector<char> buffer; buffer.resize(size); @@ -1660,14 +1676,20 @@ public: } } - std::vector<size_t> functionTable; + std::map<Index, std::vector<Index>> functionTable; void readFunctionTable() { if (debug) std::cerr << "== readFunctionTable" << std::endl; + wasm.table.initial = getU32LEB(); + wasm.table.max = getU32LEB(); auto num = getU32LEB(); for (size_t i = 0; i < num; i++) { - auto index = getU32LEB(); - functionTable.push_back(index); + wasm.table.segments.emplace_back(readExpression()); + auto& temporary = functionTable[i]; + auto size = getU32LEB(); + for (Index j = 0; j < size; j++) { + temporary.push_back(getU32LEB()); + } } } |