[ORC] Add std::tuple support to SimplePackedSerialization.
[llvm-project.git] / llvm / lib / MC / MCInstPrinter.cpp
blob7ce92b968f472e2979e8d932120fb866b85be1dd
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 //===----------------------------------------------------------------------===//
9 #include "llvm/MC/MCInstPrinter.h"
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"
15 #include "llvm/MC/MCSubtargetInfo.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/Format.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include <cinttypes>
20 #include <cstdint>
22 using namespace llvm;
24 void llvm::dumpBytes(ArrayRef<uint8_t> bytes, raw_ostream &OS) {
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];
37 MCInstPrinter::~MCInstPrinter() = default;
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);
45 void MCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
46 llvm_unreachable("Target should implement this");
49 void MCInstPrinter::printAnnotation(raw_ostream &OS, StringRef Annot) {
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;
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 bool &OrPredicateResult) {
67 // Feature tests are special, they don't consume operands.
68 if (C.Kind == AliasPatternCond::K_Feature)
69 return STI->getFeatureBits().test(C.Value);
70 if (C.Kind == AliasPatternCond::K_NegFeature)
71 return !STI->getFeatureBits().test(C.Value);
72 // For feature tests where just one feature is required in a list, set the
73 // predicate result bit to whether the expression will return true, and only
74 // return the real result at the end of list marker.
75 if (C.Kind == AliasPatternCond::K_OrFeature) {
76 OrPredicateResult |= STI->getFeatureBits().test(C.Value);
77 return true;
79 if (C.Kind == AliasPatternCond::K_OrNegFeature) {
80 OrPredicateResult |= !(STI->getFeatureBits().test(C.Value));
81 return true;
83 if (C.Kind == AliasPatternCond::K_EndOrFeatures) {
84 bool Res = OrPredicateResult;
85 OrPredicateResult = false;
86 return Res;
89 // Get and consume an operand.
90 const MCOperand &Opnd = MI.getOperand(OpIdx);
91 ++OpIdx;
93 // Check the specific condition for the operand.
94 switch (C.Kind) {
95 case AliasPatternCond::K_Imm:
96 // Operand must be a specific immediate.
97 return Opnd.isImm() && Opnd.getImm() == int32_t(C.Value);
98 case AliasPatternCond::K_Reg:
99 // Operand must be a specific register.
100 return Opnd.isReg() && Opnd.getReg() == C.Value;
101 case AliasPatternCond::K_TiedReg:
102 // Operand must match the register of another operand.
103 return Opnd.isReg() && Opnd.getReg() == MI.getOperand(C.Value).getReg();
104 case AliasPatternCond::K_RegClass:
105 // Operand must be a register in this class. Value is a register class id.
106 return Opnd.isReg() && MRI.getRegClass(C.Value).contains(Opnd.getReg());
107 case AliasPatternCond::K_Custom:
108 // Operand must match some custom criteria.
109 return M.ValidateMCOperand(Opnd, *STI, C.Value);
110 case AliasPatternCond::K_Ignore:
111 // Operand can be anything.
112 return true;
113 case AliasPatternCond::K_Feature:
114 case AliasPatternCond::K_NegFeature:
115 case AliasPatternCond::K_OrFeature:
116 case AliasPatternCond::K_OrNegFeature:
117 case AliasPatternCond::K_EndOrFeatures:
118 llvm_unreachable("handled earlier");
120 llvm_unreachable("invalid kind");
123 const char *MCInstPrinter::matchAliasPatterns(const MCInst *MI,
124 const MCSubtargetInfo *STI,
125 const AliasMatchingData &M) {
126 // Binary search by opcode. Return false if there are no aliases for this
127 // opcode.
128 auto It = lower_bound(M.OpToPatterns, MI->getOpcode(),
129 [](const PatternsForOpcode &L, unsigned Opcode) {
130 return L.Opcode < Opcode;
132 if (It == M.OpToPatterns.end() || It->Opcode != MI->getOpcode())
133 return nullptr;
135 // Try all patterns for this opcode.
136 uint32_t AsmStrOffset = ~0U;
137 ArrayRef<AliasPattern> Patterns =
138 M.Patterns.slice(It->PatternStart, It->NumPatterns);
139 for (const AliasPattern &P : Patterns) {
140 // Check operand count first.
141 if (MI->getNumOperands() != P.NumOperands)
142 return nullptr;
144 // Test all conditions for this pattern.
145 ArrayRef<AliasPatternCond> Conds =
146 M.PatternConds.slice(P.AliasCondStart, P.NumConds);
147 unsigned OpIdx = 0;
148 bool OrPredicateResult = false;
149 if (llvm::all_of(Conds, [&](const AliasPatternCond &C) {
150 return matchAliasCondition(*MI, STI, MRI, OpIdx, M, C,
151 OrPredicateResult);
152 })) {
153 // If all conditions matched, use this asm string.
154 AsmStrOffset = P.AsmStrOffset;
155 break;
159 // If no alias matched, don't print an alias.
160 if (AsmStrOffset == ~0U)
161 return nullptr;
163 // Go to offset AsmStrOffset and use the null terminated string there. The
164 // offset should point to the beginning of an alias string, so it should
165 // either be zero or be preceded by a null byte.
166 assert(AsmStrOffset < M.AsmStrings.size() &&
167 (AsmStrOffset == 0 || M.AsmStrings[AsmStrOffset - 1] == '\0') &&
168 "bad asm string offset");
169 return M.AsmStrings.data() + AsmStrOffset;
172 /// Utility functions to make adding mark ups simpler.
173 StringRef MCInstPrinter::markup(StringRef s) const {
174 if (getUseMarkup())
175 return s;
176 else
177 return "";
180 // For asm-style hex (e.g. 0ffh) the first digit always has to be a number.
181 static bool needsLeadingZero(uint64_t Value)
183 while (Value)
185 uint64_t digit = (Value >> 60) & 0xf;
186 if (digit != 0)
187 return (digit >= 0xa);
188 Value <<= 4;
190 return false;
193 format_object<int64_t> MCInstPrinter::formatDec(int64_t Value) const {
194 return format("%" PRId64, Value);
197 format_object<int64_t> MCInstPrinter::formatHex(int64_t Value) const {
198 switch (PrintHexStyle) {
199 case HexStyle::C:
200 if (Value < 0) {
201 if (Value == std::numeric_limits<int64_t>::min())
202 return format<int64_t>("-0x8000000000000000", Value);
203 return format("-0x%" PRIx64, -Value);
205 return format("0x%" PRIx64, Value);
206 case HexStyle::Asm:
207 if (Value < 0) {
208 if (Value == std::numeric_limits<int64_t>::min())
209 return format<int64_t>("-8000000000000000h", Value);
210 if (needsLeadingZero(-(uint64_t)(Value)))
211 return format("-0%" PRIx64 "h", -Value);
212 return format("-%" PRIx64 "h", -Value);
214 if (needsLeadingZero((uint64_t)(Value)))
215 return format("0%" PRIx64 "h", Value);
216 return format("%" PRIx64 "h", Value);
218 llvm_unreachable("unsupported print style");
221 format_object<uint64_t> MCInstPrinter::formatHex(uint64_t Value) const {
222 switch(PrintHexStyle) {
223 case HexStyle::C:
224 return format("0x%" PRIx64, Value);
225 case HexStyle::Asm:
226 if (needsLeadingZero(Value))
227 return format("0%" PRIx64 "h", Value);
228 else
229 return format("%" PRIx64 "h", Value);
231 llvm_unreachable("unsupported print style");