blob: 2e700b3b2ea417dfe7c7e571cca07515e6985b87 [file] [log] [blame]
Nick Lewycky2e2d75f2011-09-01 21:09:04 +00001//===-- lib/MC/Disassembler.cpp - Disassembler Public C Interface ---------===//
Kevin Enderbyf3070dc2011-03-28 18:25:07 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattner0388fb02011-05-22 04:52:24 +00009
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000010#include "Disassembler.h"
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000011#include "llvm-c/Disassembler.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000012#include "llvm/ADT/ArrayRef.h"
13#include "llvm/ADT/SmallVector.h"
14#include "llvm/ADT/Triple.h"
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000015#include "llvm/MC/MCAsmInfo.h"
Evan Cheng345b6b42011-07-20 06:54:19 +000016#include "llvm/MC/MCContext.h"
Benjamin Kramerf57c1972016-01-26 16:44:37 +000017#include "llvm/MC/MCDisassembler/MCDisassembler.h"
18#include "llvm/MC/MCDisassembler/MCRelocationInfo.h"
19#include "llvm/MC/MCDisassembler/MCSymbolizer.h"
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000020#include "llvm/MC/MCInst.h"
21#include "llvm/MC/MCInstPrinter.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000022#include "llvm/MC/MCInstrDesc.h"
Sean Callanane804b5b2012-04-06 18:21:09 +000023#include "llvm/MC/MCInstrInfo.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000024#include "llvm/MC/MCInstrItineraries.h"
Evan Cheng345b6b42011-07-20 06:54:19 +000025#include "llvm/MC/MCRegisterInfo.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000026#include "llvm/MC/MCSchedule.h"
Sean Callanane804b5b2012-04-06 18:21:09 +000027#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/Support/ErrorHandling.h"
Quentin Colombet93a98aa2013-10-01 22:14:56 +000029#include "llvm/Support/FormattedStream.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000030#include "llvm/Support/TargetRegistry.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000031#include "llvm/Support/raw_ostream.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000032#include <cassert>
33#include <cstddef>
34#include <cstring>
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000035
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000036using namespace llvm;
37
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000038// LLVMCreateDisasm() creates a disassembler for the TripleName. Symbolic
39// disassembly is supported by passing a block of information in the DisInfo
Chris Lattner0388fb02011-05-22 04:52:24 +000040// parameter and specifying the TagType and callback functions as described in
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000041// the header llvm-c/Disassembler.h . The pointer to the block and the
Chris Lattner0388fb02011-05-22 04:52:24 +000042// functions can all be passed as NULL. If successful, this returns a
43// disassembler context. If not, it returns NULL.
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000044//
Bradley Smith7a770752014-09-30 16:31:40 +000045LLVMDisasmContextRef
Eric Christopher71b1e5f2015-03-30 22:31:14 +000046LLVMCreateDisasmCPUFeatures(const char *TT, const char *CPU,
Bradley Smith7a770752014-09-30 16:31:40 +000047 const char *Features, void *DisInfo, int TagType,
48 LLVMOpInfoCallback GetOpInfo,
49 LLVMSymbolLookupCallback SymbolLookUp) {
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000050 // Get the target.
51 std::string Error;
Eric Christopher71b1e5f2015-03-30 22:31:14 +000052 const Target *TheTarget = TargetRegistry::lookupTarget(TT, Error);
Kevin Enderby64d93452013-05-23 00:32:34 +000053 if (!TheTarget)
Craig Topper353eda42014-04-24 06:44:33 +000054 return nullptr;
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000055
Eric Christopher71b1e5f2015-03-30 22:31:14 +000056 const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TT);
Rafael Espindola227144c2013-05-13 01:16:13 +000057 if (!MRI)
Craig Topper353eda42014-04-24 06:44:33 +000058 return nullptr;
Rafael Espindola227144c2013-05-13 01:16:13 +000059
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000060 // Get the assembler info needed to setup the MCContext.
Eric Christopher71b1e5f2015-03-30 22:31:14 +000061 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(*MRI, TT);
Kevin Enderbyf536c6a2013-03-12 18:12:17 +000062 if (!MAI)
Craig Topper353eda42014-04-24 06:44:33 +000063 return nullptr;
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000064
Craig Topper54bfde72012-04-02 06:09:36 +000065 const MCInstrInfo *MII = TheTarget->createMCInstrInfo();
Kevin Enderbyf536c6a2013-03-12 18:12:17 +000066 if (!MII)
Craig Topper353eda42014-04-24 06:44:33 +000067 return nullptr;
Craig Topper54bfde72012-04-02 06:09:36 +000068
Eric Christopher71b1e5f2015-03-30 22:31:14 +000069 const MCSubtargetInfo *STI =
70 TheTarget->createMCSubtargetInfo(TT, CPU, Features);
Kevin Enderbyf536c6a2013-03-12 18:12:17 +000071 if (!STI)
Craig Topper353eda42014-04-24 06:44:33 +000072 return nullptr;
James Molloy4c493e82011-09-07 17:24:38 +000073
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000074 // Set up the MCContext for creating symbols and MCExpr's.
Craig Topper353eda42014-04-24 06:44:33 +000075 MCContext *Ctx = new MCContext(MAI, MRI, nullptr);
Kevin Enderbyf536c6a2013-03-12 18:12:17 +000076 if (!Ctx)
Craig Topper353eda42014-04-24 06:44:33 +000077 return nullptr;
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000078
79 // Set up disassembler.
Lang Hamesa1bc0f52014-04-15 04:40:56 +000080 MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI, *Ctx);
Kevin Enderbyf536c6a2013-03-12 18:12:17 +000081 if (!DisAsm)
Craig Topper353eda42014-04-24 06:44:33 +000082 return nullptr;
Ahmed Bougachaad1084d2013-05-24 00:39:57 +000083
Ahmed Charles56440fd2014-03-06 05:51:42 +000084 std::unique_ptr<MCRelocationInfo> RelInfo(
Eric Christopher71b1e5f2015-03-30 22:31:14 +000085 TheTarget->createMCRelocationInfo(TT, *Ctx));
Ahmed Bougachaad1084d2013-05-24 00:39:57 +000086 if (!RelInfo)
Craig Topper353eda42014-04-24 06:44:33 +000087 return nullptr;
Ahmed Bougachaad1084d2013-05-24 00:39:57 +000088
Ahmed Charles56440fd2014-03-06 05:51:42 +000089 std::unique_ptr<MCSymbolizer> Symbolizer(TheTarget->createMCSymbolizer(
Eric Christopher71b1e5f2015-03-30 22:31:14 +000090 TT, GetOpInfo, SymbolLookUp, DisInfo, Ctx, std::move(RelInfo)));
Ahmed Charlesdf17c832014-03-07 09:38:02 +000091 DisAsm->setSymbolizer(std::move(Symbolizer));
Lang Hames95400e22014-04-11 20:07:58 +000092
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000093 // Set up the instruction printer.
94 int AsmPrinterVariant = MAI->getAssemblerDialect();
Eric Christopherf8019402015-03-31 00:10:04 +000095 MCInstPrinter *IP = TheTarget->createMCInstPrinter(
Daniel Sanders50f17232015-09-15 16:17:27 +000096 Triple(TT), AsmPrinterVariant, *MAI, *MII, *MRI);
Kevin Enderbyf536c6a2013-03-12 18:12:17 +000097 if (!IP)
Craig Topper353eda42014-04-24 06:44:33 +000098 return nullptr;
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000099
Eric Christopher71b1e5f2015-03-30 22:31:14 +0000100 LLVMDisasmContext *DC =
101 new LLVMDisasmContext(TT, DisInfo, TagType, GetOpInfo, SymbolLookUp,
102 TheTarget, MAI, MRI, STI, MII, Ctx, DisAsm, IP);
Kevin Enderbyf536c6a2013-03-12 18:12:17 +0000103 if (!DC)
Craig Topper353eda42014-04-24 06:44:33 +0000104 return nullptr;
Owen Anderson233f1302011-09-15 18:37:20 +0000105
Quentin Colombet76e55572013-10-03 17:51:49 +0000106 DC->setCPU(CPU);
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000107 return DC;
108}
109
Eric Christopher71b1e5f2015-03-30 22:31:14 +0000110LLVMDisasmContextRef
111LLVMCreateDisasmCPU(const char *TT, const char *CPU, void *DisInfo, int TagType,
112 LLVMOpInfoCallback GetOpInfo,
113 LLVMSymbolLookupCallback SymbolLookUp) {
114 return LLVMCreateDisasmCPUFeatures(TT, CPU, "", DisInfo, TagType, GetOpInfo,
115 SymbolLookUp);
Bradley Smith7a770752014-09-30 16:31:40 +0000116}
117
Eric Christopher71b1e5f2015-03-30 22:31:14 +0000118LLVMDisasmContextRef LLVMCreateDisasm(const char *TT, void *DisInfo,
Jim Grosbach0ca9d5b2012-12-07 23:53:27 +0000119 int TagType, LLVMOpInfoCallback GetOpInfo,
120 LLVMSymbolLookupCallback SymbolLookUp) {
Eric Christopher71b1e5f2015-03-30 22:31:14 +0000121 return LLVMCreateDisasmCPUFeatures(TT, "", "", DisInfo, TagType, GetOpInfo,
122 SymbolLookUp);
Jim Grosbach0ca9d5b2012-12-07 23:53:27 +0000123}
124
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000125//
126// LLVMDisasmDispose() disposes of the disassembler specified by the context.
127//
128void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000129 LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000130 delete DC;
131}
132
Quentin Colombet93a98aa2013-10-01 22:14:56 +0000133/// \brief Emits the comments that are stored in \p DC comment stream.
134/// Each comment in the comment stream must end with a newline.
135static void emitComments(LLVMDisasmContext *DC,
136 formatted_raw_ostream &FormattedOS) {
137 // Flush the stream before taking its content.
Quentin Colombet93a98aa2013-10-01 22:14:56 +0000138 StringRef Comments = DC->CommentsToEmit.str();
139 // Get the default information for printing a comment.
140 const MCAsmInfo *MAI = DC->getAsmInfo();
Mehdi Amini36d33fc2016-10-01 06:46:33 +0000141 StringRef CommentBegin = MAI->getCommentString();
Quentin Colombet93a98aa2013-10-01 22:14:56 +0000142 unsigned CommentColumn = MAI->getCommentColumn();
143 bool IsFirst = true;
144 while (!Comments.empty()) {
145 if (!IsFirst)
146 FormattedOS << '\n';
147 // Emit a line of comments.
148 FormattedOS.PadToColumn(CommentColumn);
149 size_t Position = Comments.find('\n');
150 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
151 // Move after the newline character.
152 Comments = Comments.substr(Position+1);
153 IsFirst = false;
154 }
155 FormattedOS.flush();
156
157 // Tell the comment stream that the vector changed underneath it.
158 DC->CommentsToEmit.clear();
Quentin Colombet93a98aa2013-10-01 22:14:56 +0000159}
160
Eric Christopheracf25762015-01-13 00:21:14 +0000161/// \brief Gets latency information for \p Inst from the itinerary
Quentin Colombet76e55572013-10-03 17:51:49 +0000162/// scheduling model, based on \p DC information.
163/// \return The maximum expected latency over all the operands or -1
Eric Christopheracf25762015-01-13 00:21:14 +0000164/// if no information is available.
Quentin Colombet76e55572013-10-03 17:51:49 +0000165static int getItineraryLatency(LLVMDisasmContext *DC, const MCInst &Inst) {
166 const int NoInformationAvailable = -1;
167
168 // Check if we have a CPU to get the itinerary information.
169 if (DC->getCPU().empty())
170 return NoInformationAvailable;
171
172 // Get itinerary information.
173 const MCSubtargetInfo *STI = DC->getSubtargetInfo();
174 InstrItineraryData IID = STI->getInstrItineraryForCPU(DC->getCPU());
175 // Get the scheduling class of the requested instruction.
176 const MCInstrDesc& Desc = DC->getInstrInfo()->get(Inst.getOpcode());
177 unsigned SCClass = Desc.getSchedClass();
178
179 int Latency = 0;
180 for (unsigned OpIdx = 0, OpIdxEnd = Inst.getNumOperands(); OpIdx != OpIdxEnd;
181 ++OpIdx)
182 Latency = std::max(Latency, IID.getOperandCycle(SCClass, OpIdx));
183
184 return Latency;
185}
186
Quentin Colombet5f09cb02013-10-02 22:07:57 +0000187/// \brief Gets latency information for \p Inst, based on \p DC information.
188/// \return The maximum expected latency over all the definitions or -1
Eric Christopheracf25762015-01-13 00:21:14 +0000189/// if no information is available.
Quentin Colombet5f09cb02013-10-02 22:07:57 +0000190static int getLatency(LLVMDisasmContext *DC, const MCInst &Inst) {
191 // Try to compute scheduling information.
192 const MCSubtargetInfo *STI = DC->getSubtargetInfo();
Pete Cooper11759452014-09-02 17:43:54 +0000193 const MCSchedModel SCModel = STI->getSchedModel();
Quentin Colombet5f09cb02013-10-02 22:07:57 +0000194 const int NoInformationAvailable = -1;
195
196 // Check if we have a scheduling model for instructions.
Pete Cooper11759452014-09-02 17:43:54 +0000197 if (!SCModel.hasInstrSchedModel())
198 // Try to fall back to the itinerary model if the scheduling model doesn't
199 // have a scheduling table. Note the default does not have a table.
Quentin Colombet76e55572013-10-03 17:51:49 +0000200 return getItineraryLatency(DC, Inst);
Quentin Colombet5f09cb02013-10-02 22:07:57 +0000201
202 // Get the scheduling class of the requested instruction.
203 const MCInstrDesc& Desc = DC->getInstrInfo()->get(Inst.getOpcode());
204 unsigned SCClass = Desc.getSchedClass();
Pete Cooper11759452014-09-02 17:43:54 +0000205 const MCSchedClassDesc *SCDesc = SCModel.getSchedClassDesc(SCClass);
Quentin Colombetc3665042013-10-02 23:11:47 +0000206 // Resolving the variant SchedClass requires an MI to pass to
207 // SubTargetInfo::resolveSchedClass.
208 if (!SCDesc || !SCDesc->isValid() || SCDesc->isVariant())
Quentin Colombet5f09cb02013-10-02 22:07:57 +0000209 return NoInformationAvailable;
210
211 // Compute output latency.
Benjamin Kramerb941aba2018-02-23 19:32:56 +0000212 int16_t Latency = 0;
Quentin Colombet5f09cb02013-10-02 22:07:57 +0000213 for (unsigned DefIdx = 0, DefEnd = SCDesc->NumWriteLatencyEntries;
214 DefIdx != DefEnd; ++DefIdx) {
215 // Lookup the definition's write latency in SubtargetInfo.
216 const MCWriteLatencyEntry *WLEntry = STI->getWriteLatencyEntry(SCDesc,
217 DefIdx);
218 Latency = std::max(Latency, WLEntry->Cycles);
219 }
220
221 return Latency;
222}
223
Quentin Colombet5f09cb02013-10-02 22:07:57 +0000224/// \brief Emits latency information in DC->CommentStream for \p Inst, based
225/// on the information available in \p DC.
226static void emitLatency(LLVMDisasmContext *DC, const MCInst &Inst) {
227 int Latency = getLatency(DC, Inst);
228
Eric Christopheracf25762015-01-13 00:21:14 +0000229 // Report only interesting latencies.
Quentin Colombet5f09cb02013-10-02 22:07:57 +0000230 if (Latency < 2)
231 return;
232
233 DC->CommentStream << "Latency: " << Latency << '\n';
234}
235
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000236//
Benjamin Kramer26e77682011-04-09 14:06:12 +0000237// LLVMDisasmInstruction() disassembles a single instruction using the
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000238// disassembler context specified in the parameter DC. The bytes of the
Benjamin Kramer26e77682011-04-09 14:06:12 +0000239// instruction are specified in the parameter Bytes, and contains at least
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000240// BytesSize number of bytes. The instruction is at the address specified by
241// the PC parameter. If a valid instruction can be disassembled its string is
242// returned indirectly in OutString which whos size is specified in the
243// parameter OutStringSize. This function returns the number of bytes in the
244// instruction or zero if there was no valid instruction. If this function
245// returns zero the caller will have to pick how many bytes they want to step
246// over by printing a .byte, .long etc. to continue.
247//
248size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
249 uint64_t BytesSize, uint64_t PC, char *OutString,
250 size_t OutStringSize){
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000251 LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000252 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000253 ArrayRef<uint8_t> Data(Bytes, BytesSize);
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000254
255 uint64_t Size;
256 MCInst Inst;
257 const MCDisassembler *DisAsm = DC->getDisAsm();
258 MCInstPrinter *IP = DC->getIP();
James Molloy5ada2a72011-09-01 22:01:14 +0000259 MCDisassembler::DecodeStatus S;
Alp Tokere69170a2014-06-26 22:52:05 +0000260 SmallVector<char, 64> InsnStr;
261 raw_svector_ostream Annotations(InsnStr);
Rafael Espindola6c933972014-11-13 16:52:07 +0000262 S = DisAsm->getInstruction(Inst, Size, Data, PC,
Quentin Colombet93a98aa2013-10-01 22:14:56 +0000263 /*REMOVE*/ nulls(), Annotations);
James Molloy5ada2a72011-09-01 22:01:14 +0000264 switch (S) {
265 case MCDisassembler::Fail:
266 case MCDisassembler::SoftFail:
James Molloydb4ce602011-09-01 18:02:14 +0000267 // FIXME: Do something different for soft failure modes?
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000268 return 0;
James Molloy5ada2a72011-09-01 22:01:14 +0000269
270 case MCDisassembler::Success: {
Alp Tokere69170a2014-06-26 22:52:05 +0000271 StringRef AnnotationsStr = Annotations.str();
272
Owen Andersona0c3b972011-09-15 23:38:46 +0000273 SmallVector<char, 64> InsnStr;
274 raw_svector_ostream OS(InsnStr);
Quentin Colombet93a98aa2013-10-01 22:14:56 +0000275 formatted_raw_ostream FormattedOS(OS);
Akira Hatanakab46d0232015-03-27 20:36:02 +0000276 IP->printInst(&Inst, FormattedOS, AnnotationsStr, *DC->getSubtargetInfo());
Owen Anderson233f1302011-09-15 18:37:20 +0000277
Quentin Colombet5f09cb02013-10-02 22:07:57 +0000278 if (DC->getOptions() & LLVMDisassembler_Option_PrintLatency)
279 emitLatency(DC, Inst);
280
Quentin Colombet93a98aa2013-10-01 22:14:56 +0000281 emitComments(DC, FormattedOS);
Owen Anderson233f1302011-09-15 18:37:20 +0000282
James Molloy5ada2a72011-09-01 22:01:14 +0000283 assert(OutStringSize != 0 && "Output buffer cannot be zero size");
284 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
285 std::memcpy(OutString, InsnStr.data(), OutputSize);
286 OutString[OutputSize] = '\0'; // Terminate string.
287
288 return Size;
James Molloydb4ce602011-09-01 18:02:14 +0000289 }
James Molloy5ada2a72011-09-01 22:01:14 +0000290 }
David Blaikie46a9f012012-01-20 21:51:11 +0000291 llvm_unreachable("Invalid DecodeStatus!");
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000292}
Kevin Enderby62183c42012-10-22 22:31:46 +0000293
294//
295// LLVMSetDisasmOptions() sets the disassembler's options. It returns 1 if it
296// can set all the Options and 0 otherwise.
297//
298int LLVMSetDisasmOptions(LLVMDisasmContextRef DCR, uint64_t Options){
299 if (Options & LLVMDisassembler_Option_UseMarkup){
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000300 LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
Kevin Enderby62183c42012-10-22 22:31:46 +0000301 MCInstPrinter *IP = DC->getIP();
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000302 IP->setUseMarkup(true);
Quentin Colombet5f09cb02013-10-02 22:07:57 +0000303 DC->addOptions(LLVMDisassembler_Option_UseMarkup);
Kevin Enderby62183c42012-10-22 22:31:46 +0000304 Options &= ~LLVMDisassembler_Option_UseMarkup;
305 }
Kevin Enderby168ffb32012-12-05 18:13:19 +0000306 if (Options & LLVMDisassembler_Option_PrintImmHex){
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000307 LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
Kevin Enderby168ffb32012-12-05 18:13:19 +0000308 MCInstPrinter *IP = DC->getIP();
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000309 IP->setPrintImmHex(true);
Quentin Colombet5f09cb02013-10-02 22:07:57 +0000310 DC->addOptions(LLVMDisassembler_Option_PrintImmHex);
Kevin Enderby168ffb32012-12-05 18:13:19 +0000311 Options &= ~LLVMDisassembler_Option_PrintImmHex;
312 }
Kevin Enderby85cf5312012-12-18 23:47:28 +0000313 if (Options & LLVMDisassembler_Option_AsmPrinterVariant){
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000314 LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
Kevin Enderby85cf5312012-12-18 23:47:28 +0000315 // Try to set up the new instruction printer.
316 const MCAsmInfo *MAI = DC->getAsmInfo();
317 const MCInstrInfo *MII = DC->getInstrInfo();
318 const MCRegisterInfo *MRI = DC->getRegisterInfo();
Kevin Enderby85cf5312012-12-18 23:47:28 +0000319 int AsmPrinterVariant = MAI->getAssemblerDialect();
320 AsmPrinterVariant = AsmPrinterVariant == 0 ? 1 : 0;
321 MCInstPrinter *IP = DC->getTarget()->createMCInstPrinter(
Daniel Sanders50f17232015-09-15 16:17:27 +0000322 Triple(DC->getTripleName()), AsmPrinterVariant, *MAI, *MII, *MRI);
Kevin Enderby85cf5312012-12-18 23:47:28 +0000323 if (IP) {
324 DC->setIP(IP);
Quentin Colombet5f09cb02013-10-02 22:07:57 +0000325 DC->addOptions(LLVMDisassembler_Option_AsmPrinterVariant);
Kevin Enderby85cf5312012-12-18 23:47:28 +0000326 Options &= ~LLVMDisassembler_Option_AsmPrinterVariant;
327 }
328 }
Quentin Colombet93a98aa2013-10-01 22:14:56 +0000329 if (Options & LLVMDisassembler_Option_SetInstrComments) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000330 LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
Quentin Colombet93a98aa2013-10-01 22:14:56 +0000331 MCInstPrinter *IP = DC->getIP();
332 IP->setCommentStream(DC->CommentStream);
Quentin Colombet5f09cb02013-10-02 22:07:57 +0000333 DC->addOptions(LLVMDisassembler_Option_SetInstrComments);
Quentin Colombet93a98aa2013-10-01 22:14:56 +0000334 Options &= ~LLVMDisassembler_Option_SetInstrComments;
335 }
Quentin Colombet5f09cb02013-10-02 22:07:57 +0000336 if (Options & LLVMDisassembler_Option_PrintLatency) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000337 LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
Quentin Colombet5f09cb02013-10-02 22:07:57 +0000338 DC->addOptions(LLVMDisassembler_Option_PrintLatency);
339 Options &= ~LLVMDisassembler_Option_PrintLatency;
340 }
Kevin Enderby62183c42012-10-22 22:31:46 +0000341 return (Options == 0);
342}