From: Sam Clegg Date: Thu, 7 Dec 2017 03:05:45 +0000 (+0000) Subject: Revert "[WebAssembly] Import the linear memory and function table." X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=03f3a72f26ad94b0f41acc983a6624158c129584;p=llvm Revert "[WebAssembly] Import the linear memory and function table." We need to a little time to prepare and lld-side change that supports this. Original change: https://reviews.llvm.org/D40875 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@320003 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/MC/WasmObjectWriter.cpp b/lib/MC/WasmObjectWriter.cpp index 7c50f437cfa..351927d628a 100644 --- a/lib/MC/WasmObjectWriter.cpp +++ b/lib/MC/WasmObjectWriter.cpp @@ -270,9 +270,10 @@ private: } void writeTypeSection(ArrayRef FunctionTypes); - void writeImportSection(ArrayRef Imports, uint32_t DataSize, - uint32_t NumElements); + void writeImportSection(ArrayRef Imports); void writeFunctionSection(ArrayRef Functions); + void writeTableSection(uint32_t NumElements); + void writeMemorySection(uint32_t DataSize); void writeGlobalSection(); void writeExportSection(ArrayRef Exports); void writeElemSection(ArrayRef TableElems); @@ -660,14 +661,10 @@ void WasmObjectWriter::writeTypeSection( endSection(Section); } -void WasmObjectWriter::writeImportSection(ArrayRef Imports, - uint32_t DataSize, - uint32_t NumElements) { +void WasmObjectWriter::writeImportSection(ArrayRef Imports) { if (Imports.empty()) return; - uint32_t NumPages = (DataSize + wasm::WasmPageSize - 1) / wasm::WasmPageSize; - SectionBookkeeping Section; startSection(Section, wasm::WASM_SEC_IMPORT); @@ -686,15 +683,6 @@ void WasmObjectWriter::writeImportSection(ArrayRef Imports, encodeSLEB128(int32_t(Import.Type), getStream()); encodeULEB128(int32_t(Import.IsMutable), getStream()); break; - case wasm::WASM_EXTERNAL_MEMORY: - encodeULEB128(0, getStream()); // flags - encodeULEB128(NumPages, getStream()); // initial - break; - case wasm::WASM_EXTERNAL_TABLE: - encodeSLEB128(int32_t(Import.Type), getStream()); - encodeULEB128(0, getStream()); // flags - encodeULEB128(NumElements, getStream()); // initial - break; default: llvm_unreachable("unsupported import kind"); } @@ -717,6 +705,39 @@ void WasmObjectWriter::writeFunctionSection(ArrayRef Functions) { endSection(Section); } +void WasmObjectWriter::writeTableSection(uint32_t NumElements) { + // For now, always emit the table section, since indirect calls are not + // valid without it. In the future, we could perhaps be more clever and omit + // it if there are no indirect calls. + + SectionBookkeeping Section; + startSection(Section, wasm::WASM_SEC_TABLE); + + encodeULEB128(1, getStream()); // The number of tables. + // Fixed to 1 for now. + encodeSLEB128(wasm::WASM_TYPE_ANYFUNC, getStream()); // Type of table + encodeULEB128(0, getStream()); // flags + encodeULEB128(NumElements, getStream()); // initial + + endSection(Section); +} + +void WasmObjectWriter::writeMemorySection(uint32_t DataSize) { + // 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. + SectionBookkeeping Section; + uint32_t NumPages = (DataSize + wasm::WasmPageSize - 1) / wasm::WasmPageSize; + + startSection(Section, wasm::WASM_SEC_MEMORY); + encodeULEB128(1, getStream()); // number of memory spaces + + encodeULEB128(0, getStream()); // flags + encodeULEB128(NumPages, getStream()); // initial + + endSection(Section); +} + void WasmObjectWriter::writeGlobalSection() { if (Globals.empty()) return; @@ -1064,29 +1085,6 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm, } } - // For now, always emit the memory import, 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. - MCSymbolWasm *MemorySym = - cast(Ctx.getOrCreateSymbol("__linear_memory")); - WasmImport MemImport; - MemImport.ModuleName = MemorySym->getModuleName(); - MemImport.FieldName = MemorySym->getName(); - MemImport.Kind = wasm::WASM_EXTERNAL_MEMORY; - Imports.push_back(MemImport); - - // For now, always emit the table section, since indirect calls are not - // valid without it. In the future, we could perhaps be more clever and omit - // it if there are no indirect calls. - MCSymbolWasm *TableSym = - cast(Ctx.getOrCreateSymbol("__indirect_function_table")); - WasmImport TableImport; - TableImport.ModuleName = TableSym->getModuleName(); - TableImport.FieldName = TableSym->getName(); - TableImport.Kind = wasm::WASM_EXTERNAL_TABLE; - TableImport.Type = wasm::WASM_TYPE_ANYFUNC; - Imports.push_back(TableImport); - // Populate FunctionTypeIndices and Imports. for (const MCSymbol &S : Asm.symbols()) { const auto &WS = static_cast(S); @@ -1297,10 +1295,10 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm, writeHeader(Asm); writeTypeSection(FunctionTypes); - writeImportSection(Imports, DataSize, TableElems.size()); + writeImportSection(Imports); writeFunctionSection(Functions); - // Skip the "table" section; we import the table instead. - // Skip the "memory" section; we import the memory instead. + writeTableSection(TableElems.size()); + writeMemorySection(DataSize); writeGlobalSection(); writeExportSection(Exports); // TODO: Start Section diff --git a/test/MC/WebAssembly/external-func-address.ll b/test/MC/WebAssembly/external-func-address.ll index 9386d945ccd..53da9805f98 100644 --- a/test/MC/WebAssembly/external-func-address.ll +++ b/test/MC/WebAssembly/external-func-address.ll @@ -17,11 +17,7 @@ declare void @f1(i32) #1 ; CHECK-NEXT: - I32 ; CHECK: - Type: IMPORT ; CHECK-NEXT: Imports: -; CHECK: - Module: env -; CHECK-NEXT: Field: __linear_memory -; CHECK: - Module: env -; CHECK-NEXT: Field: __indirect_function_table -; CHECK: - Module: env +; CHECK-NEXT: - Module: env ; CHECK-NEXT: Field: f1 ; CHECK-NEXT: Kind: FUNCTION ; CHECK-NEXT: SigIndex: 0 diff --git a/test/MC/WebAssembly/func-address.ll b/test/MC/WebAssembly/func-address.ll index 16d38e1ebf2..15c09e0ebd8 100644 --- a/test/MC/WebAssembly/func-address.ll +++ b/test/MC/WebAssembly/func-address.ll @@ -28,7 +28,7 @@ entry: ; CHECK: } ; CHECK: Relocations [ -; CHECK: Section (6) CODE { +; CHECK: Section (8) CODE { ; CHECK: Relocation { ; CHECK: Type: R_WEBASSEMBLY_FUNCTION_INDEX_LEB (0) ; CHECK: Offset: 0x4 diff --git a/test/MC/WebAssembly/init-fini-array.ll b/test/MC/WebAssembly/init-fini-array.ll index b7a1826bdc4..5cd32ff9bf5 100644 --- a/test/MC/WebAssembly/init-fini-array.ll +++ b/test/MC/WebAssembly/init-fini-array.ll @@ -14,18 +14,6 @@ declare void @func2() ; CHECK: - Type: IMPORT ; CHECK-NEXT: Imports: ; CHECK-NEXT: - Module: env -; CHECK-NEXT: Field: __linear_memory -; CHECK-NEXT: Kind: MEMORY -; CHECK-NEXT: Memory: -; CHECK-NEXT: Initial: 0x00000001 -; CHECK-NEXT: - Module: env -; CHECK-NEXT: Field: __indirect_function_table -; CHECK-NEXT: Kind: TABLE -; CHECK-NEXT: Table: -; CHECK-NEXT: ElemType: ANYFUNC -; CHECK-NEXT: Limits: -; CHECK-NEXT: Initial: 0x00000002 -; CHECK-NEXT: - Module: env ; CHECK-NEXT: Field: func1 ; CHECK-NEXT: Kind: FUNCTION ; CHECK-NEXT: SigIndex: 0 @@ -33,6 +21,14 @@ declare void @func2() ; CHECK-NEXT: Field: func2 ; CHECK-NEXT: Kind: FUNCTION ; CHECK-NEXT: SigIndex: 0 +; CHECK-NEXT: - Type: TABLE +; CHECK-NEXT: Tables: +; CHECK-NEXT: - ElemType: ANYFUNC +; CHECK-NEXT: Limits: +; CHECK-NEXT: Initial: 0x00000002 +; CHECK-NEXT: - Type: MEMORY +; CHECK-NEXT: Memories: +; CHECK-NEXT: - Initial: 0x00000001 ; CHECK-NEXT: - Type: GLOBAL ; CHECK-NEXT: Globals: ; CHECK-NEXT: - Type: I32 diff --git a/test/MC/WebAssembly/reloc-code.ll b/test/MC/WebAssembly/reloc-code.ll index e9aff890cff..f007b63ca83 100644 --- a/test/MC/WebAssembly/reloc-code.ll +++ b/test/MC/WebAssembly/reloc-code.ll @@ -22,7 +22,7 @@ entry: ; CHECK: Format: WASM ; CHECK: Relocations [ -; CHECK-NEXT: Section (6) CODE { +; CHECK-NEXT: Section (8) CODE { ; CHECK-NEXT: Relocation { ; CHECK-NEXT: Type: R_WEBASSEMBLY_MEMORY_ADDR_LEB (3) ; CHECK-NEXT: Offset: 0x9 diff --git a/test/MC/WebAssembly/reloc-data.ll b/test/MC/WebAssembly/reloc-data.ll index ca78d8b158f..519df036751 100644 --- a/test/MC/WebAssembly/reloc-data.ll +++ b/test/MC/WebAssembly/reloc-data.ll @@ -10,7 +10,7 @@ ; CHECK: Format: WASM ; CHECK: Relocations [ -; CHECK-NEXT: Section (4) DATA { +; CHECK-NEXT: Section (6) DATA { ; CHECK-NEXT: Relocation { ; CHECK-NEXT: Type: R_WEBASSEMBLY_MEMORY_ADDR_I32 (5) ; CHECK-NEXT: Offset: 0x13 diff --git a/test/MC/WebAssembly/sections.ll b/test/MC/WebAssembly/sections.ll index 14c9cc6ab07..85bf0818509 100644 --- a/test/MC/WebAssembly/sections.ll +++ b/test/MC/WebAssembly/sections.ll @@ -28,6 +28,17 @@ entry: ; CHECK: Type: FUNCTION (0x3) ; CHECK: } ; CHECK: Section { +; CHECK: Type: TABLE (0x4) +; CHECK: } +; CHECK: Section { +; CHECK: Type: MEMORY (0x5) +; CHECK: Memories [ +; CHECK: Memory { +; CHECK: InitialPages: 1 +; CHECK: } +; CHECK: ] +; CHECK: } +; CHECK: Section { ; CHECK: Type: GLOBAL (0x6) ; CHECK: } ; CHECK: Section { diff --git a/test/MC/WebAssembly/weak-alias.ll b/test/MC/WebAssembly/weak-alias.ll index 5c85fc09b03..4129870ce23 100644 --- a/test/MC/WebAssembly/weak-alias.ll +++ b/test/MC/WebAssembly/weak-alias.ll @@ -29,22 +29,16 @@ entry: ; CHECK-NEXT: - Index: 0 ; CHECK-NEXT: ReturnType: I32 ; CHECK-NEXT: ParamTypes: -; CHECK-NEXT: - Type: IMPORT -; CHECK-NEXT: Imports: -; CHECK-NEXT: - Module: env -; CHECK-NEXT: Field: __linear_memory -; CHECK-NEXT: Kind: MEMORY -; CHECK-NEXT: Memory: -; CHECK-NEXT: Initial: 0x00000001 -; CHECK-NEXT: - Module: env -; CHECK-NEXT: Field: __indirect_function_table -; CHECK-NEXT: Kind: TABLE -; CHECK-NEXT: Table: -; CHECK-NEXT: ElemType: ANYFUNC -; CHECK-NEXT: Limits: -; CHECK-NEXT: Initial: 0x00000000 ; CHECK-NEXT: - Type: FUNCTION ; CHECK-NEXT: FunctionTypes: [ 0, 0 ] +; CHECK-NEXT: - Type: TABLE +; CHECK-NEXT: Tables: +; CHECK-NEXT: - ElemType: ANYFUNC +; CHECK-NEXT: Limits: +; CHECK-NEXT: Initial: 0x00000000 +; CHECK-NEXT: - Type: MEMORY +; CHECK-NEXT: Memories: +; CHECK-NEXT: - Initial: 0x00000001 ; CHECK-NEXT: - Type: GLOBAL ; CHECK-NEXT: Globals: ; CHECK-NEXT: - Type: I32 diff --git a/test/MC/WebAssembly/weak.ll b/test/MC/WebAssembly/weak.ll index af177b53595..f7353569024 100644 --- a/test/MC/WebAssembly/weak.ll +++ b/test/MC/WebAssembly/weak.ll @@ -12,11 +12,7 @@ entry: ; CHECK: - Type: IMPORT ; CHECK-NEXT: Imports: -; CHECK: - Module: env -; CHECK-NEXT: Field: __linear_memory -; CHECK: - Module: env -; CHECK-NEXT: Field: __indirect_function_table -; CHECK: - Module: env +; CHECK-NEXT: - Module: env ; CHECK-NEXT: Field: weak_external_data ; CHECK-NEXT: Kind: GLOBAL ; CHECK-NEXT: GlobalType: I32