AsmPrinterInlineAsm.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. #include "llvm/CodeGen/AsmPrinter.h"
  14. #include "llvm/ADT/SmallString.h"
  15. #include "llvm/ADT/Twine.h"
  16. #include "llvm/CodeGen/MachineBasicBlock.h"
  17. #include "llvm/CodeGen/MachineFunction.h"
  18. #include "llvm/CodeGen/MachineModuleInfo.h"
  19. #include "llvm/IR/Constants.h"
  20. #include "llvm/IR/DataLayout.h"
  21. #include "llvm/IR/InlineAsm.h"
  22. #include "llvm/IR/LLVMContext.h"
  23. #include "llvm/IR/Module.h"
  24. #include "llvm/MC/MCAsmInfo.h"
  25. #include "llvm/MC/MCStreamer.h"
  26. #include "llvm/MC/MCSubtargetInfo.h"
  27. #include "llvm/MC/MCSymbol.h"
  28. #include "llvm/MC/MCTargetAsmParser.h"
  29. #include "llvm/Support/ErrorHandling.h"
  30. #include "llvm/Support/MemoryBuffer.h"
  31. #include "llvm/Support/SourceMgr.h"
  32. #include "llvm/Support/TargetRegistry.h"
  33. #include "llvm/Support/raw_ostream.h"
  34. #include "llvm/Target/TargetInstrInfo.h"
  35. #include "llvm/Target/TargetMachine.h"
  36. #include "llvm/Target/TargetRegisterInfo.h"
  37. #include "llvm/Target/TargetSubtargetInfo.h"
  38. using namespace llvm;
  39. #define DEBUG_TYPE "asm-printer"
  40. namespace {
  41. struct SrcMgrDiagInfo {
  42. const MDNode *LocInfo;
  43. LLVMContext::InlineAsmDiagHandlerTy DiagHandler;
  44. void *DiagContext;
  45. };
  46. }
  47. /// srcMgrDiagHandler - This callback is invoked when the SourceMgr for an
  48. /// inline asm has an error in it. diagInfo is a pointer to the SrcMgrDiagInfo
  49. /// struct above.
  50. static void srcMgrDiagHandler(const SMDiagnostic &Diag, void *diagInfo) {
  51. SrcMgrDiagInfo *DiagInfo = static_cast<SrcMgrDiagInfo *>(diagInfo);
  52. assert(DiagInfo && "Diagnostic context not passed down?");
  53. // If the inline asm had metadata associated with it, pull out a location
  54. // cookie corresponding to which line the error occurred on.
  55. unsigned LocCookie = 0;
  56. if (const MDNode *LocInfo = DiagInfo->LocInfo) {
  57. unsigned ErrorLine = Diag.getLineNo()-1;
  58. if (ErrorLine >= LocInfo->getNumOperands())
  59. ErrorLine = 0;
  60. if (LocInfo->getNumOperands() != 0)
  61. if (const ConstantInt *CI =
  62. mdconst::dyn_extract<ConstantInt>(LocInfo->getOperand(ErrorLine)))
  63. LocCookie = CI->getZExtValue();
  64. }
  65. DiagInfo->DiagHandler(Diag, DiagInfo->DiagContext, LocCookie);
  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. // Remember if the buffer is nul terminated or not so we can avoid a copy.
  74. bool isNullTerminated = Str.back() == 0;
  75. if (isNullTerminated)
  76. Str = Str.substr(0, Str.size()-1);
  77. // If the output streamer does not have mature MC support or the integrated
  78. // assembler has been disabled, just emit the blob textually.
  79. // Otherwise parse the asm and emit it via MC support.
  80. // This is useful in case the asm parser doesn't handle something but the
  81. // system assembler does.
  82. const MCAsmInfo *MCAI = TM.getMCAsmInfo();
  83. assert(MCAI && "No MCAsmInfo");
  84. if (!MCAI->useIntegratedAssembler() &&
  85. !OutStreamer->isIntegratedAssemblerRequired()) {
  86. emitInlineAsmStart();
  87. OutStreamer->EmitRawText(Str);
  88. emitInlineAsmEnd(STI, nullptr);
  89. return;
  90. }
  91. SourceMgr SrcMgr;
  92. SrcMgrDiagInfo DiagInfo;
  93. // If the current LLVMContext has an inline asm handler, set it in SourceMgr.
  94. LLVMContext &LLVMCtx = MMI->getModule()->getContext();
  95. bool HasDiagHandler = false;
  96. if (LLVMCtx.getInlineAsmDiagnosticHandler() != nullptr) {
  97. // If the source manager has an issue, we arrange for srcMgrDiagHandler
  98. // to be invoked, getting DiagInfo passed into it.
  99. DiagInfo.LocInfo = LocMDNode;
  100. DiagInfo.DiagHandler = LLVMCtx.getInlineAsmDiagnosticHandler();
  101. DiagInfo.DiagContext = LLVMCtx.getInlineAsmDiagnosticContext();
  102. SrcMgr.setDiagHandler(srcMgrDiagHandler, &DiagInfo);
  103. HasDiagHandler = true;
  104. }
  105. std::unique_ptr<MemoryBuffer> Buffer;
  106. if (isNullTerminated)
  107. Buffer = MemoryBuffer::getMemBuffer(Str, "<inline asm>");
  108. else
  109. Buffer = MemoryBuffer::getMemBufferCopy(Str, "<inline asm>");
  110. // Tell SrcMgr about this buffer, it takes ownership of the buffer.
  111. SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
  112. std::unique_ptr<MCAsmParser> Parser(
  113. createMCAsmParser(SrcMgr, OutContext, *OutStreamer, *MAI));
  114. // Create a temporary copy of the original STI because the parser may modify
  115. // it. For example, when switching between arm and thumb mode. If the target
  116. // needs to emit code to return to the original state it can do so in
  117. // emitInlineAsmEnd().
  118. MCSubtargetInfo TmpSTI = STI;
  119. // We create a new MCInstrInfo here since we might be at the module level
  120. // and not have a MachineFunction to initialize the TargetInstrInfo from and
  121. // we only need MCInstrInfo for asm parsing. We create one unconditionally
  122. // because it's not subtarget dependent.
  123. std::unique_ptr<MCInstrInfo> MII(TM.getTarget().createMCInstrInfo());
  124. std::unique_ptr<MCTargetAsmParser> TAP(TM.getTarget().createMCAsmParser(
  125. TmpSTI, *Parser, *MII, MCOptions));
  126. if (!TAP)
  127. report_fatal_error("Inline asm not supported by this streamer because"
  128. " we don't have an asm parser for this target\n");
  129. Parser->setAssemblerDialect(Dialect);
  130. Parser->setTargetParser(*TAP.get());
  131. if (MF) {
  132. const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
  133. TAP->SetFrameRegister(TRI->getFrameRegister(*MF));
  134. }
  135. emitInlineAsmStart();
  136. // Don't implicitly switch to the text section before the asm.
  137. int Res = Parser->Run(/*NoInitialTextSection*/ true,
  138. /*NoFinalize*/ true);
  139. emitInlineAsmEnd(STI, &TmpSTI);
  140. if (Res && !HasDiagHandler)
  141. report_fatal_error("Error parsing inline asm\n");
  142. }
  143. static void EmitMSInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
  144. MachineModuleInfo *MMI, int InlineAsmVariant,
  145. AsmPrinter *AP, unsigned LocCookie,
  146. raw_ostream &OS) {
  147. // Switch to the inline assembly variant.
  148. OS << "\t.intel_syntax\n\t";
  149. const char *LastEmitted = AsmStr; // One past the last character emitted.
  150. unsigned NumOperands = MI->getNumOperands();
  151. while (*LastEmitted) {
  152. switch (*LastEmitted) {
  153. default: {
  154. // Not a special case, emit the string section literally.
  155. const char *LiteralEnd = LastEmitted+1;
  156. while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
  157. *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
  158. ++LiteralEnd;
  159. OS.write(LastEmitted, LiteralEnd-LastEmitted);
  160. LastEmitted = LiteralEnd;
  161. break;
  162. }
  163. case '\n':
  164. ++LastEmitted; // Consume newline character.
  165. OS << '\n'; // Indent code with newline.
  166. break;
  167. case '$': {
  168. ++LastEmitted; // Consume '$' character.
  169. bool Done = true;
  170. // Handle escapes.
  171. switch (*LastEmitted) {
  172. default: Done = false; break;
  173. case '$':
  174. ++LastEmitted; // Consume second '$' character.
  175. break;
  176. }
  177. if (Done) break;
  178. const char *IDStart = LastEmitted;
  179. const char *IDEnd = IDStart;
  180. while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
  181. unsigned Val;
  182. if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
  183. report_fatal_error("Bad $ operand number in inline asm string: '" +
  184. Twine(AsmStr) + "'");
  185. LastEmitted = IDEnd;
  186. if (Val >= NumOperands-1)
  187. report_fatal_error("Invalid $ operand number in inline asm string: '" +
  188. Twine(AsmStr) + "'");
  189. // Okay, we finally have a value number. Ask the target to print this
  190. // operand!
  191. unsigned OpNo = InlineAsm::MIOp_FirstOperand;
  192. bool Error = false;
  193. // Scan to find the machine operand number for the operand.
  194. for (; Val; --Val) {
  195. if (OpNo >= MI->getNumOperands()) break;
  196. unsigned OpFlags = MI->getOperand(OpNo).getImm();
  197. OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
  198. }
  199. // We may have a location metadata attached to the end of the
  200. // instruction, and at no point should see metadata at any
  201. // other point while processing. It's an error if so.
  202. if (OpNo >= MI->getNumOperands() ||
  203. MI->getOperand(OpNo).isMetadata()) {
  204. Error = true;
  205. } else {
  206. unsigned OpFlags = MI->getOperand(OpNo).getImm();
  207. ++OpNo; // Skip over the ID number.
  208. if (InlineAsm::isMemKind(OpFlags)) {
  209. Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
  210. /*Modifier*/ nullptr, OS);
  211. } else {
  212. Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
  213. /*Modifier*/ nullptr, OS);
  214. }
  215. }
  216. if (Error) {
  217. std::string msg;
  218. raw_string_ostream Msg(msg);
  219. Msg << "invalid operand in inline asm: '" << AsmStr << "'";
  220. MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
  221. }
  222. break;
  223. }
  224. }
  225. }
  226. OS << "\n\t.att_syntax\n" << (char)0; // null terminate string.
  227. }
  228. static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
  229. MachineModuleInfo *MMI, int InlineAsmVariant,
  230. int AsmPrinterVariant, AsmPrinter *AP,
  231. unsigned LocCookie, raw_ostream &OS) {
  232. int CurVariant = -1; // The number of the {.|.|.} region we are in.
  233. const char *LastEmitted = AsmStr; // One past the last character emitted.
  234. unsigned NumOperands = MI->getNumOperands();
  235. OS << '\t';
  236. while (*LastEmitted) {
  237. switch (*LastEmitted) {
  238. default: {
  239. // Not a special case, emit the string section literally.
  240. const char *LiteralEnd = LastEmitted+1;
  241. while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
  242. *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
  243. ++LiteralEnd;
  244. if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
  245. OS.write(LastEmitted, LiteralEnd-LastEmitted);
  246. LastEmitted = LiteralEnd;
  247. break;
  248. }
  249. case '\n':
  250. ++LastEmitted; // Consume newline character.
  251. OS << '\n'; // Indent code with newline.
  252. break;
  253. case '$': {
  254. ++LastEmitted; // Consume '$' character.
  255. bool Done = true;
  256. // Handle escapes.
  257. switch (*LastEmitted) {
  258. default: Done = false; break;
  259. case '$': // $$ -> $
  260. if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
  261. OS << '$';
  262. ++LastEmitted; // Consume second '$' character.
  263. break;
  264. case '(': // $( -> same as GCC's { character.
  265. ++LastEmitted; // Consume '(' character.
  266. if (CurVariant != -1)
  267. report_fatal_error("Nested variants found in inline asm string: '" +
  268. Twine(AsmStr) + "'");
  269. CurVariant = 0; // We're in the first variant now.
  270. break;
  271. case '|':
  272. ++LastEmitted; // consume '|' character.
  273. if (CurVariant == -1)
  274. OS << '|'; // this is gcc's behavior for | outside a variant
  275. else
  276. ++CurVariant; // We're in the next variant.
  277. break;
  278. case ')': // $) -> same as GCC's } char.
  279. ++LastEmitted; // consume ')' character.
  280. if (CurVariant == -1)
  281. OS << '}'; // this is gcc's behavior for } outside a variant
  282. else
  283. CurVariant = -1;
  284. break;
  285. }
  286. if (Done) break;
  287. bool HasCurlyBraces = false;
  288. if (*LastEmitted == '{') { // ${variable}
  289. ++LastEmitted; // Consume '{' character.
  290. HasCurlyBraces = true;
  291. }
  292. // If we have ${:foo}, then this is not a real operand reference, it is a
  293. // "magic" string reference, just like in .td files. Arrange to call
  294. // PrintSpecial.
  295. if (HasCurlyBraces && *LastEmitted == ':') {
  296. ++LastEmitted;
  297. const char *StrStart = LastEmitted;
  298. const char *StrEnd = strchr(StrStart, '}');
  299. if (!StrEnd)
  300. report_fatal_error("Unterminated ${:foo} operand in inline asm"
  301. " string: '" + Twine(AsmStr) + "'");
  302. std::string Val(StrStart, StrEnd);
  303. AP->PrintSpecial(MI, OS, Val.c_str());
  304. LastEmitted = StrEnd+1;
  305. break;
  306. }
  307. const char *IDStart = LastEmitted;
  308. const char *IDEnd = IDStart;
  309. while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
  310. unsigned Val;
  311. if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
  312. report_fatal_error("Bad $ operand number in inline asm string: '" +
  313. Twine(AsmStr) + "'");
  314. LastEmitted = IDEnd;
  315. char Modifier[2] = { 0, 0 };
  316. if (HasCurlyBraces) {
  317. // If we have curly braces, check for a modifier character. This
  318. // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
  319. if (*LastEmitted == ':') {
  320. ++LastEmitted; // Consume ':' character.
  321. if (*LastEmitted == 0)
  322. report_fatal_error("Bad ${:} expression in inline asm string: '" +
  323. Twine(AsmStr) + "'");
  324. Modifier[0] = *LastEmitted;
  325. ++LastEmitted; // Consume modifier character.
  326. }
  327. if (*LastEmitted != '}')
  328. report_fatal_error("Bad ${} expression in inline asm string: '" +
  329. Twine(AsmStr) + "'");
  330. ++LastEmitted; // Consume '}' character.
  331. }
  332. if (Val >= NumOperands-1)
  333. report_fatal_error("Invalid $ operand number in inline asm string: '" +
  334. Twine(AsmStr) + "'");
  335. // Okay, we finally have a value number. Ask the target to print this
  336. // operand!
  337. if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
  338. unsigned OpNo = InlineAsm::MIOp_FirstOperand;
  339. bool Error = false;
  340. // Scan to find the machine operand number for the operand.
  341. for (; Val; --Val) {
  342. if (OpNo >= MI->getNumOperands()) break;
  343. unsigned OpFlags = MI->getOperand(OpNo).getImm();
  344. OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
  345. }
  346. // We may have a location metadata attached to the end of the
  347. // instruction, and at no point should see metadata at any
  348. // other point while processing. It's an error if so.
  349. if (OpNo >= MI->getNumOperands() ||
  350. MI->getOperand(OpNo).isMetadata()) {
  351. Error = true;
  352. } else {
  353. unsigned OpFlags = MI->getOperand(OpNo).getImm();
  354. ++OpNo; // Skip over the ID number.
  355. if (Modifier[0] == 'l') { // Labels are target independent.
  356. // FIXME: What if the operand isn't an MBB, report error?
  357. const MCSymbol *Sym = MI->getOperand(OpNo).getMBB()->getSymbol();
  358. Sym->print(OS, AP->MAI);
  359. } else {
  360. if (InlineAsm::isMemKind(OpFlags)) {
  361. Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
  362. Modifier[0] ? Modifier : nullptr,
  363. OS);
  364. } else {
  365. Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
  366. Modifier[0] ? Modifier : nullptr, OS);
  367. }
  368. }
  369. }
  370. if (Error) {
  371. std::string msg;
  372. raw_string_ostream Msg(msg);
  373. Msg << "invalid operand in inline asm: '" << AsmStr << "'";
  374. MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
  375. }
  376. }
  377. break;
  378. }
  379. }
  380. }
  381. OS << '\n' << (char)0; // null terminate string.
  382. }
  383. /// EmitInlineAsm - This method formats and emits the specified machine
  384. /// instruction that is an inline asm.
  385. void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const {
  386. assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
  387. // Count the number of register definitions to find the asm string.
  388. unsigned NumDefs = 0;
  389. for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
  390. ++NumDefs)
  391. assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
  392. assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
  393. // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
  394. const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
  395. // If this asmstr is empty, just print the #APP/#NOAPP markers.
  396. // These are useful to see where empty asm's wound up.
  397. if (AsmStr[0] == 0) {
  398. OutStreamer->emitRawComment(MAI->getInlineAsmStart());
  399. OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
  400. return;
  401. }
  402. // Emit the #APP start marker. This has to happen even if verbose-asm isn't
  403. // enabled, so we use emitRawComment.
  404. OutStreamer->emitRawComment(MAI->getInlineAsmStart());
  405. // Get the !srcloc metadata node if we have it, and decode the loc cookie from
  406. // it.
  407. unsigned LocCookie = 0;
  408. const MDNode *LocMD = nullptr;
  409. for (unsigned i = MI->getNumOperands(); i != 0; --i) {
  410. if (MI->getOperand(i-1).isMetadata() &&
  411. (LocMD = MI->getOperand(i-1).getMetadata()) &&
  412. LocMD->getNumOperands() != 0) {
  413. if (const ConstantInt *CI =
  414. mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
  415. LocCookie = CI->getZExtValue();
  416. break;
  417. }
  418. }
  419. }
  420. // Emit the inline asm to a temporary string so we can emit it through
  421. // EmitInlineAsm.
  422. SmallString<256> StringData;
  423. raw_svector_ostream OS(StringData);
  424. // The variant of the current asmprinter.
  425. int AsmPrinterVariant = MAI->getAssemblerDialect();
  426. InlineAsm::AsmDialect InlineAsmVariant = MI->getInlineAsmDialect();
  427. AsmPrinter *AP = const_cast<AsmPrinter*>(this);
  428. if (InlineAsmVariant == InlineAsm::AD_ATT)
  429. EmitGCCInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AsmPrinterVariant,
  430. AP, LocCookie, OS);
  431. else
  432. EmitMSInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AP, LocCookie, OS);
  433. // Reset SanitizeAddress based on the function's attribute.
  434. MCTargetOptions MCOptions = TM.Options.MCOptions;
  435. MCOptions.SanitizeAddress =
  436. MF->getFunction()->hasFnAttribute(Attribute::SanitizeAddress);
  437. EmitInlineAsm(OS.str(), getSubtargetInfo(), MCOptions, LocMD,
  438. MI->getInlineAsmDialect());
  439. // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't
  440. // enabled, so we use emitRawComment.
  441. OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
  442. }
  443. /// PrintSpecial - Print information related to the specified machine instr
  444. /// that is independent of the operand, and may be independent of the instr
  445. /// itself. This can be useful for portably encoding the comment character
  446. /// or other bits of target-specific knowledge into the asmstrings. The
  447. /// syntax used is ${:comment}. Targets can override this to add support
  448. /// for their own strange codes.
  449. void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
  450. const char *Code) const {
  451. const DataLayout *DL = TM.getDataLayout();
  452. if (!strcmp(Code, "private")) {
  453. OS << DL->getPrivateGlobalPrefix();
  454. } else if (!strcmp(Code, "comment")) {
  455. OS << MAI->getCommentString();
  456. } else if (!strcmp(Code, "uid")) {
  457. // Comparing the address of MI isn't sufficient, because machineinstrs may
  458. // be allocated to the same address across functions.
  459. // If this is a new LastFn instruction, bump the counter.
  460. if (LastMI != MI || LastFn != getFunctionNumber()) {
  461. ++Counter;
  462. LastMI = MI;
  463. LastFn = getFunctionNumber();
  464. }
  465. OS << Counter;
  466. } else {
  467. std::string msg;
  468. raw_string_ostream Msg(msg);
  469. Msg << "Unknown special formatter '" << Code
  470. << "' for machine instr: " << *MI;
  471. report_fatal_error(Msg.str());
  472. }
  473. }
  474. /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
  475. /// instruction, using the specified assembler variant. Targets should
  476. /// override this to format as appropriate.
  477. bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
  478. unsigned AsmVariant, const char *ExtraCode,
  479. raw_ostream &O) {
  480. // Does this asm operand have a single letter operand modifier?
  481. if (ExtraCode && ExtraCode[0]) {
  482. if (ExtraCode[1] != 0) return true; // Unknown modifier.
  483. const MachineOperand &MO = MI->getOperand(OpNo);
  484. switch (ExtraCode[0]) {
  485. default:
  486. return true; // Unknown modifier.
  487. case 'c': // Substitute immediate value without immediate syntax
  488. if (MO.getType() != MachineOperand::MO_Immediate)
  489. return true;
  490. O << MO.getImm();
  491. return false;
  492. case 'n': // Negate the immediate constant.
  493. if (MO.getType() != MachineOperand::MO_Immediate)
  494. return true;
  495. O << -MO.getImm();
  496. return false;
  497. }
  498. }
  499. return true;
  500. }
  501. bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
  502. unsigned AsmVariant,
  503. const char *ExtraCode, raw_ostream &O) {
  504. // Target doesn't support this yet!
  505. return true;
  506. }
  507. void AsmPrinter::emitInlineAsmStart() const {}
  508. void AsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
  509. const MCSubtargetInfo *EndInfo) const {}