1 //===-- AsmPrinterInlineAsm.cpp - AsmPrinter Inline Asm Handling ----------===// 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 // This file implements the inline assembler pieces of the AsmPrinter class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/ADT/SmallString.h" 14 #include "llvm/ADT/SmallVector.h" 15 #include "llvm/ADT/Twine.h" 16 #include "llvm/CodeGen/AsmPrinter.h" 17 #include "llvm/CodeGen/MachineBasicBlock.h" 18 #include "llvm/CodeGen/MachineFunction.h" 19 #include "llvm/CodeGen/MachineModuleInfo.h" 20 #include "llvm/CodeGen/TargetInstrInfo.h" 21 #include "llvm/CodeGen/TargetRegisterInfo.h" 22 #include "llvm/IR/Constants.h" 23 #include "llvm/IR/DataLayout.h" 24 #include "llvm/IR/DiagnosticInfo.h" 25 #include "llvm/IR/InlineAsm.h" 26 #include "llvm/IR/LLVMContext.h" 27 #include "llvm/IR/Module.h" 28 #include "llvm/MC/MCAsmInfo.h" 29 #include "llvm/MC/MCParser/MCTargetAsmParser.h" 30 #include "llvm/MC/MCStreamer.h" 31 #include "llvm/MC/MCSubtargetInfo.h" 32 #include "llvm/MC/MCSymbol.h" 33 #include "llvm/MC/TargetRegistry.h" 34 #include "llvm/Support/ErrorHandling.h" 35 #include "llvm/Support/MemoryBuffer.h" 36 #include "llvm/Support/SourceMgr.h" 37 #include "llvm/Support/raw_ostream.h" 38 #include "llvm/Target/TargetMachine.h" 39 using namespace llvm; 40 41 #define DEBUG_TYPE "asm-printer" 42 43 unsigned AsmPrinter::addInlineAsmDiagBuffer(StringRef AsmStr, 44 const MDNode *LocMDNode) const { 45 MCContext &Context = MMI->getContext(); 46 Context.initInlineSourceManager(); 47 SourceMgr &SrcMgr = *Context.getInlineSourceManager(); 48 std::vector<const MDNode *> &LocInfos = Context.getLocInfos(); 49 50 std::unique_ptr<MemoryBuffer> Buffer; 51 // The inline asm source manager will outlive AsmStr, so make a copy of the 52 // string for SourceMgr to own. 53 Buffer = MemoryBuffer::getMemBufferCopy(AsmStr, "<inline asm>"); 54 55 // Tell SrcMgr about this buffer, it takes ownership of the buffer. 56 unsigned BufNum = SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc()); 57 58 // Store LocMDNode in DiagInfo, using BufNum as an identifier. 59 if (LocMDNode) { 60 LocInfos.resize(BufNum); 61 LocInfos[BufNum - 1] = LocMDNode; 62 } 63 64 return BufNum; 65 } 66 67 68 /// EmitInlineAsm - Emit a blob of inline asm to the output streamer. 69 void AsmPrinter::emitInlineAsm(StringRef Str, const MCSubtargetInfo &STI, 70 const MCTargetOptions &MCOptions, 71 const MDNode *LocMDNode, 72 InlineAsm::AsmDialect Dialect) const { 73 assert(!Str.empty() && "Can't emit empty inline asm block"); 74 75 // Remember if the buffer is nul terminated or not so we can avoid a copy. 76 bool isNullTerminated = Str.back() == 0; 77 if (isNullTerminated) 78 Str = Str.substr(0, Str.size()-1); 79 80 // If the output streamer does not have mature MC support or the integrated 81 // assembler has been disabled or not required, just emit the blob textually. 82 // Otherwise parse the asm and emit it via MC support. 83 // This is useful in case the asm parser doesn't handle something but the 84 // system assembler does. 85 const MCAsmInfo *MCAI = TM.getMCAsmInfo(); 86 assert(MCAI && "No MCAsmInfo"); 87 if (!MCAI->useIntegratedAssembler() && 88 !MCAI->parseInlineAsmUsingAsmParser() && 89 !OutStreamer->isIntegratedAssemblerRequired()) { 90 emitInlineAsmStart(); 91 OutStreamer->emitRawText(Str); 92 emitInlineAsmEnd(STI, nullptr); 93 return; 94 } 95 96 unsigned BufNum = addInlineAsmDiagBuffer(Str, LocMDNode); 97 SourceMgr &SrcMgr = *MMI->getContext().getInlineSourceManager(); 98 SrcMgr.setIncludeDirs(MCOptions.IASSearchPaths); 99 100 std::unique_ptr<MCAsmParser> Parser( 101 createMCAsmParser(SrcMgr, OutContext, *OutStreamer, *MAI, BufNum)); 102 103 // Do not use assembler-level information for parsing inline assembly. 104 OutStreamer->setUseAssemblerInfoForParsing(false); 105 106 // We create a new MCInstrInfo here since we might be at the module level 107 // and not have a MachineFunction to initialize the TargetInstrInfo from and 108 // we only need MCInstrInfo for asm parsing. We create one unconditionally 109 // because it's not subtarget dependent. 110 std::unique_ptr<MCInstrInfo> MII(TM.getTarget().createMCInstrInfo()); 111 assert(MII && "Failed to create instruction info"); 112 std::unique_ptr<MCTargetAsmParser> TAP(TM.getTarget().createMCAsmParser( 113 STI, *Parser, *MII, MCOptions)); 114 if (!TAP) 115 report_fatal_error("Inline asm not supported by this streamer because" 116 " we don't have an asm parser for this target\n"); 117 Parser->setAssemblerDialect(Dialect); 118 Parser->setTargetParser(*TAP.get()); 119 // Enable lexing Masm binary and hex integer literals in intel inline 120 // assembly. 121 if (Dialect == InlineAsm::AD_Intel) 122 Parser->getLexer().setLexMasmIntegers(true); 123 124 emitInlineAsmStart(); 125 // Don't implicitly switch to the text section before the asm. 126 (void)Parser->Run(/*NoInitialTextSection*/ true, 127 /*NoFinalize*/ true); 128 emitInlineAsmEnd(STI, &TAP->getSTI()); 129 } 130 131 static void EmitMSInlineAsmStr(const char *AsmStr, const MachineInstr *MI, 132 MachineModuleInfo *MMI, AsmPrinter *AP, 133 uint64_t LocCookie, raw_ostream &OS) { 134 // Switch to the inline assembly variant. 135 OS << "\t.intel_syntax\n\t"; 136 137 const char *LastEmitted = AsmStr; // One past the last character emitted. 138 unsigned NumOperands = MI->getNumOperands(); 139 140 while (*LastEmitted) { 141 switch (*LastEmitted) { 142 default: { 143 // Not a special case, emit the string section literally. 144 const char *LiteralEnd = LastEmitted+1; 145 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' && 146 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n') 147 ++LiteralEnd; 148 149 OS.write(LastEmitted, LiteralEnd-LastEmitted); 150 LastEmitted = LiteralEnd; 151 break; 152 } 153 case '\n': 154 ++LastEmitted; // Consume newline character. 155 OS << '\n'; // Indent code with newline. 156 break; 157 case '$': { 158 ++LastEmitted; // Consume '$' character. 159 bool Done = true; 160 161 // Handle escapes. 162 switch (*LastEmitted) { 163 default: Done = false; break; 164 case '$': 165 ++LastEmitted; // Consume second '$' character. 166 break; 167 } 168 if (Done) break; 169 170 bool HasCurlyBraces = false; 171 if (*LastEmitted == '{') { // ${variable} 172 ++LastEmitted; // Consume '{' character. 173 HasCurlyBraces = true; 174 } 175 176 // If we have ${:foo}, then this is not a real operand reference, it is a 177 // "magic" string reference, just like in .td files. Arrange to call 178 // PrintSpecial. 179 if (HasCurlyBraces && *LastEmitted == ':') { 180 ++LastEmitted; 181 const char *StrStart = LastEmitted; 182 const char *StrEnd = strchr(StrStart, '}'); 183 if (!StrEnd) 184 report_fatal_error("Unterminated ${:foo} operand in inline asm" 185 " string: '" + Twine(AsmStr) + "'"); 186 187 AP->PrintSpecial(MI, OS, StringRef(StrStart, StrEnd - StrStart)); 188 LastEmitted = StrEnd+1; 189 break; 190 } 191 192 const char *IDStart = LastEmitted; 193 const char *IDEnd = IDStart; 194 while (isDigit(*IDEnd)) 195 ++IDEnd; 196 197 unsigned Val; 198 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val)) 199 report_fatal_error("Bad $ operand number in inline asm string: '" + 200 Twine(AsmStr) + "'"); 201 LastEmitted = IDEnd; 202 203 if (Val >= NumOperands - 1) 204 report_fatal_error("Invalid $ operand number in inline asm string: '" + 205 Twine(AsmStr) + "'"); 206 207 char Modifier[2] = { 0, 0 }; 208 209 if (HasCurlyBraces) { 210 // If we have curly braces, check for a modifier character. This 211 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm. 212 if (*LastEmitted == ':') { 213 ++LastEmitted; // Consume ':' character. 214 if (*LastEmitted == 0) 215 report_fatal_error("Bad ${:} expression in inline asm string: '" + 216 Twine(AsmStr) + "'"); 217 218 Modifier[0] = *LastEmitted; 219 ++LastEmitted; // Consume modifier character. 220 } 221 222 if (*LastEmitted != '}') 223 report_fatal_error("Bad ${} expression in inline asm string: '" + 224 Twine(AsmStr) + "'"); 225 ++LastEmitted; // Consume '}' character. 226 } 227 228 // Okay, we finally have a value number. Ask the target to print this 229 // operand! 230 unsigned OpNo = InlineAsm::MIOp_FirstOperand; 231 232 bool Error = false; 233 234 // Scan to find the machine operand number for the operand. 235 for (; Val; --Val) { 236 if (OpNo >= MI->getNumOperands()) break; 237 unsigned OpFlags = MI->getOperand(OpNo).getImm(); 238 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1; 239 } 240 241 // We may have a location metadata attached to the end of the 242 // instruction, and at no point should see metadata at any 243 // other point while processing. It's an error if so. 244 if (OpNo >= MI->getNumOperands() || 245 MI->getOperand(OpNo).isMetadata()) { 246 Error = true; 247 } else { 248 unsigned OpFlags = MI->getOperand(OpNo).getImm(); 249 ++OpNo; // Skip over the ID number. 250 251 if (InlineAsm::isMemKind(OpFlags)) { 252 Error = AP->PrintAsmMemoryOperand( 253 MI, OpNo, Modifier[0] ? Modifier : nullptr, OS); 254 } else { 255 Error = AP->PrintAsmOperand(MI, OpNo, 256 Modifier[0] ? Modifier : nullptr, OS); 257 } 258 } 259 if (Error) { 260 std::string msg; 261 raw_string_ostream Msg(msg); 262 Msg << "invalid operand in inline asm: '" << AsmStr << "'"; 263 MMI->getModule()->getContext().emitError(LocCookie, Msg.str()); 264 } 265 break; 266 } 267 } 268 } 269 OS << "\n\t.att_syntax\n" << (char)0; // null terminate string. 270 } 271 272 static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI, 273 MachineModuleInfo *MMI, const MCAsmInfo *MAI, 274 AsmPrinter *AP, uint64_t LocCookie, 275 raw_ostream &OS) { 276 int CurVariant = -1; // The number of the {.|.|.} region we are in. 277 const char *LastEmitted = AsmStr; // One past the last character emitted. 278 unsigned NumOperands = MI->getNumOperands(); 279 int AsmPrinterVariant = MAI->getAssemblerDialect(); 280 281 if (MAI->getEmitGNUAsmStartIndentationMarker()) 282 OS << '\t'; 283 284 while (*LastEmitted) { 285 switch (*LastEmitted) { 286 default: { 287 // Not a special case, emit the string section literally. 288 const char *LiteralEnd = LastEmitted+1; 289 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' && 290 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n') 291 ++LiteralEnd; 292 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) 293 OS.write(LastEmitted, LiteralEnd-LastEmitted); 294 LastEmitted = LiteralEnd; 295 break; 296 } 297 case '\n': 298 ++LastEmitted; // Consume newline character. 299 OS << '\n'; // Indent code with newline. 300 break; 301 case '$': { 302 ++LastEmitted; // Consume '$' character. 303 bool Done = true; 304 305 // Handle escapes. 306 switch (*LastEmitted) { 307 default: Done = false; break; 308 case '$': // $$ -> $ 309 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) 310 OS << '$'; 311 ++LastEmitted; // Consume second '$' character. 312 break; 313 case '(': // $( -> same as GCC's { character. 314 ++LastEmitted; // Consume '(' character. 315 if (CurVariant != -1) 316 report_fatal_error("Nested variants found in inline asm string: '" + 317 Twine(AsmStr) + "'"); 318 CurVariant = 0; // We're in the first variant now. 319 break; 320 case '|': 321 ++LastEmitted; // consume '|' character. 322 if (CurVariant == -1) 323 OS << '|'; // this is gcc's behavior for | outside a variant 324 else 325 ++CurVariant; // We're in the next variant. 326 break; 327 case ')': // $) -> same as GCC's } char. 328 ++LastEmitted; // consume ')' character. 329 if (CurVariant == -1) 330 OS << '}'; // this is gcc's behavior for } outside a variant 331 else 332 CurVariant = -1; 333 break; 334 } 335 if (Done) break; 336 337 bool HasCurlyBraces = false; 338 if (*LastEmitted == '{') { // ${variable} 339 ++LastEmitted; // Consume '{' character. 340 HasCurlyBraces = true; 341 } 342 343 // If we have ${:foo}, then this is not a real operand reference, it is a 344 // "magic" string reference, just like in .td files. Arrange to call 345 // PrintSpecial. 346 if (HasCurlyBraces && *LastEmitted == ':') { 347 ++LastEmitted; 348 const char *StrStart = LastEmitted; 349 const char *StrEnd = strchr(StrStart, '}'); 350 if (!StrEnd) 351 report_fatal_error("Unterminated ${:foo} operand in inline asm" 352 " string: '" + Twine(AsmStr) + "'"); 353 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) 354 AP->PrintSpecial(MI, OS, StringRef(StrStart, StrEnd - StrStart)); 355 LastEmitted = StrEnd+1; 356 break; 357 } 358 359 const char *IDStart = LastEmitted; 360 const char *IDEnd = IDStart; 361 while (isDigit(*IDEnd)) 362 ++IDEnd; 363 364 unsigned Val; 365 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val)) 366 report_fatal_error("Bad $ operand number in inline asm string: '" + 367 Twine(AsmStr) + "'"); 368 LastEmitted = IDEnd; 369 370 if (Val >= NumOperands - 1) 371 report_fatal_error("Invalid $ operand number in inline asm string: '" + 372 Twine(AsmStr) + "'"); 373 374 char Modifier[2] = { 0, 0 }; 375 376 if (HasCurlyBraces) { 377 // If we have curly braces, check for a modifier character. This 378 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm. 379 if (*LastEmitted == ':') { 380 ++LastEmitted; // Consume ':' character. 381 if (*LastEmitted == 0) 382 report_fatal_error("Bad ${:} expression in inline asm string: '" + 383 Twine(AsmStr) + "'"); 384 385 Modifier[0] = *LastEmitted; 386 ++LastEmitted; // Consume modifier character. 387 } 388 389 if (*LastEmitted != '}') 390 report_fatal_error("Bad ${} expression in inline asm string: '" + 391 Twine(AsmStr) + "'"); 392 ++LastEmitted; // Consume '}' character. 393 } 394 395 // Okay, we finally have a value number. Ask the target to print this 396 // operand! 397 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) { 398 unsigned OpNo = InlineAsm::MIOp_FirstOperand; 399 400 bool Error = false; 401 402 // Scan to find the machine operand number for the operand. 403 for (; Val; --Val) { 404 if (OpNo >= MI->getNumOperands()) break; 405 unsigned OpFlags = MI->getOperand(OpNo).getImm(); 406 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1; 407 } 408 409 // We may have a location metadata attached to the end of the 410 // instruction, and at no point should see metadata at any 411 // other point while processing. It's an error if so. 412 if (OpNo >= MI->getNumOperands() || 413 MI->getOperand(OpNo).isMetadata()) { 414 Error = true; 415 } else { 416 unsigned OpFlags = MI->getOperand(OpNo).getImm(); 417 ++OpNo; // Skip over the ID number. 418 419 // FIXME: Shouldn't arch-independent output template handling go into 420 // PrintAsmOperand? 421 // Labels are target independent. 422 if (MI->getOperand(OpNo).isBlockAddress()) { 423 const BlockAddress *BA = MI->getOperand(OpNo).getBlockAddress(); 424 MCSymbol *Sym = AP->GetBlockAddressSymbol(BA); 425 Sym->print(OS, AP->MAI); 426 MMI->getContext().registerInlineAsmLabel(Sym); 427 } else if (MI->getOperand(OpNo).isMBB()) { 428 const MCSymbol *Sym = MI->getOperand(OpNo).getMBB()->getSymbol(); 429 Sym->print(OS, AP->MAI); 430 } else if (Modifier[0] == 'l') { 431 Error = true; 432 } else if (InlineAsm::isMemKind(OpFlags)) { 433 Error = AP->PrintAsmMemoryOperand( 434 MI, OpNo, Modifier[0] ? Modifier : nullptr, OS); 435 } else { 436 Error = AP->PrintAsmOperand(MI, OpNo, 437 Modifier[0] ? Modifier : nullptr, OS); 438 } 439 } 440 if (Error) { 441 std::string msg; 442 raw_string_ostream Msg(msg); 443 Msg << "invalid operand in inline asm: '" << AsmStr << "'"; 444 MMI->getModule()->getContext().emitError(LocCookie, Msg.str()); 445 } 446 } 447 break; 448 } 449 } 450 } 451 OS << '\n' << (char)0; // null terminate string. 452 } 453 454 /// This method formats and emits the specified machine instruction that is an 455 /// inline asm. 456 void AsmPrinter::emitInlineAsm(const MachineInstr *MI) const { 457 assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms"); 458 459 // Count the number of register definitions to find the asm string. 460 unsigned NumDefs = 0; 461 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef(); 462 ++NumDefs) 463 assert(NumDefs != MI->getNumOperands()-2 && "No asm string?"); 464 465 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?"); 466 467 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc. 468 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName(); 469 470 // If this asmstr is empty, just print the #APP/#NOAPP markers. 471 // These are useful to see where empty asm's wound up. 472 if (AsmStr[0] == 0) { 473 OutStreamer->emitRawComment(MAI->getInlineAsmStart()); 474 OutStreamer->emitRawComment(MAI->getInlineAsmEnd()); 475 return; 476 } 477 478 // Emit the #APP start marker. This has to happen even if verbose-asm isn't 479 // enabled, so we use emitRawComment. 480 OutStreamer->emitRawComment(MAI->getInlineAsmStart()); 481 482 // Get the !srcloc metadata node if we have it, and decode the loc cookie from 483 // it. 484 uint64_t LocCookie = 0; 485 const MDNode *LocMD = nullptr; 486 for (unsigned i = MI->getNumOperands(); i != 0; --i) { 487 if (MI->getOperand(i-1).isMetadata() && 488 (LocMD = MI->getOperand(i-1).getMetadata()) && 489 LocMD->getNumOperands() != 0) { 490 if (const ConstantInt *CI = 491 mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) { 492 LocCookie = CI->getZExtValue(); 493 break; 494 } 495 } 496 } 497 498 // Emit the inline asm to a temporary string so we can emit it through 499 // EmitInlineAsm. 500 SmallString<256> StringData; 501 raw_svector_ostream OS(StringData); 502 503 AsmPrinter *AP = const_cast<AsmPrinter*>(this); 504 if (MI->getInlineAsmDialect() == InlineAsm::AD_ATT) 505 EmitGCCInlineAsmStr(AsmStr, MI, MMI, MAI, AP, LocCookie, OS); 506 else 507 EmitMSInlineAsmStr(AsmStr, MI, MMI, AP, LocCookie, OS); 508 509 // Emit warnings if we use reserved registers on the clobber list, as 510 // that might lead to undefined behaviour. 511 SmallVector<Register, 8> RestrRegs; 512 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); 513 // Start with the first operand descriptor, and iterate over them. 514 for (unsigned I = InlineAsm::MIOp_FirstOperand, NumOps = MI->getNumOperands(); 515 I < NumOps; ++I) { 516 const MachineOperand &MO = MI->getOperand(I); 517 if (!MO.isImm()) 518 continue; 519 unsigned Flags = MO.getImm(); 520 if (InlineAsm::getKind(Flags) == InlineAsm::Kind_Clobber) { 521 Register Reg = MI->getOperand(I + 1).getReg(); 522 if (!TRI->isAsmClobberable(*MF, Reg)) 523 RestrRegs.push_back(Reg); 524 } 525 // Skip to one before the next operand descriptor, if it exists. 526 I += InlineAsm::getNumOperandRegisters(Flags); 527 } 528 529 if (!RestrRegs.empty()) { 530 std::string Msg = "inline asm clobber list contains reserved registers: "; 531 ListSeparator LS; 532 for (const Register &RR : RestrRegs) { 533 Msg += LS; 534 Msg += TRI->getName(RR); 535 } 536 const char *Note = 537 "Reserved registers on the clobber list may not be " 538 "preserved across the asm statement, and clobbering them may " 539 "lead to undefined behaviour."; 540 MMI->getModule()->getContext().diagnose(DiagnosticInfoInlineAsm( 541 LocCookie, Msg, DiagnosticSeverity::DS_Warning)); 542 MMI->getModule()->getContext().diagnose( 543 DiagnosticInfoInlineAsm(LocCookie, Note, DiagnosticSeverity::DS_Note)); 544 } 545 546 emitInlineAsm(OS.str(), getSubtargetInfo(), TM.Options.MCOptions, LocMD, 547 MI->getInlineAsmDialect()); 548 549 // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't 550 // enabled, so we use emitRawComment. 551 OutStreamer->emitRawComment(MAI->getInlineAsmEnd()); 552 } 553 554 /// PrintSpecial - Print information related to the specified machine instr 555 /// that is independent of the operand, and may be independent of the instr 556 /// itself. This can be useful for portably encoding the comment character 557 /// or other bits of target-specific knowledge into the asmstrings. The 558 /// syntax used is ${:comment}. Targets can override this to add support 559 /// for their own strange codes. 560 void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS, 561 StringRef Code) const { 562 if (Code == "private") { 563 const DataLayout &DL = MF->getDataLayout(); 564 OS << DL.getPrivateGlobalPrefix(); 565 } else if (Code == "comment") { 566 OS << MAI->getCommentString(); 567 } else if (Code == "uid") { 568 // Comparing the address of MI isn't sufficient, because machineinstrs may 569 // be allocated to the same address across functions. 570 571 // If this is a new LastFn instruction, bump the counter. 572 if (LastMI != MI || LastFn != getFunctionNumber()) { 573 ++Counter; 574 LastMI = MI; 575 LastFn = getFunctionNumber(); 576 } 577 OS << Counter; 578 } else { 579 std::string msg; 580 raw_string_ostream Msg(msg); 581 Msg << "Unknown special formatter '" << Code 582 << "' for machine instr: " << *MI; 583 report_fatal_error(Twine(Msg.str())); 584 } 585 } 586 587 void AsmPrinter::PrintSymbolOperand(const MachineOperand &MO, raw_ostream &OS) { 588 assert(MO.isGlobal() && "caller should check MO.isGlobal"); 589 getSymbolPreferLocal(*MO.getGlobal())->print(OS, MAI); 590 printOffset(MO.getOffset(), OS); 591 } 592 593 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM 594 /// instruction, using the specified assembler variant. Targets should 595 /// override this to format as appropriate for machine specific ExtraCodes 596 /// or when the arch-independent handling would be too complex otherwise. 597 bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 598 const char *ExtraCode, raw_ostream &O) { 599 // Does this asm operand have a single letter operand modifier? 600 if (ExtraCode && ExtraCode[0]) { 601 if (ExtraCode[1] != 0) return true; // Unknown modifier. 602 603 // https://gcc.gnu.org/onlinedocs/gccint/Output-Template.html 604 const MachineOperand &MO = MI->getOperand(OpNo); 605 switch (ExtraCode[0]) { 606 default: 607 return true; // Unknown modifier. 608 case 'a': // Print as memory address. 609 if (MO.isReg()) { 610 PrintAsmMemoryOperand(MI, OpNo, nullptr, O); 611 return false; 612 } 613 LLVM_FALLTHROUGH; // GCC allows '%a' to behave like '%c' with immediates. 614 case 'c': // Substitute immediate value without immediate syntax 615 if (MO.isImm()) { 616 O << MO.getImm(); 617 return false; 618 } 619 if (MO.isGlobal()) { 620 PrintSymbolOperand(MO, O); 621 return false; 622 } 623 return true; 624 case 'n': // Negate the immediate constant. 625 if (!MO.isImm()) 626 return true; 627 O << -MO.getImm(); 628 return false; 629 case 's': // The GCC deprecated s modifier 630 if (!MO.isImm()) 631 return true; 632 O << ((32 - MO.getImm()) & 31); 633 return false; 634 } 635 } 636 return true; 637 } 638 639 bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 640 const char *ExtraCode, raw_ostream &O) { 641 // Target doesn't support this yet! 642 return true; 643 } 644 645 void AsmPrinter::emitInlineAsmStart() const {} 646 647 void AsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo, 648 const MCSubtargetInfo *EndInfo) const {} 649