summaryrefslogtreecommitdiff
path: root/lib/MC
diff options
context:
space:
mode:
authorDan Gohman <dan433584@gmail.com>2017-12-06 23:57:11 +0000
committerDan Gohman <dan433584@gmail.com>2017-12-06 23:57:11 +0000
commitedc3af3e9ac7436552e6bfdd264e619315ee6247 (patch)
tree946c1bb1c7a74d9bdce38bce49ae5da97c58233d /lib/MC
parent72671d6350a4f516ec3849c54dfdc7c1546e16ae (diff)
[WebAssembly] Import the linear memory and function table.
Instead of having .o files contain linear-memory and function table definitions, use imports. This is more consistent with the stack pointer being imported, and it's consistent with the linker being the one to decide whether linear memory and function table are imported or defined in the linked output. This implements tool-conventions #23. Differential Revision: https://reviews.llvm.org/D40875 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@319989 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/MC')
-rw-r--r--lib/MC/WasmObjectWriter.cpp82
1 files changed, 42 insertions, 40 deletions
diff --git a/lib/MC/WasmObjectWriter.cpp b/lib/MC/WasmObjectWriter.cpp
index 351927d628a..7c50f437cfa 100644
--- a/lib/MC/WasmObjectWriter.cpp
+++ b/lib/MC/WasmObjectWriter.cpp
@@ -270,10 +270,9 @@ private:
}
void writeTypeSection(ArrayRef<WasmFunctionType> FunctionTypes);
- void writeImportSection(ArrayRef<WasmImport> Imports);
+ void writeImportSection(ArrayRef<WasmImport> Imports, uint32_t DataSize,
+ uint32_t NumElements);
void writeFunctionSection(ArrayRef<WasmFunction> Functions);
- void writeTableSection(uint32_t NumElements);
- void writeMemorySection(uint32_t DataSize);
void writeGlobalSection();
void writeExportSection(ArrayRef<WasmExport> Exports);
void writeElemSection(ArrayRef<uint32_t> TableElems);
@@ -661,10 +660,14 @@ void WasmObjectWriter::writeTypeSection(
endSection(Section);
}
-void WasmObjectWriter::writeImportSection(ArrayRef<WasmImport> Imports) {
+void WasmObjectWriter::writeImportSection(ArrayRef<WasmImport> Imports,
+ uint32_t DataSize,
+ uint32_t NumElements) {
if (Imports.empty())
return;
+ uint32_t NumPages = (DataSize + wasm::WasmPageSize - 1) / wasm::WasmPageSize;
+
SectionBookkeeping Section;
startSection(Section, wasm::WASM_SEC_IMPORT);
@@ -683,6 +686,15 @@ void WasmObjectWriter::writeImportSection(ArrayRef<WasmImport> 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");
}
@@ -705,39 +717,6 @@ void WasmObjectWriter::writeFunctionSection(ArrayRef<WasmFunction> 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;
@@ -1085,6 +1064,29 @@ 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<MCSymbolWasm>(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<MCSymbolWasm>(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<const MCSymbolWasm &>(S);
@@ -1295,10 +1297,10 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
writeHeader(Asm);
writeTypeSection(FunctionTypes);
- writeImportSection(Imports);
+ writeImportSection(Imports, DataSize, TableElems.size());
writeFunctionSection(Functions);
- writeTableSection(TableElems.size());
- writeMemorySection(DataSize);
+ // Skip the "table" section; we import the table instead.
+ // Skip the "memory" section; we import the memory instead.
writeGlobalSection();
writeExportSection(Exports);
// TODO: Start Section