1 //===-- lib/MC/Disassembler.cpp - Disassembler Public C Interface ---------===//
2 //
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 //===----------------------------------------------------------------------===//
9 
10 #include "Disassembler.h"
11 #include "llvm-c/Disassembler.h"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCDisassembler.h"
15 #include "llvm/MC/MCInst.h"
16 #include "llvm/MC/MCInstPrinter.h"
17 #include "llvm/MC/MCInstrInfo.h"
18 #include "llvm/MC/MCRegisterInfo.h"
19 #include "llvm/MC/MCRelocationInfo.h"
20 #include "llvm/MC/MCSubtargetInfo.h"
21 #include "llvm/MC/MCSymbolizer.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/FormattedStream.h"
24 #include "llvm/Support/TargetRegistry.h"
25 
26 using namespace llvm;
27 
28 // LLVMCreateDisasm() creates a disassembler for the TripleName.  Symbolic
29 // disassembly is supported by passing a block of information in the DisInfo
30 // parameter and specifying the TagType and callback functions as described in
31 // the header llvm-c/Disassembler.h .  The pointer to the block and the
32 // functions can all be passed as NULL.  If successful, this returns a
33 // disassembler context.  If not, it returns NULL.
34 //
35 LLVMDisasmContextRef
LLVMCreateDisasmCPUFeatures(const char * TT,const char * CPU,const char * Features,void * DisInfo,int TagType,LLVMOpInfoCallback GetOpInfo,LLVMSymbolLookupCallback SymbolLookUp)36 LLVMCreateDisasmCPUFeatures(const char *TT, const char *CPU,
37                             const char *Features, void *DisInfo, int TagType,
38                             LLVMOpInfoCallback GetOpInfo,
39                             LLVMSymbolLookupCallback SymbolLookUp) {
40   // Get the target.
41   std::string Error;
42   const Target *TheTarget = TargetRegistry::lookupTarget(TT, Error);
43   if (!TheTarget)
44     return nullptr;
45 
46   const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TT);
47   if (!MRI)
48     return nullptr;
49 
50   // Get the assembler info needed to setup the MCContext.
51   const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(*MRI, TT);
52   if (!MAI)
53     return nullptr;
54 
55   const MCInstrInfo *MII = TheTarget->createMCInstrInfo();
56   if (!MII)
57     return nullptr;
58 
59   const MCSubtargetInfo *STI =
60       TheTarget->createMCSubtargetInfo(TT, CPU, Features);
61   if (!STI)
62     return nullptr;
63 
64   // Set up the MCContext for creating symbols and MCExpr's.
65   MCContext *Ctx = new MCContext(MAI, MRI, nullptr);
66   if (!Ctx)
67     return nullptr;
68 
69   // Set up disassembler.
70   MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI, *Ctx);
71   if (!DisAsm)
72     return nullptr;
73 
74   std::unique_ptr<MCRelocationInfo> RelInfo(
75       TheTarget->createMCRelocationInfo(TT, *Ctx));
76   if (!RelInfo)
77     return nullptr;
78 
79   std::unique_ptr<MCSymbolizer> Symbolizer(TheTarget->createMCSymbolizer(
80       TT, GetOpInfo, SymbolLookUp, DisInfo, Ctx, std::move(RelInfo)));
81   DisAsm->setSymbolizer(std::move(Symbolizer));
82 
83   // Set up the instruction printer.
84   int AsmPrinterVariant = MAI->getAssemblerDialect();
85   MCInstPrinter *IP = TheTarget->createMCInstPrinter(
86       Triple(TT), AsmPrinterVariant, *MAI, *MII, *MRI);
87   if (!IP)
88     return nullptr;
89 
90   LLVMDisasmContext *DC =
91       new LLVMDisasmContext(TT, DisInfo, TagType, GetOpInfo, SymbolLookUp,
92                             TheTarget, MAI, MRI, STI, MII, Ctx, DisAsm, IP);
93   if (!DC)
94     return nullptr;
95 
96   DC->setCPU(CPU);
97   return DC;
98 }
99 
100 LLVMDisasmContextRef
LLVMCreateDisasmCPU(const char * TT,const char * CPU,void * DisInfo,int TagType,LLVMOpInfoCallback GetOpInfo,LLVMSymbolLookupCallback SymbolLookUp)101 LLVMCreateDisasmCPU(const char *TT, const char *CPU, void *DisInfo, int TagType,
102                     LLVMOpInfoCallback GetOpInfo,
103                     LLVMSymbolLookupCallback SymbolLookUp) {
104   return LLVMCreateDisasmCPUFeatures(TT, CPU, "", DisInfo, TagType, GetOpInfo,
105                                      SymbolLookUp);
106 }
107 
LLVMCreateDisasm(const char * TT,void * DisInfo,int TagType,LLVMOpInfoCallback GetOpInfo,LLVMSymbolLookupCallback SymbolLookUp)108 LLVMDisasmContextRef LLVMCreateDisasm(const char *TT, void *DisInfo,
109                                       int TagType, LLVMOpInfoCallback GetOpInfo,
110                                       LLVMSymbolLookupCallback SymbolLookUp) {
111   return LLVMCreateDisasmCPUFeatures(TT, "", "", DisInfo, TagType, GetOpInfo,
112                                      SymbolLookUp);
113 }
114 
115 //
116 // LLVMDisasmDispose() disposes of the disassembler specified by the context.
117 //
LLVMDisasmDispose(LLVMDisasmContextRef DCR)118 void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
119   LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
120   delete DC;
121 }
122 
123 /// \brief Emits the comments that are stored in \p DC comment stream.
124 /// Each comment in the comment stream must end with a newline.
emitComments(LLVMDisasmContext * DC,formatted_raw_ostream & FormattedOS)125 static void emitComments(LLVMDisasmContext *DC,
126                          formatted_raw_ostream &FormattedOS) {
127   // Flush the stream before taking its content.
128   DC->CommentStream.flush();
129   StringRef Comments = DC->CommentsToEmit.str();
130   // Get the default information for printing a comment.
131   const MCAsmInfo *MAI = DC->getAsmInfo();
132   const char *CommentBegin = MAI->getCommentString();
133   unsigned CommentColumn = MAI->getCommentColumn();
134   bool IsFirst = true;
135   while (!Comments.empty()) {
136     if (!IsFirst)
137       FormattedOS << '\n';
138     // Emit a line of comments.
139     FormattedOS.PadToColumn(CommentColumn);
140     size_t Position = Comments.find('\n');
141     FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
142     // Move after the newline character.
143     Comments = Comments.substr(Position+1);
144     IsFirst = false;
145   }
146   FormattedOS.flush();
147 
148   // Tell the comment stream that the vector changed underneath it.
149   DC->CommentsToEmit.clear();
150   DC->CommentStream.resync();
151 }
152 
153 /// \brief Gets latency information for \p Inst from the itinerary
154 /// scheduling model, based on \p DC information.
155 /// \return The maximum expected latency over all the operands or -1
156 /// if no information is available.
getItineraryLatency(LLVMDisasmContext * DC,const MCInst & Inst)157 static int getItineraryLatency(LLVMDisasmContext *DC, const MCInst &Inst) {
158   const int NoInformationAvailable = -1;
159 
160   // Check if we have a CPU to get the itinerary information.
161   if (DC->getCPU().empty())
162     return NoInformationAvailable;
163 
164   // Get itinerary information.
165   const MCSubtargetInfo *STI = DC->getSubtargetInfo();
166   InstrItineraryData IID = STI->getInstrItineraryForCPU(DC->getCPU());
167   // Get the scheduling class of the requested instruction.
168   const MCInstrDesc& Desc = DC->getInstrInfo()->get(Inst.getOpcode());
169   unsigned SCClass = Desc.getSchedClass();
170 
171   int Latency = 0;
172   for (unsigned OpIdx = 0, OpIdxEnd = Inst.getNumOperands(); OpIdx != OpIdxEnd;
173        ++OpIdx)
174     Latency = std::max(Latency, IID.getOperandCycle(SCClass, OpIdx));
175 
176   return Latency;
177 }
178 
179 /// \brief Gets latency information for \p Inst, based on \p DC information.
180 /// \return The maximum expected latency over all the definitions or -1
181 /// if no information is available.
getLatency(LLVMDisasmContext * DC,const MCInst & Inst)182 static int getLatency(LLVMDisasmContext *DC, const MCInst &Inst) {
183   // Try to compute scheduling information.
184   const MCSubtargetInfo *STI = DC->getSubtargetInfo();
185   const MCSchedModel SCModel = STI->getSchedModel();
186   const int NoInformationAvailable = -1;
187 
188   // Check if we have a scheduling model for instructions.
189   if (!SCModel.hasInstrSchedModel())
190     // Try to fall back to the itinerary model if the scheduling model doesn't
191     // have a scheduling table.  Note the default does not have a table.
192     return getItineraryLatency(DC, Inst);
193 
194   // Get the scheduling class of the requested instruction.
195   const MCInstrDesc& Desc = DC->getInstrInfo()->get(Inst.getOpcode());
196   unsigned SCClass = Desc.getSchedClass();
197   const MCSchedClassDesc *SCDesc = SCModel.getSchedClassDesc(SCClass);
198   // Resolving the variant SchedClass requires an MI to pass to
199   // SubTargetInfo::resolveSchedClass.
200   if (!SCDesc || !SCDesc->isValid() || SCDesc->isVariant())
201     return NoInformationAvailable;
202 
203   // Compute output latency.
204   int Latency = 0;
205   for (unsigned DefIdx = 0, DefEnd = SCDesc->NumWriteLatencyEntries;
206        DefIdx != DefEnd; ++DefIdx) {
207     // Lookup the definition's write latency in SubtargetInfo.
208     const MCWriteLatencyEntry *WLEntry = STI->getWriteLatencyEntry(SCDesc,
209                                                                    DefIdx);
210     Latency = std::max(Latency, WLEntry->Cycles);
211   }
212 
213   return Latency;
214 }
215 
216 
217 /// \brief Emits latency information in DC->CommentStream for \p Inst, based
218 /// on the information available in \p DC.
emitLatency(LLVMDisasmContext * DC,const MCInst & Inst)219 static void emitLatency(LLVMDisasmContext *DC, const MCInst &Inst) {
220   int Latency = getLatency(DC, Inst);
221 
222   // Report only interesting latencies.
223   if (Latency < 2)
224     return;
225 
226   DC->CommentStream << "Latency: " << Latency << '\n';
227 }
228 
229 //
230 // LLVMDisasmInstruction() disassembles a single instruction using the
231 // disassembler context specified in the parameter DC.  The bytes of the
232 // instruction are specified in the parameter Bytes, and contains at least
233 // BytesSize number of bytes.  The instruction is at the address specified by
234 // the PC parameter.  If a valid instruction can be disassembled its string is
235 // returned indirectly in OutString which whos size is specified in the
236 // parameter OutStringSize.  This function returns the number of bytes in the
237 // instruction or zero if there was no valid instruction.  If this function
238 // returns zero the caller will have to pick how many bytes they want to step
239 // over by printing a .byte, .long etc. to continue.
240 //
LLVMDisasmInstruction(LLVMDisasmContextRef DCR,uint8_t * Bytes,uint64_t BytesSize,uint64_t PC,char * OutString,size_t OutStringSize)241 size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
242                              uint64_t BytesSize, uint64_t PC, char *OutString,
243                              size_t OutStringSize){
244   LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
245   // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
246   ArrayRef<uint8_t> Data(Bytes, BytesSize);
247 
248   uint64_t Size;
249   MCInst Inst;
250   const MCDisassembler *DisAsm = DC->getDisAsm();
251   MCInstPrinter *IP = DC->getIP();
252   MCDisassembler::DecodeStatus S;
253   SmallVector<char, 64> InsnStr;
254   raw_svector_ostream Annotations(InsnStr);
255   S = DisAsm->getInstruction(Inst, Size, Data, PC,
256                              /*REMOVE*/ nulls(), Annotations);
257   switch (S) {
258   case MCDisassembler::Fail:
259   case MCDisassembler::SoftFail:
260     // FIXME: Do something different for soft failure modes?
261     return 0;
262 
263   case MCDisassembler::Success: {
264     Annotations.flush();
265     StringRef AnnotationsStr = Annotations.str();
266 
267     SmallVector<char, 64> InsnStr;
268     raw_svector_ostream OS(InsnStr);
269     formatted_raw_ostream FormattedOS(OS);
270     IP->printInst(&Inst, FormattedOS, AnnotationsStr, *DC->getSubtargetInfo());
271 
272     if (DC->getOptions() & LLVMDisassembler_Option_PrintLatency)
273       emitLatency(DC, Inst);
274 
275     emitComments(DC, FormattedOS);
276     OS.flush();
277 
278     assert(OutStringSize != 0 && "Output buffer cannot be zero size");
279     size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
280     std::memcpy(OutString, InsnStr.data(), OutputSize);
281     OutString[OutputSize] = '\0'; // Terminate string.
282 
283     return Size;
284   }
285   }
286   llvm_unreachable("Invalid DecodeStatus!");
287 }
288 
289 //
290 // LLVMSetDisasmOptions() sets the disassembler's options.  It returns 1 if it
291 // can set all the Options and 0 otherwise.
292 //
LLVMSetDisasmOptions(LLVMDisasmContextRef DCR,uint64_t Options)293 int LLVMSetDisasmOptions(LLVMDisasmContextRef DCR, uint64_t Options){
294   if (Options & LLVMDisassembler_Option_UseMarkup){
295       LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
296       MCInstPrinter *IP = DC->getIP();
297       IP->setUseMarkup(1);
298       DC->addOptions(LLVMDisassembler_Option_UseMarkup);
299       Options &= ~LLVMDisassembler_Option_UseMarkup;
300   }
301   if (Options & LLVMDisassembler_Option_PrintImmHex){
302       LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
303       MCInstPrinter *IP = DC->getIP();
304       IP->setPrintImmHex(1);
305       DC->addOptions(LLVMDisassembler_Option_PrintImmHex);
306       Options &= ~LLVMDisassembler_Option_PrintImmHex;
307   }
308   if (Options & LLVMDisassembler_Option_AsmPrinterVariant){
309       LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
310       // Try to set up the new instruction printer.
311       const MCAsmInfo *MAI = DC->getAsmInfo();
312       const MCInstrInfo *MII = DC->getInstrInfo();
313       const MCRegisterInfo *MRI = DC->getRegisterInfo();
314       int AsmPrinterVariant = MAI->getAssemblerDialect();
315       AsmPrinterVariant = AsmPrinterVariant == 0 ? 1 : 0;
316       MCInstPrinter *IP = DC->getTarget()->createMCInstPrinter(
317           Triple(DC->getTripleName()), AsmPrinterVariant, *MAI, *MII, *MRI);
318       if (IP) {
319         DC->setIP(IP);
320         DC->addOptions(LLVMDisassembler_Option_AsmPrinterVariant);
321         Options &= ~LLVMDisassembler_Option_AsmPrinterVariant;
322       }
323   }
324   if (Options & LLVMDisassembler_Option_SetInstrComments) {
325     LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
326     MCInstPrinter *IP = DC->getIP();
327     IP->setCommentStream(DC->CommentStream);
328     DC->addOptions(LLVMDisassembler_Option_SetInstrComments);
329     Options &= ~LLVMDisassembler_Option_SetInstrComments;
330   }
331   if (Options & LLVMDisassembler_Option_PrintLatency) {
332     LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
333     DC->addOptions(LLVMDisassembler_Option_PrintLatency);
334     Options &= ~LLVMDisassembler_Option_PrintLatency;
335   }
336   return (Options == 0);
337 }
338