[lld] r326285 - [WebAssembly] Refactor ObjFile::initializeSymbols.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 27 16:50:54 PST 2018


Author: ruiu
Date: Tue Feb 27 16:50:54 2018
New Revision: 326285

URL: http://llvm.org/viewvc/llvm-project?rev=326285&view=rev
Log:
[WebAssembly] Refactor ObjFile::initializeSymbols.

The main purpose of this change is to make initializeSymbols shorter.

Differential Revision: https://reviews.llvm.org/D43691

Modified:
    lld/trunk/wasm/InputFiles.cpp
    lld/trunk/wasm/InputFiles.h

Modified: lld/trunk/wasm/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/InputFiles.cpp?rev=326285&r1=326284&r2=326285&view=diff
==============================================================================
--- lld/trunk/wasm/InputFiles.cpp (original)
+++ lld/trunk/wasm/InputFiles.cpp Tue Feb 27 16:50:54 2018
@@ -187,47 +187,55 @@ void ObjFile::initializeSymbols() {
   // Populate `Symbols` based on the WasmSymbols in the object
   for (const SymbolRef &Sym : WasmObj->symbols()) {
     const WasmSymbol &WasmSym = WasmObj->getWasmSymbol(Sym.getRawDataRefImpl());
-    bool IsDefined = WasmSym.isDefined();
 
-    if (IsDefined) {
-      switch (WasmSym.Info.Kind) {
-      case WASM_SYMBOL_TYPE_FUNCTION: {
-        InputFunction *Function = getFunction(WasmSym);
-        if (isExcludedByComdat(Function)) {
-          Function->Live = false;
-          IsDefined = false;
-          break;
-        }
-        Symbols.push_back(createDefinedFunction(WasmSym, Function));
-        break;
-      }
-      case WASM_SYMBOL_TYPE_DATA: {
-        InputSegment *Segment = getSegment(WasmSym);
-        if (isExcludedByComdat(Segment)) {
-          Segment->Live = false;
-          IsDefined = false;
-          break;
-        }
-        Symbols.push_back(createDefinedData(WasmSym, Segment,
-                                            WasmSym.Info.DataRef.Offset,
-                                            WasmSym.Info.DataRef.Size));
-        break;
-      }
-      case WASM_SYMBOL_TYPE_GLOBAL:
-        Symbols.push_back(createDefinedGlobal(WasmSym, getGlobal(WasmSym)));
-        break;
-      default:
-        llvm_unreachable("unkown symbol kind");
-        break;
-      }
+    if (Symbol *Sym = createDefined(WasmSym))
+      Symbols.push_back(Sym);
+    else
+      Symbols.push_back(createUndefined(WasmSym));
+  }
+}
+
+Symbol *ObjFile::createDefined(const WasmSymbol &Sym) {
+  if (!Sym.isDefined())
+    return nullptr;
+
+  switch (Sym.Info.Kind) {
+  case WASM_SYMBOL_TYPE_FUNCTION: {
+    InputFunction *Func = getFunction(Sym);
+    if (isExcludedByComdat(Func)) {
+      Func->Live = false;
+      return nullptr;
     }
 
-    // Either the the symbol itself was undefined, or was excluded via comdat
-    // in which case this simply insertes the existing symbol into the correct
-    // slot in the Symbols array.
-    if (!IsDefined)
-      Symbols.push_back(createUndefined(WasmSym));
+    if (Sym.isBindingLocal())
+      return make<DefinedFunction>(Sym.Info.Name, Sym.Info.Flags, this, Func);
+    return Symtab->addDefinedFunction(Sym.Info.Name, Sym.Info.Flags, this,
+                                      Func);
+  }
+  case WASM_SYMBOL_TYPE_DATA: {
+    InputSegment *Seg = getSegment(Sym);
+    if (isExcludedByComdat(Seg)) {
+      Seg->Live = false;
+      return nullptr;
+    }
+
+    uint32_t Offset = Sym.Info.DataRef.Offset;
+    uint32_t Size = Sym.Info.DataRef.Size;
+
+    if (Sym.isBindingLocal())
+      return make<DefinedData>(Sym.Info.Name, Sym.Info.Flags, this, Seg, Offset,
+                               Size);
+    return Symtab->addDefinedData(Sym.Info.Name, Sym.Info.Flags, this, Seg,
+                                  Offset, Size);
+  }
+  case WASM_SYMBOL_TYPE_GLOBAL:
+    if (Sym.isBindingLocal())
+      return make<DefinedGlobal>(Sym.Info.Name, Sym.Info.Flags, this,
+                                 getGlobal(Sym));
+    return Symtab->addDefinedGlobal(Sym.Info.Name, Sym.Info.Flags, this,
+                                    getGlobal(Sym));
   }
+  llvm_unreachable("unkown symbol kind");
 }
 
 Symbol *ObjFile::createUndefined(const WasmSymbol &Sym) {
@@ -245,30 +253,6 @@ Symbol *ObjFile::createUndefined(const W
   llvm_unreachable("unkown symbol kind");
 }
 
-Symbol *ObjFile::createDefinedFunction(const WasmSymbol &Sym,
-                                       InputFunction *Function) {
-  if (Sym.isBindingLocal())
-    return make<DefinedFunction>(Sym.Info.Name, Sym.Info.Flags, this, Function);
-  return Symtab->addDefinedFunction(Sym.Info.Name, Sym.Info.Flags, this,
-                                    Function);
-}
-
-Symbol *ObjFile::createDefinedData(const WasmSymbol &Sym, InputSegment *Segment,
-                                   uint32_t Offset, uint32_t Size) {
-  if (Sym.isBindingLocal())
-    return make<DefinedData>(Sym.Info.Name, Sym.Info.Flags, this, Segment,
-                             Offset, Size);
-  return Symtab->addDefinedData(Sym.Info.Name, Sym.Info.Flags, this, Segment,
-                                Offset, Size);
-}
-
-Symbol *ObjFile::createDefinedGlobal(const WasmSymbol &Sym,
-                                     InputGlobal *Global) {
-  if (Sym.isBindingLocal())
-    return make<DefinedGlobal>(Sym.Info.Name, Sym.Info.Flags, this, Global);
-  return Symtab->addDefinedGlobal(Sym.Info.Name, Sym.Info.Flags, this, Global);
-}
-
 void ArchiveFile::parse() {
   // Parse a MemoryBufferRef as an archive file.
   DEBUG(dbgs() << "Parsing library: " << toString(this) << "\n");

Modified: lld/trunk/wasm/InputFiles.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/InputFiles.h?rev=326285&r1=326284&r2=326285&view=diff
==============================================================================
--- lld/trunk/wasm/InputFiles.h (original)
+++ lld/trunk/wasm/InputFiles.h Tue Feb 27 16:50:54 2018
@@ -111,10 +111,7 @@ public:
   GlobalSymbol *getGlobalSymbol(uint32_t Index) const;
 
 private:
-  Symbol *createDefinedData(const WasmSymbol &Sym, InputSegment *Segment,
-                            uint32_t Offset, uint32_t DataSize);
-  Symbol *createDefinedFunction(const WasmSymbol &Sym, InputFunction *Function);
-  Symbol *createDefinedGlobal(const WasmSymbol &Sym, InputGlobal *Global);
+  Symbol *createDefined(const WasmSymbol &Sym);
   Symbol *createUndefined(const WasmSymbol &Sym);
 
   void initializeSymbols();




More information about the llvm-commits mailing list