X86ATTInstPrinter.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. //===-- X86ATTInstPrinter.cpp - AT&T assembly instruction printing --------===//
  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 includes code for rendering MCInst instances as AT&T-style
  10. // assembly.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "X86ATTInstPrinter.h"
  14. #include "X86BaseInfo.h"
  15. #include "X86InstComments.h"
  16. #include "llvm/MC/MCExpr.h"
  17. #include "llvm/MC/MCInst.h"
  18. #include "llvm/MC/MCInstrAnalysis.h"
  19. #include "llvm/MC/MCInstrInfo.h"
  20. #include "llvm/MC/MCSubtargetInfo.h"
  21. #include "llvm/Support/Casting.h"
  22. #include "llvm/Support/ErrorHandling.h"
  23. #include "llvm/Support/Format.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. #include <cassert>
  26. #include <cinttypes>
  27. #include <cstdint>
  28. using namespace llvm;
  29. #define DEBUG_TYPE "asm-printer"
  30. // Include the auto-generated portion of the assembly writer.
  31. #define PRINT_ALIAS_INSTR
  32. #include "X86GenAsmWriter.inc"
  33. void X86ATTInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
  34. OS << markup("<reg:") << '%' << getRegisterName(RegNo) << markup(">");
  35. }
  36. void X86ATTInstPrinter::printInst(const MCInst *MI, uint64_t Address,
  37. StringRef Annot, const MCSubtargetInfo &STI,
  38. raw_ostream &OS) {
  39. // If verbose assembly is enabled, we can print some informative comments.
  40. if (CommentStream)
  41. HasCustomInstComment = EmitAnyX86InstComments(MI, *CommentStream, MII);
  42. printInstFlags(MI, OS);
  43. // Output CALLpcrel32 as "callq" in 64-bit mode.
  44. // In Intel annotation it's always emitted as "call".
  45. //
  46. // TODO: Probably this hack should be redesigned via InstAlias in
  47. // InstrInfo.td as soon as Requires clause is supported properly
  48. // for InstAlias.
  49. if (MI->getOpcode() == X86::CALLpcrel32 &&
  50. (STI.getFeatureBits()[X86::Mode64Bit])) {
  51. OS << "\tcallq\t";
  52. printPCRelImm(MI, Address, 0, OS);
  53. }
  54. // data16 and data32 both have the same encoding of 0x66. While data32 is
  55. // valid only in 16 bit systems, data16 is valid in the rest.
  56. // There seems to be some lack of support of the Requires clause that causes
  57. // 0x66 to be interpreted as "data16" by the asm printer.
  58. // Thus we add an adjustment here in order to print the "right" instruction.
  59. else if (MI->getOpcode() == X86::DATA16_PREFIX &&
  60. STI.getFeatureBits()[X86::Mode16Bit]) {
  61. OS << "\tdata32";
  62. }
  63. // Try to print any aliases first.
  64. else if (!printAliasInstr(MI, Address, OS) && !printVecCompareInstr(MI, OS))
  65. printInstruction(MI, Address, OS);
  66. // Next always print the annotation.
  67. printAnnotation(OS, Annot);
  68. }
  69. bool X86ATTInstPrinter::printVecCompareInstr(const MCInst *MI,
  70. raw_ostream &OS) {
  71. if (MI->getNumOperands() == 0 ||
  72. !MI->getOperand(MI->getNumOperands() - 1).isImm())
  73. return false;
  74. int64_t Imm = MI->getOperand(MI->getNumOperands() - 1).getImm();
  75. const MCInstrDesc &Desc = MII.get(MI->getOpcode());
  76. // Custom print the vector compare instructions to get the immediate
  77. // translated into the mnemonic.
  78. switch (MI->getOpcode()) {
  79. case X86::CMPPDrmi: case X86::CMPPDrri:
  80. case X86::CMPPSrmi: case X86::CMPPSrri:
  81. case X86::CMPSDrm: case X86::CMPSDrr:
  82. case X86::CMPSDrm_Int: case X86::CMPSDrr_Int:
  83. case X86::CMPSSrm: case X86::CMPSSrr:
  84. case X86::CMPSSrm_Int: case X86::CMPSSrr_Int:
  85. if (Imm >= 0 && Imm <= 7) {
  86. OS << '\t';
  87. printCMPMnemonic(MI, /*IsVCMP*/false, OS);
  88. if ((Desc.TSFlags & X86II::FormMask) == X86II::MRMSrcMem) {
  89. if ((Desc.TSFlags & X86II::OpPrefixMask) == X86II::XS)
  90. printdwordmem(MI, 2, OS);
  91. else if ((Desc.TSFlags & X86II::OpPrefixMask) == X86II::XD)
  92. printqwordmem(MI, 2, OS);
  93. else
  94. printxmmwordmem(MI, 2, OS);
  95. } else
  96. printOperand(MI, 2, OS);
  97. // Skip operand 1 as its tied to the dest.
  98. OS << ", ";
  99. printOperand(MI, 0, OS);
  100. return true;
  101. }
  102. break;
  103. case X86::VCMPPDrmi: case X86::VCMPPDrri:
  104. case X86::VCMPPDYrmi: case X86::VCMPPDYrri:
  105. case X86::VCMPPDZ128rmi: case X86::VCMPPDZ128rri:
  106. case X86::VCMPPDZ256rmi: case X86::VCMPPDZ256rri:
  107. case X86::VCMPPDZrmi: case X86::VCMPPDZrri:
  108. case X86::VCMPPSrmi: case X86::VCMPPSrri:
  109. case X86::VCMPPSYrmi: case X86::VCMPPSYrri:
  110. case X86::VCMPPSZ128rmi: case X86::VCMPPSZ128rri:
  111. case X86::VCMPPSZ256rmi: case X86::VCMPPSZ256rri:
  112. case X86::VCMPPSZrmi: case X86::VCMPPSZrri:
  113. case X86::VCMPSDrm: case X86::VCMPSDrr:
  114. case X86::VCMPSDZrm: case X86::VCMPSDZrr:
  115. case X86::VCMPSDrm_Int: case X86::VCMPSDrr_Int:
  116. case X86::VCMPSDZrm_Int: case X86::VCMPSDZrr_Int:
  117. case X86::VCMPSSrm: case X86::VCMPSSrr:
  118. case X86::VCMPSSZrm: case X86::VCMPSSZrr:
  119. case X86::VCMPSSrm_Int: case X86::VCMPSSrr_Int:
  120. case X86::VCMPSSZrm_Int: case X86::VCMPSSZrr_Int:
  121. case X86::VCMPPDZ128rmik: case X86::VCMPPDZ128rrik:
  122. case X86::VCMPPDZ256rmik: case X86::VCMPPDZ256rrik:
  123. case X86::VCMPPDZrmik: case X86::VCMPPDZrrik:
  124. case X86::VCMPPSZ128rmik: case X86::VCMPPSZ128rrik:
  125. case X86::VCMPPSZ256rmik: case X86::VCMPPSZ256rrik:
  126. case X86::VCMPPSZrmik: case X86::VCMPPSZrrik:
  127. case X86::VCMPSDZrm_Intk: case X86::VCMPSDZrr_Intk:
  128. case X86::VCMPSSZrm_Intk: case X86::VCMPSSZrr_Intk:
  129. case X86::VCMPPDZ128rmbi: case X86::VCMPPDZ128rmbik:
  130. case X86::VCMPPDZ256rmbi: case X86::VCMPPDZ256rmbik:
  131. case X86::VCMPPDZrmbi: case X86::VCMPPDZrmbik:
  132. case X86::VCMPPSZ128rmbi: case X86::VCMPPSZ128rmbik:
  133. case X86::VCMPPSZ256rmbi: case X86::VCMPPSZ256rmbik:
  134. case X86::VCMPPSZrmbi: case X86::VCMPPSZrmbik:
  135. case X86::VCMPPDZrrib: case X86::VCMPPDZrribk:
  136. case X86::VCMPPSZrrib: case X86::VCMPPSZrribk:
  137. case X86::VCMPSDZrrb_Int: case X86::VCMPSDZrrb_Intk:
  138. case X86::VCMPSSZrrb_Int: case X86::VCMPSSZrrb_Intk:
  139. case X86::VCMPPHZ128rmi: case X86::VCMPPHZ128rri:
  140. case X86::VCMPPHZ256rmi: case X86::VCMPPHZ256rri:
  141. case X86::VCMPPHZrmi: case X86::VCMPPHZrri:
  142. case X86::VCMPSHZrm: case X86::VCMPSHZrr:
  143. case X86::VCMPSHZrm_Int: case X86::VCMPSHZrr_Int:
  144. case X86::VCMPPHZ128rmik: case X86::VCMPPHZ128rrik:
  145. case X86::VCMPPHZ256rmik: case X86::VCMPPHZ256rrik:
  146. case X86::VCMPPHZrmik: case X86::VCMPPHZrrik:
  147. case X86::VCMPSHZrm_Intk: case X86::VCMPSHZrr_Intk:
  148. case X86::VCMPPHZ128rmbi: case X86::VCMPPHZ128rmbik:
  149. case X86::VCMPPHZ256rmbi: case X86::VCMPPHZ256rmbik:
  150. case X86::VCMPPHZrmbi: case X86::VCMPPHZrmbik:
  151. case X86::VCMPPHZrrib: case X86::VCMPPHZrribk:
  152. case X86::VCMPSHZrrb_Int: case X86::VCMPSHZrrb_Intk:
  153. if (Imm >= 0 && Imm <= 31) {
  154. OS << '\t';
  155. printCMPMnemonic(MI, /*IsVCMP*/true, OS);
  156. unsigned CurOp = (Desc.TSFlags & X86II::EVEX_K) ? 3 : 2;
  157. if ((Desc.TSFlags & X86II::FormMask) == X86II::MRMSrcMem) {
  158. if (Desc.TSFlags & X86II::EVEX_B) {
  159. // Broadcast form.
  160. // Load size is word for TA map. Otherwise it is based on W-bit.
  161. if ((Desc.TSFlags & X86II::OpMapMask) == X86II::TA) {
  162. assert(!(Desc.TSFlags & X86II::VEX_W) && "Unknown W-bit value!");
  163. printwordmem(MI, CurOp--, OS);
  164. } else if (Desc.TSFlags & X86II::VEX_W) {
  165. printqwordmem(MI, CurOp--, OS);
  166. } else {
  167. printdwordmem(MI, CurOp--, OS);
  168. }
  169. // Print the number of elements broadcasted.
  170. unsigned NumElts;
  171. if (Desc.TSFlags & X86II::EVEX_L2)
  172. NumElts = (Desc.TSFlags & X86II::VEX_W) ? 8 : 16;
  173. else if (Desc.TSFlags & X86II::VEX_L)
  174. NumElts = (Desc.TSFlags & X86II::VEX_W) ? 4 : 8;
  175. else
  176. NumElts = (Desc.TSFlags & X86II::VEX_W) ? 2 : 4;
  177. if ((Desc.TSFlags & X86II::OpMapMask) == X86II::TA) {
  178. assert(!(Desc.TSFlags & X86II::VEX_W) && "Unknown W-bit value!");
  179. NumElts *= 2;
  180. }
  181. OS << "{1to" << NumElts << "}";
  182. } else {
  183. if ((Desc.TSFlags & X86II::OpPrefixMask) == X86II::XS) {
  184. if ((Desc.TSFlags & X86II::OpMapMask) == X86II::TA)
  185. printwordmem(MI, CurOp--, OS);
  186. else
  187. printdwordmem(MI, CurOp--, OS);
  188. } else if ((Desc.TSFlags & X86II::OpPrefixMask) == X86II::XD) {
  189. assert((Desc.TSFlags & X86II::OpMapMask) != X86II::TA &&
  190. "Unexpected op map!");
  191. printqwordmem(MI, CurOp--, OS);
  192. } else if (Desc.TSFlags & X86II::EVEX_L2) {
  193. printzmmwordmem(MI, CurOp--, OS);
  194. } else if (Desc.TSFlags & X86II::VEX_L) {
  195. printymmwordmem(MI, CurOp--, OS);
  196. } else {
  197. printxmmwordmem(MI, CurOp--, OS);
  198. }
  199. }
  200. } else {
  201. if (Desc.TSFlags & X86II::EVEX_B)
  202. OS << "{sae}, ";
  203. printOperand(MI, CurOp--, OS);
  204. }
  205. OS << ", ";
  206. printOperand(MI, CurOp--, OS);
  207. OS << ", ";
  208. printOperand(MI, 0, OS);
  209. if (CurOp > 0) {
  210. // Print mask operand.
  211. OS << " {";
  212. printOperand(MI, CurOp--, OS);
  213. OS << "}";
  214. }
  215. return true;
  216. }
  217. break;
  218. case X86::VPCOMBmi: case X86::VPCOMBri:
  219. case X86::VPCOMDmi: case X86::VPCOMDri:
  220. case X86::VPCOMQmi: case X86::VPCOMQri:
  221. case X86::VPCOMUBmi: case X86::VPCOMUBri:
  222. case X86::VPCOMUDmi: case X86::VPCOMUDri:
  223. case X86::VPCOMUQmi: case X86::VPCOMUQri:
  224. case X86::VPCOMUWmi: case X86::VPCOMUWri:
  225. case X86::VPCOMWmi: case X86::VPCOMWri:
  226. if (Imm >= 0 && Imm <= 7) {
  227. OS << '\t';
  228. printVPCOMMnemonic(MI, OS);
  229. if ((Desc.TSFlags & X86II::FormMask) == X86II::MRMSrcMem)
  230. printxmmwordmem(MI, 2, OS);
  231. else
  232. printOperand(MI, 2, OS);
  233. OS << ", ";
  234. printOperand(MI, 1, OS);
  235. OS << ", ";
  236. printOperand(MI, 0, OS);
  237. return true;
  238. }
  239. break;
  240. case X86::VPCMPBZ128rmi: case X86::VPCMPBZ128rri:
  241. case X86::VPCMPBZ256rmi: case X86::VPCMPBZ256rri:
  242. case X86::VPCMPBZrmi: case X86::VPCMPBZrri:
  243. case X86::VPCMPDZ128rmi: case X86::VPCMPDZ128rri:
  244. case X86::VPCMPDZ256rmi: case X86::VPCMPDZ256rri:
  245. case X86::VPCMPDZrmi: case X86::VPCMPDZrri:
  246. case X86::VPCMPQZ128rmi: case X86::VPCMPQZ128rri:
  247. case X86::VPCMPQZ256rmi: case X86::VPCMPQZ256rri:
  248. case X86::VPCMPQZrmi: case X86::VPCMPQZrri:
  249. case X86::VPCMPUBZ128rmi: case X86::VPCMPUBZ128rri:
  250. case X86::VPCMPUBZ256rmi: case X86::VPCMPUBZ256rri:
  251. case X86::VPCMPUBZrmi: case X86::VPCMPUBZrri:
  252. case X86::VPCMPUDZ128rmi: case X86::VPCMPUDZ128rri:
  253. case X86::VPCMPUDZ256rmi: case X86::VPCMPUDZ256rri:
  254. case X86::VPCMPUDZrmi: case X86::VPCMPUDZrri:
  255. case X86::VPCMPUQZ128rmi: case X86::VPCMPUQZ128rri:
  256. case X86::VPCMPUQZ256rmi: case X86::VPCMPUQZ256rri:
  257. case X86::VPCMPUQZrmi: case X86::VPCMPUQZrri:
  258. case X86::VPCMPUWZ128rmi: case X86::VPCMPUWZ128rri:
  259. case X86::VPCMPUWZ256rmi: case X86::VPCMPUWZ256rri:
  260. case X86::VPCMPUWZrmi: case X86::VPCMPUWZrri:
  261. case X86::VPCMPWZ128rmi: case X86::VPCMPWZ128rri:
  262. case X86::VPCMPWZ256rmi: case X86::VPCMPWZ256rri:
  263. case X86::VPCMPWZrmi: case X86::VPCMPWZrri:
  264. case X86::VPCMPBZ128rmik: case X86::VPCMPBZ128rrik:
  265. case X86::VPCMPBZ256rmik: case X86::VPCMPBZ256rrik:
  266. case X86::VPCMPBZrmik: case X86::VPCMPBZrrik:
  267. case X86::VPCMPDZ128rmik: case X86::VPCMPDZ128rrik:
  268. case X86::VPCMPDZ256rmik: case X86::VPCMPDZ256rrik:
  269. case X86::VPCMPDZrmik: case X86::VPCMPDZrrik:
  270. case X86::VPCMPQZ128rmik: case X86::VPCMPQZ128rrik:
  271. case X86::VPCMPQZ256rmik: case X86::VPCMPQZ256rrik:
  272. case X86::VPCMPQZrmik: case X86::VPCMPQZrrik:
  273. case X86::VPCMPUBZ128rmik: case X86::VPCMPUBZ128rrik:
  274. case X86::VPCMPUBZ256rmik: case X86::VPCMPUBZ256rrik:
  275. case X86::VPCMPUBZrmik: case X86::VPCMPUBZrrik:
  276. case X86::VPCMPUDZ128rmik: case X86::VPCMPUDZ128rrik:
  277. case X86::VPCMPUDZ256rmik: case X86::VPCMPUDZ256rrik:
  278. case X86::VPCMPUDZrmik: case X86::VPCMPUDZrrik:
  279. case X86::VPCMPUQZ128rmik: case X86::VPCMPUQZ128rrik:
  280. case X86::VPCMPUQZ256rmik: case X86::VPCMPUQZ256rrik:
  281. case X86::VPCMPUQZrmik: case X86::VPCMPUQZrrik:
  282. case X86::VPCMPUWZ128rmik: case X86::VPCMPUWZ128rrik:
  283. case X86::VPCMPUWZ256rmik: case X86::VPCMPUWZ256rrik:
  284. case X86::VPCMPUWZrmik: case X86::VPCMPUWZrrik:
  285. case X86::VPCMPWZ128rmik: case X86::VPCMPWZ128rrik:
  286. case X86::VPCMPWZ256rmik: case X86::VPCMPWZ256rrik:
  287. case X86::VPCMPWZrmik: case X86::VPCMPWZrrik:
  288. case X86::VPCMPDZ128rmib: case X86::VPCMPDZ128rmibk:
  289. case X86::VPCMPDZ256rmib: case X86::VPCMPDZ256rmibk:
  290. case X86::VPCMPDZrmib: case X86::VPCMPDZrmibk:
  291. case X86::VPCMPQZ128rmib: case X86::VPCMPQZ128rmibk:
  292. case X86::VPCMPQZ256rmib: case X86::VPCMPQZ256rmibk:
  293. case X86::VPCMPQZrmib: case X86::VPCMPQZrmibk:
  294. case X86::VPCMPUDZ128rmib: case X86::VPCMPUDZ128rmibk:
  295. case X86::VPCMPUDZ256rmib: case X86::VPCMPUDZ256rmibk:
  296. case X86::VPCMPUDZrmib: case X86::VPCMPUDZrmibk:
  297. case X86::VPCMPUQZ128rmib: case X86::VPCMPUQZ128rmibk:
  298. case X86::VPCMPUQZ256rmib: case X86::VPCMPUQZ256rmibk:
  299. case X86::VPCMPUQZrmib: case X86::VPCMPUQZrmibk:
  300. if ((Imm >= 0 && Imm <= 2) || (Imm >= 4 && Imm <= 6)) {
  301. OS << '\t';
  302. printVPCMPMnemonic(MI, OS);
  303. unsigned CurOp = (Desc.TSFlags & X86II::EVEX_K) ? 3 : 2;
  304. if ((Desc.TSFlags & X86II::FormMask) == X86II::MRMSrcMem) {
  305. if (Desc.TSFlags & X86II::EVEX_B) {
  306. // Broadcast form.
  307. // Load size is based on W-bit as only D and Q are supported.
  308. if (Desc.TSFlags & X86II::VEX_W)
  309. printqwordmem(MI, CurOp--, OS);
  310. else
  311. printdwordmem(MI, CurOp--, OS);
  312. // Print the number of elements broadcasted.
  313. unsigned NumElts;
  314. if (Desc.TSFlags & X86II::EVEX_L2)
  315. NumElts = (Desc.TSFlags & X86II::VEX_W) ? 8 : 16;
  316. else if (Desc.TSFlags & X86II::VEX_L)
  317. NumElts = (Desc.TSFlags & X86II::VEX_W) ? 4 : 8;
  318. else
  319. NumElts = (Desc.TSFlags & X86II::VEX_W) ? 2 : 4;
  320. OS << "{1to" << NumElts << "}";
  321. } else {
  322. if (Desc.TSFlags & X86II::EVEX_L2)
  323. printzmmwordmem(MI, CurOp--, OS);
  324. else if (Desc.TSFlags & X86II::VEX_L)
  325. printymmwordmem(MI, CurOp--, OS);
  326. else
  327. printxmmwordmem(MI, CurOp--, OS);
  328. }
  329. } else {
  330. printOperand(MI, CurOp--, OS);
  331. }
  332. OS << ", ";
  333. printOperand(MI, CurOp--, OS);
  334. OS << ", ";
  335. printOperand(MI, 0, OS);
  336. if (CurOp > 0) {
  337. // Print mask operand.
  338. OS << " {";
  339. printOperand(MI, CurOp--, OS);
  340. OS << "}";
  341. }
  342. return true;
  343. }
  344. break;
  345. }
  346. return false;
  347. }
  348. void X86ATTInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
  349. raw_ostream &O) {
  350. const MCOperand &Op = MI->getOperand(OpNo);
  351. if (Op.isReg()) {
  352. printRegName(O, Op.getReg());
  353. } else if (Op.isImm()) {
  354. // Print immediates as signed values.
  355. int64_t Imm = Op.getImm();
  356. O << markup("<imm:") << '$' << formatImm(Imm) << markup(">");
  357. // TODO: This should be in a helper function in the base class, so it can
  358. // be used by other printers.
  359. // If there are no instruction-specific comments, add a comment clarifying
  360. // the hex value of the immediate operand when it isn't in the range
  361. // [-256,255].
  362. if (CommentStream && !HasCustomInstComment && (Imm > 255 || Imm < -256)) {
  363. // Don't print unnecessary hex sign bits.
  364. if (Imm == (int16_t)(Imm))
  365. *CommentStream << format("imm = 0x%" PRIX16 "\n", (uint16_t)Imm);
  366. else if (Imm == (int32_t)(Imm))
  367. *CommentStream << format("imm = 0x%" PRIX32 "\n", (uint32_t)Imm);
  368. else
  369. *CommentStream << format("imm = 0x%" PRIX64 "\n", (uint64_t)Imm);
  370. }
  371. } else {
  372. assert(Op.isExpr() && "unknown operand kind in printOperand");
  373. O << markup("<imm:") << '$';
  374. Op.getExpr()->print(O, &MAI);
  375. O << markup(">");
  376. }
  377. }
  378. void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
  379. raw_ostream &O) {
  380. // Do not print the exact form of the memory operand if it references a known
  381. // binary object.
  382. if (SymbolizeOperands && MIA) {
  383. uint64_t Target;
  384. if (MIA->evaluateBranch(*MI, 0, 0, Target))
  385. return;
  386. if (MIA->evaluateMemoryOperandAddress(*MI, /*STI=*/nullptr, 0, 0))
  387. return;
  388. }
  389. const MCOperand &BaseReg = MI->getOperand(Op + X86::AddrBaseReg);
  390. const MCOperand &IndexReg = MI->getOperand(Op + X86::AddrIndexReg);
  391. const MCOperand &DispSpec = MI->getOperand(Op + X86::AddrDisp);
  392. O << markup("<mem:");
  393. // If this has a segment register, print it.
  394. printOptionalSegReg(MI, Op + X86::AddrSegmentReg, O);
  395. if (DispSpec.isImm()) {
  396. int64_t DispVal = DispSpec.getImm();
  397. if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
  398. O << formatImm(DispVal);
  399. } else {
  400. assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
  401. DispSpec.getExpr()->print(O, &MAI);
  402. }
  403. if (IndexReg.getReg() || BaseReg.getReg()) {
  404. O << '(';
  405. if (BaseReg.getReg())
  406. printOperand(MI, Op + X86::AddrBaseReg, O);
  407. if (IndexReg.getReg()) {
  408. O << ',';
  409. printOperand(MI, Op + X86::AddrIndexReg, O);
  410. unsigned ScaleVal = MI->getOperand(Op + X86::AddrScaleAmt).getImm();
  411. if (ScaleVal != 1) {
  412. O << ',' << markup("<imm:") << ScaleVal // never printed in hex.
  413. << markup(">");
  414. }
  415. }
  416. O << ')';
  417. }
  418. O << markup(">");
  419. }
  420. void X86ATTInstPrinter::printSrcIdx(const MCInst *MI, unsigned Op,
  421. raw_ostream &O) {
  422. O << markup("<mem:");
  423. // If this has a segment register, print it.
  424. printOptionalSegReg(MI, Op + 1, O);
  425. O << "(";
  426. printOperand(MI, Op, O);
  427. O << ")";
  428. O << markup(">");
  429. }
  430. void X86ATTInstPrinter::printDstIdx(const MCInst *MI, unsigned Op,
  431. raw_ostream &O) {
  432. O << markup("<mem:");
  433. O << "%es:(";
  434. printOperand(MI, Op, O);
  435. O << ")";
  436. O << markup(">");
  437. }
  438. void X86ATTInstPrinter::printMemOffset(const MCInst *MI, unsigned Op,
  439. raw_ostream &O) {
  440. const MCOperand &DispSpec = MI->getOperand(Op);
  441. O << markup("<mem:");
  442. // If this has a segment register, print it.
  443. printOptionalSegReg(MI, Op + 1, O);
  444. if (DispSpec.isImm()) {
  445. O << formatImm(DispSpec.getImm());
  446. } else {
  447. assert(DispSpec.isExpr() && "non-immediate displacement?");
  448. DispSpec.getExpr()->print(O, &MAI);
  449. }
  450. O << markup(">");
  451. }
  452. void X86ATTInstPrinter::printU8Imm(const MCInst *MI, unsigned Op,
  453. raw_ostream &O) {
  454. if (MI->getOperand(Op).isExpr())
  455. return printOperand(MI, Op, O);
  456. O << markup("<imm:") << '$' << formatImm(MI->getOperand(Op).getImm() & 0xff)
  457. << markup(">");
  458. }
  459. void X86ATTInstPrinter::printSTiRegOperand(const MCInst *MI, unsigned OpNo,
  460. raw_ostream &OS) {
  461. const MCOperand &Op = MI->getOperand(OpNo);
  462. unsigned Reg = Op.getReg();
  463. // Override the default printing to print st(0) instead st.
  464. if (Reg == X86::ST0)
  465. OS << markup("<reg:") << "%st(0)" << markup(">");
  466. else
  467. printRegName(OS, Reg);
  468. }