12e2d75fbSNick Lewycky //===-- lib/MC/Disassembler.cpp - Disassembler Public C Interface ---------===//
2f3070dc4SKevin Enderby //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f3070dc4SKevin Enderby //
7f3070dc4SKevin Enderby //===----------------------------------------------------------------------===//
80388fb0eSChris Lattner 
9f3070dc4SKevin Enderby #include "Disassembler.h"
10f3070dc4SKevin Enderby #include "llvm-c/Disassembler.h"
1133d7b762SEugene Zelenko #include "llvm/ADT/ArrayRef.h"
1233d7b762SEugene Zelenko #include "llvm/ADT/SmallVector.h"
1333d7b762SEugene Zelenko #include "llvm/ADT/Triple.h"
14f3070dc4SKevin Enderby #include "llvm/MC/MCAsmInfo.h"
15345b6b45SEvan Cheng #include "llvm/MC/MCContext.h"
16f57c1977SBenjamin Kramer #include "llvm/MC/MCDisassembler/MCDisassembler.h"
17f57c1977SBenjamin Kramer #include "llvm/MC/MCDisassembler/MCRelocationInfo.h"
18f57c1977SBenjamin Kramer #include "llvm/MC/MCDisassembler/MCSymbolizer.h"
19f3070dc4SKevin Enderby #include "llvm/MC/MCInst.h"
20f3070dc4SKevin Enderby #include "llvm/MC/MCInstPrinter.h"
2133d7b762SEugene Zelenko #include "llvm/MC/MCInstrDesc.h"
22e804b5b7SSean Callanan #include "llvm/MC/MCInstrInfo.h"
2333d7b762SEugene Zelenko #include "llvm/MC/MCInstrItineraries.h"
24345b6b45SEvan Cheng #include "llvm/MC/MCRegisterInfo.h"
2533d7b762SEugene Zelenko #include "llvm/MC/MCSchedule.h"
26e804b5b7SSean Callanan #include "llvm/MC/MCSubtargetInfo.h"
274b63ca13SMirko Brkusanin #include "llvm/MC/MCTargetOptions.h"
28*89b57061SReid Kleckner #include "llvm/MC/TargetRegistry.h"
29ed0881b2SChandler Carruth #include "llvm/Support/ErrorHandling.h"
3093a98aacSQuentin Colombet #include "llvm/Support/FormattedStream.h"
316bda14b3SChandler Carruth #include "llvm/Support/raw_ostream.h"
3233d7b762SEugene Zelenko #include <cassert>
3333d7b762SEugene Zelenko #include <cstring>
34f3070dc4SKevin Enderby 
35f3070dc4SKevin Enderby using namespace llvm;
36f3070dc4SKevin Enderby 
37f3070dc4SKevin Enderby // LLVMCreateDisasm() creates a disassembler for the TripleName.  Symbolic
38f3070dc4SKevin Enderby // disassembly is supported by passing a block of information in the DisInfo
390388fb0eSChris Lattner // parameter and specifying the TagType and callback functions as described in
40f3070dc4SKevin Enderby // the header llvm-c/Disassembler.h .  The pointer to the block and the
410388fb0eSChris Lattner // functions can all be passed as NULL.  If successful, this returns a
420388fb0eSChris Lattner // disassembler context.  If not, it returns NULL.
43f3070dc4SKevin Enderby //
447a770755SBradley Smith LLVMDisasmContextRef
LLVMCreateDisasmCPUFeatures(const char * TT,const char * CPU,const char * Features,void * DisInfo,int TagType,LLVMOpInfoCallback GetOpInfo,LLVMSymbolLookupCallback SymbolLookUp)4571b1e5f1SEric Christopher LLVMCreateDisasmCPUFeatures(const char *TT, const char *CPU,
467a770755SBradley Smith                             const char *Features, void *DisInfo, int TagType,
470ca9d5b7SJim Grosbach                             LLVMOpInfoCallback GetOpInfo,
48f3070dc4SKevin Enderby                             LLVMSymbolLookupCallback SymbolLookUp) {
49f3070dc4SKevin Enderby   // Get the target.
50f3070dc4SKevin Enderby   std::string Error;
5171b1e5f1SEric Christopher   const Target *TheTarget = TargetRegistry::lookupTarget(TT, Error);
5264d93450SKevin Enderby   if (!TheTarget)
53353eda48SCraig Topper     return nullptr;
54f3070dc4SKevin Enderby 
55c9504650SScott Linder   std::unique_ptr<const MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TT));
56227144c2SRafael Espindola   if (!MRI)
57353eda48SCraig Topper     return nullptr;
58227144c2SRafael Espindola 
594b63ca13SMirko Brkusanin   MCTargetOptions MCOptions;
60f3070dc4SKevin Enderby   // Get the assembler info needed to setup the MCContext.
614b63ca13SMirko Brkusanin   std::unique_ptr<const MCAsmInfo> MAI(
624b63ca13SMirko Brkusanin       TheTarget->createMCAsmInfo(*MRI, TT, MCOptions));
63f536c6afSKevin Enderby   if (!MAI)
64353eda48SCraig Topper     return nullptr;
65f3070dc4SKevin Enderby 
66c9504650SScott Linder   std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
67f536c6afSKevin Enderby   if (!MII)
68353eda48SCraig Topper     return nullptr;
6954bfde79SCraig Topper 
70c9504650SScott Linder   std::unique_ptr<const MCSubtargetInfo> STI(
71c9504650SScott Linder       TheTarget->createMCSubtargetInfo(TT, CPU, Features));
72f536c6afSKevin Enderby   if (!STI)
73353eda48SCraig Topper     return nullptr;
744c493e80SJames Molloy 
75f3070dc4SKevin Enderby   // Set up the MCContext for creating symbols and MCExpr's.
76c2f819afSPhilipp Krones   std::unique_ptr<MCContext> Ctx(
77c2f819afSPhilipp Krones       new MCContext(Triple(TT), MAI.get(), MRI.get(), STI.get()));
78f536c6afSKevin Enderby   if (!Ctx)
79353eda48SCraig Topper     return nullptr;
80f3070dc4SKevin Enderby 
81f3070dc4SKevin Enderby   // Set up disassembler.
82c9504650SScott Linder   std::unique_ptr<MCDisassembler> DisAsm(
83c9504650SScott Linder       TheTarget->createMCDisassembler(*STI, *Ctx));
84f536c6afSKevin Enderby   if (!DisAsm)
85353eda48SCraig Topper     return nullptr;
86ad1084deSAhmed Bougacha 
8756440fd8SAhmed Charles   std::unique_ptr<MCRelocationInfo> RelInfo(
8871b1e5f1SEric Christopher       TheTarget->createMCRelocationInfo(TT, *Ctx));
89ad1084deSAhmed Bougacha   if (!RelInfo)
90353eda48SCraig Topper     return nullptr;
91ad1084deSAhmed Bougacha 
9256440fd8SAhmed Charles   std::unique_ptr<MCSymbolizer> Symbolizer(TheTarget->createMCSymbolizer(
93c9504650SScott Linder       TT, GetOpInfo, SymbolLookUp, DisInfo, Ctx.get(), std::move(RelInfo)));
94df17c83fSAhmed Charles   DisAsm->setSymbolizer(std::move(Symbolizer));
9595400e22SLang Hames 
96f3070dc4SKevin Enderby   // Set up the instruction printer.
97f3070dc4SKevin Enderby   int AsmPrinterVariant = MAI->getAssemblerDialect();
98c9504650SScott Linder   std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
99c9504650SScott Linder       Triple(TT), AsmPrinterVariant, *MAI, *MII, *MRI));
100f536c6afSKevin Enderby   if (!IP)
101353eda48SCraig Topper     return nullptr;
102f3070dc4SKevin Enderby 
103c9504650SScott Linder   LLVMDisasmContext *DC = new LLVMDisasmContext(
104c9504650SScott Linder       TT, DisInfo, TagType, GetOpInfo, SymbolLookUp, TheTarget, std::move(MAI),
105c9504650SScott Linder       std::move(MRI), std::move(STI), std::move(MII), std::move(Ctx),
106c9504650SScott Linder       std::move(DisAsm), std::move(IP));
107f536c6afSKevin Enderby   if (!DC)
108353eda48SCraig Topper     return nullptr;
109233f1301SOwen Anderson 
11076e55579SQuentin Colombet   DC->setCPU(CPU);
111f3070dc4SKevin Enderby   return DC;
112f3070dc4SKevin Enderby }
113f3070dc4SKevin Enderby 
11471b1e5f1SEric Christopher LLVMDisasmContextRef
LLVMCreateDisasmCPU(const char * TT,const char * CPU,void * DisInfo,int TagType,LLVMOpInfoCallback GetOpInfo,LLVMSymbolLookupCallback SymbolLookUp)11571b1e5f1SEric Christopher LLVMCreateDisasmCPU(const char *TT, const char *CPU, void *DisInfo, int TagType,
1167a770755SBradley Smith                     LLVMOpInfoCallback GetOpInfo,
1177a770755SBradley Smith                     LLVMSymbolLookupCallback SymbolLookUp) {
11871b1e5f1SEric Christopher   return LLVMCreateDisasmCPUFeatures(TT, CPU, "", DisInfo, TagType, GetOpInfo,
11971b1e5f1SEric Christopher                                      SymbolLookUp);
1207a770755SBradley Smith }
1217a770755SBradley Smith 
LLVMCreateDisasm(const char * TT,void * DisInfo,int TagType,LLVMOpInfoCallback GetOpInfo,LLVMSymbolLookupCallback SymbolLookUp)12271b1e5f1SEric Christopher LLVMDisasmContextRef LLVMCreateDisasm(const char *TT, void *DisInfo,
1230ca9d5b7SJim Grosbach                                       int TagType, LLVMOpInfoCallback GetOpInfo,
1240ca9d5b7SJim Grosbach                                       LLVMSymbolLookupCallback SymbolLookUp) {
12571b1e5f1SEric Christopher   return LLVMCreateDisasmCPUFeatures(TT, "", "", DisInfo, TagType, GetOpInfo,
12671b1e5f1SEric Christopher                                      SymbolLookUp);
1270ca9d5b7SJim Grosbach }
1280ca9d5b7SJim Grosbach 
129f3070dc4SKevin Enderby //
130f3070dc4SKevin Enderby // LLVMDisasmDispose() disposes of the disassembler specified by the context.
131f3070dc4SKevin Enderby //
LLVMDisasmDispose(LLVMDisasmContextRef DCR)132f3070dc4SKevin Enderby void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
13333d7b762SEugene Zelenko   LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
134f3070dc4SKevin Enderby   delete DC;
135f3070dc4SKevin Enderby }
136f3070dc4SKevin Enderby 
1375f8f34e4SAdrian Prantl /// Emits the comments that are stored in \p DC comment stream.
13893a98aacSQuentin Colombet /// Each comment in the comment stream must end with a newline.
emitComments(LLVMDisasmContext * DC,formatted_raw_ostream & FormattedOS)13993a98aacSQuentin Colombet static void emitComments(LLVMDisasmContext *DC,
14093a98aacSQuentin Colombet                          formatted_raw_ostream &FormattedOS) {
14193a98aacSQuentin Colombet   // Flush the stream before taking its content.
14293a98aacSQuentin Colombet   StringRef Comments = DC->CommentsToEmit.str();
14393a98aacSQuentin Colombet   // Get the default information for printing a comment.
14493a98aacSQuentin Colombet   const MCAsmInfo *MAI = DC->getAsmInfo();
14536d33fc1SMehdi Amini   StringRef CommentBegin = MAI->getCommentString();
14693a98aacSQuentin Colombet   unsigned CommentColumn = MAI->getCommentColumn();
14793a98aacSQuentin Colombet   bool IsFirst = true;
14893a98aacSQuentin Colombet   while (!Comments.empty()) {
14993a98aacSQuentin Colombet     if (!IsFirst)
15093a98aacSQuentin Colombet       FormattedOS << '\n';
15193a98aacSQuentin Colombet     // Emit a line of comments.
15293a98aacSQuentin Colombet     FormattedOS.PadToColumn(CommentColumn);
15393a98aacSQuentin Colombet     size_t Position = Comments.find('\n');
15493a98aacSQuentin Colombet     FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
15593a98aacSQuentin Colombet     // Move after the newline character.
15693a98aacSQuentin Colombet     Comments = Comments.substr(Position+1);
15793a98aacSQuentin Colombet     IsFirst = false;
15893a98aacSQuentin Colombet   }
15993a98aacSQuentin Colombet   FormattedOS.flush();
16093a98aacSQuentin Colombet 
16193a98aacSQuentin Colombet   // Tell the comment stream that the vector changed underneath it.
16293a98aacSQuentin Colombet   DC->CommentsToEmit.clear();
16393a98aacSQuentin Colombet }
16493a98aacSQuentin Colombet 
1655f8f34e4SAdrian Prantl /// Gets latency information for \p Inst from the itinerary
16676e55579SQuentin Colombet /// scheduling model, based on \p DC information.
16776e55579SQuentin Colombet /// \return The maximum expected latency over all the operands or -1
168acf25766SEric Christopher /// if no information is available.
getItineraryLatency(LLVMDisasmContext * DC,const MCInst & Inst)16976e55579SQuentin Colombet static int getItineraryLatency(LLVMDisasmContext *DC, const MCInst &Inst) {
17076e55579SQuentin Colombet   const int NoInformationAvailable = -1;
17176e55579SQuentin Colombet 
17276e55579SQuentin Colombet   // Check if we have a CPU to get the itinerary information.
17376e55579SQuentin Colombet   if (DC->getCPU().empty())
17476e55579SQuentin Colombet     return NoInformationAvailable;
17576e55579SQuentin Colombet 
17676e55579SQuentin Colombet   // Get itinerary information.
17776e55579SQuentin Colombet   const MCSubtargetInfo *STI = DC->getSubtargetInfo();
17876e55579SQuentin Colombet   InstrItineraryData IID = STI->getInstrItineraryForCPU(DC->getCPU());
17976e55579SQuentin Colombet   // Get the scheduling class of the requested instruction.
18076e55579SQuentin Colombet   const MCInstrDesc& Desc = DC->getInstrInfo()->get(Inst.getOpcode());
18176e55579SQuentin Colombet   unsigned SCClass = Desc.getSchedClass();
18276e55579SQuentin Colombet 
18376e55579SQuentin Colombet   int Latency = 0;
18476e55579SQuentin Colombet   for (unsigned OpIdx = 0, OpIdxEnd = Inst.getNumOperands(); OpIdx != OpIdxEnd;
18576e55579SQuentin Colombet        ++OpIdx)
18676e55579SQuentin Colombet     Latency = std::max(Latency, IID.getOperandCycle(SCClass, OpIdx));
18776e55579SQuentin Colombet 
18876e55579SQuentin Colombet   return Latency;
18976e55579SQuentin Colombet }
19076e55579SQuentin Colombet 
1915f8f34e4SAdrian Prantl /// Gets latency information for \p Inst, based on \p DC information.
1925f09cb0dSQuentin Colombet /// \return The maximum expected latency over all the definitions or -1
193acf25766SEric Christopher /// if no information is available.
getLatency(LLVMDisasmContext * DC,const MCInst & Inst)1945f09cb0dSQuentin Colombet static int getLatency(LLVMDisasmContext *DC, const MCInst &Inst) {
1955f09cb0dSQuentin Colombet   // Try to compute scheduling information.
1965f09cb0dSQuentin Colombet   const MCSubtargetInfo *STI = DC->getSubtargetInfo();
19711759457SPete Cooper   const MCSchedModel SCModel = STI->getSchedModel();
1985f09cb0dSQuentin Colombet   const int NoInformationAvailable = -1;
1995f09cb0dSQuentin Colombet 
2005f09cb0dSQuentin Colombet   // Check if we have a scheduling model for instructions.
20111759457SPete Cooper   if (!SCModel.hasInstrSchedModel())
20211759457SPete Cooper     // Try to fall back to the itinerary model if the scheduling model doesn't
20311759457SPete Cooper     // have a scheduling table.  Note the default does not have a table.
20476e55579SQuentin Colombet     return getItineraryLatency(DC, Inst);
2055f09cb0dSQuentin Colombet 
2065f09cb0dSQuentin Colombet   // Get the scheduling class of the requested instruction.
2075f09cb0dSQuentin Colombet   const MCInstrDesc& Desc = DC->getInstrInfo()->get(Inst.getOpcode());
2085f09cb0dSQuentin Colombet   unsigned SCClass = Desc.getSchedClass();
20911759457SPete Cooper   const MCSchedClassDesc *SCDesc = SCModel.getSchedClassDesc(SCClass);
210c3665045SQuentin Colombet   // Resolving the variant SchedClass requires an MI to pass to
211c3665045SQuentin Colombet   // SubTargetInfo::resolveSchedClass.
212c3665045SQuentin Colombet   if (!SCDesc || !SCDesc->isValid() || SCDesc->isVariant())
2135f09cb0dSQuentin Colombet     return NoInformationAvailable;
2145f09cb0dSQuentin Colombet 
2155f09cb0dSQuentin Colombet   // Compute output latency.
216b941ababSBenjamin Kramer   int16_t Latency = 0;
2175f09cb0dSQuentin Colombet   for (unsigned DefIdx = 0, DefEnd = SCDesc->NumWriteLatencyEntries;
2185f09cb0dSQuentin Colombet        DefIdx != DefEnd; ++DefIdx) {
2195f09cb0dSQuentin Colombet     // Lookup the definition's write latency in SubtargetInfo.
2205f09cb0dSQuentin Colombet     const MCWriteLatencyEntry *WLEntry = STI->getWriteLatencyEntry(SCDesc,
2215f09cb0dSQuentin Colombet                                                                    DefIdx);
2225f09cb0dSQuentin Colombet     Latency = std::max(Latency, WLEntry->Cycles);
2235f09cb0dSQuentin Colombet   }
2245f09cb0dSQuentin Colombet 
2255f09cb0dSQuentin Colombet   return Latency;
2265f09cb0dSQuentin Colombet }
2275f09cb0dSQuentin Colombet 
2285f8f34e4SAdrian Prantl /// Emits latency information in DC->CommentStream for \p Inst, based
2295f09cb0dSQuentin Colombet /// on the information available in \p DC.
emitLatency(LLVMDisasmContext * DC,const MCInst & Inst)2305f09cb0dSQuentin Colombet static void emitLatency(LLVMDisasmContext *DC, const MCInst &Inst) {
2315f09cb0dSQuentin Colombet   int Latency = getLatency(DC, Inst);
2325f09cb0dSQuentin Colombet 
233acf25766SEric Christopher   // Report only interesting latencies.
2345f09cb0dSQuentin Colombet   if (Latency < 2)
2355f09cb0dSQuentin Colombet     return;
2365f09cb0dSQuentin Colombet 
2375f09cb0dSQuentin Colombet   DC->CommentStream << "Latency: " << Latency << '\n';
2385f09cb0dSQuentin Colombet }
2395f09cb0dSQuentin Colombet 
240f3070dc4SKevin Enderby //
24126e7768fSBenjamin Kramer // LLVMDisasmInstruction() disassembles a single instruction using the
242f3070dc4SKevin Enderby // disassembler context specified in the parameter DC.  The bytes of the
24326e7768fSBenjamin Kramer // instruction are specified in the parameter Bytes, and contains at least
244f3070dc4SKevin Enderby // BytesSize number of bytes.  The instruction is at the address specified by
245f3070dc4SKevin Enderby // the PC parameter.  If a valid instruction can be disassembled its string is
246f3070dc4SKevin Enderby // returned indirectly in OutString which whos size is specified in the
247f3070dc4SKevin Enderby // parameter OutStringSize.  This function returns the number of bytes in the
248f3070dc4SKevin Enderby // instruction or zero if there was no valid instruction.  If this function
249f3070dc4SKevin Enderby // returns zero the caller will have to pick how many bytes they want to step
250f3070dc4SKevin Enderby // over by printing a .byte, .long etc. to continue.
251f3070dc4SKevin Enderby //
LLVMDisasmInstruction(LLVMDisasmContextRef DCR,uint8_t * Bytes,uint64_t BytesSize,uint64_t PC,char * OutString,size_t OutStringSize)252f3070dc4SKevin Enderby size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
253f3070dc4SKevin Enderby                              uint64_t BytesSize, uint64_t PC, char *OutString,
254f3070dc4SKevin Enderby                              size_t OutStringSize){
25533d7b762SEugene Zelenko   LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
256f3070dc4SKevin Enderby   // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
2577fc5b874SRafael Espindola   ArrayRef<uint8_t> Data(Bytes, BytesSize);
258f3070dc4SKevin Enderby 
259f3070dc4SKevin Enderby   uint64_t Size;
260f3070dc4SKevin Enderby   MCInst Inst;
261f3070dc4SKevin Enderby   const MCDisassembler *DisAsm = DC->getDisAsm();
262f3070dc4SKevin Enderby   MCInstPrinter *IP = DC->getIP();
2635ada2a7eSJames Molloy   MCDisassembler::DecodeStatus S;
264e69170a1SAlp Toker   SmallVector<char, 64> InsnStr;
265e69170a1SAlp Toker   raw_svector_ostream Annotations(InsnStr);
2666fdd6a7bSFangrui Song   S = DisAsm->getInstruction(Inst, Size, Data, PC, Annotations);
2675ada2a7eSJames Molloy   switch (S) {
2685ada2a7eSJames Molloy   case MCDisassembler::Fail:
2695ada2a7eSJames Molloy   case MCDisassembler::SoftFail:
270db4ce603SJames Molloy     // FIXME: Do something different for soft failure modes?
271f3070dc4SKevin Enderby     return 0;
2722e2d75fbSNick Lewycky 
2735ada2a7eSJames Molloy   case MCDisassembler::Success: {
274e69170a1SAlp Toker     StringRef AnnotationsStr = Annotations.str();
275e69170a1SAlp Toker 
276a0c3b972SOwen Anderson     SmallVector<char, 64> InsnStr;
277a0c3b972SOwen Anderson     raw_svector_ostream OS(InsnStr);
27893a98aacSQuentin Colombet     formatted_raw_ostream FormattedOS(OS);
279aa708763SFangrui Song     IP->printInst(&Inst, PC, AnnotationsStr, *DC->getSubtargetInfo(),
280aa708763SFangrui Song                   FormattedOS);
281233f1301SOwen Anderson 
2825f09cb0dSQuentin Colombet     if (DC->getOptions() & LLVMDisassembler_Option_PrintLatency)
2835f09cb0dSQuentin Colombet       emitLatency(DC, Inst);
2845f09cb0dSQuentin Colombet 
28593a98aacSQuentin Colombet     emitComments(DC, FormattedOS);
286233f1301SOwen Anderson 
2870388fb0eSChris Lattner     assert(OutStringSize != 0 && "Output buffer cannot be zero size");
28826e7768fSBenjamin Kramer     size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
28926e7768fSBenjamin Kramer     std::memcpy(OutString, InsnStr.data(), OutputSize);
29026e7768fSBenjamin Kramer     OutString[OutputSize] = '\0'; // Terminate string.
29126e7768fSBenjamin Kramer 
292f3070dc4SKevin Enderby     return Size;
293f3070dc4SKevin Enderby   }
2945ada2a7eSJames Molloy   }
29546a9f016SDavid Blaikie   llvm_unreachable("Invalid DecodeStatus!");
2965ada2a7eSJames Molloy }
29762183c4eSKevin Enderby 
29862183c4eSKevin Enderby //
29962183c4eSKevin Enderby // LLVMSetDisasmOptions() sets the disassembler's options.  It returns 1 if it
30062183c4eSKevin Enderby // can set all the Options and 0 otherwise.
30162183c4eSKevin Enderby //
LLVMSetDisasmOptions(LLVMDisasmContextRef DCR,uint64_t Options)30262183c4eSKevin Enderby int LLVMSetDisasmOptions(LLVMDisasmContextRef DCR, uint64_t Options){
30362183c4eSKevin Enderby   if (Options & LLVMDisassembler_Option_UseMarkup){
30433d7b762SEugene Zelenko       LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
30562183c4eSKevin Enderby       MCInstPrinter *IP = DC->getIP();
30633d7b762SEugene Zelenko       IP->setUseMarkup(true);
3075f09cb0dSQuentin Colombet       DC->addOptions(LLVMDisassembler_Option_UseMarkup);
30862183c4eSKevin Enderby       Options &= ~LLVMDisassembler_Option_UseMarkup;
30962183c4eSKevin Enderby   }
310168ffb36SKevin Enderby   if (Options & LLVMDisassembler_Option_PrintImmHex){
31133d7b762SEugene Zelenko       LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
312168ffb36SKevin Enderby       MCInstPrinter *IP = DC->getIP();
31333d7b762SEugene Zelenko       IP->setPrintImmHex(true);
3145f09cb0dSQuentin Colombet       DC->addOptions(LLVMDisassembler_Option_PrintImmHex);
315168ffb36SKevin Enderby       Options &= ~LLVMDisassembler_Option_PrintImmHex;
316168ffb36SKevin Enderby   }
31785cf5315SKevin Enderby   if (Options & LLVMDisassembler_Option_AsmPrinterVariant){
31833d7b762SEugene Zelenko       LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
31985cf5315SKevin Enderby       // Try to set up the new instruction printer.
32085cf5315SKevin Enderby       const MCAsmInfo *MAI = DC->getAsmInfo();
32185cf5315SKevin Enderby       const MCInstrInfo *MII = DC->getInstrInfo();
32285cf5315SKevin Enderby       const MCRegisterInfo *MRI = DC->getRegisterInfo();
32385cf5315SKevin Enderby       int AsmPrinterVariant = MAI->getAssemblerDialect();
32485cf5315SKevin Enderby       AsmPrinterVariant = AsmPrinterVariant == 0 ? 1 : 0;
32585cf5315SKevin Enderby       MCInstPrinter *IP = DC->getTarget()->createMCInstPrinter(
32650f17235SDaniel Sanders           Triple(DC->getTripleName()), AsmPrinterVariant, *MAI, *MII, *MRI);
32785cf5315SKevin Enderby       if (IP) {
32885cf5315SKevin Enderby         DC->setIP(IP);
3295f09cb0dSQuentin Colombet         DC->addOptions(LLVMDisassembler_Option_AsmPrinterVariant);
33085cf5315SKevin Enderby         Options &= ~LLVMDisassembler_Option_AsmPrinterVariant;
33185cf5315SKevin Enderby       }
33285cf5315SKevin Enderby   }
33393a98aacSQuentin Colombet   if (Options & LLVMDisassembler_Option_SetInstrComments) {
33433d7b762SEugene Zelenko     LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
33593a98aacSQuentin Colombet     MCInstPrinter *IP = DC->getIP();
33693a98aacSQuentin Colombet     IP->setCommentStream(DC->CommentStream);
3375f09cb0dSQuentin Colombet     DC->addOptions(LLVMDisassembler_Option_SetInstrComments);
33893a98aacSQuentin Colombet     Options &= ~LLVMDisassembler_Option_SetInstrComments;
33993a98aacSQuentin Colombet   }
3405f09cb0dSQuentin Colombet   if (Options & LLVMDisassembler_Option_PrintLatency) {
34133d7b762SEugene Zelenko     LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
3425f09cb0dSQuentin Colombet     DC->addOptions(LLVMDisassembler_Option_PrintLatency);
3435f09cb0dSQuentin Colombet     Options &= ~LLVMDisassembler_Option_PrintLatency;
3445f09cb0dSQuentin Colombet   }
34562183c4eSKevin Enderby   return (Options == 0);
34662183c4eSKevin Enderby }
347