106f32e7eSjoerg //===-- lib/MC/Disassembler.cpp - Disassembler Public C Interface ---------===//
206f32e7eSjoerg //
306f32e7eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406f32e7eSjoerg // See https://llvm.org/LICENSE.txt for license information.
506f32e7eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606f32e7eSjoerg //
706f32e7eSjoerg //===----------------------------------------------------------------------===//
806f32e7eSjoerg 
906f32e7eSjoerg #include "Disassembler.h"
1006f32e7eSjoerg #include "llvm-c/Disassembler.h"
1106f32e7eSjoerg #include "llvm/ADT/ArrayRef.h"
1206f32e7eSjoerg #include "llvm/ADT/SmallVector.h"
1306f32e7eSjoerg #include "llvm/ADT/Triple.h"
1406f32e7eSjoerg #include "llvm/MC/MCAsmInfo.h"
1506f32e7eSjoerg #include "llvm/MC/MCContext.h"
1606f32e7eSjoerg #include "llvm/MC/MCDisassembler/MCDisassembler.h"
1706f32e7eSjoerg #include "llvm/MC/MCDisassembler/MCRelocationInfo.h"
1806f32e7eSjoerg #include "llvm/MC/MCDisassembler/MCSymbolizer.h"
1906f32e7eSjoerg #include "llvm/MC/MCInst.h"
2006f32e7eSjoerg #include "llvm/MC/MCInstPrinter.h"
2106f32e7eSjoerg #include "llvm/MC/MCInstrDesc.h"
2206f32e7eSjoerg #include "llvm/MC/MCInstrInfo.h"
2306f32e7eSjoerg #include "llvm/MC/MCInstrItineraries.h"
2406f32e7eSjoerg #include "llvm/MC/MCRegisterInfo.h"
2506f32e7eSjoerg #include "llvm/MC/MCSchedule.h"
2606f32e7eSjoerg #include "llvm/MC/MCSubtargetInfo.h"
2706f32e7eSjoerg #include "llvm/MC/MCTargetOptions.h"
2806f32e7eSjoerg #include "llvm/Support/ErrorHandling.h"
2906f32e7eSjoerg #include "llvm/Support/FormattedStream.h"
3006f32e7eSjoerg #include "llvm/Support/TargetRegistry.h"
3106f32e7eSjoerg #include "llvm/Support/raw_ostream.h"
3206f32e7eSjoerg #include <cassert>
3306f32e7eSjoerg #include <cstddef>
3406f32e7eSjoerg #include <cstring>
3506f32e7eSjoerg 
3606f32e7eSjoerg using namespace llvm;
3706f32e7eSjoerg 
3806f32e7eSjoerg // LLVMCreateDisasm() creates a disassembler for the TripleName.  Symbolic
3906f32e7eSjoerg // disassembly is supported by passing a block of information in the DisInfo
4006f32e7eSjoerg // parameter and specifying the TagType and callback functions as described in
4106f32e7eSjoerg // the header llvm-c/Disassembler.h .  The pointer to the block and the
4206f32e7eSjoerg // functions can all be passed as NULL.  If successful, this returns a
4306f32e7eSjoerg // disassembler context.  If not, it returns NULL.
4406f32e7eSjoerg //
4506f32e7eSjoerg LLVMDisasmContextRef
LLVMCreateDisasmCPUFeatures(const char * TT,const char * CPU,const char * Features,void * DisInfo,int TagType,LLVMOpInfoCallback GetOpInfo,LLVMSymbolLookupCallback SymbolLookUp)4606f32e7eSjoerg LLVMCreateDisasmCPUFeatures(const char *TT, const char *CPU,
4706f32e7eSjoerg                             const char *Features, void *DisInfo, int TagType,
4806f32e7eSjoerg                             LLVMOpInfoCallback GetOpInfo,
4906f32e7eSjoerg                             LLVMSymbolLookupCallback SymbolLookUp) {
5006f32e7eSjoerg   // Get the target.
5106f32e7eSjoerg   std::string Error;
5206f32e7eSjoerg   const Target *TheTarget = TargetRegistry::lookupTarget(TT, Error);
5306f32e7eSjoerg   if (!TheTarget)
5406f32e7eSjoerg     return nullptr;
5506f32e7eSjoerg 
5606f32e7eSjoerg   std::unique_ptr<const MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TT));
5706f32e7eSjoerg   if (!MRI)
5806f32e7eSjoerg     return nullptr;
5906f32e7eSjoerg 
6006f32e7eSjoerg   MCTargetOptions MCOptions;
6106f32e7eSjoerg   // Get the assembler info needed to setup the MCContext.
6206f32e7eSjoerg   std::unique_ptr<const MCAsmInfo> MAI(
6306f32e7eSjoerg       TheTarget->createMCAsmInfo(*MRI, TT, MCOptions));
6406f32e7eSjoerg   if (!MAI)
6506f32e7eSjoerg     return nullptr;
6606f32e7eSjoerg 
6706f32e7eSjoerg   std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
6806f32e7eSjoerg   if (!MII)
6906f32e7eSjoerg     return nullptr;
7006f32e7eSjoerg 
7106f32e7eSjoerg   std::unique_ptr<const MCSubtargetInfo> STI(
7206f32e7eSjoerg       TheTarget->createMCSubtargetInfo(TT, CPU, Features));
7306f32e7eSjoerg   if (!STI)
7406f32e7eSjoerg     return nullptr;
7506f32e7eSjoerg 
7606f32e7eSjoerg   // Set up the MCContext for creating symbols and MCExpr's.
77*da58b97aSjoerg   std::unique_ptr<MCContext> Ctx(
78*da58b97aSjoerg       new MCContext(Triple(TT), MAI.get(), MRI.get(), STI.get()));
7906f32e7eSjoerg   if (!Ctx)
8006f32e7eSjoerg     return nullptr;
8106f32e7eSjoerg 
8206f32e7eSjoerg   // Set up disassembler.
8306f32e7eSjoerg   std::unique_ptr<MCDisassembler> DisAsm(
8406f32e7eSjoerg       TheTarget->createMCDisassembler(*STI, *Ctx));
8506f32e7eSjoerg   if (!DisAsm)
8606f32e7eSjoerg     return nullptr;
8706f32e7eSjoerg 
8806f32e7eSjoerg   std::unique_ptr<MCRelocationInfo> RelInfo(
8906f32e7eSjoerg       TheTarget->createMCRelocationInfo(TT, *Ctx));
9006f32e7eSjoerg   if (!RelInfo)
9106f32e7eSjoerg     return nullptr;
9206f32e7eSjoerg 
9306f32e7eSjoerg   std::unique_ptr<MCSymbolizer> Symbolizer(TheTarget->createMCSymbolizer(
9406f32e7eSjoerg       TT, GetOpInfo, SymbolLookUp, DisInfo, Ctx.get(), std::move(RelInfo)));
9506f32e7eSjoerg   DisAsm->setSymbolizer(std::move(Symbolizer));
9606f32e7eSjoerg 
9706f32e7eSjoerg   // Set up the instruction printer.
9806f32e7eSjoerg   int AsmPrinterVariant = MAI->getAssemblerDialect();
9906f32e7eSjoerg   std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
10006f32e7eSjoerg       Triple(TT), AsmPrinterVariant, *MAI, *MII, *MRI));
10106f32e7eSjoerg   if (!IP)
10206f32e7eSjoerg     return nullptr;
10306f32e7eSjoerg 
10406f32e7eSjoerg   LLVMDisasmContext *DC = new LLVMDisasmContext(
10506f32e7eSjoerg       TT, DisInfo, TagType, GetOpInfo, SymbolLookUp, TheTarget, std::move(MAI),
10606f32e7eSjoerg       std::move(MRI), std::move(STI), std::move(MII), std::move(Ctx),
10706f32e7eSjoerg       std::move(DisAsm), std::move(IP));
10806f32e7eSjoerg   if (!DC)
10906f32e7eSjoerg     return nullptr;
11006f32e7eSjoerg 
11106f32e7eSjoerg   DC->setCPU(CPU);
11206f32e7eSjoerg   return DC;
11306f32e7eSjoerg }
11406f32e7eSjoerg 
11506f32e7eSjoerg LLVMDisasmContextRef
LLVMCreateDisasmCPU(const char * TT,const char * CPU,void * DisInfo,int TagType,LLVMOpInfoCallback GetOpInfo,LLVMSymbolLookupCallback SymbolLookUp)11606f32e7eSjoerg LLVMCreateDisasmCPU(const char *TT, const char *CPU, void *DisInfo, int TagType,
11706f32e7eSjoerg                     LLVMOpInfoCallback GetOpInfo,
11806f32e7eSjoerg                     LLVMSymbolLookupCallback SymbolLookUp) {
11906f32e7eSjoerg   return LLVMCreateDisasmCPUFeatures(TT, CPU, "", DisInfo, TagType, GetOpInfo,
12006f32e7eSjoerg                                      SymbolLookUp);
12106f32e7eSjoerg }
12206f32e7eSjoerg 
LLVMCreateDisasm(const char * TT,void * DisInfo,int TagType,LLVMOpInfoCallback GetOpInfo,LLVMSymbolLookupCallback SymbolLookUp)12306f32e7eSjoerg LLVMDisasmContextRef LLVMCreateDisasm(const char *TT, void *DisInfo,
12406f32e7eSjoerg                                       int TagType, LLVMOpInfoCallback GetOpInfo,
12506f32e7eSjoerg                                       LLVMSymbolLookupCallback SymbolLookUp) {
12606f32e7eSjoerg   return LLVMCreateDisasmCPUFeatures(TT, "", "", DisInfo, TagType, GetOpInfo,
12706f32e7eSjoerg                                      SymbolLookUp);
12806f32e7eSjoerg }
12906f32e7eSjoerg 
13006f32e7eSjoerg //
13106f32e7eSjoerg // LLVMDisasmDispose() disposes of the disassembler specified by the context.
13206f32e7eSjoerg //
LLVMDisasmDispose(LLVMDisasmContextRef DCR)13306f32e7eSjoerg void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
13406f32e7eSjoerg   LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
13506f32e7eSjoerg   delete DC;
13606f32e7eSjoerg }
13706f32e7eSjoerg 
13806f32e7eSjoerg /// Emits the comments that are stored in \p DC comment stream.
13906f32e7eSjoerg /// Each comment in the comment stream must end with a newline.
emitComments(LLVMDisasmContext * DC,formatted_raw_ostream & FormattedOS)14006f32e7eSjoerg static void emitComments(LLVMDisasmContext *DC,
14106f32e7eSjoerg                          formatted_raw_ostream &FormattedOS) {
14206f32e7eSjoerg   // Flush the stream before taking its content.
14306f32e7eSjoerg   StringRef Comments = DC->CommentsToEmit.str();
14406f32e7eSjoerg   // Get the default information for printing a comment.
14506f32e7eSjoerg   const MCAsmInfo *MAI = DC->getAsmInfo();
14606f32e7eSjoerg   StringRef CommentBegin = MAI->getCommentString();
14706f32e7eSjoerg   unsigned CommentColumn = MAI->getCommentColumn();
14806f32e7eSjoerg   bool IsFirst = true;
14906f32e7eSjoerg   while (!Comments.empty()) {
15006f32e7eSjoerg     if (!IsFirst)
15106f32e7eSjoerg       FormattedOS << '\n';
15206f32e7eSjoerg     // Emit a line of comments.
15306f32e7eSjoerg     FormattedOS.PadToColumn(CommentColumn);
15406f32e7eSjoerg     size_t Position = Comments.find('\n');
15506f32e7eSjoerg     FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
15606f32e7eSjoerg     // Move after the newline character.
15706f32e7eSjoerg     Comments = Comments.substr(Position+1);
15806f32e7eSjoerg     IsFirst = false;
15906f32e7eSjoerg   }
16006f32e7eSjoerg   FormattedOS.flush();
16106f32e7eSjoerg 
16206f32e7eSjoerg   // Tell the comment stream that the vector changed underneath it.
16306f32e7eSjoerg   DC->CommentsToEmit.clear();
16406f32e7eSjoerg }
16506f32e7eSjoerg 
16606f32e7eSjoerg /// Gets latency information for \p Inst from the itinerary
16706f32e7eSjoerg /// scheduling model, based on \p DC information.
16806f32e7eSjoerg /// \return The maximum expected latency over all the operands or -1
16906f32e7eSjoerg /// if no information is available.
getItineraryLatency(LLVMDisasmContext * DC,const MCInst & Inst)17006f32e7eSjoerg static int getItineraryLatency(LLVMDisasmContext *DC, const MCInst &Inst) {
17106f32e7eSjoerg   const int NoInformationAvailable = -1;
17206f32e7eSjoerg 
17306f32e7eSjoerg   // Check if we have a CPU to get the itinerary information.
17406f32e7eSjoerg   if (DC->getCPU().empty())
17506f32e7eSjoerg     return NoInformationAvailable;
17606f32e7eSjoerg 
17706f32e7eSjoerg   // Get itinerary information.
17806f32e7eSjoerg   const MCSubtargetInfo *STI = DC->getSubtargetInfo();
17906f32e7eSjoerg   InstrItineraryData IID = STI->getInstrItineraryForCPU(DC->getCPU());
18006f32e7eSjoerg   // Get the scheduling class of the requested instruction.
18106f32e7eSjoerg   const MCInstrDesc& Desc = DC->getInstrInfo()->get(Inst.getOpcode());
18206f32e7eSjoerg   unsigned SCClass = Desc.getSchedClass();
18306f32e7eSjoerg 
18406f32e7eSjoerg   int Latency = 0;
18506f32e7eSjoerg   for (unsigned OpIdx = 0, OpIdxEnd = Inst.getNumOperands(); OpIdx != OpIdxEnd;
18606f32e7eSjoerg        ++OpIdx)
18706f32e7eSjoerg     Latency = std::max(Latency, IID.getOperandCycle(SCClass, OpIdx));
18806f32e7eSjoerg 
18906f32e7eSjoerg   return Latency;
19006f32e7eSjoerg }
19106f32e7eSjoerg 
19206f32e7eSjoerg /// Gets latency information for \p Inst, based on \p DC information.
19306f32e7eSjoerg /// \return The maximum expected latency over all the definitions or -1
19406f32e7eSjoerg /// if no information is available.
getLatency(LLVMDisasmContext * DC,const MCInst & Inst)19506f32e7eSjoerg static int getLatency(LLVMDisasmContext *DC, const MCInst &Inst) {
19606f32e7eSjoerg   // Try to compute scheduling information.
19706f32e7eSjoerg   const MCSubtargetInfo *STI = DC->getSubtargetInfo();
19806f32e7eSjoerg   const MCSchedModel SCModel = STI->getSchedModel();
19906f32e7eSjoerg   const int NoInformationAvailable = -1;
20006f32e7eSjoerg 
20106f32e7eSjoerg   // Check if we have a scheduling model for instructions.
20206f32e7eSjoerg   if (!SCModel.hasInstrSchedModel())
20306f32e7eSjoerg     // Try to fall back to the itinerary model if the scheduling model doesn't
20406f32e7eSjoerg     // have a scheduling table.  Note the default does not have a table.
20506f32e7eSjoerg     return getItineraryLatency(DC, Inst);
20606f32e7eSjoerg 
20706f32e7eSjoerg   // Get the scheduling class of the requested instruction.
20806f32e7eSjoerg   const MCInstrDesc& Desc = DC->getInstrInfo()->get(Inst.getOpcode());
20906f32e7eSjoerg   unsigned SCClass = Desc.getSchedClass();
21006f32e7eSjoerg   const MCSchedClassDesc *SCDesc = SCModel.getSchedClassDesc(SCClass);
21106f32e7eSjoerg   // Resolving the variant SchedClass requires an MI to pass to
21206f32e7eSjoerg   // SubTargetInfo::resolveSchedClass.
21306f32e7eSjoerg   if (!SCDesc || !SCDesc->isValid() || SCDesc->isVariant())
21406f32e7eSjoerg     return NoInformationAvailable;
21506f32e7eSjoerg 
21606f32e7eSjoerg   // Compute output latency.
21706f32e7eSjoerg   int16_t Latency = 0;
21806f32e7eSjoerg   for (unsigned DefIdx = 0, DefEnd = SCDesc->NumWriteLatencyEntries;
21906f32e7eSjoerg        DefIdx != DefEnd; ++DefIdx) {
22006f32e7eSjoerg     // Lookup the definition's write latency in SubtargetInfo.
22106f32e7eSjoerg     const MCWriteLatencyEntry *WLEntry = STI->getWriteLatencyEntry(SCDesc,
22206f32e7eSjoerg                                                                    DefIdx);
22306f32e7eSjoerg     Latency = std::max(Latency, WLEntry->Cycles);
22406f32e7eSjoerg   }
22506f32e7eSjoerg 
22606f32e7eSjoerg   return Latency;
22706f32e7eSjoerg }
22806f32e7eSjoerg 
22906f32e7eSjoerg /// Emits latency information in DC->CommentStream for \p Inst, based
23006f32e7eSjoerg /// on the information available in \p DC.
emitLatency(LLVMDisasmContext * DC,const MCInst & Inst)23106f32e7eSjoerg static void emitLatency(LLVMDisasmContext *DC, const MCInst &Inst) {
23206f32e7eSjoerg   int Latency = getLatency(DC, Inst);
23306f32e7eSjoerg 
23406f32e7eSjoerg   // Report only interesting latencies.
23506f32e7eSjoerg   if (Latency < 2)
23606f32e7eSjoerg     return;
23706f32e7eSjoerg 
23806f32e7eSjoerg   DC->CommentStream << "Latency: " << Latency << '\n';
23906f32e7eSjoerg }
24006f32e7eSjoerg 
24106f32e7eSjoerg //
24206f32e7eSjoerg // LLVMDisasmInstruction() disassembles a single instruction using the
24306f32e7eSjoerg // disassembler context specified in the parameter DC.  The bytes of the
24406f32e7eSjoerg // instruction are specified in the parameter Bytes, and contains at least
24506f32e7eSjoerg // BytesSize number of bytes.  The instruction is at the address specified by
24606f32e7eSjoerg // the PC parameter.  If a valid instruction can be disassembled its string is
24706f32e7eSjoerg // returned indirectly in OutString which whos size is specified in the
24806f32e7eSjoerg // parameter OutStringSize.  This function returns the number of bytes in the
24906f32e7eSjoerg // instruction or zero if there was no valid instruction.  If this function
25006f32e7eSjoerg // returns zero the caller will have to pick how many bytes they want to step
25106f32e7eSjoerg // over by printing a .byte, .long etc. to continue.
25206f32e7eSjoerg //
LLVMDisasmInstruction(LLVMDisasmContextRef DCR,uint8_t * Bytes,uint64_t BytesSize,uint64_t PC,char * OutString,size_t OutStringSize)25306f32e7eSjoerg size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
25406f32e7eSjoerg                              uint64_t BytesSize, uint64_t PC, char *OutString,
25506f32e7eSjoerg                              size_t OutStringSize){
25606f32e7eSjoerg   LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
25706f32e7eSjoerg   // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
25806f32e7eSjoerg   ArrayRef<uint8_t> Data(Bytes, BytesSize);
25906f32e7eSjoerg 
26006f32e7eSjoerg   uint64_t Size;
26106f32e7eSjoerg   MCInst Inst;
26206f32e7eSjoerg   const MCDisassembler *DisAsm = DC->getDisAsm();
26306f32e7eSjoerg   MCInstPrinter *IP = DC->getIP();
26406f32e7eSjoerg   MCDisassembler::DecodeStatus S;
26506f32e7eSjoerg   SmallVector<char, 64> InsnStr;
26606f32e7eSjoerg   raw_svector_ostream Annotations(InsnStr);
267*da58b97aSjoerg   S = DisAsm->getInstruction(Inst, Size, Data, PC, Annotations);
26806f32e7eSjoerg   switch (S) {
26906f32e7eSjoerg   case MCDisassembler::Fail:
27006f32e7eSjoerg   case MCDisassembler::SoftFail:
27106f32e7eSjoerg     // FIXME: Do something different for soft failure modes?
27206f32e7eSjoerg     return 0;
27306f32e7eSjoerg 
27406f32e7eSjoerg   case MCDisassembler::Success: {
27506f32e7eSjoerg     StringRef AnnotationsStr = Annotations.str();
27606f32e7eSjoerg 
27706f32e7eSjoerg     SmallVector<char, 64> InsnStr;
27806f32e7eSjoerg     raw_svector_ostream OS(InsnStr);
27906f32e7eSjoerg     formatted_raw_ostream FormattedOS(OS);
280*da58b97aSjoerg     IP->printInst(&Inst, PC, AnnotationsStr, *DC->getSubtargetInfo(),
281*da58b97aSjoerg                   FormattedOS);
28206f32e7eSjoerg 
28306f32e7eSjoerg     if (DC->getOptions() & LLVMDisassembler_Option_PrintLatency)
28406f32e7eSjoerg       emitLatency(DC, Inst);
28506f32e7eSjoerg 
28606f32e7eSjoerg     emitComments(DC, FormattedOS);
28706f32e7eSjoerg 
28806f32e7eSjoerg     assert(OutStringSize != 0 && "Output buffer cannot be zero size");
28906f32e7eSjoerg     size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
29006f32e7eSjoerg     std::memcpy(OutString, InsnStr.data(), OutputSize);
29106f32e7eSjoerg     OutString[OutputSize] = '\0'; // Terminate string.
29206f32e7eSjoerg 
29306f32e7eSjoerg     return Size;
29406f32e7eSjoerg   }
29506f32e7eSjoerg   }
29606f32e7eSjoerg   llvm_unreachable("Invalid DecodeStatus!");
29706f32e7eSjoerg }
29806f32e7eSjoerg 
29906f32e7eSjoerg //
30006f32e7eSjoerg // LLVMSetDisasmOptions() sets the disassembler's options.  It returns 1 if it
30106f32e7eSjoerg // can set all the Options and 0 otherwise.
30206f32e7eSjoerg //
LLVMSetDisasmOptions(LLVMDisasmContextRef DCR,uint64_t Options)30306f32e7eSjoerg int LLVMSetDisasmOptions(LLVMDisasmContextRef DCR, uint64_t Options){
30406f32e7eSjoerg   if (Options & LLVMDisassembler_Option_UseMarkup){
30506f32e7eSjoerg       LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
30606f32e7eSjoerg       MCInstPrinter *IP = DC->getIP();
30706f32e7eSjoerg       IP->setUseMarkup(true);
30806f32e7eSjoerg       DC->addOptions(LLVMDisassembler_Option_UseMarkup);
30906f32e7eSjoerg       Options &= ~LLVMDisassembler_Option_UseMarkup;
31006f32e7eSjoerg   }
31106f32e7eSjoerg   if (Options & LLVMDisassembler_Option_PrintImmHex){
31206f32e7eSjoerg       LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
31306f32e7eSjoerg       MCInstPrinter *IP = DC->getIP();
31406f32e7eSjoerg       IP->setPrintImmHex(true);
31506f32e7eSjoerg       DC->addOptions(LLVMDisassembler_Option_PrintImmHex);
31606f32e7eSjoerg       Options &= ~LLVMDisassembler_Option_PrintImmHex;
31706f32e7eSjoerg   }
31806f32e7eSjoerg   if (Options & LLVMDisassembler_Option_AsmPrinterVariant){
31906f32e7eSjoerg       LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
32006f32e7eSjoerg       // Try to set up the new instruction printer.
32106f32e7eSjoerg       const MCAsmInfo *MAI = DC->getAsmInfo();
32206f32e7eSjoerg       const MCInstrInfo *MII = DC->getInstrInfo();
32306f32e7eSjoerg       const MCRegisterInfo *MRI = DC->getRegisterInfo();
32406f32e7eSjoerg       int AsmPrinterVariant = MAI->getAssemblerDialect();
32506f32e7eSjoerg       AsmPrinterVariant = AsmPrinterVariant == 0 ? 1 : 0;
32606f32e7eSjoerg       MCInstPrinter *IP = DC->getTarget()->createMCInstPrinter(
32706f32e7eSjoerg           Triple(DC->getTripleName()), AsmPrinterVariant, *MAI, *MII, *MRI);
32806f32e7eSjoerg       if (IP) {
32906f32e7eSjoerg         DC->setIP(IP);
33006f32e7eSjoerg         DC->addOptions(LLVMDisassembler_Option_AsmPrinterVariant);
33106f32e7eSjoerg         Options &= ~LLVMDisassembler_Option_AsmPrinterVariant;
33206f32e7eSjoerg       }
33306f32e7eSjoerg   }
33406f32e7eSjoerg   if (Options & LLVMDisassembler_Option_SetInstrComments) {
33506f32e7eSjoerg     LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
33606f32e7eSjoerg     MCInstPrinter *IP = DC->getIP();
33706f32e7eSjoerg     IP->setCommentStream(DC->CommentStream);
33806f32e7eSjoerg     DC->addOptions(LLVMDisassembler_Option_SetInstrComments);
33906f32e7eSjoerg     Options &= ~LLVMDisassembler_Option_SetInstrComments;
34006f32e7eSjoerg   }
34106f32e7eSjoerg   if (Options & LLVMDisassembler_Option_PrintLatency) {
34206f32e7eSjoerg     LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
34306f32e7eSjoerg     DC->addOptions(LLVMDisassembler_Option_PrintLatency);
34406f32e7eSjoerg     Options &= ~LLVMDisassembler_Option_PrintLatency;
34506f32e7eSjoerg   }
34606f32e7eSjoerg   return (Options == 0);
34706f32e7eSjoerg }
348