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