1*4ba319b5SDimitry Andric //===--- X86InstPrinterCommon.cpp - X86 assembly instruction printing -----===//
2*4ba319b5SDimitry Andric //
3*4ba319b5SDimitry Andric //                     The LLVM Compiler Infrastructure
4*4ba319b5SDimitry Andric //
5*4ba319b5SDimitry Andric // This file is distributed under the University of Illinois Open Source
6*4ba319b5SDimitry Andric // License. See LICENSE.TXT for details.
7*4ba319b5SDimitry Andric //
8*4ba319b5SDimitry Andric //===----------------------------------------------------------------------===//
9*4ba319b5SDimitry Andric //
10*4ba319b5SDimitry Andric // This file includes common code for rendering MCInst instances as Intel-style
11*4ba319b5SDimitry Andric // and Intel-style assembly.
12*4ba319b5SDimitry Andric //
13*4ba319b5SDimitry Andric //===----------------------------------------------------------------------===//
14*4ba319b5SDimitry Andric 
15*4ba319b5SDimitry Andric #include "X86InstPrinterCommon.h"
16*4ba319b5SDimitry Andric #include "MCTargetDesc/X86BaseInfo.h"
17*4ba319b5SDimitry Andric #include "llvm/MC/MCExpr.h"
18*4ba319b5SDimitry Andric #include "llvm/MC/MCInst.h"
19*4ba319b5SDimitry Andric #include "llvm/MC/MCInstrDesc.h"
20*4ba319b5SDimitry Andric #include "llvm/MC/MCInstrInfo.h"
21*4ba319b5SDimitry Andric #include "llvm/Support/raw_ostream.h"
22*4ba319b5SDimitry Andric #include "llvm/Support/Casting.h"
23*4ba319b5SDimitry Andric #include <cstdint>
24*4ba319b5SDimitry Andric #include <cassert>
25*4ba319b5SDimitry Andric 
26*4ba319b5SDimitry Andric using namespace llvm;
27*4ba319b5SDimitry Andric 
printSSEAVXCC(const MCInst * MI,unsigned Op,raw_ostream & O)28*4ba319b5SDimitry Andric void X86InstPrinterCommon::printSSEAVXCC(const MCInst *MI, unsigned Op,
29*4ba319b5SDimitry Andric                                          raw_ostream &O) {
30*4ba319b5SDimitry Andric   int64_t Imm = MI->getOperand(Op).getImm();
31*4ba319b5SDimitry Andric   switch (Imm) {
32*4ba319b5SDimitry Andric   default: llvm_unreachable("Invalid ssecc/avxcc argument!");
33*4ba319b5SDimitry Andric   case    0: O << "eq"; break;
34*4ba319b5SDimitry Andric   case    1: O << "lt"; break;
35*4ba319b5SDimitry Andric   case    2: O << "le"; break;
36*4ba319b5SDimitry Andric   case    3: O << "unord"; break;
37*4ba319b5SDimitry Andric   case    4: O << "neq"; break;
38*4ba319b5SDimitry Andric   case    5: O << "nlt"; break;
39*4ba319b5SDimitry Andric   case    6: O << "nle"; break;
40*4ba319b5SDimitry Andric   case    7: O << "ord"; break;
41*4ba319b5SDimitry Andric   case    8: O << "eq_uq"; break;
42*4ba319b5SDimitry Andric   case    9: O << "nge"; break;
43*4ba319b5SDimitry Andric   case  0xa: O << "ngt"; break;
44*4ba319b5SDimitry Andric   case  0xb: O << "false"; break;
45*4ba319b5SDimitry Andric   case  0xc: O << "neq_oq"; break;
46*4ba319b5SDimitry Andric   case  0xd: O << "ge"; break;
47*4ba319b5SDimitry Andric   case  0xe: O << "gt"; break;
48*4ba319b5SDimitry Andric   case  0xf: O << "true"; break;
49*4ba319b5SDimitry Andric   case 0x10: O << "eq_os"; break;
50*4ba319b5SDimitry Andric   case 0x11: O << "lt_oq"; break;
51*4ba319b5SDimitry Andric   case 0x12: O << "le_oq"; break;
52*4ba319b5SDimitry Andric   case 0x13: O << "unord_s"; break;
53*4ba319b5SDimitry Andric   case 0x14: O << "neq_us"; break;
54*4ba319b5SDimitry Andric   case 0x15: O << "nlt_uq"; break;
55*4ba319b5SDimitry Andric   case 0x16: O << "nle_uq"; break;
56*4ba319b5SDimitry Andric   case 0x17: O << "ord_s"; break;
57*4ba319b5SDimitry Andric   case 0x18: O << "eq_us"; break;
58*4ba319b5SDimitry Andric   case 0x19: O << "nge_uq"; break;
59*4ba319b5SDimitry Andric   case 0x1a: O << "ngt_uq"; break;
60*4ba319b5SDimitry Andric   case 0x1b: O << "false_os"; break;
61*4ba319b5SDimitry Andric   case 0x1c: O << "neq_os"; break;
62*4ba319b5SDimitry Andric   case 0x1d: O << "ge_oq"; break;
63*4ba319b5SDimitry Andric   case 0x1e: O << "gt_oq"; break;
64*4ba319b5SDimitry Andric   case 0x1f: O << "true_us"; break;
65*4ba319b5SDimitry Andric   }
66*4ba319b5SDimitry Andric }
67*4ba319b5SDimitry Andric 
printXOPCC(const MCInst * MI,unsigned Op,raw_ostream & O)68*4ba319b5SDimitry Andric void X86InstPrinterCommon::printXOPCC(const MCInst *MI, unsigned Op,
69*4ba319b5SDimitry Andric                                       raw_ostream &O) {
70*4ba319b5SDimitry Andric   int64_t Imm = MI->getOperand(Op).getImm();
71*4ba319b5SDimitry Andric   switch (Imm) {
72*4ba319b5SDimitry Andric   default: llvm_unreachable("Invalid xopcc argument!");
73*4ba319b5SDimitry Andric   case 0: O << "lt"; break;
74*4ba319b5SDimitry Andric   case 1: O << "le"; break;
75*4ba319b5SDimitry Andric   case 2: O << "gt"; break;
76*4ba319b5SDimitry Andric   case 3: O << "ge"; break;
77*4ba319b5SDimitry Andric   case 4: O << "eq"; break;
78*4ba319b5SDimitry Andric   case 5: O << "neq"; break;
79*4ba319b5SDimitry Andric   case 6: O << "false"; break;
80*4ba319b5SDimitry Andric   case 7: O << "true"; break;
81*4ba319b5SDimitry Andric   }
82*4ba319b5SDimitry Andric }
83*4ba319b5SDimitry Andric 
printRoundingControl(const MCInst * MI,unsigned Op,raw_ostream & O)84*4ba319b5SDimitry Andric void X86InstPrinterCommon::printRoundingControl(const MCInst *MI, unsigned Op,
85*4ba319b5SDimitry Andric                                                 raw_ostream &O) {
86*4ba319b5SDimitry Andric   int64_t Imm = MI->getOperand(Op).getImm() & 0x3;
87*4ba319b5SDimitry Andric   switch (Imm) {
88*4ba319b5SDimitry Andric   case 0: O << "{rn-sae}"; break;
89*4ba319b5SDimitry Andric   case 1: O << "{rd-sae}"; break;
90*4ba319b5SDimitry Andric   case 2: O << "{ru-sae}"; break;
91*4ba319b5SDimitry Andric   case 3: O << "{rz-sae}"; break;
92*4ba319b5SDimitry Andric   }
93*4ba319b5SDimitry Andric }
94*4ba319b5SDimitry Andric 
95*4ba319b5SDimitry Andric /// printPCRelImm - This is used to print an immediate value that ends up
96*4ba319b5SDimitry Andric /// being encoded as a pc-relative value (e.g. for jumps and calls).  In
97*4ba319b5SDimitry Andric /// Intel-style these print slightly differently than normal immediates.
98*4ba319b5SDimitry Andric /// for example, a $ is not emitted.
printPCRelImm(const MCInst * MI,unsigned OpNo,raw_ostream & O)99*4ba319b5SDimitry Andric void X86InstPrinterCommon::printPCRelImm(const MCInst *MI, unsigned OpNo,
100*4ba319b5SDimitry Andric                                          raw_ostream &O) {
101*4ba319b5SDimitry Andric   const MCOperand &Op = MI->getOperand(OpNo);
102*4ba319b5SDimitry Andric   if (Op.isImm())
103*4ba319b5SDimitry Andric     O << formatImm(Op.getImm());
104*4ba319b5SDimitry Andric   else {
105*4ba319b5SDimitry Andric     assert(Op.isExpr() && "unknown pcrel immediate operand");
106*4ba319b5SDimitry Andric     // If a symbolic branch target was added as a constant expression then print
107*4ba319b5SDimitry Andric     // that address in hex.
108*4ba319b5SDimitry Andric     const MCConstantExpr *BranchTarget = dyn_cast<MCConstantExpr>(Op.getExpr());
109*4ba319b5SDimitry Andric     int64_t Address;
110*4ba319b5SDimitry Andric     if (BranchTarget && BranchTarget->evaluateAsAbsolute(Address)) {
111*4ba319b5SDimitry Andric       O << formatHex((uint64_t)Address);
112*4ba319b5SDimitry Andric     } else {
113*4ba319b5SDimitry Andric       // Otherwise, just print the expression.
114*4ba319b5SDimitry Andric       Op.getExpr()->print(O, &MAI);
115*4ba319b5SDimitry Andric     }
116*4ba319b5SDimitry Andric   }
117*4ba319b5SDimitry Andric }
118*4ba319b5SDimitry Andric 
printOptionalSegReg(const MCInst * MI,unsigned OpNo,raw_ostream & O)119*4ba319b5SDimitry Andric void X86InstPrinterCommon::printOptionalSegReg(const MCInst *MI, unsigned OpNo,
120*4ba319b5SDimitry Andric                                                raw_ostream &O) {
121*4ba319b5SDimitry Andric   if (MI->getOperand(OpNo).getReg()) {
122*4ba319b5SDimitry Andric     printOperand(MI, OpNo, O);
123*4ba319b5SDimitry Andric     O << ':';
124*4ba319b5SDimitry Andric   }
125*4ba319b5SDimitry Andric }
126*4ba319b5SDimitry Andric 
printInstFlags(const MCInst * MI,raw_ostream & O)127*4ba319b5SDimitry Andric void X86InstPrinterCommon::printInstFlags(const MCInst *MI, raw_ostream &O) {
128*4ba319b5SDimitry Andric   const MCInstrDesc &Desc = MII.get(MI->getOpcode());
129*4ba319b5SDimitry Andric   uint64_t TSFlags = Desc.TSFlags;
130*4ba319b5SDimitry Andric   unsigned Flags = MI->getFlags();
131*4ba319b5SDimitry Andric 
132*4ba319b5SDimitry Andric   if ((TSFlags & X86II::LOCK) || (Flags & X86::IP_HAS_LOCK))
133*4ba319b5SDimitry Andric     O << "\tlock\t";
134*4ba319b5SDimitry Andric 
135*4ba319b5SDimitry Andric   if ((TSFlags & X86II::NOTRACK) || (Flags & X86::IP_HAS_NOTRACK))
136*4ba319b5SDimitry Andric     O << "\tnotrack\t";
137*4ba319b5SDimitry Andric 
138*4ba319b5SDimitry Andric   if (Flags & X86::IP_HAS_REPEAT_NE)
139*4ba319b5SDimitry Andric     O << "\trepne\t";
140*4ba319b5SDimitry Andric   else if (Flags & X86::IP_HAS_REPEAT)
141*4ba319b5SDimitry Andric     O << "\trep\t";
142*4ba319b5SDimitry Andric }
143