Skip to content

Commit d6beb32

Browse files
committed
[WebAssembly] Simplify writing of exports section. NFC.
Differential Revision: https://reviews.llvm.org/D43963 llvm-svn: 332011
1 parent a428eba commit d6beb32

File tree

1 file changed

+25
-34
lines changed

1 file changed

+25
-34
lines changed

lld/wasm/Writer.cpp

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class Writer {
106106
std::vector<const Symbol *> ImportedSymbols;
107107
unsigned NumImportedFunctions = 0;
108108
unsigned NumImportedGlobals = 0;
109-
std::vector<Symbol *> ExportedSymbols;
109+
std::vector<WasmExport> Exports;
110110
std::vector<const DefinedData *> DefinedFakeGlobals;
111111
std::vector<InputGlobal *> InputGlobals;
112112
std::vector<InputFunction *> InputFunctions;
@@ -264,41 +264,15 @@ void Writer::createTableSection() {
264264
}
265265

266266
void Writer::createExportSection() {
267-
bool ExportMemory = !Config->Relocatable && !Config->ImportMemory;
268-
bool ExportTable = !Config->Relocatable && Config->ExportTable;
269-
270-
uint32_t NumExports =
271-
(ExportMemory ? 1 : 0) + (ExportTable ? 1 : 0) + ExportedSymbols.size();
272-
if (!NumExports)
267+
if (!Exports.size())
273268
return;
274269

275270
SyntheticSection *Section = createSyntheticSection(WASM_SEC_EXPORT);
276271
raw_ostream &OS = Section->getStream();
277272

278-
writeUleb128(OS, NumExports, "export count");
279-
280-
if (ExportMemory)
281-
writeExport(OS, {"memory", WASM_EXTERNAL_MEMORY, 0});
282-
if (ExportTable)
283-
writeExport(OS, {kFunctionTableName, WASM_EXTERNAL_TABLE, 0});
284-
285-
unsigned FakeGlobalIndex = NumImportedGlobals + InputGlobals.size();
286-
287-
for (const Symbol *Sym : ExportedSymbols) {
288-
StringRef Name = Sym->getName();
289-
WasmExport Export;
290-
DEBUG(dbgs() << "Export: " << Name << "\n");
291-
292-
if (auto *F = dyn_cast<DefinedFunction>(Sym))
293-
Export = {Name, WASM_EXTERNAL_FUNCTION, F->getFunctionIndex()};
294-
else if (auto *G = dyn_cast<DefinedGlobal>(Sym))
295-
Export = {Name, WASM_EXTERNAL_GLOBAL, G->getGlobalIndex()};
296-
else if (isa<DefinedData>(Sym))
297-
Export = {Name, WASM_EXTERNAL_GLOBAL, FakeGlobalIndex++};
298-
else
299-
llvm_unreachable("unexpected symbol type");
273+
writeUleb128(OS, Exports.size(), "export count");
274+
for (const WasmExport &Export : Exports)
300275
writeExport(OS, Export);
301-
}
302276
}
303277

304278
void Writer::calculateCustomSections() {
@@ -755,6 +729,14 @@ void Writer::calculateExports() {
755729
if (Config->Relocatable)
756730
return;
757731

732+
if (!Config->Relocatable && !Config->ImportMemory)
733+
Exports.push_back(WasmExport{"memory", WASM_EXTERNAL_MEMORY, 0});
734+
735+
if (!Config->Relocatable && Config->ExportTable)
736+
Exports.push_back(WasmExport{kFunctionTableName, WASM_EXTERNAL_TABLE, 0});
737+
738+
unsigned FakeGlobalIndex = NumImportedGlobals + InputGlobals.size();
739+
758740
for (Symbol *Sym : Symtab->getSymbols()) {
759741
if (!Sym->isDefined())
760742
continue;
@@ -763,11 +745,20 @@ void Writer::calculateExports() {
763745
if (!Sym->isLive())
764746
continue;
765747

766-
DEBUG(dbgs() << "exporting sym: " << Sym->getName() << "\n");
767-
768-
if (auto *D = dyn_cast<DefinedData>(Sym))
748+
StringRef Name = Sym->getName();
749+
WasmExport Export;
750+
if (auto *F = dyn_cast<DefinedFunction>(Sym)) {
751+
Export = {Name, WASM_EXTERNAL_FUNCTION, F->getFunctionIndex()};
752+
} else if (auto *G = dyn_cast<DefinedGlobal>(Sym)) {
753+
Export = {Name, WASM_EXTERNAL_GLOBAL, G->getGlobalIndex()};
754+
} else {
755+
auto *D = cast<DefinedData>(Sym);
769756
DefinedFakeGlobals.emplace_back(D);
770-
ExportedSymbols.emplace_back(Sym);
757+
Export = {Name, WASM_EXTERNAL_GLOBAL, FakeGlobalIndex++};
758+
}
759+
760+
DEBUG(dbgs() << "Export: " << Name << "\n");
761+
Exports.push_back(Export);
771762
}
772763
}
773764

0 commit comments

Comments
 (0)