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