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