LLVM  8.0.1
X86InstPrinterCommon.cpp
Go to the documentation of this file.
1 //===--- X86InstPrinterCommon.cpp - X86 assembly instruction printing -----===//
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 includes common code for rendering MCInst instances as Intel-style
11 // and Intel-style assembly.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "X86InstPrinterCommon.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCInstrDesc.h"
20 #include "llvm/MC/MCInstrInfo.h"
22 #include "llvm/Support/Casting.h"
23 #include <cstdint>
24 #include <cassert>
25 
26 using namespace llvm;
27 
29  raw_ostream &O) {
30  int64_t Imm = MI->getOperand(Op).getImm();
31  switch (Imm) {
32  default: llvm_unreachable("Invalid ssecc/avxcc argument!");
33  case 0: O << "eq"; break;
34  case 1: O << "lt"; break;
35  case 2: O << "le"; break;
36  case 3: O << "unord"; break;
37  case 4: O << "neq"; break;
38  case 5: O << "nlt"; break;
39  case 6: O << "nle"; break;
40  case 7: O << "ord"; break;
41  case 8: O << "eq_uq"; break;
42  case 9: O << "nge"; break;
43  case 0xa: O << "ngt"; break;
44  case 0xb: O << "false"; break;
45  case 0xc: O << "neq_oq"; break;
46  case 0xd: O << "ge"; break;
47  case 0xe: O << "gt"; break;
48  case 0xf: O << "true"; break;
49  case 0x10: O << "eq_os"; break;
50  case 0x11: O << "lt_oq"; break;
51  case 0x12: O << "le_oq"; break;
52  case 0x13: O << "unord_s"; break;
53  case 0x14: O << "neq_us"; break;
54  case 0x15: O << "nlt_uq"; break;
55  case 0x16: O << "nle_uq"; break;
56  case 0x17: O << "ord_s"; break;
57  case 0x18: O << "eq_us"; break;
58  case 0x19: O << "nge_uq"; break;
59  case 0x1a: O << "ngt_uq"; break;
60  case 0x1b: O << "false_os"; break;
61  case 0x1c: O << "neq_os"; break;
62  case 0x1d: O << "ge_oq"; break;
63  case 0x1e: O << "gt_oq"; break;
64  case 0x1f: O << "true_us"; break;
65  }
66 }
67 
69  raw_ostream &O) {
70  int64_t Imm = MI->getOperand(Op).getImm();
71  switch (Imm) {
72  default: llvm_unreachable("Invalid xopcc argument!");
73  case 0: O << "lt"; break;
74  case 1: O << "le"; break;
75  case 2: O << "gt"; break;
76  case 3: O << "ge"; break;
77  case 4: O << "eq"; break;
78  case 5: O << "neq"; break;
79  case 6: O << "false"; break;
80  case 7: O << "true"; break;
81  }
82 }
83 
85  raw_ostream &O) {
86  int64_t Imm = MI->getOperand(Op).getImm() & 0x3;
87  switch (Imm) {
88  case 0: O << "{rn-sae}"; break;
89  case 1: O << "{rd-sae}"; break;
90  case 2: O << "{ru-sae}"; break;
91  case 3: O << "{rz-sae}"; break;
92  }
93 }
94 
95 /// printPCRelImm - This is used to print an immediate value that ends up
96 /// being encoded as a pc-relative value (e.g. for jumps and calls). In
97 /// Intel-style these print slightly differently than normal immediates.
98 /// for example, a $ is not emitted.
99 void X86InstPrinterCommon::printPCRelImm(const MCInst *MI, unsigned OpNo,
100  raw_ostream &O) {
101  const MCOperand &Op = MI->getOperand(OpNo);
102  if (Op.isImm())
103  O << formatImm(Op.getImm());
104  else {
105  assert(Op.isExpr() && "unknown pcrel immediate operand");
106  // If a symbolic branch target was added as a constant expression then print
107  // that address in hex.
108  const MCConstantExpr *BranchTarget = dyn_cast<MCConstantExpr>(Op.getExpr());
109  int64_t Address;
110  if (BranchTarget && BranchTarget->evaluateAsAbsolute(Address)) {
111  O << formatHex((uint64_t)Address);
112  } else {
113  // Otherwise, just print the expression.
114  Op.getExpr()->print(O, &MAI);
115  }
116  }
117 }
118 
120  raw_ostream &O) {
121  if (MI->getOperand(OpNo).getReg()) {
122  printOperand(MI, OpNo, O);
123  O << ':';
124  }
125 }
126 
128  const MCInstrDesc &Desc = MII.get(MI->getOpcode());
129  uint64_t TSFlags = Desc.TSFlags;
130  unsigned Flags = MI->getFlags();
131 
132  if ((TSFlags & X86II::LOCK) || (Flags & X86::IP_HAS_LOCK))
133  O << "\tlock\t";
134 
135  if ((TSFlags & X86II::NOTRACK) || (Flags & X86::IP_HAS_NOTRACK))
136  O << "\tnotrack\t";
137 
138  if (Flags & X86::IP_HAS_REPEAT_NE)
139  O << "\trepne\t";
140  else if (Flags & X86::IP_HAS_REPEAT)
141  O << "\trep\t";
142 }
bool isImm() const
Definition: MCInst.h:59
This class represents lattice values for constants.
Definition: AllocatorList.h:24
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:164
unsigned getReg() const
Returns the register number.
Definition: MCInst.h:65
format_object< int64_t > formatImm(int64_t Value) const
Utility function to print immediates in decimal or hex.
Definition: MCInstPrinter.h:96
const MCExpr * getExpr() const
Definition: MCInst.h:96
void printXOPCC(const MCInst *MI, unsigned Op, raw_ostream &OS)
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:161
void printOptionalSegReg(const MCInst *MI, unsigned OpNo, raw_ostream &O)
void printPCRelImm(const MCInst *MI, unsigned OpNo, raw_ostream &O)
printPCRelImm - This is used to print an immediate value that ends up being encoded as a pc-relative ...
int64_t getImm() const
Definition: MCInst.h:76
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:42
virtual void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O)=0
unsigned getFlags() const
Definition: MCInst.h:177
bool isExpr() const
Definition: MCInst.h:61
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void printInstFlags(const MCInst *MI, raw_ostream &O)
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:182
void printRoundingControl(const MCInst *MI, unsigned Op, raw_ostream &O)
const MCAsmInfo & MAI
Definition: MCInstPrinter.h:46
void printSSEAVXCC(const MCInst *MI, unsigned Op, raw_ostream &OS)
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
Definition: MCInstrInfo.h:45
format_object< int64_t > formatHex(int64_t Value) const
const MCInstrInfo & MII
Definition: MCInstPrinter.h:47
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
IRTranslator LLVM IR MI
unsigned getOpcode() const
Definition: MCInst.h:174
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:35