]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonInstPrinter.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r303571, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Target / Hexagon / MCTargetDesc / HexagonInstPrinter.cpp
1 //===- HexagonInstPrinter.cpp - Convert Hexagon MCInst to assembly syntax -===//
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 class prints an Hexagon MCInst to a .s file.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "HexagonAsmPrinter.h"
15 #include "HexagonInstPrinter.h"
16 #include "MCTargetDesc/HexagonBaseInfo.h"
17 #include "MCTargetDesc/HexagonMCInstrInfo.h"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
23
24 using namespace llvm;
25
26 #define DEBUG_TYPE "asm-printer"
27
28 #define GET_INSTRUCTION_NAME
29 #include "HexagonGenAsmWriter.inc"
30
31 HexagonInstPrinter::HexagonInstPrinter(MCAsmInfo const &MAI,
32                                        MCInstrInfo const &MII,
33                                        MCRegisterInfo const &MRI)
34     : MCInstPrinter(MAI, MII, MRI), MII(MII), HasExtender(false) {
35 }
36
37 StringRef HexagonInstPrinter::getOpcodeName(unsigned Opcode) const {
38   return MII.getName(Opcode);
39 }
40
41 void HexagonInstPrinter::printRegName(raw_ostream &O, unsigned RegNo) const {
42   O << getRegName(RegNo);
43 }
44
45 StringRef HexagonInstPrinter::getRegName(unsigned RegNo) const {
46   return getRegisterName(RegNo);
47 }
48
49 void HexagonInstPrinter::setExtender(MCInst const &MCI) {
50   HasExtender = HexagonMCInstrInfo::isImmext(MCI);
51 }
52
53 void HexagonInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
54                                    StringRef Annot, const MCSubtargetInfo &STI) {
55   assert(HexagonMCInstrInfo::isBundle(*MI));
56   assert(HexagonMCInstrInfo::bundleSize(*MI) <= HEXAGON_PACKET_SIZE);
57   assert(HexagonMCInstrInfo::bundleSize(*MI) > 0);
58   HasExtender = false;
59   for (auto const &I : HexagonMCInstrInfo::bundleInstructions(*MI)) {
60     MCInst const &MCI = *I.getInst();
61     if (HexagonMCInstrInfo::isDuplex(MII, MCI)) {
62       printInstruction(MCI.getOperand(1).getInst(), OS);
63       OS << '\v';
64       HasExtender = false;
65       printInstruction(MCI.getOperand(0).getInst(), OS);
66     } else
67       printInstruction(&MCI, OS);
68     setExtender(MCI);
69     OS << "\n";
70   }
71
72   auto Separator = "";
73   if (HexagonMCInstrInfo::isInnerLoop(*MI)) {
74     OS << Separator;
75     Separator = " ";
76     MCInst ME;
77     ME.setOpcode(Hexagon::ENDLOOP0);
78     printInstruction(&ME, OS);
79   }
80   if (HexagonMCInstrInfo::isOuterLoop(*MI)) {
81     OS << Separator;
82     MCInst ME;
83     ME.setOpcode(Hexagon::ENDLOOP1);
84     printInstruction(&ME, OS);
85   }
86 }
87
88 void HexagonInstPrinter::printOperand(MCInst const *MI, unsigned OpNo,
89                                       raw_ostream &O) const {
90   if (HexagonMCInstrInfo::getExtendableOp(MII, *MI) == OpNo &&
91       (HasExtender || HexagonMCInstrInfo::isConstExtended(MII, *MI)))
92     O << "#";
93   MCOperand const &MO = MI->getOperand(OpNo);
94   if (MO.isReg()) {
95     O << getRegisterName(MO.getReg());
96   } else if (MO.isExpr()) {
97     int64_t Value;
98     if (MO.getExpr()->evaluateAsAbsolute(Value))
99       O << formatImm(Value);
100     else
101       O << *MO.getExpr();
102   } else {
103     llvm_unreachable("Unknown operand");
104   }
105 }
106
107 void HexagonInstPrinter::printExtOperand(MCInst const *MI, unsigned OpNo,
108                                          raw_ostream &O) const {
109   printOperand(MI, OpNo, O);
110 }
111
112 void HexagonInstPrinter::printUnsignedImmOperand(MCInst const *MI,
113                                                  unsigned OpNo,
114                                                  raw_ostream &O) const {
115   O << MI->getOperand(OpNo).getImm();
116 }
117
118 void HexagonInstPrinter::printNegImmOperand(MCInst const *MI, unsigned OpNo,
119                                             raw_ostream &O) const {
120   O << -MI->getOperand(OpNo).getImm();
121 }
122
123 void HexagonInstPrinter::printNOneImmOperand(MCInst const *MI, unsigned OpNo,
124                                              raw_ostream &O) const {
125   O << -1;
126 }
127
128 void HexagonInstPrinter::printGlobalOperand(MCInst const *MI, unsigned OpNo,
129                                             raw_ostream &O) const {
130   printOperand(MI, OpNo, O);
131 }
132
133 void HexagonInstPrinter::printJumpTable(MCInst const *MI, unsigned OpNo,
134                                         raw_ostream &O) const {
135   assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
136
137   printOperand(MI, OpNo, O);
138 }
139
140 void HexagonInstPrinter::printConstantPool(MCInst const *MI, unsigned OpNo,
141                                            raw_ostream &O) const {
142   assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
143
144   printOperand(MI, OpNo, O);
145 }
146
147 void HexagonInstPrinter::printBranchOperand(MCInst const *MI, unsigned OpNo,
148                                             raw_ostream &O) const {
149   // Branches can take an immediate operand.  This is used by the branch
150   // selection pass to print $+8, an eight byte displacement from the PC.
151   llvm_unreachable("Unknown branch operand.");
152 }
153
154 void HexagonInstPrinter::printCallOperand(MCInst const *MI, unsigned OpNo,
155                                           raw_ostream &O) const {}
156
157 void HexagonInstPrinter::printAbsAddrOperand(MCInst const *MI, unsigned OpNo,
158                                              raw_ostream &O) const {}
159
160 void HexagonInstPrinter::printPredicateOperand(MCInst const *MI, unsigned OpNo,
161                                                raw_ostream &O) const {}
162
163 void HexagonInstPrinter::printSymbol(MCInst const *MI, unsigned OpNo,
164                                      raw_ostream &O, bool hi) const {
165   assert(MI->getOperand(OpNo).isImm() && "Unknown symbol operand");
166
167   O << '#' << (hi ? "HI" : "LO") << '(';
168   O << '#';
169   printOperand(MI, OpNo, O);
170   O << ')';
171 }
172
173 void HexagonInstPrinter::printBrtarget(MCInst const *MI, unsigned OpNo,
174                                        raw_ostream &O) const {
175   MCOperand const &MO = MI->getOperand(OpNo);
176   assert (MO.isExpr());
177   MCExpr const &Expr = *MO.getExpr();
178   int64_t Value;
179   if (Expr.evaluateAsAbsolute(Value))
180     O << format("0x%" PRIx64, Value);
181   else {
182     if (HasExtender || HexagonMCInstrInfo::isConstExtended(MII, *MI))
183       if (HexagonMCInstrInfo::getExtendableOp(MII, *MI) == OpNo)
184         O << "##";
185     O << Expr;
186   }
187 }