}
void writeTypeSection(ArrayRef<WasmFunctionType> FunctionTypes);
- void writeImportSection(ArrayRef<WasmImport> Imports, uint32_t DataSize,
- uint32_t NumElements);
+ void writeImportSection(ArrayRef<WasmImport> Imports);
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);
endSection(Section);
}
-void WasmObjectWriter::writeImportSection(ArrayRef<WasmImport> Imports,
- uint32_t DataSize,
- uint32_t NumElements) {
+void WasmObjectWriter::writeImportSection(ArrayRef<WasmImport> Imports) {
if (Imports.empty())
return;
- uint32_t NumPages = (DataSize + wasm::WasmPageSize - 1) / wasm::WasmPageSize;
-
SectionBookkeeping Section;
startSection(Section, wasm::WASM_SEC_IMPORT);
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");
}
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;
}
}
- // 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);
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
; 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
; 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
; 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