Index: llvm/trunk/lib/MC/WasmObjectWriter.cpp =================================================================== --- llvm/trunk/lib/MC/WasmObjectWriter.cpp +++ llvm/trunk/lib/MC/WasmObjectWriter.cpp @@ -111,28 +111,12 @@ SmallVector Data; }; -// A wasm import to be written into the import section. -struct WasmImport { - StringRef ModuleName; - StringRef FieldName; - unsigned Kind; - int32_t Type; - bool IsMutable; -}; - // A wasm function to be written into the function section. struct WasmFunction { int32_t Type; const MCSymbolWasm *Sym; }; -// A wasm export to be written into the export section. -struct WasmExport { - StringRef FieldName; - unsigned Kind; - uint32_t Index; -}; - // A wasm global to be written into the global section. struct WasmGlobal { wasm::WasmGlobalType Type; @@ -270,11 +254,11 @@ } void writeTypeSection(ArrayRef FunctionTypes); - void writeImportSection(ArrayRef Imports, uint32_t DataSize, + void writeImportSection(ArrayRef Imports, uint32_t DataSize, uint32_t NumElements); void writeFunctionSection(ArrayRef Functions); void writeGlobalSection(); - void writeExportSection(ArrayRef Exports); + void writeExportSection(ArrayRef Exports); void writeElemSection(ArrayRef TableElems); void writeCodeSection(const MCAssembler &Asm, const MCAsmLayout &Layout, ArrayRef Functions); @@ -659,7 +643,7 @@ endSection(Section); } -void WasmObjectWriter::writeImportSection(ArrayRef Imports, +void WasmObjectWriter::writeImportSection(ArrayRef Imports, uint32_t DataSize, uint32_t NumElements) { if (Imports.empty()) @@ -671,26 +655,25 @@ startSection(Section, wasm::WASM_SEC_IMPORT); encodeULEB128(Imports.size(), getStream()); - for (const WasmImport &Import : Imports) { - writeString(Import.ModuleName); - writeString(Import.FieldName); - + for (const wasm::WasmImport &Import : Imports) { + writeString(Import.Module); + writeString(Import.Field); encodeULEB128(Import.Kind, getStream()); switch (Import.Kind) { case wasm::WASM_EXTERNAL_FUNCTION: - encodeULEB128(Import.Type, getStream()); + encodeULEB128(Import.SigIndex, getStream()); break; case wasm::WASM_EXTERNAL_GLOBAL: - encodeSLEB128(int32_t(Import.Type), getStream()); - encodeULEB128(int32_t(Import.IsMutable), getStream()); + encodeSLEB128(Import.Global.Type, getStream()); + encodeULEB128(uint32_t(Import.Global.Mutable), 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()); + encodeSLEB128(Import.Table.ElemType, getStream()); encodeULEB128(0, getStream()); // flags encodeULEB128(NumElements, getStream()); // initial break; @@ -736,7 +719,7 @@ endSection(Section); } -void WasmObjectWriter::writeExportSection(ArrayRef Exports) { +void WasmObjectWriter::writeExportSection(ArrayRef Exports) { if (Exports.empty()) return; @@ -744,8 +727,8 @@ startSection(Section, wasm::WASM_SEC_EXPORT); encodeULEB128(Exports.size(), getStream()); - for (const WasmExport &Export : Exports) { - writeString(Export.FieldName); + for (const wasm::WasmExport &Export : Exports) { + writeString(Export.Name); encodeSLEB128(Export.Kind, getStream()); encodeULEB128(Export.Index, getStream()); } @@ -963,8 +946,8 @@ // Collect information from the available symbols. SmallVector Functions; SmallVector TableElems; - SmallVector Imports; - SmallVector Exports; + SmallVector Imports; + SmallVector Exports; SmallVector, 4> SymbolFlags; SmallVector, 2> InitFuncs; std::map> Comdats; @@ -976,9 +959,9 @@ // 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(); + wasm::WasmImport MemImport; + MemImport.Module = MemorySym->getModuleName(); + MemImport.Field = MemorySym->getName(); MemImport.Kind = wasm::WASM_EXTERNAL_MEMORY; Imports.push_back(MemImport); @@ -987,11 +970,11 @@ // 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(); + wasm::WasmImport TableImport; + TableImport.Module = TableSym->getModuleName(); + TableImport.Field = TableSym->getName(); TableImport.Kind = wasm::WASM_EXTERNAL_TABLE; - TableImport.Type = wasm::WASM_TYPE_ANYFUNC; + TableImport.Table.ElemType = wasm::WASM_TYPE_ANYFUNC; Imports.push_back(TableImport); // Populate FunctionTypeIndices and Imports. @@ -1009,25 +992,25 @@ // If the symbol is not defined in this translation unit, import it. if ((!WS.isDefined() && !WS.isComdat()) || WS.isVariable()) { - WasmImport Import; - Import.ModuleName = WS.getModuleName(); - Import.FieldName = WS.getName(); + wasm::WasmImport Import; + Import.Module = WS.getModuleName(); + Import.Field = WS.getName(); if (WS.isFunction()) { Import.Kind = wasm::WASM_EXTERNAL_FUNCTION; - Import.Type = getFunctionType(WS); + Import.SigIndex = getFunctionType(WS); SymbolIndices[&WS] = NumFunctionImports; ++NumFunctionImports; } else { Import.Kind = wasm::WASM_EXTERNAL_GLOBAL; - Import.Type = PtrType; - Import.IsMutable = false; - SymbolIndices[&WS] = NumGlobalImports; - + Import.Global.Type = PtrType; // If this global is the stack pointer, make it mutable. if (WS.getName() == "__stack_pointer") - Import.IsMutable = true; + Import.Global.Mutable = true; + else + Import.Global.Mutable = false; + SymbolIndices[&WS] = NumGlobalImports; ++NumGlobalImports; } @@ -1148,8 +1131,8 @@ // If the symbol is visible outside this translation unit, export it. if (WS.isDefined()) { - WasmExport Export; - Export.FieldName = WS.getName(); + wasm::WasmExport Export; + Export.Name = WS.getName(); Export.Index = Index; if (WS.isFunction()) Export.Kind = wasm::WASM_EXTERNAL_FUNCTION; @@ -1187,8 +1170,8 @@ uint32_t Index = SymbolIndices.find(ResolvedSym)->second; DEBUG(dbgs() << " -> index:" << Index << "\n"); - WasmExport Export; - Export.FieldName = WS.getName(); + wasm::WasmExport Export; + Export.Name = WS.getName(); Export.Index = Index; if (WS.isFunction()) Export.Kind = wasm::WASM_EXTERNAL_FUNCTION;