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