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