Subscribers: jfb, dschuff
Differential Revision: https://reviews.llvm.org/D32660
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@301687
91177308-0d34-0410-b5e6-
96231b3b80d8
const char WasmMagic[] = {'\0', 'a', 's', 'm'};
// Wasm binary format version
const uint32_t WasmVersion = 0x1;
+// Wasm uses a 64k page size
+const uint32_t WasmPageSize = 65536;
struct WasmObjectHeader {
StringRef Magic;
// For now, always emit the memory section, since loads and stores are not
// valid without it. In the future, we could perhaps be more clever and omit
// it if there are no loads or stores.
- startSection(Section, wasm::WASM_SEC_MEMORY);
+ uint32_t NumPages =
+ (DataBytes.size() + wasm::WasmPageSize - 1) / wasm::WasmPageSize;
+ startSection(Section, wasm::WASM_SEC_MEMORY);
encodeULEB128(1, getStream()); // number of memory spaces
encodeULEB128(0, getStream()); // flags
- encodeULEB128(DataBytes.size(), getStream()); // initial
+ encodeULEB128(NumPages, getStream()); // initial
endSection(Section);
; CHECK: }
; CHECK: Section {
; CHECK: Type: MEMORY (0x5)
+; CHECK: Memories [
+; CHECK: Memory {
+; CHECK: InitialPages: 1
+; CHECK: }
+; CHECK: ]
; CHECK: }
; CHECK: Section {
; CHECK: Type: GLOBAL (0x6)
WASM-NEXT: Type: MEMORY (0x5)
WASM-NEXT: Size: 3
WASM-NEXT: Offset: 66
+WASM-NEXT: Memories [
+WASM-NEXT: Memory {
+WASM-NEXT: InitialPages: 0
+WASM-NEXT: }
+WASM-NEXT: ]
WASM-NEXT: }
WASM-NEXT: Section {
WASM-NEXT: Type: EXPORT (0x7)
W.printEnum("Type", WasmSec.Type, makeArrayRef(WasmSectionTypes));
W.printNumber("Size", (uint64_t)WasmSec.Content.size());
W.printNumber("Offset", WasmSec.Offset);
- if (WasmSec.Type == wasm::WASM_SEC_CUSTOM) {
+ switch (WasmSec.Type) {
+ case wasm::WASM_SEC_CUSTOM:
W.printString("Name", WasmSec.Name);
+ break;
+ case wasm::WASM_SEC_MEMORY:
+ ListScope Group(W, "Memories");
+ for (const wasm::WasmLimits &Memory : Obj->memories()) {
+ DictScope Group(W, "Memory");
+ W.printNumber("InitialPages", Memory.Initial);
+ if (Memory.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) {
+ W.printNumber("MaxPages", WasmSec.Offset);
+ }
+ }
+ break;
}
if (opts::SectionRelocations) {