LLVM  4.0.0
HexagonInstPrinter.cpp
Go to the documentation of this file.
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"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/Support/Debug.h"
23 
24 using namespace llvm;
25 
26 #define DEBUG_TYPE "asm-printer"
27 
28 #define GET_INSTRUCTION_NAME
29 #include "HexagonGenAsmWriter.inc"
30 
32  MCInstrInfo const &MII,
33  MCRegisterInfo const &MRI)
34  : MCInstPrinter(MAI, MII, MRI), MII(MII), HasExtender(false) {
35 }
36 
38  return MII.getName(Opcode);
39 }
40 
41 void HexagonInstPrinter::printRegName(raw_ostream &O, unsigned RegNo) const {
42  O << getRegName(RegNo);
43 }
44 
46  return getRegisterName(RegNo);
47 }
48 
49 void HexagonInstPrinter::setExtender(MCInst const &MCI) {
50  HasExtender = HexagonMCInstrInfo::isImmext(MCI);
51 }
52 
54  StringRef Annot, const MCSubtargetInfo &STI) {
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 = "";
74  OS << Separator;
75  Separator = " ";
76  MCInst ME;
77  ME.setOpcode(Hexagon::ENDLOOP0);
78  printInstruction(&ME, OS);
79  }
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 
113  unsigned OpNo,
114  raw_ostream &O) const {
115  O << MI->getOperand(OpNo).getImm();
116 }
117 
119  raw_ostream &O) const {
120  O << -MI->getOperand(OpNo).getImm();
121 }
122 
124  raw_ostream &O) const {
125  O << -1;
126 }
127 
129  raw_ostream &O) const {
130  int64_t Imm;
131  bool Success = MI->getOperand(OpNo).getExpr()->evaluateAsAbsolute(Imm);
132  Imm = SignExtend64<9>(Imm);
133  assert(Success); (void)Success;
134  assert(((Imm & 0x3f) == 0) && "Lower 6 bits must be ZERO.");
135  O << formatImm(Imm/64);
136 }
137 
139  raw_ostream &O) const {
140  int64_t Imm;
141  bool Success = MI->getOperand(OpNo).getExpr()->evaluateAsAbsolute(Imm);
142  Imm = SignExtend64<10>(Imm);
143  assert(Success); (void)Success;
144  assert(((Imm & 0x7f) == 0) && "Lower 7 bits must be ZERO.");
145  O << formatImm(Imm/128);
146 }
147 
149  raw_ostream &O) const {
150  int64_t Imm;
151  bool Success = MI->getOperand(OpNo).getExpr()->evaluateAsAbsolute(Imm);
152  Imm = SignExtend64<10>(Imm);
153  assert(Success); (void)Success;
154  assert(((Imm & 0x3f) == 0) && "Lower 6 bits must be ZERO.");
155  O << formatImm(Imm/64);
156 }
157 
159  raw_ostream &O) const {
160  int64_t Imm;
161  bool Success = MI->getOperand(OpNo).getExpr()->evaluateAsAbsolute(Imm);
162  Imm = SignExtend64<11>(Imm);
163  assert(Success); (void)Success;
164  assert(((Imm & 0x7f) == 0) && "Lower 7 bits must be ZERO.");
165  O << formatImm(Imm/128);
166 }
167 
169  raw_ostream &O) const {
170  printOperand(MI, OpNo, O);
171 }
172 
173 void HexagonInstPrinter::printJumpTable(MCInst const *MI, unsigned OpNo,
174  raw_ostream &O) const {
175  assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
176 
177  printOperand(MI, OpNo, O);
178 }
179 
181  raw_ostream &O) const {
182  assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
183 
184  printOperand(MI, OpNo, O);
185 }
186 
188  raw_ostream &O) const {
189  // Branches can take an immediate operand. This is used by the branch
190  // selection pass to print $+8, an eight byte displacement from the PC.
191  llvm_unreachable("Unknown branch operand.");
192 }
193 
194 void HexagonInstPrinter::printCallOperand(MCInst const *MI, unsigned OpNo,
195  raw_ostream &O) const {}
196 
198  raw_ostream &O) const {}
199 
201  raw_ostream &O) const {}
202 
203 void HexagonInstPrinter::printSymbol(MCInst const *MI, unsigned OpNo,
204  raw_ostream &O, bool hi) const {
205  assert(MI->getOperand(OpNo).isImm() && "Unknown symbol operand");
206 
207  O << '#' << (hi ? "HI" : "LO") << '(';
208  O << '#';
209  printOperand(MI, OpNo, O);
210  O << ')';
211 }
212 
213 void HexagonInstPrinter::printBrtarget(MCInst const *MI, unsigned OpNo,
214  raw_ostream &O) const {
215  MCOperand const &MO = MI->getOperand(OpNo);
216  assert (MO.isExpr());
217  MCExpr const &Expr = *MO.getExpr();
218  int64_t Value;
219  if (Expr.evaluateAsAbsolute(Value))
220  O << format("0x%" PRIx64, Value);
221  else {
222  if (HasExtender || HexagonMCInstrInfo::isConstExtended(MII, *MI))
223  if (HexagonMCInstrInfo::getExtendableOp(MII, *MI) == OpNo)
224  O << "##";
225  O << Expr;
226  }
227 }
bool isDuplex(MCInstrInfo const &MCII, MCInst const &MCI)
void printRegName(raw_ostream &O, unsigned RegNo) const override
Print the assembler register name.
void printSymbol(MCInst const *MI, unsigned OpNo, raw_ostream &O, bool hi) const
bool isReg() const
Definition: MCInst.h:56
bool isBundle(MCInst const &MCI)
void printBrtarget(MCInst const *MI, unsigned OpNo, raw_ostream &O) const
bool isOuterLoop(MCInst const &MCI)
void printInst(MCInst const *MI, raw_ostream &O, StringRef Annot, const MCSubtargetInfo &STI) override
Print the specified MCInst to the specified raw_ostream.
bool isImmext(MCInst const &MCI)
void prints3_6ImmOperand(MCInst const *MI, unsigned OpNo, raw_ostream &O) const
#define HEXAGON_PACKET_SIZE
Definition: Hexagon.h:33
StringRef getName(unsigned Opcode) const
Returns the name for the instructions with the given opcode.
Definition: MCInstrInfo.h:51
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
void printConstantPool(MCInst const *MI, unsigned OpNo, raw_ostream &O) const
void printExtOperand(MCInst const *MI, unsigned OpNo, raw_ostream &O) const
unsigned getReg() const
Returns the register number.
Definition: MCInst.h:63
Function Alias Analysis false
void prints3_7ImmOperand(MCInst const *MI, unsigned OpNo, raw_ostream &O) const
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:150
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
format_object< Ts...> format(const char *Fmt, const Ts &...Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
bool isImm() const
Definition: MCInst.h:57
void prints4_7ImmOperand(MCInst const *MI, unsigned OpNo, raw_ostream &O) const
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:57
void prints4_6ImmOperand(MCInst const *MI, unsigned OpNo, raw_ostream &O) const
const MCExpr * getExpr() const
Definition: MCInst.h:93
unsigned const MachineRegisterInfo * MRI
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:24
bool isExpr() const
Definition: MCInst.h:59
void printPredicateOperand(MCInst const *MI, unsigned OpNo, raw_ostream &O) const
void printBranchOperand(MCInst const *MI, unsigned OpNo, raw_ostream &O) const
void printGlobalOperand(MCInst const *MI, unsigned OpNo, raw_ostream &O) const
void printAbsAddrOperand(MCInst const *MI, unsigned OpNo, raw_ostream &O) const
iterator_range< MCInst::const_iterator > bundleInstructions(MCInst const &MCI)
void printOperand(MCInst const *MI, unsigned OpNo, raw_ostream &O) const
format_object< int64_t > formatImm(int64_t Value) const
Utility function to print immediates in decimal or hex.
Definition: MCInstPrinter.h:99
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned short getExtendableOp(MCInstrInfo const &MCII, MCInst const &MCI)
StringRef getRegName(unsigned RegNo) const
void setOpcode(unsigned Op)
Definition: MCInst.h:158
static const char * Separator
void printInstruction(MCInst const *MI, raw_ostream &O)
int64_t getImm() const
Definition: MCInst.h:74
#define Success
void printUnsignedImmOperand(MCInst const *MI, unsigned OpNo, raw_ostream &O) const
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:41
void printNOneImmOperand(MCInst const *MI, unsigned OpNo, raw_ostream &O) const
HexagonInstPrinter(MCAsmInfo const &MAI, MCInstrInfo const &MII, MCRegisterInfo const &MRI)
#define I(x, y, z)
Definition: MD5.cpp:54
MCSubtargetInfo - Generic base class for all target subtargets.
const MCInst * getInst() const
Definition: MCInst.h:102
void printNegImmOperand(MCInst const *MI, unsigned OpNo, raw_ostream &O) const
size_t bundleSize(MCInst const &MCI)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
virtual StringRef getOpcodeName(unsigned Opcode) const
bool isInnerLoop(MCInst const &MCI)
LLVM Value Representation.
Definition: Value.h:71
static char const * getRegisterName(unsigned RegNo)
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
bool isConstExtended(MCInstrInfo const &MCII, MCInst const &MCI)
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
void printJumpTable(MCInst const *MI, unsigned OpNo, raw_ostream &O) const
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:33
void printCallOperand(MCInst const *MI, unsigned OpNo, raw_ostream &O) const
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:164