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