diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp --- a/lld/wasm/Driver.cpp +++ b/lld/wasm/Driver.cpp @@ -37,10 +37,9 @@ using namespace llvm::sys; using namespace llvm::wasm; -using namespace lld; -using namespace lld::wasm; - -Configuration *lld::wasm::config; +namespace lld { +namespace wasm { +Configuration *config; namespace { @@ -79,8 +78,7 @@ }; } // anonymous namespace -bool lld::wasm::link(ArrayRef args, bool canExitEarly, - raw_ostream &error) { +bool link(ArrayRef args, bool canExitEarly, raw_ostream &error) { errorHandler().logName = args::getFilenameWithoutExe(args[0]); errorHandler().errorOS = &error; errorHandler().errorLimitExceededMsg = @@ -787,3 +785,6 @@ // Write the result to the file. writeResult(); } + +} // namespace wasm +} // namespace lld diff --git a/lld/wasm/InputChunks.cpp b/lld/wasm/InputChunks.cpp --- a/lld/wasm/InputChunks.cpp +++ b/lld/wasm/InputChunks.cpp @@ -19,10 +19,9 @@ using namespace llvm; using namespace llvm::wasm; using namespace llvm::support::endian; -using namespace lld; -using namespace lld::wasm; -StringRef lld::relocTypeToString(uint8_t relocType) { +namespace lld { +StringRef relocTypeToString(uint8_t relocType) { switch (relocType) { #define WASM_RELOC(NAME, REL) \ case REL: \ @@ -33,10 +32,11 @@ llvm_unreachable("unknown reloc type"); } -std::string lld::toString(const InputChunk *c) { +std::string toString(const wasm::InputChunk *c) { return (toString(c->file) + ":(" + c->getName() + ")").str(); } +namespace wasm { StringRef InputChunk::getComdatName() const { uint32_t index = getComdat(); if (index == UINT32_MAX) @@ -346,3 +346,6 @@ writeUleb128(os, 0, "offset"); } } + +} // namespace wasm +} // namespace lld diff --git a/lld/wasm/InputFiles.cpp b/lld/wasm/InputFiles.cpp --- a/lld/wasm/InputFiles.cpp +++ b/lld/wasm/InputFiles.cpp @@ -22,16 +22,27 @@ #define DEBUG_TYPE "lld" -using namespace lld; -using namespace lld::wasm; - using namespace llvm; using namespace llvm::object; using namespace llvm::wasm; -std::unique_ptr lld::wasm::tar; +namespace lld { + +// Returns a string in the format of "foo.o" or "foo.a(bar.o)". +std::string toString(const wasm::InputFile *file) { + if (!file) + return ""; + + if (file->archiveName.empty()) + return file->getName(); + + return (file->archiveName + "(" + file->getName() + ")").str(); +} -Optional lld::wasm::readFile(StringRef path) { +namespace wasm { +std::unique_ptr tar; + +Optional readFile(StringRef path) { log("Loading: " + path); auto mbOrErr = MemoryBuffer::getFile(path); @@ -48,7 +59,7 @@ return mbref; } -InputFile *lld::wasm::createObjectFile(MemoryBufferRef mb, +InputFile *createObjectFile(MemoryBufferRef mb, StringRef archiveName) { file_magic magic = identify_magic(mb.getBuffer()); if (magic == file_magic::wasm_object) { @@ -542,13 +553,5 @@ symbols.push_back(createBitcodeSymbol(keptComdats, objSym, *this)); } -// Returns a string in the format of "foo.o" or "foo.a(bar.o)". -std::string lld::toString(const wasm::InputFile *file) { - if (!file) - return ""; - - if (file->archiveName.empty()) - return file->getName(); - - return (file->archiveName + "(" + file->getName() + ")").str(); -} +} // namespace wasm +} // namespace lld diff --git a/lld/wasm/LTO.cpp b/lld/wasm/LTO.cpp --- a/lld/wasm/LTO.cpp +++ b/lld/wasm/LTO.cpp @@ -36,9 +36,9 @@ #include using namespace llvm; -using namespace lld; -using namespace lld::wasm; +namespace lld { +namespace wasm { static std::unique_ptr createLTO() { lto::Config c; c.Options = initTargetOptionsFromCodeGenFlags(); @@ -165,3 +165,6 @@ return ret; } + +} // namespace wasm +} // namespace lld diff --git a/lld/wasm/OutputSections.cpp b/lld/wasm/OutputSections.cpp --- a/lld/wasm/OutputSections.cpp +++ b/lld/wasm/OutputSections.cpp @@ -20,9 +20,17 @@ using namespace llvm; using namespace llvm::wasm; -using namespace lld; -using namespace lld::wasm; +namespace lld { + +// Returns a string, e.g. "FUNCTION(.text)". +std::string toString(const wasm::OutputSection &sec) { + if (!sec.name.empty()) + return (sec.getSectionName() + "(" + sec.name + ")").str(); + return sec.getSectionName(); +} + +namespace wasm { static StringRef sectionTypeToString(uint32_t sectionType) { switch (sectionType) { case WASM_SEC_CUSTOM: @@ -58,13 +66,6 @@ } } -// Returns a string, e.g. "FUNCTION(.text)". -std::string lld::toString(const OutputSection &sec) { - if (!sec.name.empty()) - return (sec.getSectionName() + "(" + sec.name + ")").str(); - return sec.getSectionName(); -} - StringRef OutputSection::getSectionName() const { return sectionTypeToString(type); } @@ -248,3 +249,6 @@ for (const InputSection *s : inputSections) s->writeRelocations(os); } + +} // namespace wasm +} // namespace lld diff --git a/lld/wasm/Relocations.cpp b/lld/wasm/Relocations.cpp --- a/lld/wasm/Relocations.cpp +++ b/lld/wasm/Relocations.cpp @@ -14,9 +14,8 @@ using namespace llvm; using namespace llvm::wasm; -using namespace lld; -using namespace lld::wasm; - +namespace lld { +namespace wasm { static bool requiresGOTAccess(const Symbol *sym) { return config->isPic && !sym->isHidden() && !sym->isLocal(); } @@ -54,7 +53,7 @@ out.globalSec->addStaticGOTEntry(sym); } -void lld::wasm::scanRelocations(InputChunk *chunk) { +void scanRelocations(InputChunk *chunk) { if (!chunk->live) return; ObjFile *file = chunk->file; @@ -113,3 +112,6 @@ } } + +} // namespace wasm +} // namespace lld diff --git a/lld/wasm/SymbolTable.cpp b/lld/wasm/SymbolTable.cpp --- a/lld/wasm/SymbolTable.cpp +++ b/lld/wasm/SymbolTable.cpp @@ -21,10 +21,10 @@ using namespace llvm; using namespace llvm::wasm; using namespace llvm::object; -using namespace lld; -using namespace lld::wasm; -SymbolTable *lld::wasm::symtab; +namespace lld { +namespace wasm { +SymbolTable *symtab; void SymbolTable::addFile(InputFile *file) { log("Processing: " + toString(file)); @@ -692,3 +692,6 @@ } } } + +} // namespace wasm +} // namespace lld diff --git a/lld/wasm/Symbols.cpp b/lld/wasm/Symbols.cpp --- a/lld/wasm/Symbols.cpp +++ b/lld/wasm/Symbols.cpp @@ -21,9 +21,45 @@ using namespace llvm; using namespace llvm::wasm; -using namespace lld; -using namespace lld::wasm; +namespace lld { +std::string toString(const wasm::Symbol &sym) { + return maybeDemangleSymbol(sym.getName()); +} + +std::string maybeDemangleSymbol(StringRef name) { + if (wasm::config->demangle) + return demangleItanium(name); + return name; +} + +std::string toString(wasm::Symbol::Kind kind) { + switch (kind) { + case wasm::Symbol::DefinedFunctionKind: + return "DefinedFunction"; + case wasm::Symbol::DefinedDataKind: + return "DefinedData"; + case wasm::Symbol::DefinedGlobalKind: + return "DefinedGlobal"; + case wasm::Symbol::DefinedEventKind: + return "DefinedEvent"; + case wasm::Symbol::UndefinedFunctionKind: + return "UndefinedFunction"; + case wasm::Symbol::UndefinedDataKind: + return "UndefinedData"; + case wasm::Symbol::UndefinedGlobalKind: + return "UndefinedGlobal"; + case wasm::Symbol::LazyKind: + return "LazyKind"; + case wasm::Symbol::SectionKind: + return "SectionKind"; + case wasm::Symbol::OutputSectionKind: + return "OutputSectionKind"; + } + llvm_unreachable("invalid symbol kind"); +} + +namespace wasm { DefinedFunction *WasmSym::callCtors; DefinedFunction *WasmSym::initMemory; DefinedFunction *WasmSym::applyRelocs; @@ -298,49 +334,12 @@ void LazySymbol::fetch() { cast(file)->addMember(&archiveSymbol); } -std::string lld::toString(const wasm::Symbol &sym) { - return lld::maybeDemangleSymbol(sym.getName()); -} - -std::string lld::maybeDemangleSymbol(StringRef name) { - if (config->demangle) - return demangleItanium(name); - return name; -} - -std::string lld::toString(wasm::Symbol::Kind kind) { - switch (kind) { - case wasm::Symbol::DefinedFunctionKind: - return "DefinedFunction"; - case wasm::Symbol::DefinedDataKind: - return "DefinedData"; - case wasm::Symbol::DefinedGlobalKind: - return "DefinedGlobal"; - case wasm::Symbol::DefinedEventKind: - return "DefinedEvent"; - case wasm::Symbol::UndefinedFunctionKind: - return "UndefinedFunction"; - case wasm::Symbol::UndefinedDataKind: - return "UndefinedData"; - case wasm::Symbol::UndefinedGlobalKind: - return "UndefinedGlobal"; - case wasm::Symbol::LazyKind: - return "LazyKind"; - case wasm::Symbol::SectionKind: - return "SectionKind"; - case wasm::Symbol::OutputSectionKind: - return "OutputSectionKind"; - } - llvm_unreachable("invalid symbol kind"); -} - - -void lld::wasm::printTraceSymbolUndefined(StringRef name, const InputFile* file) { +void printTraceSymbolUndefined(StringRef name, const InputFile* file) { message(toString(file) + ": reference to " + name); } // Print out a log message for --trace-symbol. -void lld::wasm::printTraceSymbol(Symbol *sym) { +void printTraceSymbol(Symbol *sym) { // Undefined symbols are traced via printTraceSymbolUndefined if (sym->isUndefined()) return; @@ -354,5 +353,8 @@ message(toString(sym->getFile()) + s + sym->getName()); } -const char *lld::wasm::defaultModule = "env"; -const char *lld::wasm::functionTableName = "__indirect_function_table"; +const char *defaultModule = "env"; +const char *functionTableName = "__indirect_function_table"; + +} // namespace wasm +} // namespace lld diff --git a/lld/wasm/SyntheticSections.cpp b/lld/wasm/SyntheticSections.cpp --- a/lld/wasm/SyntheticSections.cpp +++ b/lld/wasm/SyntheticSections.cpp @@ -22,10 +22,10 @@ using namespace llvm; using namespace llvm::wasm; -using namespace lld; -using namespace lld::wasm; +namespace lld { +namespace wasm { -OutStruct lld::wasm::out; +OutStruct out; namespace { @@ -567,3 +567,6 @@ writeUleb128(bodyOutputStream, count, "reloc count"); sec->writeRelocations(bodyOutputStream); } + +} // namespace wasm +} // namespace lld diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp --- a/lld/wasm/Writer.cpp +++ b/lld/wasm/Writer.cpp @@ -39,9 +39,9 @@ using namespace llvm; using namespace llvm::wasm; -using namespace lld; -using namespace lld::wasm; +namespace lld { +namespace wasm { static constexpr int stackAlignment = 16; namespace { @@ -1088,4 +1088,7 @@ fileSize += header.size(); } -void lld::wasm::writeResult() { Writer().run(); } +void writeResult() { Writer().run(); } + +} // namespace wasm +} // namespace lld diff --git a/lld/wasm/WriterUtils.cpp b/lld/wasm/WriterUtils.cpp --- a/lld/wasm/WriterUtils.cpp +++ b/lld/wasm/WriterUtils.cpp @@ -18,50 +18,94 @@ using namespace llvm::wasm; namespace lld { +std::string toString(ValType type) { + switch (type) { + case ValType::I32: + return "i32"; + case ValType::I64: + return "i64"; + case ValType::F32: + return "f32"; + case ValType::F64: + return "f64"; + case ValType::V128: + return "v128"; + case ValType::EXNREF: + return "exnref"; + } + llvm_unreachable("Invalid wasm::ValType"); +} + +std::string toString(const WasmSignature &sig) { + SmallString<128> s("("); + for (ValType type : sig.Params) { + if (s.size() != 1) + s += ", "; + s += toString(type); + } + s += ") -> "; + if (sig.Returns.empty()) + s += "void"; + else + s += toString(sig.Returns[0]); + return s.str(); +} -void wasm::debugWrite(uint64_t offset, const Twine &msg) { +std::string toString(const WasmGlobalType &type) { + return (type.Mutable ? "var " : "const ") + + toString(static_cast(type.Type)); +} + +std::string toString(const WasmEventType &type) { + if (type.Attribute == WASM_EVENT_ATTRIBUTE_EXCEPTION) + return "exception"; + return "unknown"; +} + +namespace wasm { +void debugWrite(uint64_t offset, const Twine &msg) { LLVM_DEBUG(dbgs() << format(" | %08lld: ", offset) << msg << "\n"); } -void wasm::writeUleb128(raw_ostream &os, uint32_t number, const Twine &msg) { +void writeUleb128(raw_ostream &os, uint32_t number, const Twine &msg) { debugWrite(os.tell(), msg + "[" + utohexstr(number) + "]"); encodeULEB128(number, os); } -void wasm::writeSleb128(raw_ostream &os, int32_t number, const Twine &msg) { +void writeSleb128(raw_ostream &os, int32_t number, const Twine &msg) { debugWrite(os.tell(), msg + "[" + utohexstr(number) + "]"); encodeSLEB128(number, os); } -void wasm::writeBytes(raw_ostream &os, const char *bytes, size_t count, +void writeBytes(raw_ostream &os, const char *bytes, size_t count, const Twine &msg) { debugWrite(os.tell(), msg + " [data[" + Twine(count) + "]]"); os.write(bytes, count); } -void wasm::writeStr(raw_ostream &os, StringRef string, const Twine &msg) { +void writeStr(raw_ostream &os, StringRef string, const Twine &msg) { debugWrite(os.tell(), msg + " [str[" + Twine(string.size()) + "]: " + string + "]"); encodeULEB128(string.size(), os); os.write(string.data(), string.size()); } -void wasm::writeU8(raw_ostream &os, uint8_t byte, const Twine &msg) { +void writeU8(raw_ostream &os, uint8_t byte, const Twine &msg) { debugWrite(os.tell(), msg + " [0x" + utohexstr(byte) + "]"); os << byte; } -void wasm::writeU32(raw_ostream &os, uint32_t number, const Twine &msg) { +void writeU32(raw_ostream &os, uint32_t number, const Twine &msg) { debugWrite(os.tell(), msg + "[0x" + utohexstr(number) + "]"); support::endian::write(os, number, support::little); } -void wasm::writeValueType(raw_ostream &os, ValType type, const Twine &msg) { +void writeValueType(raw_ostream &os, ValType type, const Twine &msg) { writeU8(os, static_cast(type), msg + "[type: " + toString(type) + "]"); } -void wasm::writeSig(raw_ostream &os, const WasmSignature &sig) { +void writeSig(raw_ostream &os, const WasmSignature &sig) { writeU8(os, WASM_TYPE_FUNC, "signature type"); writeUleb128(os, sig.Params.size(), "param Count"); for (ValType paramType : sig.Params) { @@ -73,22 +117,22 @@ } } -void wasm::writeI32Const(raw_ostream &os, int32_t number, const Twine &msg) { +void writeI32Const(raw_ostream &os, int32_t number, const Twine &msg) { writeU8(os, WASM_OPCODE_I32_CONST, "i32.const"); writeSleb128(os, number, msg); } -void wasm::writeI64Const(raw_ostream &os, int32_t number, const Twine &msg) { +void writeI64Const(raw_ostream &os, int32_t number, const Twine &msg) { writeU8(os, WASM_OPCODE_I64_CONST, "i64.const"); writeSleb128(os, number, msg); } -void wasm::writeMemArg(raw_ostream &os, uint32_t alignment, uint32_t offset) { +void writeMemArg(raw_ostream &os, uint32_t alignment, uint32_t offset) { writeUleb128(os, alignment, "alignment"); writeUleb128(os, offset, "offset"); } -void wasm::writeInitExpr(raw_ostream &os, const WasmInitExpr &initExpr) { +void writeInitExpr(raw_ostream &os, const WasmInitExpr &initExpr) { writeU8(os, initExpr.Opcode, "opcode"); switch (initExpr.Opcode) { case WASM_OPCODE_I32_CONST: @@ -106,39 +150,39 @@ writeU8(os, WASM_OPCODE_END, "opcode:end"); } -void wasm::writeLimits(raw_ostream &os, const WasmLimits &limits) { +void writeLimits(raw_ostream &os, const WasmLimits &limits) { writeU8(os, limits.Flags, "limits flags"); writeUleb128(os, limits.Initial, "limits initial"); if (limits.Flags & WASM_LIMITS_FLAG_HAS_MAX) writeUleb128(os, limits.Maximum, "limits max"); } -void wasm::writeGlobalType(raw_ostream &os, const WasmGlobalType &type) { +void writeGlobalType(raw_ostream &os, const WasmGlobalType &type) { // TODO: Update WasmGlobalType to use ValType and remove this cast. writeValueType(os, ValType(type.Type), "global type"); writeU8(os, type.Mutable, "global mutable"); } -void wasm::writeGlobal(raw_ostream &os, const WasmGlobal &global) { +void writeGlobal(raw_ostream &os, const WasmGlobal &global) { writeGlobalType(os, global.Type); writeInitExpr(os, global.InitExpr); } -void wasm::writeEventType(raw_ostream &os, const WasmEventType &type) { +void writeEventType(raw_ostream &os, const WasmEventType &type) { writeUleb128(os, type.Attribute, "event attribute"); writeUleb128(os, type.SigIndex, "sig index"); } -void wasm::writeEvent(raw_ostream &os, const WasmEvent &event) { +void writeEvent(raw_ostream &os, const WasmEvent &event) { writeEventType(os, event.Type); } -void wasm::writeTableType(raw_ostream &os, const llvm::wasm::WasmTable &type) { +void writeTableType(raw_ostream &os, const llvm::wasm::WasmTable &type) { writeU8(os, WASM_TYPE_FUNCREF, "table type"); writeLimits(os, type.Limits); } -void wasm::writeImport(raw_ostream &os, const WasmImport &import) { +void writeImport(raw_ostream &os, const WasmImport &import) { writeStr(os, import.Module, "import module name"); writeStr(os, import.Field, "import field name"); writeU8(os, import.Kind, "import kind"); @@ -163,7 +207,7 @@ } } -void wasm::writeExport(raw_ostream &os, const WasmExport &export_) { +void writeExport(raw_ostream &os, const WasmExport &export_) { writeStr(os, export_.Name, "export name"); writeU8(os, export_.Kind, "export kind"); switch (export_.Kind) { @@ -183,48 +227,6 @@ fatal("unsupported export type: " + Twine(export_.Kind)); } } -} // namespace lld - -std::string lld::toString(ValType type) { - switch (type) { - case ValType::I32: - return "i32"; - case ValType::I64: - return "i64"; - case ValType::F32: - return "f32"; - case ValType::F64: - return "f64"; - case ValType::V128: - return "v128"; - case ValType::EXNREF: - return "exnref"; - } - llvm_unreachable("Invalid wasm::ValType"); -} - -std::string lld::toString(const WasmSignature &sig) { - SmallString<128> s("("); - for (ValType type : sig.Params) { - if (s.size() != 1) - s += ", "; - s += toString(type); - } - s += ") -> "; - if (sig.Returns.empty()) - s += "void"; - else - s += toString(sig.Returns[0]); - return s.str(); -} - -std::string lld::toString(const WasmGlobalType &type) { - return (type.Mutable ? "var " : "const ") + - toString(static_cast(type.Type)); -} -std::string lld::toString(const WasmEventType &type) { - if (type.Attribute == WASM_EVENT_ATTRIBUTE_EXCEPTION) - return "exception"; - return "unknown"; -} +} // namespace wasm +} // namespace lld