1 //===-- WebAssemblyAsmPrinter.cpp - WebAssembly LLVM assembly writer ------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// 9 /// \file 10 /// This file contains a printer that converts from our internal 11 /// representation of machine-dependent LLVM code to the WebAssembly assembly 12 /// language. 13 /// 14 //===----------------------------------------------------------------------===// 15 16 #include "WebAssemblyAsmPrinter.h" 17 #include "MCTargetDesc/WebAssemblyMCAsmInfo.h" 18 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" 19 #include "MCTargetDesc/WebAssemblyTargetStreamer.h" 20 #include "TargetInfo/WebAssemblyTargetInfo.h" 21 #include "Utils/WebAssemblyTypeUtilities.h" 22 #include "WebAssembly.h" 23 #include "WebAssemblyMCInstLower.h" 24 #include "WebAssemblyMachineFunctionInfo.h" 25 #include "WebAssemblyRegisterInfo.h" 26 #include "WebAssemblyRuntimeLibcallSignatures.h" 27 #include "WebAssemblyTargetMachine.h" 28 #include "WebAssemblyUtilities.h" 29 #include "llvm/ADT/MapVector.h" 30 #include "llvm/ADT/SmallSet.h" 31 #include "llvm/ADT/StringExtras.h" 32 #include "llvm/Analysis/ValueTracking.h" 33 #include "llvm/BinaryFormat/Wasm.h" 34 #include "llvm/CodeGen/Analysis.h" 35 #include "llvm/CodeGen/AsmPrinter.h" 36 #include "llvm/CodeGen/MachineConstantPool.h" 37 #include "llvm/CodeGen/MachineInstr.h" 38 #include "llvm/CodeGen/MachineModuleInfoImpls.h" 39 #include "llvm/IR/DataLayout.h" 40 #include "llvm/IR/DebugInfoMetadata.h" 41 #include "llvm/IR/GlobalVariable.h" 42 #include "llvm/IR/Metadata.h" 43 #include "llvm/IR/Module.h" 44 #include "llvm/MC/MCContext.h" 45 #include "llvm/MC/MCSectionWasm.h" 46 #include "llvm/MC/MCStreamer.h" 47 #include "llvm/MC/MCSymbol.h" 48 #include "llvm/MC/MCSymbolWasm.h" 49 #include "llvm/MC/TargetRegistry.h" 50 #include "llvm/Support/Compiler.h" 51 #include "llvm/Support/Debug.h" 52 #include "llvm/Support/raw_ostream.h" 53 54 using namespace llvm; 55 56 #define DEBUG_TYPE "asm-printer" 57 58 extern cl::opt<bool> WasmKeepRegisters; 59 60 //===----------------------------------------------------------------------===// 61 // Helpers. 62 //===----------------------------------------------------------------------===// 63 64 MVT WebAssemblyAsmPrinter::getRegType(unsigned RegNo) const { 65 const TargetRegisterInfo *TRI = Subtarget->getRegisterInfo(); 66 const TargetRegisterClass *TRC = MRI->getRegClass(RegNo); 67 for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64, MVT::v16i8, MVT::v8i16, 68 MVT::v4i32, MVT::v2i64, MVT::v4f32, MVT::v2f64, MVT::v8f16}) 69 if (TRI->isTypeLegalForClass(*TRC, T)) 70 return T; 71 LLVM_DEBUG(errs() << "Unknown type for register number: " << RegNo); 72 llvm_unreachable("Unknown register type"); 73 return MVT::Other; 74 } 75 76 std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) { 77 Register RegNo = MO.getReg(); 78 assert(RegNo.isVirtual() && 79 "Unlowered physical register encountered during assembly printing"); 80 assert(!MFI->isVRegStackified(RegNo)); 81 unsigned WAReg = MFI->getWAReg(RegNo); 82 assert(WAReg != WebAssembly::UnusedReg); 83 return '$' + utostr(WAReg); 84 } 85 86 WebAssemblyTargetStreamer *WebAssemblyAsmPrinter::getTargetStreamer() { 87 MCTargetStreamer *TS = OutStreamer->getTargetStreamer(); 88 return static_cast<WebAssemblyTargetStreamer *>(TS); 89 } 90 91 // Emscripten exception handling helpers 92 // 93 // This converts invoke names generated by LowerEmscriptenEHSjLj to real names 94 // that are expected by JavaScript glue code. The invoke names generated by 95 // Emscripten JS glue code are based on their argument and return types; for 96 // example, for a function that takes an i32 and returns nothing, it is 97 // 'invoke_vi'. But the format of invoke generated by LowerEmscriptenEHSjLj pass 98 // contains a mangled string generated from their IR types, for example, 99 // "__invoke_void_%struct.mystruct*_int", because final wasm types are not 100 // available in the IR pass. So we convert those names to the form that 101 // Emscripten JS code expects. 102 // 103 // Refer to LowerEmscriptenEHSjLj pass for more details. 104 105 // Returns true if the given function name is an invoke name generated by 106 // LowerEmscriptenEHSjLj pass. 107 static bool isEmscriptenInvokeName(StringRef Name) { 108 if (Name.front() == '"' && Name.back() == '"') 109 Name = Name.substr(1, Name.size() - 2); 110 return Name.starts_with("__invoke_"); 111 } 112 113 // Returns a character that represents the given wasm value type in invoke 114 // signatures. 115 static char getInvokeSig(wasm::ValType VT) { 116 switch (VT) { 117 case wasm::ValType::I32: 118 return 'i'; 119 case wasm::ValType::I64: 120 return 'j'; 121 case wasm::ValType::F32: 122 return 'f'; 123 case wasm::ValType::F64: 124 return 'd'; 125 case wasm::ValType::V128: 126 return 'V'; 127 case wasm::ValType::FUNCREF: 128 return 'F'; 129 case wasm::ValType::EXTERNREF: 130 return 'X'; 131 case wasm::ValType::EXNREF: 132 return 'E'; 133 default: 134 llvm_unreachable("Unhandled wasm::ValType enum"); 135 } 136 } 137 138 // Given the wasm signature, generate the invoke name in the format JS glue code 139 // expects. 140 static std::string getEmscriptenInvokeSymbolName(wasm::WasmSignature *Sig) { 141 assert(Sig->Returns.size() <= 1); 142 std::string Ret = "invoke_"; 143 if (!Sig->Returns.empty()) 144 for (auto VT : Sig->Returns) 145 Ret += getInvokeSig(VT); 146 else 147 Ret += 'v'; 148 // Invokes' first argument is a pointer to the original function, so skip it 149 for (unsigned I = 1, E = Sig->Params.size(); I < E; I++) 150 Ret += getInvokeSig(Sig->Params[I]); 151 return Ret; 152 } 153 154 //===----------------------------------------------------------------------===// 155 // WebAssemblyAsmPrinter Implementation. 156 //===----------------------------------------------------------------------===// 157 158 MCSymbolWasm *WebAssemblyAsmPrinter::getMCSymbolForFunction( 159 const Function *F, wasm::WasmSignature *Sig, bool &InvokeDetected) { 160 MCSymbolWasm *WasmSym = nullptr; 161 162 const bool EnableEmEH = 163 WebAssembly::WasmEnableEmEH || WebAssembly::WasmEnableEmSjLj; 164 if (EnableEmEH && isEmscriptenInvokeName(F->getName())) { 165 assert(Sig); 166 InvokeDetected = true; 167 if (Sig->Returns.size() > 1) { 168 std::string Msg = 169 "Emscripten EH/SjLj does not support multivalue returns: " + 170 std::string(F->getName()) + ": " + 171 WebAssembly::signatureToString(Sig); 172 report_fatal_error(Twine(Msg)); 173 } 174 WasmSym = cast<MCSymbolWasm>( 175 GetExternalSymbolSymbol(getEmscriptenInvokeSymbolName(Sig))); 176 } else { 177 WasmSym = cast<MCSymbolWasm>(getSymbol(F)); 178 } 179 return WasmSym; 180 } 181 182 void WebAssemblyAsmPrinter::emitGlobalVariable(const GlobalVariable *GV) { 183 if (!WebAssembly::isWasmVarAddressSpace(GV->getAddressSpace())) { 184 AsmPrinter::emitGlobalVariable(GV); 185 return; 186 } 187 188 assert(!GV->isThreadLocal()); 189 190 MCSymbolWasm *Sym = cast<MCSymbolWasm>(getSymbol(GV)); 191 192 if (!Sym->getType()) { 193 SmallVector<MVT, 1> VTs; 194 Type *GlobalVT = GV->getValueType(); 195 if (Subtarget) { 196 // Subtarget is only set when a function is defined, because 197 // each function can declare a different subtarget. For example, 198 // on ARM a compilation unit might have a function on ARM and 199 // another on Thumb. Therefore only if Subtarget is non-null we 200 // can actually calculate the legal VTs. 201 const WebAssemblyTargetLowering &TLI = *Subtarget->getTargetLowering(); 202 computeLegalValueVTs(TLI, GV->getParent()->getContext(), 203 GV->getDataLayout(), GlobalVT, VTs); 204 } 205 WebAssembly::wasmSymbolSetType(Sym, GlobalVT, VTs); 206 } 207 208 emitVisibility(Sym, GV->getVisibility(), !GV->isDeclaration()); 209 emitSymbolType(Sym); 210 if (GV->hasInitializer()) { 211 assert(getSymbolPreferLocal(*GV) == Sym); 212 emitLinkage(GV, Sym); 213 OutStreamer->emitLabel(Sym); 214 // TODO: Actually emit the initializer value. Otherwise the global has the 215 // default value for its type (0, ref.null, etc). 216 OutStreamer->addBlankLine(); 217 } 218 } 219 220 MCSymbol *WebAssemblyAsmPrinter::getOrCreateWasmSymbol(StringRef Name) { 221 auto *WasmSym = cast<MCSymbolWasm>(GetExternalSymbolSymbol(Name)); 222 223 // May be called multiple times, so early out. 224 if (WasmSym->getType()) 225 return WasmSym; 226 227 const WebAssemblySubtarget &Subtarget = getSubtarget(); 228 229 // Except for certain known symbols, all symbols used by CodeGen are 230 // functions. It's OK to hardcode knowledge of specific symbols here; this 231 // method is precisely there for fetching the signatures of known 232 // Clang-provided symbols. 233 if (Name == "__stack_pointer" || Name == "__tls_base" || 234 Name == "__memory_base" || Name == "__table_base" || 235 Name == "__tls_size" || Name == "__tls_align") { 236 bool Mutable = 237 Name == "__stack_pointer" || Name == "__tls_base"; 238 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL); 239 WasmSym->setGlobalType(wasm::WasmGlobalType{ 240 uint8_t(Subtarget.hasAddr64() ? wasm::WASM_TYPE_I64 241 : wasm::WASM_TYPE_I32), 242 Mutable}); 243 return WasmSym; 244 } 245 246 if (Name.starts_with("GCC_except_table")) { 247 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_DATA); 248 return WasmSym; 249 } 250 251 SmallVector<wasm::ValType, 4> Returns; 252 SmallVector<wasm::ValType, 4> Params; 253 if (Name == "__cpp_exception" || Name == "__c_longjmp") { 254 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_TAG); 255 // In static linking we define tag symbols in WasmException::endModule(). 256 // But we may have multiple objects to be linked together, each of which 257 // defines the tag symbols. To resolve them, we declare them as weak. In 258 // dynamic linking we make tag symbols undefined in the backend, define it 259 // in JS, and feed them to each importing module. 260 if (!isPositionIndependent()) 261 WasmSym->setWeak(true); 262 WasmSym->setExternal(true); 263 264 // Currently both C++ exceptions and C longjmps have a single pointer type 265 // param. For C++ exceptions it is a pointer to an exception object, and for 266 // C longjmps it is pointer to a struct that contains a setjmp buffer and a 267 // longjmp return value. We may consider using multiple value parameters for 268 // longjmps later when multivalue support is ready. 269 wasm::ValType AddrType = 270 Subtarget.hasAddr64() ? wasm::ValType::I64 : wasm::ValType::I32; 271 Params.push_back(AddrType); 272 } else { // Function symbols 273 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); 274 WebAssembly::getLibcallSignature(Subtarget, Name, Returns, Params); 275 } 276 auto Signature = OutContext.createWasmSignature(); 277 Signature->Returns = std::move(Returns); 278 Signature->Params = std::move(Params); 279 WasmSym->setSignature(Signature); 280 281 return WasmSym; 282 } 283 284 void WebAssemblyAsmPrinter::emitSymbolType(const MCSymbolWasm *Sym) { 285 std::optional<wasm::WasmSymbolType> WasmTy = Sym->getType(); 286 if (!WasmTy) 287 return; 288 289 switch (*WasmTy) { 290 case wasm::WASM_SYMBOL_TYPE_GLOBAL: 291 getTargetStreamer()->emitGlobalType(Sym); 292 break; 293 case wasm::WASM_SYMBOL_TYPE_TAG: 294 getTargetStreamer()->emitTagType(Sym); 295 break; 296 case wasm::WASM_SYMBOL_TYPE_TABLE: 297 getTargetStreamer()->emitTableType(Sym); 298 break; 299 default: 300 break; // We only handle globals, tags and tables here 301 } 302 } 303 304 void WebAssemblyAsmPrinter::emitDecls(const Module &M) { 305 if (signaturesEmitted) 306 return; 307 signaturesEmitted = true; 308 309 // Normally symbols for globals get discovered as the MI gets lowered, 310 // but we need to know about them ahead of time. This will however, 311 // only find symbols that have been used. Unused symbols from globals will 312 // not be found here. 313 MachineModuleInfoWasm &MMIW = MMI->getObjFileInfo<MachineModuleInfoWasm>(); 314 for (StringRef Name : MMIW.MachineSymbolsUsed) { 315 auto *WasmSym = cast<MCSymbolWasm>(getOrCreateWasmSymbol(Name)); 316 if (WasmSym->isFunction()) { 317 // TODO(wvo): is there any case where this overlaps with the call to 318 // emitFunctionType in the loop below? 319 getTargetStreamer()->emitFunctionType(WasmSym); 320 } 321 } 322 323 for (auto &It : OutContext.getSymbols()) { 324 // Emit .globaltype, .tagtype, or .tabletype declarations for extern 325 // declarations, i.e. those that have only been declared (but not defined) 326 // in the current module 327 auto Sym = cast_or_null<MCSymbolWasm>(It.getValue().Symbol); 328 if (Sym && !Sym->isDefined()) 329 emitSymbolType(Sym); 330 } 331 332 DenseSet<MCSymbol *> InvokeSymbols; 333 for (const auto &F : M) { 334 if (F.isIntrinsic()) 335 continue; 336 337 // Emit function type info for all functions. This will emit duplicate 338 // information for defined functions (which already have function type 339 // info emitted alongside their definition), but this is necessary in 340 // order to enable the single-pass WebAssemblyAsmTypeCheck to succeed. 341 SmallVector<MVT, 4> Results; 342 SmallVector<MVT, 4> Params; 343 computeSignatureVTs(F.getFunctionType(), &F, F, TM, Params, Results); 344 // At this point these MCSymbols may or may not have been created already 345 // and thus also contain a signature, but we need to get the signature 346 // anyway here in case it is an invoke that has not yet been created. We 347 // will discard it later if it turns out not to be necessary. 348 auto Signature = signatureFromMVTs(OutContext, Results, Params); 349 bool InvokeDetected = false; 350 auto *Sym = getMCSymbolForFunction(&F, Signature, InvokeDetected); 351 352 // Multiple functions can be mapped to the same invoke symbol. For 353 // example, two IR functions '__invoke_void_i8*' and '__invoke_void_i32' 354 // are both mapped to '__invoke_vi'. We keep them in a set once we emit an 355 // Emscripten EH symbol so we don't emit the same symbol twice. 356 if (InvokeDetected && !InvokeSymbols.insert(Sym).second) 357 continue; 358 359 Sym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); 360 if (!Sym->getSignature()) { 361 Sym->setSignature(Signature); 362 } 363 364 getTargetStreamer()->emitFunctionType(Sym); 365 366 if (F.hasFnAttribute("wasm-import-module")) { 367 StringRef Name = 368 F.getFnAttribute("wasm-import-module").getValueAsString(); 369 Sym->setImportModule(OutContext.allocateString(Name)); 370 getTargetStreamer()->emitImportModule(Sym, Name); 371 } 372 if (F.hasFnAttribute("wasm-import-name")) { 373 // If this is a converted Emscripten EH/SjLj symbol, we shouldn't use 374 // the original function name but the converted symbol name. 375 StringRef Name = 376 InvokeDetected 377 ? Sym->getName() 378 : F.getFnAttribute("wasm-import-name").getValueAsString(); 379 Sym->setImportName(OutContext.allocateString(Name)); 380 getTargetStreamer()->emitImportName(Sym, Name); 381 } 382 383 if (F.hasFnAttribute("wasm-export-name")) { 384 auto *Sym = cast<MCSymbolWasm>(getSymbol(&F)); 385 StringRef Name = F.getFnAttribute("wasm-export-name").getValueAsString(); 386 Sym->setExportName(OutContext.allocateString(Name)); 387 getTargetStreamer()->emitExportName(Sym, Name); 388 } 389 } 390 } 391 392 void WebAssemblyAsmPrinter::emitEndOfAsmFile(Module &M) { 393 // This is required to emit external declarations (like .functypes) when 394 // no functions are defined in the compilation unit and therefore, 395 // emitDecls() is not called until now. 396 emitDecls(M); 397 398 // When a function's address is taken, a TABLE_INDEX relocation is emitted 399 // against the function symbol at the use site. However the relocation 400 // doesn't explicitly refer to the table. In the future we may want to 401 // define a new kind of reloc against both the function and the table, so 402 // that the linker can see that the function symbol keeps the table alive, 403 // but for now manually mark the table as live. 404 for (const auto &F : M) { 405 if (!F.isIntrinsic() && F.hasAddressTaken()) { 406 MCSymbolWasm *FunctionTable = 407 WebAssembly::getOrCreateFunctionTableSymbol(OutContext, Subtarget); 408 OutStreamer->emitSymbolAttribute(FunctionTable, MCSA_NoDeadStrip); 409 break; 410 } 411 } 412 413 for (const auto &G : M.globals()) { 414 if (!G.hasInitializer() && G.hasExternalLinkage() && 415 !WebAssembly::isWasmVarAddressSpace(G.getAddressSpace()) && 416 G.getValueType()->isSized()) { 417 uint16_t Size = M.getDataLayout().getTypeAllocSize(G.getValueType()); 418 OutStreamer->emitELFSize(getSymbol(&G), 419 MCConstantExpr::create(Size, OutContext)); 420 } 421 } 422 423 if (const NamedMDNode *Named = M.getNamedMetadata("wasm.custom_sections")) { 424 for (const Metadata *MD : Named->operands()) { 425 const auto *Tuple = dyn_cast<MDTuple>(MD); 426 if (!Tuple || Tuple->getNumOperands() != 2) 427 continue; 428 const MDString *Name = dyn_cast<MDString>(Tuple->getOperand(0)); 429 const MDString *Contents = dyn_cast<MDString>(Tuple->getOperand(1)); 430 if (!Name || !Contents) 431 continue; 432 433 OutStreamer->pushSection(); 434 std::string SectionName = (".custom_section." + Name->getString()).str(); 435 MCSectionWasm *MySection = 436 OutContext.getWasmSection(SectionName, SectionKind::getMetadata()); 437 OutStreamer->switchSection(MySection); 438 OutStreamer->emitBytes(Contents->getString()); 439 OutStreamer->popSection(); 440 } 441 } 442 443 EmitProducerInfo(M); 444 EmitTargetFeatures(M); 445 EmitFunctionAttributes(M); 446 } 447 448 void WebAssemblyAsmPrinter::EmitProducerInfo(Module &M) { 449 llvm::SmallVector<std::pair<std::string, std::string>, 4> Languages; 450 if (const NamedMDNode *Debug = M.getNamedMetadata("llvm.dbg.cu")) { 451 llvm::SmallSet<StringRef, 4> SeenLanguages; 452 for (size_t I = 0, E = Debug->getNumOperands(); I < E; ++I) { 453 const auto *CU = cast<DICompileUnit>(Debug->getOperand(I)); 454 StringRef Language = dwarf::LanguageString(CU->getSourceLanguage()); 455 Language.consume_front("DW_LANG_"); 456 if (SeenLanguages.insert(Language).second) 457 Languages.emplace_back(Language.str(), ""); 458 } 459 } 460 461 llvm::SmallVector<std::pair<std::string, std::string>, 4> Tools; 462 if (const NamedMDNode *Ident = M.getNamedMetadata("llvm.ident")) { 463 llvm::SmallSet<StringRef, 4> SeenTools; 464 for (size_t I = 0, E = Ident->getNumOperands(); I < E; ++I) { 465 const auto *S = cast<MDString>(Ident->getOperand(I)->getOperand(0)); 466 std::pair<StringRef, StringRef> Field = S->getString().split("version"); 467 StringRef Name = Field.first.trim(); 468 StringRef Version = Field.second.trim(); 469 if (SeenTools.insert(Name).second) 470 Tools.emplace_back(Name.str(), Version.str()); 471 } 472 } 473 474 int FieldCount = int(!Languages.empty()) + int(!Tools.empty()); 475 if (FieldCount != 0) { 476 MCSectionWasm *Producers = OutContext.getWasmSection( 477 ".custom_section.producers", SectionKind::getMetadata()); 478 OutStreamer->pushSection(); 479 OutStreamer->switchSection(Producers); 480 OutStreamer->emitULEB128IntValue(FieldCount); 481 for (auto &Producers : {std::make_pair("language", &Languages), 482 std::make_pair("processed-by", &Tools)}) { 483 if (Producers.second->empty()) 484 continue; 485 OutStreamer->emitULEB128IntValue(strlen(Producers.first)); 486 OutStreamer->emitBytes(Producers.first); 487 OutStreamer->emitULEB128IntValue(Producers.second->size()); 488 for (auto &Producer : *Producers.second) { 489 OutStreamer->emitULEB128IntValue(Producer.first.size()); 490 OutStreamer->emitBytes(Producer.first); 491 OutStreamer->emitULEB128IntValue(Producer.second.size()); 492 OutStreamer->emitBytes(Producer.second); 493 } 494 } 495 OutStreamer->popSection(); 496 } 497 } 498 499 void WebAssemblyAsmPrinter::EmitTargetFeatures(Module &M) { 500 struct FeatureEntry { 501 uint8_t Prefix; 502 std::string Name; 503 }; 504 505 // Read target features and linkage policies from module metadata 506 SmallVector<FeatureEntry, 4> EmittedFeatures; 507 auto EmitFeature = [&](std::string Feature) { 508 std::string MDKey = (StringRef("wasm-feature-") + Feature).str(); 509 Metadata *Policy = M.getModuleFlag(MDKey); 510 if (Policy == nullptr) 511 return; 512 513 FeatureEntry Entry; 514 Entry.Prefix = 0; 515 Entry.Name = Feature; 516 517 if (auto *MD = cast<ConstantAsMetadata>(Policy)) 518 if (auto *I = cast<ConstantInt>(MD->getValue())) 519 Entry.Prefix = I->getZExtValue(); 520 521 // Silently ignore invalid metadata 522 if (Entry.Prefix != wasm::WASM_FEATURE_PREFIX_USED && 523 Entry.Prefix != wasm::WASM_FEATURE_PREFIX_DISALLOWED) 524 return; 525 526 EmittedFeatures.push_back(Entry); 527 }; 528 529 for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) { 530 EmitFeature(KV.Key); 531 } 532 // This pseudo-feature tells the linker whether shared memory would be safe 533 EmitFeature("shared-mem"); 534 535 // This is an "architecture", not a "feature", but we emit it as such for 536 // the benefit of tools like Binaryen and consistency with other producers. 537 // FIXME: Subtarget is null here, so can't Subtarget->hasAddr64() ? 538 if (M.getDataLayout().getPointerSize() == 8) { 539 // Can't use EmitFeature since "wasm-feature-memory64" is not a module 540 // flag. 541 EmittedFeatures.push_back({wasm::WASM_FEATURE_PREFIX_USED, "memory64"}); 542 } 543 544 if (EmittedFeatures.size() == 0) 545 return; 546 547 // Emit features and linkage policies into the "target_features" section 548 MCSectionWasm *FeaturesSection = OutContext.getWasmSection( 549 ".custom_section.target_features", SectionKind::getMetadata()); 550 OutStreamer->pushSection(); 551 OutStreamer->switchSection(FeaturesSection); 552 553 OutStreamer->emitULEB128IntValue(EmittedFeatures.size()); 554 for (auto &F : EmittedFeatures) { 555 OutStreamer->emitIntValue(F.Prefix, 1); 556 OutStreamer->emitULEB128IntValue(F.Name.size()); 557 OutStreamer->emitBytes(F.Name); 558 } 559 560 OutStreamer->popSection(); 561 } 562 563 void WebAssemblyAsmPrinter::EmitFunctionAttributes(Module &M) { 564 auto V = M.getNamedGlobal("llvm.global.annotations"); 565 if (!V) 566 return; 567 568 // Group all the custom attributes by name. 569 MapVector<StringRef, SmallVector<MCSymbol *, 4>> CustomSections; 570 const ConstantArray *CA = cast<ConstantArray>(V->getOperand(0)); 571 for (Value *Op : CA->operands()) { 572 auto *CS = cast<ConstantStruct>(Op); 573 // The first field is a pointer to the annotated variable. 574 Value *AnnotatedVar = CS->getOperand(0)->stripPointerCasts(); 575 // Only annotated functions are supported for now. 576 if (!isa<Function>(AnnotatedVar)) 577 continue; 578 auto *F = cast<Function>(AnnotatedVar); 579 580 // The second field is a pointer to a global annotation string. 581 auto *GV = cast<GlobalVariable>(CS->getOperand(1)->stripPointerCasts()); 582 StringRef AnnotationString; 583 getConstantStringInfo(GV, AnnotationString); 584 auto *Sym = cast<MCSymbolWasm>(getSymbol(F)); 585 CustomSections[AnnotationString].push_back(Sym); 586 } 587 588 // Emit a custom section for each unique attribute. 589 for (const auto &[Name, Symbols] : CustomSections) { 590 MCSectionWasm *CustomSection = OutContext.getWasmSection( 591 ".custom_section.llvm.func_attr.annotate." + Name, SectionKind::getMetadata()); 592 OutStreamer->pushSection(); 593 OutStreamer->switchSection(CustomSection); 594 595 for (auto &Sym : Symbols) { 596 OutStreamer->emitValue( 597 MCSymbolRefExpr::create(Sym, WebAssembly::S_FUNCINDEX, OutContext), 598 4); 599 } 600 OutStreamer->popSection(); 601 } 602 } 603 604 void WebAssemblyAsmPrinter::emitConstantPool() { 605 emitDecls(*MMI->getModule()); 606 assert(MF->getConstantPool()->getConstants().empty() && 607 "WebAssembly disables constant pools"); 608 } 609 610 void WebAssemblyAsmPrinter::emitJumpTableInfo() { 611 // Nothing to do; jump tables are incorporated into the instruction stream. 612 } 613 614 void WebAssemblyAsmPrinter::emitFunctionBodyStart() { 615 const Function &F = MF->getFunction(); 616 SmallVector<MVT, 1> ResultVTs; 617 SmallVector<MVT, 4> ParamVTs; 618 computeSignatureVTs(F.getFunctionType(), &F, F, TM, ParamVTs, ResultVTs); 619 620 auto Signature = signatureFromMVTs(OutContext, ResultVTs, ParamVTs); 621 auto *WasmSym = cast<MCSymbolWasm>(CurrentFnSym); 622 WasmSym->setSignature(Signature); 623 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); 624 625 getTargetStreamer()->emitFunctionType(WasmSym); 626 627 // Emit the function index. 628 if (MDNode *Idx = F.getMetadata("wasm.index")) { 629 assert(Idx->getNumOperands() == 1); 630 631 getTargetStreamer()->emitIndIdx(AsmPrinter::lowerConstant( 632 cast<ConstantAsMetadata>(Idx->getOperand(0))->getValue())); 633 } 634 635 SmallVector<wasm::ValType, 16> Locals; 636 valTypesFromMVTs(MFI->getLocals(), Locals); 637 getTargetStreamer()->emitLocal(Locals); 638 639 AsmPrinter::emitFunctionBodyStart(); 640 } 641 642 void WebAssemblyAsmPrinter::emitInstruction(const MachineInstr *MI) { 643 LLVM_DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n'); 644 WebAssembly_MC::verifyInstructionPredicates(MI->getOpcode(), 645 Subtarget->getFeatureBits()); 646 647 switch (MI->getOpcode()) { 648 case WebAssembly::ARGUMENT_i32: 649 case WebAssembly::ARGUMENT_i32_S: 650 case WebAssembly::ARGUMENT_i64: 651 case WebAssembly::ARGUMENT_i64_S: 652 case WebAssembly::ARGUMENT_f32: 653 case WebAssembly::ARGUMENT_f32_S: 654 case WebAssembly::ARGUMENT_f64: 655 case WebAssembly::ARGUMENT_f64_S: 656 case WebAssembly::ARGUMENT_v16i8: 657 case WebAssembly::ARGUMENT_v16i8_S: 658 case WebAssembly::ARGUMENT_v8i16: 659 case WebAssembly::ARGUMENT_v8i16_S: 660 case WebAssembly::ARGUMENT_v4i32: 661 case WebAssembly::ARGUMENT_v4i32_S: 662 case WebAssembly::ARGUMENT_v2i64: 663 case WebAssembly::ARGUMENT_v2i64_S: 664 case WebAssembly::ARGUMENT_v4f32: 665 case WebAssembly::ARGUMENT_v4f32_S: 666 case WebAssembly::ARGUMENT_v2f64: 667 case WebAssembly::ARGUMENT_v2f64_S: 668 case WebAssembly::ARGUMENT_v8f16: 669 case WebAssembly::ARGUMENT_v8f16_S: 670 // These represent values which are live into the function entry, so there's 671 // no instruction to emit. 672 break; 673 case WebAssembly::FALLTHROUGH_RETURN: { 674 // These instructions represent the implicit return at the end of a 675 // function body. 676 if (isVerbose()) { 677 OutStreamer->AddComment("fallthrough-return"); 678 OutStreamer->addBlankLine(); 679 } 680 break; 681 } 682 case WebAssembly::COMPILER_FENCE: 683 // This is a compiler barrier that prevents instruction reordering during 684 // backend compilation, and should not be emitted. 685 break; 686 case WebAssembly::CATCH: 687 case WebAssembly::CATCH_S: 688 case WebAssembly::CATCH_REF: 689 case WebAssembly::CATCH_REF_S: 690 case WebAssembly::CATCH_ALL: 691 case WebAssembly::CATCH_ALL_S: 692 case WebAssembly::CATCH_ALL_REF: 693 case WebAssembly::CATCH_ALL_REF_S: 694 // These are pseudo instructions to represent catch clauses in try_table 695 // instruction to simulate block return values. 696 break; 697 default: { 698 WebAssemblyMCInstLower MCInstLowering(OutContext, *this); 699 MCInst TmpInst; 700 MCInstLowering.lower(MI, TmpInst); 701 EmitToStreamer(*OutStreamer, TmpInst); 702 break; 703 } 704 } 705 } 706 707 bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI, 708 unsigned OpNo, 709 const char *ExtraCode, 710 raw_ostream &OS) { 711 // First try the generic code, which knows about modifiers like 'c' and 'n'. 712 if (!AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS)) 713 return false; 714 715 if (!ExtraCode) { 716 const MachineOperand &MO = MI->getOperand(OpNo); 717 switch (MO.getType()) { 718 case MachineOperand::MO_Immediate: 719 OS << MO.getImm(); 720 return false; 721 case MachineOperand::MO_Register: 722 // FIXME: only opcode that still contains registers, as required by 723 // MachineInstr::getDebugVariable(). 724 assert(MI->getOpcode() == WebAssembly::INLINEASM); 725 OS << regToString(MO); 726 return false; 727 case MachineOperand::MO_GlobalAddress: 728 PrintSymbolOperand(MO, OS); 729 return false; 730 case MachineOperand::MO_ExternalSymbol: 731 GetExternalSymbolSymbol(MO.getSymbolName())->print(OS, MAI); 732 printOffset(MO.getOffset(), OS); 733 return false; 734 case MachineOperand::MO_MachineBasicBlock: 735 MO.getMBB()->getSymbol()->print(OS, MAI); 736 return false; 737 default: 738 break; 739 } 740 } 741 742 return true; 743 } 744 745 bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, 746 unsigned OpNo, 747 const char *ExtraCode, 748 raw_ostream &OS) { 749 // The current approach to inline asm is that "r" constraints are expressed 750 // as local indices, rather than values on the operand stack. This simplifies 751 // using "r" as it eliminates the need to push and pop the values in a 752 // particular order, however it also makes it impossible to have an "m" 753 // constraint. So we don't support it. 754 755 return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, ExtraCode, OS); 756 } 757 758 char WebAssemblyAsmPrinter::ID = 0; 759 760 INITIALIZE_PASS(WebAssemblyAsmPrinter, "webassembly-asm-printer", 761 "WebAssembly Assmebly Printer", false, false) 762 763 // Force static initialization. 764 extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void 765 LLVMInitializeWebAssemblyAsmPrinter() { 766 RegisterAsmPrinter<WebAssemblyAsmPrinter> X(getTheWebAssemblyTarget32()); 767 RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(getTheWebAssemblyTarget64()); 768 } 769