[PATCH] D43963: [WebAssembly] Simplify writing of exports section. NFC.

Sam Clegg via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 1 12:49:51 PST 2018


sbc100 created this revision.
Herald added subscribers: llvm-commits, sunfish, aheejin, jgravelle-google, dschuff, jfb.
sbc100 added a dependency: D43930: [WebAssembly] Export all defined data symbols with a "fake global".

Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D43963

Files:
  wasm/Writer.cpp


Index: wasm/Writer.cpp
===================================================================
--- wasm/Writer.cpp
+++ wasm/Writer.cpp
@@ -124,7 +124,7 @@
   std::vector<const Symbol *> ImportedSymbols;
   unsigned NumImportedFunctions = 0;
   unsigned NumImportedGlobals = 0;
-  std::vector<Symbol *> ExportedSymbols;
+  std::vector<WasmExport> Exports;
   std::vector<const DefinedData *> DefinedFakeGlobals;
   std::vector<InputGlobal *> InputGlobals;
   std::vector<InputFunction *> InputFunctions;
@@ -268,37 +268,15 @@
 }
 
 void Writer::createExportSection() {
-  bool ExportMemory = !Config->Relocatable && !Config->ImportMemory;
-
-  uint32_t NumExports = (ExportMemory ? 1 : 0) + ExportedSymbols.size();
-  if (!NumExports)
+  if (!Exports.size())
     return;
 
   SyntheticSection *Section = createSyntheticSection(WASM_SEC_EXPORT);
   raw_ostream &OS = Section->getStream();
 
-  writeUleb128(OS, NumExports, "export count");
-
-  if (ExportMemory)
-    writeExport(OS, {"memory", WASM_EXTERNAL_MEMORY, 0});
-
-  unsigned FakeGlobalIndex = NumImportedGlobals + InputGlobals.size();
-
-  for (const Symbol *Sym : ExportedSymbols) {
-    StringRef Name = Sym->getName();
-    WasmExport Export;
-    DEBUG(dbgs() << "Export: " << Name << "\n");
-
-    if (isa<DefinedFunction>(Sym))
-      Export = {Name, WASM_EXTERNAL_FUNCTION, Sym->getOutputIndex()};
-    else if (isa<DefinedGlobal>(Sym))
-      Export = {Name, WASM_EXTERNAL_GLOBAL, Sym->getOutputIndex()};
-    else if (isa<DefinedData>(Sym))
-      Export = {Name, WASM_EXTERNAL_GLOBAL, FakeGlobalIndex++};
-    else
-      llvm_unreachable("unexpected symbol type");
+  writeUleb128(OS, Exports.size(), "export count");
+  for (const WasmExport &Export : Exports)
     writeExport(OS, Export);
-  }
 }
 
 void Writer::createElemSection() {
@@ -669,25 +647,36 @@
   if (Config->Relocatable)
     return;
 
+  if (!Config->Relocatable && !Config->ImportMemory)
+    Exports.push_back(WasmExport{"memory", WASM_EXTERNAL_MEMORY, 0});
+
+  unsigned FakeGlobalIndex = NumImportedGlobals + InputGlobals.size();
+
   for (Symbol *Sym : Symtab->getSymbols()) {
     if (!Sym->isDefined())
       continue;
     if (Sym->isHidden() || Sym->isLocal())
       continue;
     if (!Sym->isLive())
       continue;
 
-    DEBUG(dbgs() << "exporting sym: " << Sym->getName() << "\n");
-
-    if (auto *D = dyn_cast<DefinedData>(Sym)) {
-      // TODO Remove this check here; for non-relocatable output we actually
-      // used only to create fake-global exports for the synthetic symbols.  Fix
-      // this in a future commit
+    StringRef Name = Sym->getName();
+    WasmExport Export;
+    if (isa<DefinedFunction>(Sym)) {
+      Export = {Name, WASM_EXTERNAL_FUNCTION, Sym->getOutputIndex()};
+    } else if (isa<DefinedGlobal>(Sym)) {
+      Export = {Name, WASM_EXTERNAL_GLOBAL, Sym->getOutputIndex()};
+    } else if (auto *D = dyn_cast<DefinedData>(Sym)) {
       if (Sym != WasmSym::DataEnd && Sym != WasmSym::HeapBase)
         continue;
       DefinedFakeGlobals.emplace_back(D);
+      Export = {Name, WASM_EXTERNAL_GLOBAL, FakeGlobalIndex++};
+    } else {
+      llvm_unreachable("unexpected symbol type");
     }
-    ExportedSymbols.emplace_back(Sym);
+
+    DEBUG(dbgs() << "Export: " << Name << "\n");
+    Exports.push_back(Export);
   }
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43963.136588.patch
Type: text/x-patch
Size: 3315 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180301/8b7fc932/attachment.bin>


More information about the llvm-commits mailing list