Index: lld/trunk/wasm/InputChunks.h =================================================================== --- lld/trunk/wasm/InputChunks.h +++ lld/trunk/wasm/InputChunks.h @@ -126,9 +126,9 @@ StringRef getName() const override { return Function->Name; } StringRef getComdat() const override { return Function->Comdat; } - uint32_t getOutputIndex() const { return OutputIndex.getValue(); } - bool hasOutputIndex() const { return OutputIndex.hasValue(); } - void setOutputIndex(uint32_t Index); + uint32_t getFunctionIndex() const { return FunctionIndex.getValue(); } + bool hasFunctionIndex() const { return FunctionIndex.hasValue(); } + void setFunctionIndex(uint32_t Index); uint32_t getTableIndex() const { return TableIndex.getValue(); } bool hasTableIndex() const { return TableIndex.hasValue(); } void setTableIndex(uint32_t Index); @@ -145,7 +145,7 @@ } const WasmFunction *Function; - llvm::Optional OutputIndex; + llvm::Optional FunctionIndex; llvm::Optional TableIndex; }; Index: lld/trunk/wasm/InputChunks.cpp =================================================================== --- lld/trunk/wasm/InputChunks.cpp +++ lld/trunk/wasm/InputChunks.cpp @@ -122,11 +122,11 @@ } } -void InputFunction::setOutputIndex(uint32_t Index) { - DEBUG(dbgs() << "InputFunction::setOutputIndex: " << getName() << " -> " +void InputFunction::setFunctionIndex(uint32_t Index) { + DEBUG(dbgs() << "InputFunction::setFunctionIndex: " << getName() << " -> " << Index << "\n"); - assert(!hasOutputIndex()); - OutputIndex = Index; + assert(!hasFunctionIndex()); + FunctionIndex = Index; } void InputFunction::setTableIndex(uint32_t Index) { Index: lld/trunk/wasm/InputFiles.cpp =================================================================== --- lld/trunk/wasm/InputFiles.cpp +++ lld/trunk/wasm/InputFiles.cpp @@ -106,9 +106,9 @@ case R_WEBASSEMBLY_TYPE_INDEX_LEB: return TypeMap[Reloc.Index]; case R_WEBASSEMBLY_FUNCTION_INDEX_LEB: - return getFunctionSymbol(Reloc.Index)->getOutputIndex(); + return getFunctionSymbol(Reloc.Index)->getFunctionIndex(); case R_WEBASSEMBLY_GLOBAL_INDEX_LEB: - return getGlobalSymbol(Reloc.Index)->getOutputIndex(); + return getGlobalSymbol(Reloc.Index)->getGlobalIndex(); default: llvm_unreachable("unknown relocation type"); } Index: lld/trunk/wasm/InputGlobal.h =================================================================== --- lld/trunk/wasm/InputGlobal.h +++ lld/trunk/wasm/InputGlobal.h @@ -30,11 +30,11 @@ const WasmGlobalType &getType() const { return Global.Type; } - uint32_t getOutputIndex() const { return OutputIndex.getValue(); } - bool hasOutputIndex() const { return OutputIndex.hasValue(); } - void setOutputIndex(uint32_t Index) { - assert(!hasOutputIndex()); - OutputIndex = Index; + uint32_t getGlobalIndex() const { return GlobalIndex.getValue(); } + bool hasGlobalIndex() const { return GlobalIndex.hasValue(); } + void setGlobalIndex(uint32_t Index) { + assert(!hasGlobalIndex()); + GlobalIndex = Index; } bool Live = false; @@ -42,7 +42,7 @@ WasmGlobal Global; protected: - llvm::Optional OutputIndex; + llvm::Optional GlobalIndex; }; } // namespace wasm Index: lld/trunk/wasm/Symbols.h =================================================================== --- lld/trunk/wasm/Symbols.h +++ lld/trunk/wasm/Symbols.h @@ -76,18 +76,8 @@ void setHidden(bool IsHidden); - uint32_t getOutputIndex() const; - - // Returns true if an output index has been set for this symbol - bool hasOutputIndex() const; - - // Set the output index of the symbol, in the Wasm index space of the output - // object - that is, for defined symbols only, its position in the list of - // Wasm imports+code for functions, imports+globals for globals. - void setOutputIndex(uint32_t Index); - - // Get/set the output symbol index, in the Symbol index space. This is - // only used for relocatable output. + // Get/set the index in the output symbol table. This is only used for + // relocatable output. uint32_t getOutputSymbolIndex() const; void setOutputSymbolIndex(uint32_t Index); @@ -101,7 +91,6 @@ Kind SymbolKind; uint32_t Flags; InputFile *File; - uint32_t OutputIndex = INVALID_INDEX; uint32_t OutputSymbolIndex = INVALID_INDEX; }; @@ -114,13 +103,15 @@ const WasmSignature *getFunctionType() const { return FunctionType; } + // Get/set the table index + void setTableIndex(uint32_t Index); uint32_t getTableIndex() const; - - // Returns true if a table index has been set for this symbol bool hasTableIndex() const; - // Set the table index of the symbol - void setTableIndex(uint32_t Index); + // Get/set the function index + uint32_t getFunctionIndex() const; + void setFunctionIndex(uint32_t Index); + bool hasFunctionIndex() const; protected: FunctionSymbol(StringRef Name, Kind K, uint32_t Flags, InputFile *F, @@ -128,6 +119,7 @@ : Symbol(Name, K, Flags, F), FunctionType(Type) {} uint32_t TableIndex = INVALID_INDEX; + uint32_t FunctionIndex = INVALID_INDEX; const WasmSignature *FunctionType; }; @@ -213,6 +205,11 @@ const WasmGlobalType *getGlobalType() const { return GlobalType; } + // Get/set the global index + uint32_t getGlobalIndex() const; + void setGlobalIndex(uint32_t Index); + bool hasGlobalIndex() const; + protected: GlobalSymbol(StringRef Name, Kind K, uint32_t Flags, InputFile *F, const WasmGlobalType *GlobalType) @@ -221,6 +218,7 @@ // Explicit function type, needed for undefined or synthetic functions only. // For regular defined globals this information comes from the InputChunk. const WasmGlobalType *GlobalType; + uint32_t GlobalIndex = INVALID_INDEX; }; class DefinedGlobal : public GlobalSymbol { Index: lld/trunk/wasm/Symbols.cpp =================================================================== --- lld/trunk/wasm/Symbols.cpp +++ lld/trunk/wasm/Symbols.cpp @@ -39,28 +39,6 @@ llvm_unreachable("invalid symbol kind"); } -bool Symbol::hasOutputIndex() const { - if (auto *F = dyn_cast(this)) - if (F->Function) - return F->Function->hasOutputIndex(); - if (auto *G = dyn_cast(this)) - if (G->Global) - return G->Global->hasOutputIndex(); - return OutputIndex != INVALID_INDEX; -} - -uint32_t Symbol::getOutputIndex() const { - assert(!isa(this)); - if (auto *F = dyn_cast(this)) - if (F->Function) - return F->Function->getOutputIndex(); - if (auto *G = dyn_cast(this)) - if (G->Global) - return G->Global->getOutputIndex(); - assert(OutputIndex != INVALID_INDEX); - return OutputIndex; -} - InputChunk *Symbol::getChunk() const { if (auto *F = dyn_cast(this)) return F->Function; @@ -89,13 +67,6 @@ OutputSymbolIndex = Index; } -void Symbol::setOutputIndex(uint32_t Index) { - DEBUG(dbgs() << "setOutputIndex " << Name << " -> " << Index << "\n"); - assert(!isa(this)); - assert(OutputIndex == INVALID_INDEX); - OutputIndex = Index; -} - bool Symbol::isWeak() const { return (Flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK; } @@ -117,6 +88,25 @@ Flags |= WASM_SYMBOL_VISIBILITY_DEFAULT; } +uint32_t FunctionSymbol::getFunctionIndex() const { + if (auto *F = dyn_cast(this)) + return F->Function->getFunctionIndex(); + assert(FunctionIndex != INVALID_INDEX); + return FunctionIndex; +} + +void FunctionSymbol::setFunctionIndex(uint32_t Index) { + DEBUG(dbgs() << "setFunctionIndex " << Name << " -> " << Index << "\n"); + assert(FunctionIndex == INVALID_INDEX); + FunctionIndex = Index; +} + +bool FunctionSymbol::hasFunctionIndex() const { + if (auto *F = dyn_cast(this)) + return F->Function->hasFunctionIndex(); + return FunctionIndex != INVALID_INDEX; +} + uint32_t FunctionSymbol::getTableIndex() const { if (auto *F = dyn_cast(this)) return F->Function->getTableIndex(); @@ -172,6 +162,25 @@ return Segment->OutputSeg->Index; } +uint32_t GlobalSymbol::getGlobalIndex() const { + if (auto *F = dyn_cast(this)) + return F->Global->getGlobalIndex(); + assert(GlobalIndex != INVALID_INDEX); + return GlobalIndex; +} + +void GlobalSymbol::setGlobalIndex(uint32_t Index) { + DEBUG(dbgs() << "setGlobalIndex " << Name << " -> " << Index << "\n"); + assert(GlobalIndex == INVALID_INDEX); + GlobalIndex = Index; +} + +bool GlobalSymbol::hasGlobalIndex() const { + if (auto *F = dyn_cast(this)) + return F->Global->hasGlobalIndex(); + return GlobalIndex != INVALID_INDEX; +} + DefinedGlobal::DefinedGlobal(StringRef Name, uint32_t Flags, InputFile *File, InputGlobal *Global) : GlobalSymbol(Name, DefinedGlobalKind, Flags, File, Index: lld/trunk/wasm/Writer.cpp =================================================================== --- lld/trunk/wasm/Writer.cpp +++ lld/trunk/wasm/Writer.cpp @@ -67,7 +67,7 @@ // An init entry to be written to either the synthetic init func or the // linking metadata. struct WasmInitEntry { - const Symbol *Sym; + const FunctionSymbol *Sym; uint32_t Priority; }; @@ -287,10 +287,10 @@ WasmExport Export; DEBUG(dbgs() << "Export: " << Name << "\n"); - if (isa(Sym)) - Export = {Name, WASM_EXTERNAL_FUNCTION, Sym->getOutputIndex()}; - else if (isa(Sym)) - Export = {Name, WASM_EXTERNAL_GLOBAL, Sym->getOutputIndex()}; + if (auto *F = dyn_cast(Sym)) + Export = {Name, WASM_EXTERNAL_FUNCTION, F->getFunctionIndex()}; + else if (auto *G = dyn_cast(Sym)) + Export = {Name, WASM_EXTERNAL_GLOBAL, G->getGlobalIndex()}; else if (isa(Sym)) Export = {Name, WASM_EXTERNAL_GLOBAL, FakeGlobalIndex++}; else @@ -317,7 +317,7 @@ uint32_t TableIndex = kInitialTableOffset; for (const FunctionSymbol *Sym : IndirectFunctions) { assert(Sym->getTableIndex() == TableIndex); - writeUleb128(OS, Sym->getOutputIndex(), "function index"); + writeUleb128(OS, Sym->getFunctionIndex(), "function index"); ++TableIndex; } } @@ -428,14 +428,16 @@ writeU8(Sub.OS, Kind, "sym kind"); writeUleb128(Sub.OS, Flags, "sym flags"); - switch (Kind) { - case llvm::wasm::WASM_SYMBOL_TYPE_FUNCTION: - case llvm::wasm::WASM_SYMBOL_TYPE_GLOBAL: - writeUleb128(Sub.OS, Sym->getOutputIndex(), "index"); + if (auto *F = dyn_cast(Sym)) { + writeUleb128(Sub.OS, F->getFunctionIndex(), "index"); if (Sym->isDefined()) writeStr(Sub.OS, Sym->getName(), "sym name"); - break; - case llvm::wasm::WASM_SYMBOL_TYPE_DATA: + } else if (auto *G = dyn_cast(Sym)) { + writeUleb128(Sub.OS, G->getGlobalIndex(), "index"); + if (Sym->isDefined()) + writeStr(Sub.OS, Sym->getName(), "sym name"); + } else { + assert(isa(Sym)); writeStr(Sub.OS, Sym->getName(), "sym name"); if (auto *DataSym = dyn_cast(Sym)) { writeUleb128(Sub.OS, DataSym->getOutputSegmentIndex(), "index"); @@ -443,7 +445,6 @@ "data offset"); writeUleb128(Sub.OS, DataSym->getSize(), "data size"); } - break; } } @@ -481,7 +482,7 @@ StringRef Comdat = F->getComdat(); if (!Comdat.empty()) Comdats[Comdat].emplace_back( - ComdatEntry{WASM_COMDAT_FUNCTION, F->getOutputIndex()}); + ComdatEntry{WASM_COMDAT_FUNCTION, F->getFunctionIndex()}); } for (uint32_t I = 0; I < Segments.size(); ++I) { const auto &InputSegments = Segments[I]->InputSegments; @@ -531,14 +532,14 @@ // and InputFunctions are numbered in order with imported functions coming // first. for (const Symbol *S : ImportedSymbols) { - if (!isa(S)) - continue; - writeUleb128(Sub.OS, S->getOutputIndex(), "import index"); - writeStr(Sub.OS, S->getName(), "symbol name"); + if (auto *F = dyn_cast(S)) { + writeUleb128(Sub.OS, F->getFunctionIndex(), "func index"); + writeStr(Sub.OS, F->getName(), "symbol name"); + } } for (const InputFunction *F : InputFunctions) { if (!F->getName().empty()) { - writeUleb128(Sub.OS, F->getOutputIndex(), "func index"); + writeUleb128(Sub.OS, F->getFunctionIndex(), "func index"); writeStr(Sub.OS, F->getName(), "symbol name"); } } @@ -658,10 +659,10 @@ DEBUG(dbgs() << "import: " << Sym->getName() << "\n"); ImportedSymbols.emplace_back(Sym); - if (isa(Sym)) - Sym->setOutputIndex(NumImportedFunctions++); + if (auto *F = dyn_cast(Sym)) + F->setFunctionIndex(NumImportedFunctions++); else - Sym->setOutputIndex(NumImportedGlobals++); + cast(Sym)->setGlobalIndex(NumImportedGlobals++); } } @@ -753,7 +754,7 @@ if (!Func->Live) return; InputFunctions.emplace_back(Func); - Func->setOutputIndex(FunctionIndex++); + Func->setFunctionIndex(FunctionIndex++); }; for (InputFunction *Func : Symtab->SyntheticFunctions) @@ -775,7 +776,7 @@ if (Reloc.Type == R_WEBASSEMBLY_TABLE_INDEX_I32 || Reloc.Type == R_WEBASSEMBLY_TABLE_INDEX_SLEB) { FunctionSymbol *Sym = File->getFunctionSymbol(Reloc.Index); - if (Sym->hasTableIndex() || !Sym->hasOutputIndex()) + if (Sym->hasTableIndex() || !Sym->hasFunctionIndex()) continue; Sym->setTableIndex(TableIndex++); IndirectFunctions.emplace_back(Sym); @@ -806,7 +807,7 @@ auto AddDefinedGlobal = [&](InputGlobal *Global) { if (Global->Live) { DEBUG(dbgs() << "AddDefinedGlobal: " << GlobalIndex << "\n"); - Global->setOutputIndex(GlobalIndex++); + Global->setGlobalIndex(GlobalIndex++); InputGlobals.push_back(Global); } }; @@ -864,7 +865,7 @@ writeUleb128(OS, 0, "num locals"); for (const WasmInitEntry &F : InitFunctions) { writeU8(OS, OPCODE_CALL, "CALL"); - writeUleb128(OS, F.Sym->getOutputIndex(), "function index"); + writeUleb128(OS, F.Sym->getFunctionIndex(), "function index"); } writeU8(OS, OPCODE_END, "END"); }