From ff0730b3fcd488caf121e2e4d1aa8506ff13f3c6 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Fri, 28 Apr 2017 21:12:09 +0000 Subject: [PATCH] [WebAssembly] Write initial memory in pages not bytes Subscribers: jfb, dschuff Differential Revision: https://reviews.llvm.org/D32660 llvm-svn: 301687 --- llvm/include/llvm/Support/Wasm.h | 2 ++ llvm/lib/MC/WasmObjectWriter.cpp | 6 ++++-- llvm/test/MC/WebAssembly/sections.ll | 5 +++++ llvm/test/tools/llvm-readobj/sections.test | 5 +++++ llvm/tools/llvm-readobj/WasmDumper.cpp | 14 +++++++++++++- 5 files changed, 29 insertions(+), 3 deletions(-) diff --git a/llvm/include/llvm/Support/Wasm.h b/llvm/include/llvm/Support/Wasm.h index 8e6c418..b02fcb1 100644 --- a/llvm/include/llvm/Support/Wasm.h +++ b/llvm/include/llvm/Support/Wasm.h @@ -24,6 +24,8 @@ namespace wasm { 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; diff --git a/llvm/lib/MC/WasmObjectWriter.cpp b/llvm/lib/MC/WasmObjectWriter.cpp index 6444046..ae66829 100644 --- a/llvm/lib/MC/WasmObjectWriter.cpp +++ b/llvm/lib/MC/WasmObjectWriter.cpp @@ -913,12 +913,14 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm, // 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); diff --git a/llvm/test/MC/WebAssembly/sections.ll b/llvm/test/MC/WebAssembly/sections.ll index e7ae2b3..85bf081 100644 --- a/llvm/test/MC/WebAssembly/sections.ll +++ b/llvm/test/MC/WebAssembly/sections.ll @@ -32,6 +32,11 @@ entry: ; CHECK: } ; CHECK: Section { ; CHECK: Type: MEMORY (0x5) +; CHECK: Memories [ +; CHECK: Memory { +; CHECK: InitialPages: 1 +; CHECK: } +; CHECK: ] ; CHECK: } ; CHECK: Section { ; CHECK: Type: GLOBAL (0x6) diff --git a/llvm/test/tools/llvm-readobj/sections.test b/llvm/test/tools/llvm-readobj/sections.test index 312c47f..1747ee4 100644 --- a/llvm/test/tools/llvm-readobj/sections.test +++ b/llvm/test/tools/llvm-readobj/sections.test @@ -518,6 +518,11 @@ WASM-NEXT: Section { 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) diff --git a/llvm/tools/llvm-readobj/WasmDumper.cpp b/llvm/tools/llvm-readobj/WasmDumper.cpp index f07dd07..2161429 100644 --- a/llvm/tools/llvm-readobj/WasmDumper.cpp +++ b/llvm/tools/llvm-readobj/WasmDumper.cpp @@ -150,8 +150,20 @@ void WasmDumper::printSections() { 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) { -- 2.7.4