}
}
-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) {
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);
}
const WasmFunction *Function;
- llvm::Optional<uint32_t> OutputIndex;
+ llvm::Optional<uint32_t> FunctionIndex;
llvm::Optional<uint32_t> TableIndex;
};
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");
}
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;
WasmGlobal Global;
protected:
- llvm::Optional<uint32_t> OutputIndex;
+ llvm::Optional<uint32_t> GlobalIndex;
};
} // namespace wasm
llvm_unreachable("invalid symbol kind");
}
-bool Symbol::hasOutputIndex() const {
- if (auto *F = dyn_cast<DefinedFunction>(this))
- if (F->Function)
- return F->Function->hasOutputIndex();
- if (auto *G = dyn_cast<DefinedGlobal>(this))
- if (G->Global)
- return G->Global->hasOutputIndex();
- return OutputIndex != INVALID_INDEX;
-}
-
-uint32_t Symbol::getOutputIndex() const {
- assert(!isa<DataSymbol>(this));
- if (auto *F = dyn_cast<DefinedFunction>(this))
- if (F->Function)
- return F->Function->getOutputIndex();
- if (auto *G = dyn_cast<DefinedGlobal>(this))
- if (G->Global)
- return G->Global->getOutputIndex();
- assert(OutputIndex != INVALID_INDEX);
- return OutputIndex;
-}
-
InputChunk *Symbol::getChunk() const {
if (auto *F = dyn_cast<DefinedFunction>(this))
return F->Function;
OutputSymbolIndex = Index;
}
-void Symbol::setOutputIndex(uint32_t Index) {
- DEBUG(dbgs() << "setOutputIndex " << Name << " -> " << Index << "\n");
- assert(!isa<DataSymbol>(this));
- assert(OutputIndex == INVALID_INDEX);
- OutputIndex = Index;
-}
-
bool Symbol::isWeak() const {
return (Flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK;
}
Flags |= WASM_SYMBOL_VISIBILITY_DEFAULT;
}
+uint32_t FunctionSymbol::getFunctionIndex() const {
+ if (auto *F = dyn_cast<DefinedFunction>(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<DefinedFunction>(this))
+ return F->Function->hasFunctionIndex();
+ return FunctionIndex != INVALID_INDEX;
+}
+
uint32_t FunctionSymbol::getTableIndex() const {
if (auto *F = dyn_cast<DefinedFunction>(this))
return F->Function->getTableIndex();
return Segment->OutputSeg->Index;
}
+uint32_t GlobalSymbol::getGlobalIndex() const {
+ if (auto *F = dyn_cast<DefinedGlobal>(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<DefinedGlobal>(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,
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);
Kind SymbolKind;
uint32_t Flags;
InputFile *File;
- uint32_t OutputIndex = INVALID_INDEX;
uint32_t OutputSymbolIndex = INVALID_INDEX;
};
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,
: Symbol(Name, K, Flags, F), FunctionType(Type) {}
uint32_t TableIndex = INVALID_INDEX;
+ uint32_t FunctionIndex = INVALID_INDEX;
const WasmSignature *FunctionType;
};
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)
// 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 {
// 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;
};
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()};
+ if (auto *F = dyn_cast<DefinedFunction>(Sym))
+ Export = {Name, WASM_EXTERNAL_FUNCTION, F->getFunctionIndex()};
+ else if (auto *G = dyn_cast<DefinedGlobal>(Sym))
+ Export = {Name, WASM_EXTERNAL_GLOBAL, G->getGlobalIndex()};
else if (isa<DefinedData>(Sym))
Export = {Name, WASM_EXTERNAL_GLOBAL, FakeGlobalIndex++};
else
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;
}
}
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<FunctionSymbol>(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<GlobalSymbol>(Sym)) {
+ writeUleb128(Sub.OS, G->getGlobalIndex(), "index");
+ if (Sym->isDefined())
+ writeStr(Sub.OS, Sym->getName(), "sym name");
+ } else {
+ assert(isa<DataSymbol>(Sym));
writeStr(Sub.OS, Sym->getName(), "sym name");
if (auto *DataSym = dyn_cast<DefinedData>(Sym)) {
writeUleb128(Sub.OS, DataSym->getOutputSegmentIndex(), "index");
"data offset");
writeUleb128(Sub.OS, DataSym->getSize(), "data size");
}
- break;
}
}
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;
// and InputFunctions are numbered in order with imported functions coming
// first.
for (const Symbol *S : ImportedSymbols) {
- if (!isa<FunctionSymbol>(S))
- continue;
- writeUleb128(Sub.OS, S->getOutputIndex(), "import index");
- writeStr(Sub.OS, S->getName(), "symbol name");
+ if (auto *F = dyn_cast<FunctionSymbol>(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");
}
}
DEBUG(dbgs() << "import: " << Sym->getName() << "\n");
ImportedSymbols.emplace_back(Sym);
- if (isa<FunctionSymbol>(Sym))
- Sym->setOutputIndex(NumImportedFunctions++);
+ if (auto *F = dyn_cast<FunctionSymbol>(Sym))
+ F->setFunctionIndex(NumImportedFunctions++);
else
- Sym->setOutputIndex(NumImportedGlobals++);
+ cast<GlobalSymbol>(Sym)->setGlobalIndex(NumImportedGlobals++);
}
}
if (!Func->Live)
return;
InputFunctions.emplace_back(Func);
- Func->setOutputIndex(FunctionIndex++);
+ Func->setFunctionIndex(FunctionIndex++);
};
for (InputFunction *Func : Symtab->SyntheticFunctions)
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);
auto AddDefinedGlobal = [&](InputGlobal *Global) {
if (Global->Live) {
DEBUG(dbgs() << "AddDefinedGlobal: " << GlobalIndex << "\n");
- Global->setOutputIndex(GlobalIndex++);
+ Global->setGlobalIndex(GlobalIndex++);
InputGlobals.push_back(Global);
}
};
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");
}