diff --git a/llvm/include/llvm/MC/MCSymbolWasm.h b/llvm/include/llvm/MC/MCSymbolWasm.h --- a/llvm/include/llvm/MC/MCSymbolWasm.h +++ b/llvm/include/llvm/MC/MCSymbolWasm.h @@ -19,13 +19,15 @@ bool IsHidden = false; bool IsComdat = false; mutable bool IsUsedInGOT = false; - Optional ImportModule; - Optional ImportName; - Optional ExportName; - wasm::WasmSignature *Signature = nullptr; Optional GlobalType; Optional EventType; + // Non-owning pointers since MCSymbol must be trivially destructible. + std::string *ImportModule = nullptr; + std::string *ImportName = nullptr; + std::string *ExportName = nullptr; + wasm::WasmSignature *Signature = nullptr; + /// An expression describing how to calculate the size of a symbol. If a /// symbol has no size this field will be NULL. const MCExpr *SymbolSize = nullptr; @@ -69,37 +71,32 @@ bool isComdat() const { return IsComdat; } void setComdat(bool isComdat) { IsComdat = isComdat; } - bool hasImportModule() const { return ImportModule.hasValue(); } + bool hasImportModule() const { return ImportModule != nullptr; } const StringRef getImportModule() const { - if (ImportModule.hasValue()) { - return ImportModule.getValue(); - } - // Use a default module name of "env" for now, for compatibility with - // existing tools. - // TODO(sbc): Find a way to specify a default value in the object format - // without picking a hardcoded value like this. - return "env"; - } - void setImportModule(StringRef Name) { - ImportModule = std::string(std::string(Name)); + if (ImportModule) + return StringRef(*ImportModule); + // Use a default module name of "env" for now, for compatibility with + // existing tools. + // TODO(sbc): Find a way to specify a default value in the object format + // without picking a hardcoded value like this. + return "env"; } + void setImportModule(std::string *Name) { ImportModule = Name; } - bool hasImportName() const { return ImportName.hasValue(); } + bool hasImportName() const { return ImportName != nullptr; } const StringRef getImportName() const { - if (ImportName.hasValue()) { - return ImportName.getValue(); - } - return getName(); - } - void setImportName(StringRef Name) { - ImportName = std::string(std::string(Name)); + if (ImportName) + return StringRef(*ImportName); + return getName(); } + void setImportName(std::string *Name) { ImportName = Name; } - bool hasExportName() const { return ExportName.hasValue(); } - const StringRef getExportName() const { return ExportName.getValue(); } - void setExportName(StringRef Name) { - ExportName = std::string(std::string(Name)); + bool hasExportName() const { return ExportName != nullptr; } + const StringRef getExportName() const { + assert(ExportName); + return StringRef(*ExportName); } + void setExportName(std::string *Name) { ExportName = Name; } void setUsedInGOT() const { IsUsedInGOT = true; } bool isUsedInGOT() const { return IsUsedInGOT; } diff --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp --- a/llvm/lib/MC/MCContext.cpp +++ b/llvm/lib/MC/MCContext.cpp @@ -158,6 +158,16 @@ MCSymbol *MCContext::createSymbolImpl(const StringMapEntry *Name, bool IsTemporary) { + static_assert(std::is_trivially_destructible(), + "MCSymbol classes must be trivially destructible"); + static_assert(std::is_trivially_destructible(), + "MCSymbol classes must be trivially destructible"); + static_assert(std::is_trivially_destructible(), + "MCSymbol classes must be trivially destructible"); + static_assert(std::is_trivially_destructible(), + "MCSymbol classes must be trivially destructible"); + static_assert(std::is_trivially_destructible(), + "MCSymbol classes must be trivially destructible"); if (MOFI) { switch (MOFI->getObjectFileType()) { case MCObjectFileInfo::IsCOFF: diff --git a/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp b/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp --- a/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp +++ b/llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp @@ -164,6 +164,7 @@ // Much like WebAssemblyAsmPrinter in the backend, we have to own these. std::vector> Signatures; + std::vector> Names; // Order of labels, directives and instructions in a .s file have no // syntactical enforcement. This class is a callback from the actual parser, @@ -232,6 +233,12 @@ Signatures.push_back(std::move(Sig)); } + std::string *storeName(StringRef Name) { + std::unique_ptr N = std::make_unique(Name); + Names.push_back(std::move(N)); + return Names.back().get(); + } + std::pair nestingString(NestingType NT) { switch (NT) { case Function: @@ -725,7 +732,7 @@ return true; auto ExportName = expectIdent(); auto WasmSym = cast(Ctx.getOrCreateSymbol(SymName)); - WasmSym->setExportName(ExportName); + WasmSym->setExportName(storeName(ExportName)); TOut.emitExportName(WasmSym, ExportName); } @@ -737,7 +744,7 @@ return true; auto ImportModule = expectIdent(); auto WasmSym = cast(Ctx.getOrCreateSymbol(SymName)); - WasmSym->setImportModule(ImportModule); + WasmSym->setImportModule(storeName(ImportModule)); TOut.emitImportModule(WasmSym, ImportModule); } @@ -749,7 +756,7 @@ return true; auto ImportName = expectIdent(); auto WasmSym = cast(Ctx.getOrCreateSymbol(SymName)); - WasmSym->setImportName(ImportName); + WasmSym->setImportName(storeName(ImportName)); TOut.emitImportName(WasmSym, ImportName); } diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h --- a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h +++ b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h @@ -26,6 +26,13 @@ WebAssemblyFunctionInfo *MFI; // TODO: Do the uniquing of Signatures here instead of ObjectFileWriter? std::vector> Signatures; + std::vector> Names; + + std::string *storeName(StringRef Name) { + std::unique_ptr N = std::make_unique(Name); + Names.push_back(std::move(N)); + return Names.back().get(); + } public: explicit WebAssemblyAsmPrinter(TargetMachine &TM, diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp --- a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp @@ -122,14 +122,14 @@ F.hasFnAttribute("wasm-import-module")) { StringRef Name = F.getFnAttribute("wasm-import-module").getValueAsString(); - Sym->setImportModule(Name); + Sym->setImportModule(storeName(Name)); getTargetStreamer()->emitImportModule(Sym, Name); } if (TM.getTargetTriple().isOSBinFormatWasm() && F.hasFnAttribute("wasm-import-name")) { StringRef Name = F.getFnAttribute("wasm-import-name").getValueAsString(); - Sym->setImportName(Name); + Sym->setImportName(storeName(Name)); getTargetStreamer()->emitImportName(Sym, Name); } } @@ -137,7 +137,7 @@ if (F.hasFnAttribute("wasm-export-name")) { auto *Sym = cast(getSymbol(&F)); StringRef Name = F.getFnAttribute("wasm-export-name").getValueAsString(); - Sym->setExportName(Name); + Sym->setExportName(storeName(Name)); getTargetStreamer()->emitExportName(Sym, Name); } }