diff options
Diffstat (limited to 'src/wasm-binary.h')
-rw-r--r-- | src/wasm-binary.h | 54 |
1 files changed, 31 insertions, 23 deletions
diff --git a/src/wasm-binary.h b/src/wasm-binary.h index a6c388c52..89cb85535 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -252,7 +252,7 @@ enum ASTNodes { F64CopySign = 0x91, F64Ceil = 0x92, F64Floor = 0x93, - F64Trunc = 0x94, // XXX what is this? trunc f64 to f64? + F64Trunc = 0x94, F64NearestInt = 0x95, F64Sqrt = 0x96, F64Eq = 0x97, @@ -388,6 +388,7 @@ public: } void writeMemory() { + if (wasm->memory.max == 0) return; if (debug) std::cerr << "== writeMemory" << std::endl; o << int8_t(BinaryConsts::Memory) << int8_t(log2(wasm->memory.initial)) << int8_t(log2(wasm->memory.max)) @@ -395,6 +396,7 @@ public: } void writeSignatures() { + if (wasm->functionTypes.size() == 0) return; if (debug) std::cerr << "== writeSignatures" << std::endl; o << int8_t(BinaryConsts::Signatures) << LEB128(wasm->functionTypes.size()); for (auto* type : wasm->functionTypes) { @@ -456,6 +458,7 @@ public: } void writeFunctions() { + if (wasm->functions.size() + wasm->imports.size() == 0) return; if (debug) std::cerr << "== writeFunctions" << std::endl; size_t total = wasm->imports.size() + wasm->functions.size(); o << int8_t(BinaryConsts::Functions) << LEB128(total); @@ -491,7 +494,7 @@ public: } if (function) { size_t sizePos = o.size(); - o << (uint16_t)0; // placeholder, we fill in the size later when we have it + o << (uint32_t)0; // placeholder, we fill in the size later when we have it // XXX int32, diverge from v8 format, to get more code to compile size_t start = o.size(); depth = 0; recurse(function->body); @@ -499,12 +502,13 @@ public: size_t size = o.size() - start; assert(size <= std::numeric_limits<uint16_t>::max()); if (debug) std::cerr << "body size: " << size << ", writing at " << sizePos << ", next starts at " << o.size() << std::endl; - o.writeAt(sizePos, uint16_t(size)); + o.writeAt(sizePos, uint32_t(size)); // XXX int32, diverge from v8 format, to get more code to compile } } } void writeDataSegments() { + if (wasm->memory.segments.size() == 0) return; o << int8_t(BinaryConsts::DataSegments) << LEB128(wasm->memory.segments.size()); for (auto& segment : wasm->memory.segments) { o << int32_t(segment.offset); @@ -526,6 +530,7 @@ public: } void writeFunctionTable() { + if (wasm->table.names.size() == 0) return; if (debug) std::cerr << "== writeFunctionTable" << std::endl; o << int8_t(BinaryConsts::FunctionTable) << LEB128(wasm->table.names.size()); for (auto name : wasm->table.names) { @@ -585,7 +590,7 @@ public: void visitBlock(Block *curr) { if (debug) std::cerr << "zz node: Block" << std::endl; - o << int8_t(BinaryConsts::Block) << int8_t(curr->list.size()); + o << int8_t(BinaryConsts::Block) << LEB128(curr->list.size()); // XXX larger block size, divergence from v8 to get more code to build breakStack.push_back(curr->name); size_t i = 0; for (auto* child : curr->list) { @@ -924,12 +929,24 @@ public: WasmBinaryBuilder(AllocatingModule& wasm, std::vector<char>& input, bool debug) : wasm(wasm), allocator(wasm.allocator), input(input), debug(debug), pos(0) {} void read() { - readMemory(); - readSignatures(); - readFunctions(); - readDataSegments(); - readFunctionTable(); - readEnd(); + // read sections until the end + while (1) { + int8_t section = getInt8(); + + if (section == BinaryConsts::End) { + if (debug) std::cerr << "== readEnd" << std::endl; + break; + } + + switch (section) { + case BinaryConsts::Memory: readMemory(); break; + case BinaryConsts::Signatures: readSignatures(); break; + case BinaryConsts::Functions: readFunctions(); break; + case BinaryConsts::DataSegments: readDataSegments(); break; + case BinaryConsts::FunctionTable: readFunctionTable(); break; + default: abort(); + } + } processFunctions(); } @@ -1026,7 +1043,6 @@ public: void readMemory() { if (debug) std::cerr << "== readMemory" << std::endl; - verifyInt8(BinaryConsts::Memory); wasm.memory.initial = std::pow<size_t>(2, getInt8()); wasm.memory.max = std::pow<size_t>(2, getInt8()); verifyInt8(1); // export memory @@ -1034,7 +1050,6 @@ public: void readSignatures() { if (debug) std::cerr << "== readSignatures" << std::endl; - verifyInt8(BinaryConsts::Signatures); size_t numTypes = getLEB128(); if (debug) std::cerr << "num: " << numTypes << std::endl; for (size_t i = 0; i < numTypes; i++) { @@ -1060,7 +1075,6 @@ public: void readFunctions() { if (debug) std::cerr << "== readFunctions" << std::endl; - verifyInt8(BinaryConsts::Functions); size_t total = getLEB128(); // imports and functions for (size_t i = 0; i < total; i++) { if (debug) std::cerr << "read one at " << pos << std::endl; @@ -1106,7 +1120,7 @@ public: addLocals(f32); addLocals(f64); } - size_t size = getInt16(); + size_t size = getInt32(); // XXX int32, diverge from v8 format, to get more code to compile // we can't read the function yet - it might call other functions that are defined later, // and we do depend on the function type, as well as the mappedFunctions table. functions.emplace_back(func, pos, size); @@ -1165,7 +1179,6 @@ public: void readDataSegments() { if (debug) std::cerr << "== readDataSegments" << std::endl; - verifyInt8(BinaryConsts::DataSegments); auto num = getLEB128(); for (size_t i = 0; i < num; i++) { auto curr = allocator.alloc<Memory::Segment>(); @@ -1182,18 +1195,12 @@ public: void readFunctionTable() { if (debug) std::cerr << "== readFunctionTable" << std::endl; - verifyInt8(BinaryConsts::FunctionTable); auto num = getLEB128(); for (size_t i = 0; i < num; i++) { wasm.table.names.push_back(mappedFunctions[getInt16()]); } } - void readEnd() { - if (debug) std::cerr << "== readEnd" << std::endl; - verifyInt8(BinaryConsts::End); - } - // AST reading int depth; // only for debugging @@ -1258,10 +1265,10 @@ public: void visitBlock(Block *curr) { if (debug) std::cerr << "zz node: Block" << std::endl; - auto num = getInt8(); + auto num = getLEB128(); // XXX larger block size, divergence from v8 to get more code to build curr->name = getNextLabel(); breakStack.push_back(curr->name); - for (auto i = 0; i < num; i++) { + for (uint32_t i = 0; i < num; i++) { if (debug) std::cerr << " " << size_t(curr) << "\n zz Block element " << i << std::endl; Expression* child; readExpression(child); @@ -1569,6 +1576,7 @@ public: case BinaryConsts::MemorySize: curr->op = MemorySize; break; case BinaryConsts::GrowMemory: { curr->op = GrowMemory; + curr->operands.resize(1); readExpression(curr->operands[0]); break; } |