LLVM  10.0.0
MCInstPrinter.cpp
Go to the documentation of this file.
1 //===- MCInstPrinter.cpp - Convert an MCInst to target assembly syntax ----===//
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 
10 #include "llvm/ADT/ArrayRef.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCInst.h"
14 #include "llvm/MC/MCInstrInfo.h"
17 #include "llvm/Support/Format.h"
19 #include <cinttypes>
20 #include <cstdint>
21 
22 using namespace llvm;
23 
25  static const char hex_rep[] = "0123456789abcdef";
26  bool First = true;
27  for (char i: bytes) {
28  if (First)
29  First = false;
30  else
31  OS << ' ';
32  OS << hex_rep[(i & 0xF0) >> 4];
33  OS << hex_rep[i & 0xF];
34  }
35 }
36 
38 
39 /// getOpcodeName - Return the name of the specified opcode enum (e.g.
40 /// "MOV32ri") or empty if we can't resolve it.
41 StringRef MCInstPrinter::getOpcodeName(unsigned Opcode) const {
42  return MII.getName(Opcode);
43 }
44 
45 void MCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
46  llvm_unreachable("Target should implement this");
47 }
48 
50  if (!Annot.empty()) {
51  if (CommentStream) {
52  (*CommentStream) << Annot;
53  // By definition (see MCInstPrinter.h), CommentStream must end with
54  // a newline after each comment.
55  if (Annot.back() != '\n')
56  (*CommentStream) << '\n';
57  } else
58  OS << " " << MAI.getCommentString() << " " << Annot;
59  }
60 }
61 
62 static bool matchAliasCondition(const MCInst &MI, const MCSubtargetInfo *STI,
63  const MCRegisterInfo &MRI, unsigned &OpIdx,
64  const AliasMatchingData &M,
65  const AliasPatternCond &C) {
66  // Feature tests are special, they don't consume operands.
68  return STI->getFeatureBits().test(C.Value);
70  return !STI->getFeatureBits().test(C.Value);
71 
72  // Get and consume an operand.
73  const MCOperand &Opnd = MI.getOperand(OpIdx);
74  ++OpIdx;
75 
76  // Check the specific condition for the operand.
77  switch (C.Kind) {
79  // Operand must be a specific immediate.
80  return Opnd.isImm() && Opnd.getImm() == int32_t(C.Value);
82  // Operand must be a specific register.
83  return Opnd.isReg() && Opnd.getReg() == C.Value;
85  // Operand must match the register of another operand.
86  return Opnd.isReg() && Opnd.getReg() == MI.getOperand(C.Value).getReg();
88  // Operand must be a register in this class. Value is a register class id.
89  return Opnd.isReg() && MRI.getRegClass(C.Value).contains(Opnd.getReg());
91  // Operand must match some custom criteria.
92  return M.ValidateMCOperand(Opnd, *STI, C.Value);
94  // Operand can be anything.
95  return true;
98  llvm_unreachable("handled earlier");
99  }
100  llvm_unreachable("invalid kind");
101 }
102 
104  const MCSubtargetInfo *STI,
105  const AliasMatchingData &M) {
106  // Binary search by opcode. Return false if there are no aliases for this
107  // opcode.
108  auto It = lower_bound(M.OpToPatterns, MI->getOpcode(),
109  [](const PatternsForOpcode &L, unsigned Opcode) {
110  return L.Opcode < Opcode;
111  });
112  if (It == M.OpToPatterns.end() || It->Opcode != MI->getOpcode())
113  return nullptr;
114 
115  // Try all patterns for this opcode.
116  uint32_t AsmStrOffset = ~0U;
117  ArrayRef<AliasPattern> Patterns =
118  M.Patterns.slice(It->PatternStart, It->NumPatterns);
119  for (const AliasPattern &P : Patterns) {
120  // Check operand count first.
121  if (MI->getNumOperands() != P.NumOperands)
122  return nullptr;
123 
124  // Test all conditions for this pattern.
126  M.PatternConds.slice(P.AliasCondStart, P.NumConds);
127  unsigned OpIdx = 0;
128  if (llvm::all_of(Conds, [&](const AliasPatternCond &C) {
129  return matchAliasCondition(*MI, STI, MRI, OpIdx, M, C);
130  })) {
131  // If all conditions matched, use this asm string.
132  AsmStrOffset = P.AsmStrOffset;
133  break;
134  }
135  }
136 
137  // If no alias matched, don't print an alias.
138  if (AsmStrOffset == ~0U)
139  return nullptr;
140 
141  // Go to offset AsmStrOffset and use the null terminated string there. The
142  // offset should point to the beginning of an alias string, so it should
143  // either be zero or be preceded by a null byte.
144  assert(AsmStrOffset < M.AsmStrings.size() &&
145  (AsmStrOffset == 0 || M.AsmStrings[AsmStrOffset - 1] == '\0') &&
146  "bad asm string offset");
147  return M.AsmStrings.data() + AsmStrOffset;
148 }
149 
150 /// Utility functions to make adding mark ups simpler.
152  if (getUseMarkup())
153  return s;
154  else
155  return "";
156 }
157 
158 // For asm-style hex (e.g. 0ffh) the first digit always has to be a number.
159 static bool needsLeadingZero(uint64_t Value)
160 {
161  while (Value)
162  {
163  uint64_t digit = (Value >> 60) & 0xf;
164  if (digit != 0)
165  return (digit >= 0xa);
166  Value <<= 4;
167  }
168  return false;
169 }
170 
172  return format("%" PRId64, Value);
173 }
174 
176  switch (PrintHexStyle) {
177  case HexStyle::C:
178  if (Value < 0) {
179  if (Value == std::numeric_limits<int64_t>::min())
180  return format<int64_t>("-0x8000000000000000", Value);
181  return format("-0x%" PRIx64, -Value);
182  }
183  return format("0x%" PRIx64, Value);
184  case HexStyle::Asm:
185  if (Value < 0) {
186  if (Value == std::numeric_limits<int64_t>::min())
187  return format<int64_t>("-8000000000000000h", Value);
188  if (needsLeadingZero(-(uint64_t)(Value)))
189  return format("-0%" PRIx64 "h", -Value);
190  return format("-%" PRIx64 "h", -Value);
191  }
192  if (needsLeadingZero((uint64_t)(Value)))
193  return format("0%" PRIx64 "h", Value);
194  return format("%" PRIx64 "h", Value);
195  }
196  llvm_unreachable("unsupported print style");
197 }
198 
200  switch(PrintHexStyle) {
201  case HexStyle::C:
202  return format("0x%" PRIx64, Value);
203  case HexStyle::Asm:
204  if (needsLeadingZero(Value))
205  return format("0%" PRIx64 "h", Value);
206  else
207  return format("%" PRIx64 "h", Value);
208  }
209  llvm_unreachable("unsupported print style");
210 }
auto lower_bound(R &&Range, T &&Value) -> decltype(adl_begin(Range))
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1278
Data for each alias pattern.
uint64_t CallInst * C
bool isImm() const
Definition: MCInst.h:58
HexStyle::Style PrintHexStyle
Which style to use for printing hexadecimal values.
Definition: MCInstPrinter.h:59
static bool needsLeadingZero(uint64_t Value)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
ArrayRef< AliasPattern > Patterns
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
bool isReg() const
Definition: MCInst.h:57
virtual void printRegName(raw_ostream &OS, unsigned RegNo) const
Print the assembler register name.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1182
const FeatureBitset & getFeatureBits() const
Map from opcode to pattern list by binary search.
static bool matchAliasCondition(const MCInst &MI, const MCSubtargetInfo *STI, const MCRegisterInfo &MRI, unsigned &OpIdx, const AliasMatchingData &M, const AliasPatternCond &C)
format_object< int64_t > formatDec(int64_t Value) const
Utility functions to print decimal/hexadecimal values.
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:147
unsigned getReg() const
Returns the register number.
Definition: MCInst.h:64
constexpr bool test(unsigned I) const
bool contains(MCRegister Reg) const
contains - Return true if the specified register is included in this register class.
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:151
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
int64_t getImm() const
Definition: MCInst.h:75
#define P(N)
ArrayRef< PatternsForOpcode > OpToPatterns
StringRef markup(StringRef s) const
Utility functions to make adding mark ups simpler.
virtual ~MCInstPrinter()
Tablegenerated data structures needed to match alias patterns.
StringRef getCommentString() const
Definition: MCAsmInfo.h:510
unsigned getNumOperands() const
Definition: MCInst.h:182
StringRef getName(unsigned Opcode) const
Returns the name for the instructions with the given opcode.
Definition: MCInstrInfo.h:50
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
ArrayRef< AliasPatternCond > PatternConds
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:180
raw_ostream * CommentStream
A stream that comments can be emitted to if desired.
Definition: MCInstPrinter.h:47
const char * matchAliasPatterns(const MCInst *MI, const MCSubtargetInfo *STI, const AliasMatchingData &M)
Helper for matching MCInsts to alias patterns when printing instructions.
const MCAsmInfo & MAI
Definition: MCInstPrinter.h:48
bool getUseMarkup() const
Definition: MCInstPrinter.h:92
StringRef getOpcodeName(unsigned Opcode) const
Return the name of the specified opcode enum (e.g.
format_object< int64_t > formatHex(int64_t Value) const
bool(* ValidateMCOperand)(const MCOperand &MCOp, const MCSubtargetInfo &STI, unsigned PredicateIndex)
Generic base class for all target subtargets.
const MCInstrInfo & MII
Definition: MCInstPrinter.h:49
void dumpBytes(ArrayRef< uint8_t > Bytes, raw_ostream &OS)
Convert `Bytes&#39; to a hex string and output to `OS&#39;.
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:143
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:74
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
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
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
unsigned getOpcode() const
Definition: MCInst.h:172
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:34
const MCRegisterClass & getRegClass(unsigned i) const
Returns the register class associated with the enumeration value.
const MCRegisterInfo & MRI
Definition: MCInstPrinter.h:50