2011-09-01 21:09:04 +00:00
|
|
|
//===-- lib/MC/Disassembler.cpp - Disassembler Public C Interface ---------===//
|
2011-03-28 18:25:07 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2011-05-22 04:52:24 +00:00
|
|
|
|
2011-03-28 18:25:07 +00:00
|
|
|
#include "Disassembler.h"
|
|
|
|
#include "llvm-c/Disassembler.h"
|
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2011-07-20 06:54:19 +00:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2011-03-28 18:25:07 +00:00
|
|
|
#include "llvm/MC/MCDisassembler.h"
|
|
|
|
#include "llvm/MC/MCInst.h"
|
|
|
|
#include "llvm/MC/MCInstPrinter.h"
|
2012-04-06 18:21:09 +00:00
|
|
|
#include "llvm/MC/MCInstrInfo.h"
|
2011-07-20 06:54:19 +00:00
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
Add MCSymbolizer for symbolic/annotated disassembly.
This is a basic first step towards symbolization of disassembled
instructions. This used to be done using externally provided (C API)
callbacks. This patch introduces:
- the MCSymbolizer class, that mimics the same functions that were used
in the X86 and ARM disassemblers to symbolize immediate operands and
to annotate loads based off PC (for things like c string literals).
- the MCExternalSymbolizer class, which implements the old C API.
- the MCRelocationInfo class, which provides a way for targets to
translate relocations (either object::RelocationRef, or disassembler
C API VariantKinds) to MCExprs.
- the MCObjectSymbolizer class, which does symbolization using what it
finds in an object::ObjectFile. This makes simple symbolization (with
no fancy relocation stuff) work for all object formats!
- x86-64 Mach-O and ELF MCRelocationInfos.
- A basic ARM Mach-O MCRelocationInfo, that provides just enough to
support the C API VariantKinds.
Most of what works in otool (the only user of the old symbolization API
that I know of) for x86-64 symbolic disassembly (-tvV) works, namely:
- symbol references: call _foo; jmp 15 <_foo+50>
- relocations: call _foo-_bar; call _foo-4
- __cf?string: leaq 193(%rip), %rax ## literal pool for "hello"
Stub support is the main missing part (because libObject doesn't know,
among other things, about mach-o indirect symbols).
As for the MCSymbolizer API, instead of relying on the disassemblers
to call the tryAdding* methods, maybe this could be done automagically
using InstrInfo? For instance, even though PC-relative LEAs are used
to get the address of string literals in a typical Mach-O file, a MOV
would be used in an ELF file. And right now, the explicit symbolization
only recognizes PC-relative LEAs. InstrInfo should have already have
most of what is needed to know what to symbolize, so this can
definitely be improved.
I'd also like to remove object::RelocationRef::getValueString (it seems
only used by relocation printing in objdump), as simply printing the
created MCExpr is definitely enough (and cleaner than string concats).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@182625 91177308-0d34-0410-b5e6-96231b3b80d8
2013-05-24 00:39:57 +00:00
|
|
|
#include "llvm/MC/MCRelocationInfo.h"
|
2012-04-06 18:21:09 +00:00
|
|
|
#include "llvm/MC/MCSubtargetInfo.h"
|
2013-05-24 22:51:52 +00:00
|
|
|
#include "llvm/MC/MCSymbolizer.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2013-10-01 22:14:56 +00:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2011-03-28 18:25:07 +00:00
|
|
|
#include "llvm/Support/MemoryObject.h"
|
2011-08-24 18:08:43 +00:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2011-03-28 18:25:07 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
// LLVMCreateDisasm() creates a disassembler for the TripleName. Symbolic
|
|
|
|
// disassembly is supported by passing a block of information in the DisInfo
|
2011-05-22 04:52:24 +00:00
|
|
|
// parameter and specifying the TagType and callback functions as described in
|
2011-03-28 18:25:07 +00:00
|
|
|
// the header llvm-c/Disassembler.h . The pointer to the block and the
|
2011-05-22 04:52:24 +00:00
|
|
|
// functions can all be passed as NULL. If successful, this returns a
|
|
|
|
// disassembler context. If not, it returns NULL.
|
2011-03-28 18:25:07 +00:00
|
|
|
//
|
2012-12-07 23:53:27 +00:00
|
|
|
LLVMDisasmContextRef LLVMCreateDisasmCPU(const char *Triple, const char *CPU,
|
|
|
|
void *DisInfo, int TagType,
|
|
|
|
LLVMOpInfoCallback GetOpInfo,
|
|
|
|
LLVMSymbolLookupCallback SymbolLookUp){
|
2011-03-28 18:25:07 +00:00
|
|
|
// Get the target.
|
|
|
|
std::string Error;
|
2012-12-07 23:53:27 +00:00
|
|
|
const Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
|
2013-05-23 00:32:34 +00:00
|
|
|
if (!TheTarget)
|
2014-04-24 06:44:33 +00:00
|
|
|
return nullptr;
|
2011-03-28 18:25:07 +00:00
|
|
|
|
2013-05-13 01:16:13 +00:00
|
|
|
const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(Triple);
|
|
|
|
if (!MRI)
|
2014-04-24 06:44:33 +00:00
|
|
|
return nullptr;
|
2013-05-13 01:16:13 +00:00
|
|
|
|
2011-03-28 18:25:07 +00:00
|
|
|
// Get the assembler info needed to setup the MCContext.
|
2013-05-13 01:16:13 +00:00
|
|
|
const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(*MRI, Triple);
|
2013-03-12 18:12:17 +00:00
|
|
|
if (!MAI)
|
2014-04-24 06:44:33 +00:00
|
|
|
return nullptr;
|
2011-03-28 18:25:07 +00:00
|
|
|
|
2012-04-02 06:09:36 +00:00
|
|
|
const MCInstrInfo *MII = TheTarget->createMCInstrInfo();
|
2013-03-12 18:12:17 +00:00
|
|
|
if (!MII)
|
2014-04-24 06:44:33 +00:00
|
|
|
return nullptr;
|
2012-04-02 06:09:36 +00:00
|
|
|
|
2011-03-28 18:25:07 +00:00
|
|
|
// Package up features to be passed to target/subtarget
|
|
|
|
std::string FeaturesStr;
|
|
|
|
|
2012-12-07 23:53:27 +00:00
|
|
|
const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(Triple, CPU,
|
2011-09-07 17:24:38 +00:00
|
|
|
FeaturesStr);
|
2013-03-12 18:12:17 +00:00
|
|
|
if (!STI)
|
2014-04-24 06:44:33 +00:00
|
|
|
return nullptr;
|
2011-09-07 17:24:38 +00:00
|
|
|
|
2011-03-28 18:25:07 +00:00
|
|
|
// Set up the MCContext for creating symbols and MCExpr's.
|
2014-04-24 06:44:33 +00:00
|
|
|
MCContext *Ctx = new MCContext(MAI, MRI, nullptr);
|
2013-03-12 18:12:17 +00:00
|
|
|
if (!Ctx)
|
2014-04-24 06:44:33 +00:00
|
|
|
return nullptr;
|
2011-03-28 18:25:07 +00:00
|
|
|
|
|
|
|
// Set up disassembler.
|
2014-04-15 04:40:56 +00:00
|
|
|
MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI, *Ctx);
|
2013-03-12 18:12:17 +00:00
|
|
|
if (!DisAsm)
|
2014-04-24 06:44:33 +00:00
|
|
|
return nullptr;
|
Add MCSymbolizer for symbolic/annotated disassembly.
This is a basic first step towards symbolization of disassembled
instructions. This used to be done using externally provided (C API)
callbacks. This patch introduces:
- the MCSymbolizer class, that mimics the same functions that were used
in the X86 and ARM disassemblers to symbolize immediate operands and
to annotate loads based off PC (for things like c string literals).
- the MCExternalSymbolizer class, which implements the old C API.
- the MCRelocationInfo class, which provides a way for targets to
translate relocations (either object::RelocationRef, or disassembler
C API VariantKinds) to MCExprs.
- the MCObjectSymbolizer class, which does symbolization using what it
finds in an object::ObjectFile. This makes simple symbolization (with
no fancy relocation stuff) work for all object formats!
- x86-64 Mach-O and ELF MCRelocationInfos.
- A basic ARM Mach-O MCRelocationInfo, that provides just enough to
support the C API VariantKinds.
Most of what works in otool (the only user of the old symbolization API
that I know of) for x86-64 symbolic disassembly (-tvV) works, namely:
- symbol references: call _foo; jmp 15 <_foo+50>
- relocations: call _foo-_bar; call _foo-4
- __cf?string: leaq 193(%rip), %rax ## literal pool for "hello"
Stub support is the main missing part (because libObject doesn't know,
among other things, about mach-o indirect symbols).
As for the MCSymbolizer API, instead of relying on the disassemblers
to call the tryAdding* methods, maybe this could be done automagically
using InstrInfo? For instance, even though PC-relative LEAs are used
to get the address of string literals in a typical Mach-O file, a MOV
would be used in an ELF file. And right now, the explicit symbolization
only recognizes PC-relative LEAs. InstrInfo should have already have
most of what is needed to know what to symbolize, so this can
definitely be improved.
I'd also like to remove object::RelocationRef::getValueString (it seems
only used by relocation printing in objdump), as simply printing the
created MCExpr is definitely enough (and cleaner than string concats).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@182625 91177308-0d34-0410-b5e6-96231b3b80d8
2013-05-24 00:39:57 +00:00
|
|
|
|
2014-03-06 05:51:42 +00:00
|
|
|
std::unique_ptr<MCRelocationInfo> RelInfo(
|
|
|
|
TheTarget->createMCRelocationInfo(Triple, *Ctx));
|
Add MCSymbolizer for symbolic/annotated disassembly.
This is a basic first step towards symbolization of disassembled
instructions. This used to be done using externally provided (C API)
callbacks. This patch introduces:
- the MCSymbolizer class, that mimics the same functions that were used
in the X86 and ARM disassemblers to symbolize immediate operands and
to annotate loads based off PC (for things like c string literals).
- the MCExternalSymbolizer class, which implements the old C API.
- the MCRelocationInfo class, which provides a way for targets to
translate relocations (either object::RelocationRef, or disassembler
C API VariantKinds) to MCExprs.
- the MCObjectSymbolizer class, which does symbolization using what it
finds in an object::ObjectFile. This makes simple symbolization (with
no fancy relocation stuff) work for all object formats!
- x86-64 Mach-O and ELF MCRelocationInfos.
- A basic ARM Mach-O MCRelocationInfo, that provides just enough to
support the C API VariantKinds.
Most of what works in otool (the only user of the old symbolization API
that I know of) for x86-64 symbolic disassembly (-tvV) works, namely:
- symbol references: call _foo; jmp 15 <_foo+50>
- relocations: call _foo-_bar; call _foo-4
- __cf?string: leaq 193(%rip), %rax ## literal pool for "hello"
Stub support is the main missing part (because libObject doesn't know,
among other things, about mach-o indirect symbols).
As for the MCSymbolizer API, instead of relying on the disassemblers
to call the tryAdding* methods, maybe this could be done automagically
using InstrInfo? For instance, even though PC-relative LEAs are used
to get the address of string literals in a typical Mach-O file, a MOV
would be used in an ELF file. And right now, the explicit symbolization
only recognizes PC-relative LEAs. InstrInfo should have already have
most of what is needed to know what to symbolize, so this can
definitely be improved.
I'd also like to remove object::RelocationRef::getValueString (it seems
only used by relocation printing in objdump), as simply printing the
created MCExpr is definitely enough (and cleaner than string concats).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@182625 91177308-0d34-0410-b5e6-96231b3b80d8
2013-05-24 00:39:57 +00:00
|
|
|
if (!RelInfo)
|
2014-04-24 06:44:33 +00:00
|
|
|
return nullptr;
|
Add MCSymbolizer for symbolic/annotated disassembly.
This is a basic first step towards symbolization of disassembled
instructions. This used to be done using externally provided (C API)
callbacks. This patch introduces:
- the MCSymbolizer class, that mimics the same functions that were used
in the X86 and ARM disassemblers to symbolize immediate operands and
to annotate loads based off PC (for things like c string literals).
- the MCExternalSymbolizer class, which implements the old C API.
- the MCRelocationInfo class, which provides a way for targets to
translate relocations (either object::RelocationRef, or disassembler
C API VariantKinds) to MCExprs.
- the MCObjectSymbolizer class, which does symbolization using what it
finds in an object::ObjectFile. This makes simple symbolization (with
no fancy relocation stuff) work for all object formats!
- x86-64 Mach-O and ELF MCRelocationInfos.
- A basic ARM Mach-O MCRelocationInfo, that provides just enough to
support the C API VariantKinds.
Most of what works in otool (the only user of the old symbolization API
that I know of) for x86-64 symbolic disassembly (-tvV) works, namely:
- symbol references: call _foo; jmp 15 <_foo+50>
- relocations: call _foo-_bar; call _foo-4
- __cf?string: leaq 193(%rip), %rax ## literal pool for "hello"
Stub support is the main missing part (because libObject doesn't know,
among other things, about mach-o indirect symbols).
As for the MCSymbolizer API, instead of relying on the disassemblers
to call the tryAdding* methods, maybe this could be done automagically
using InstrInfo? For instance, even though PC-relative LEAs are used
to get the address of string literals in a typical Mach-O file, a MOV
would be used in an ELF file. And right now, the explicit symbolization
only recognizes PC-relative LEAs. InstrInfo should have already have
most of what is needed to know what to symbolize, so this can
definitely be improved.
I'd also like to remove object::RelocationRef::getValueString (it seems
only used by relocation printing in objdump), as simply printing the
created MCExpr is definitely enough (and cleaner than string concats).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@182625 91177308-0d34-0410-b5e6-96231b3b80d8
2013-05-24 00:39:57 +00:00
|
|
|
|
2014-03-06 05:51:42 +00:00
|
|
|
std::unique_ptr<MCSymbolizer> Symbolizer(TheTarget->createMCSymbolizer(
|
|
|
|
Triple, GetOpInfo, SymbolLookUp, DisInfo, Ctx, RelInfo.release()));
|
2014-03-07 09:38:02 +00:00
|
|
|
DisAsm->setSymbolizer(std::move(Symbolizer));
|
2014-04-11 20:07:58 +00:00
|
|
|
|
2011-03-28 18:25:07 +00:00
|
|
|
// Set up the instruction printer.
|
|
|
|
int AsmPrinterVariant = MAI->getAssemblerDialect();
|
2011-07-06 19:45:42 +00:00
|
|
|
MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant,
|
2012-04-02 06:09:36 +00:00
|
|
|
*MAI, *MII, *MRI, *STI);
|
2013-03-12 18:12:17 +00:00
|
|
|
if (!IP)
|
2014-04-24 06:44:33 +00:00
|
|
|
return nullptr;
|
2011-03-28 18:25:07 +00:00
|
|
|
|
2012-12-07 23:53:27 +00:00
|
|
|
LLVMDisasmContext *DC = new LLVMDisasmContext(Triple, DisInfo, TagType,
|
2011-03-28 18:25:07 +00:00
|
|
|
GetOpInfo, SymbolLookUp,
|
2011-07-20 19:50:42 +00:00
|
|
|
TheTarget, MAI, MRI,
|
2012-04-06 18:21:09 +00:00
|
|
|
STI, MII, Ctx, DisAsm, IP);
|
2013-03-12 18:12:17 +00:00
|
|
|
if (!DC)
|
2014-04-24 06:44:33 +00:00
|
|
|
return nullptr;
|
2011-09-15 18:37:20 +00:00
|
|
|
|
2013-10-03 17:51:49 +00:00
|
|
|
DC->setCPU(CPU);
|
2011-03-28 18:25:07 +00:00
|
|
|
return DC;
|
|
|
|
}
|
|
|
|
|
2012-12-07 23:53:27 +00:00
|
|
|
LLVMDisasmContextRef LLVMCreateDisasm(const char *Triple, void *DisInfo,
|
|
|
|
int TagType, LLVMOpInfoCallback GetOpInfo,
|
|
|
|
LLVMSymbolLookupCallback SymbolLookUp) {
|
|
|
|
return LLVMCreateDisasmCPU(Triple, "", DisInfo, TagType, GetOpInfo,
|
|
|
|
SymbolLookUp);
|
|
|
|
}
|
|
|
|
|
2011-03-28 18:25:07 +00:00
|
|
|
//
|
|
|
|
// LLVMDisasmDispose() disposes of the disassembler specified by the context.
|
|
|
|
//
|
|
|
|
void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
|
|
|
|
LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
|
|
|
|
delete DC;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
//
|
|
|
|
// The memory object created by LLVMDisasmInstruction().
|
|
|
|
//
|
|
|
|
class DisasmMemoryObject : public MemoryObject {
|
|
|
|
uint8_t *Bytes;
|
|
|
|
uint64_t Size;
|
|
|
|
uint64_t BasePC;
|
|
|
|
public:
|
|
|
|
DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) :
|
|
|
|
Bytes(bytes), Size(size), BasePC(basePC) {}
|
|
|
|
|
2014-03-08 07:14:16 +00:00
|
|
|
uint64_t getBase() const override { return BasePC; }
|
|
|
|
uint64_t getExtent() const override { return Size; }
|
|
|
|
|
|
|
|
int readByte(uint64_t Addr, uint8_t *Byte) const override {
|
2011-03-28 18:25:07 +00:00
|
|
|
if (Addr - BasePC >= Size)
|
|
|
|
return -1;
|
|
|
|
*Byte = Bytes[Addr - BasePC];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
2011-05-22 04:52:24 +00:00
|
|
|
} // end anonymous namespace
|
2011-03-28 18:25:07 +00:00
|
|
|
|
2013-10-01 22:14:56 +00:00
|
|
|
/// \brief Emits the comments that are stored in \p DC comment stream.
|
|
|
|
/// Each comment in the comment stream must end with a newline.
|
|
|
|
static void emitComments(LLVMDisasmContext *DC,
|
|
|
|
formatted_raw_ostream &FormattedOS) {
|
|
|
|
// Flush the stream before taking its content.
|
|
|
|
DC->CommentStream.flush();
|
|
|
|
StringRef Comments = DC->CommentsToEmit.str();
|
|
|
|
// Get the default information for printing a comment.
|
|
|
|
const MCAsmInfo *MAI = DC->getAsmInfo();
|
|
|
|
const char *CommentBegin = MAI->getCommentString();
|
|
|
|
unsigned CommentColumn = MAI->getCommentColumn();
|
|
|
|
bool IsFirst = true;
|
|
|
|
while (!Comments.empty()) {
|
|
|
|
if (!IsFirst)
|
|
|
|
FormattedOS << '\n';
|
|
|
|
// Emit a line of comments.
|
|
|
|
FormattedOS.PadToColumn(CommentColumn);
|
|
|
|
size_t Position = Comments.find('\n');
|
|
|
|
FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
|
|
|
|
// Move after the newline character.
|
|
|
|
Comments = Comments.substr(Position+1);
|
|
|
|
IsFirst = false;
|
|
|
|
}
|
|
|
|
FormattedOS.flush();
|
|
|
|
|
|
|
|
// Tell the comment stream that the vector changed underneath it.
|
|
|
|
DC->CommentsToEmit.clear();
|
|
|
|
DC->CommentStream.resync();
|
|
|
|
}
|
|
|
|
|
2013-10-03 17:51:49 +00:00
|
|
|
/// \brief Gets latency information for \p Inst form the itinerary
|
|
|
|
/// scheduling model, based on \p DC information.
|
|
|
|
/// \return The maximum expected latency over all the operands or -1
|
|
|
|
/// if no information are available.
|
|
|
|
static int getItineraryLatency(LLVMDisasmContext *DC, const MCInst &Inst) {
|
|
|
|
const int NoInformationAvailable = -1;
|
|
|
|
|
|
|
|
// Check if we have a CPU to get the itinerary information.
|
|
|
|
if (DC->getCPU().empty())
|
|
|
|
return NoInformationAvailable;
|
|
|
|
|
|
|
|
// Get itinerary information.
|
|
|
|
const MCSubtargetInfo *STI = DC->getSubtargetInfo();
|
|
|
|
InstrItineraryData IID = STI->getInstrItineraryForCPU(DC->getCPU());
|
|
|
|
// Get the scheduling class of the requested instruction.
|
|
|
|
const MCInstrDesc& Desc = DC->getInstrInfo()->get(Inst.getOpcode());
|
|
|
|
unsigned SCClass = Desc.getSchedClass();
|
|
|
|
|
|
|
|
int Latency = 0;
|
|
|
|
for (unsigned OpIdx = 0, OpIdxEnd = Inst.getNumOperands(); OpIdx != OpIdxEnd;
|
|
|
|
++OpIdx)
|
|
|
|
Latency = std::max(Latency, IID.getOperandCycle(SCClass, OpIdx));
|
|
|
|
|
|
|
|
return Latency;
|
|
|
|
}
|
|
|
|
|
2013-10-02 22:07:57 +00:00
|
|
|
/// \brief Gets latency information for \p Inst, based on \p DC information.
|
|
|
|
/// \return The maximum expected latency over all the definitions or -1
|
|
|
|
/// if no information are available.
|
|
|
|
static int getLatency(LLVMDisasmContext *DC, const MCInst &Inst) {
|
|
|
|
// Try to compute scheduling information.
|
|
|
|
const MCSubtargetInfo *STI = DC->getSubtargetInfo();
|
|
|
|
const MCSchedModel *SCModel = STI->getSchedModel();
|
|
|
|
const int NoInformationAvailable = -1;
|
|
|
|
|
|
|
|
// Check if we have a scheduling model for instructions.
|
|
|
|
if (!SCModel || !SCModel->hasInstrSchedModel())
|
2013-10-03 17:51:49 +00:00
|
|
|
// Try to fall back to the itinerary model if we do not have a
|
|
|
|
// scheduling model.
|
|
|
|
return getItineraryLatency(DC, Inst);
|
2013-10-02 22:07:57 +00:00
|
|
|
|
|
|
|
// Get the scheduling class of the requested instruction.
|
|
|
|
const MCInstrDesc& Desc = DC->getInstrInfo()->get(Inst.getOpcode());
|
|
|
|
unsigned SCClass = Desc.getSchedClass();
|
|
|
|
const MCSchedClassDesc *SCDesc = SCModel->getSchedClassDesc(SCClass);
|
2013-10-02 23:11:47 +00:00
|
|
|
// Resolving the variant SchedClass requires an MI to pass to
|
|
|
|
// SubTargetInfo::resolveSchedClass.
|
|
|
|
if (!SCDesc || !SCDesc->isValid() || SCDesc->isVariant())
|
2013-10-02 22:07:57 +00:00
|
|
|
return NoInformationAvailable;
|
|
|
|
|
|
|
|
// Compute output latency.
|
|
|
|
int Latency = 0;
|
|
|
|
for (unsigned DefIdx = 0, DefEnd = SCDesc->NumWriteLatencyEntries;
|
|
|
|
DefIdx != DefEnd; ++DefIdx) {
|
|
|
|
// Lookup the definition's write latency in SubtargetInfo.
|
|
|
|
const MCWriteLatencyEntry *WLEntry = STI->getWriteLatencyEntry(SCDesc,
|
|
|
|
DefIdx);
|
|
|
|
Latency = std::max(Latency, WLEntry->Cycles);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Latency;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// \brief Emits latency information in DC->CommentStream for \p Inst, based
|
|
|
|
/// on the information available in \p DC.
|
|
|
|
static void emitLatency(LLVMDisasmContext *DC, const MCInst &Inst) {
|
|
|
|
int Latency = getLatency(DC, Inst);
|
|
|
|
|
|
|
|
// Report only interesting latency.
|
|
|
|
if (Latency < 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
DC->CommentStream << "Latency: " << Latency << '\n';
|
|
|
|
}
|
|
|
|
|
2011-03-28 18:25:07 +00:00
|
|
|
//
|
2011-04-09 14:06:12 +00:00
|
|
|
// LLVMDisasmInstruction() disassembles a single instruction using the
|
2011-03-28 18:25:07 +00:00
|
|
|
// disassembler context specified in the parameter DC. The bytes of the
|
2011-04-09 14:06:12 +00:00
|
|
|
// instruction are specified in the parameter Bytes, and contains at least
|
2011-03-28 18:25:07 +00:00
|
|
|
// BytesSize number of bytes. The instruction is at the address specified by
|
|
|
|
// the PC parameter. If a valid instruction can be disassembled its string is
|
|
|
|
// returned indirectly in OutString which whos size is specified in the
|
|
|
|
// parameter OutStringSize. This function returns the number of bytes in the
|
|
|
|
// instruction or zero if there was no valid instruction. If this function
|
|
|
|
// returns zero the caller will have to pick how many bytes they want to step
|
|
|
|
// over by printing a .byte, .long etc. to continue.
|
|
|
|
//
|
|
|
|
size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
|
|
|
|
uint64_t BytesSize, uint64_t PC, char *OutString,
|
|
|
|
size_t OutStringSize){
|
|
|
|
LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
|
|
|
|
// Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
|
|
|
|
DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC);
|
|
|
|
|
|
|
|
uint64_t Size;
|
|
|
|
MCInst Inst;
|
|
|
|
const MCDisassembler *DisAsm = DC->getDisAsm();
|
|
|
|
MCInstPrinter *IP = DC->getIP();
|
2011-09-01 22:01:14 +00:00
|
|
|
MCDisassembler::DecodeStatus S;
|
2013-10-01 22:14:56 +00:00
|
|
|
SmallVector<char, 64> InsnStr;
|
|
|
|
raw_svector_ostream Annotations(InsnStr);
|
2011-09-01 22:01:14 +00:00
|
|
|
S = DisAsm->getInstruction(Inst, Size, MemoryObject, PC,
|
2013-10-01 22:14:56 +00:00
|
|
|
/*REMOVE*/ nulls(), Annotations);
|
2011-09-01 22:01:14 +00:00
|
|
|
switch (S) {
|
|
|
|
case MCDisassembler::Fail:
|
|
|
|
case MCDisassembler::SoftFail:
|
2011-09-01 18:02:14 +00:00
|
|
|
// FIXME: Do something different for soft failure modes?
|
2011-03-28 18:25:07 +00:00
|
|
|
return 0;
|
2011-09-01 21:09:04 +00:00
|
|
|
|
2011-09-01 22:01:14 +00:00
|
|
|
case MCDisassembler::Success: {
|
2013-10-01 22:14:56 +00:00
|
|
|
Annotations.flush();
|
|
|
|
StringRef AnnotationsStr = Annotations.str();
|
2011-09-15 18:37:20 +00:00
|
|
|
|
2011-09-15 23:38:46 +00:00
|
|
|
SmallVector<char, 64> InsnStr;
|
|
|
|
raw_svector_ostream OS(InsnStr);
|
2013-10-01 22:14:56 +00:00
|
|
|
formatted_raw_ostream FormattedOS(OS);
|
|
|
|
IP->printInst(&Inst, FormattedOS, AnnotationsStr);
|
2011-09-15 18:37:20 +00:00
|
|
|
|
2013-10-02 22:07:57 +00:00
|
|
|
if (DC->getOptions() & LLVMDisassembler_Option_PrintLatency)
|
|
|
|
emitLatency(DC, Inst);
|
|
|
|
|
2013-10-01 22:14:56 +00:00
|
|
|
emitComments(DC, FormattedOS);
|
2014-01-06 22:08:08 +00:00
|
|
|
OS.flush();
|
2011-09-15 18:37:20 +00:00
|
|
|
|
2011-09-01 22:01:14 +00:00
|
|
|
assert(OutStringSize != 0 && "Output buffer cannot be zero size");
|
|
|
|
size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
|
|
|
|
std::memcpy(OutString, InsnStr.data(), OutputSize);
|
|
|
|
OutString[OutputSize] = '\0'; // Terminate string.
|
2011-09-01 21:09:04 +00:00
|
|
|
|
2011-09-01 22:01:14 +00:00
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
}
|
2012-01-20 21:51:11 +00:00
|
|
|
llvm_unreachable("Invalid DecodeStatus!");
|
2011-03-28 18:25:07 +00:00
|
|
|
}
|
2012-10-22 22:31:46 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// LLVMSetDisasmOptions() sets the disassembler's options. It returns 1 if it
|
|
|
|
// can set all the Options and 0 otherwise.
|
|
|
|
//
|
|
|
|
int LLVMSetDisasmOptions(LLVMDisasmContextRef DCR, uint64_t Options){
|
|
|
|
if (Options & LLVMDisassembler_Option_UseMarkup){
|
|
|
|
LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
|
|
|
|
MCInstPrinter *IP = DC->getIP();
|
|
|
|
IP->setUseMarkup(1);
|
2013-10-02 22:07:57 +00:00
|
|
|
DC->addOptions(LLVMDisassembler_Option_UseMarkup);
|
2012-10-22 22:31:46 +00:00
|
|
|
Options &= ~LLVMDisassembler_Option_UseMarkup;
|
|
|
|
}
|
2012-12-05 18:13:19 +00:00
|
|
|
if (Options & LLVMDisassembler_Option_PrintImmHex){
|
|
|
|
LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
|
|
|
|
MCInstPrinter *IP = DC->getIP();
|
|
|
|
IP->setPrintImmHex(1);
|
2013-10-02 22:07:57 +00:00
|
|
|
DC->addOptions(LLVMDisassembler_Option_PrintImmHex);
|
2012-12-05 18:13:19 +00:00
|
|
|
Options &= ~LLVMDisassembler_Option_PrintImmHex;
|
|
|
|
}
|
2012-12-18 23:47:28 +00:00
|
|
|
if (Options & LLVMDisassembler_Option_AsmPrinterVariant){
|
|
|
|
LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
|
|
|
|
// Try to set up the new instruction printer.
|
|
|
|
const MCAsmInfo *MAI = DC->getAsmInfo();
|
|
|
|
const MCInstrInfo *MII = DC->getInstrInfo();
|
|
|
|
const MCRegisterInfo *MRI = DC->getRegisterInfo();
|
|
|
|
const MCSubtargetInfo *STI = DC->getSubtargetInfo();
|
|
|
|
int AsmPrinterVariant = MAI->getAssemblerDialect();
|
|
|
|
AsmPrinterVariant = AsmPrinterVariant == 0 ? 1 : 0;
|
|
|
|
MCInstPrinter *IP = DC->getTarget()->createMCInstPrinter(
|
|
|
|
AsmPrinterVariant, *MAI, *MII, *MRI, *STI);
|
|
|
|
if (IP) {
|
|
|
|
DC->setIP(IP);
|
2013-10-02 22:07:57 +00:00
|
|
|
DC->addOptions(LLVMDisassembler_Option_AsmPrinterVariant);
|
2012-12-18 23:47:28 +00:00
|
|
|
Options &= ~LLVMDisassembler_Option_AsmPrinterVariant;
|
|
|
|
}
|
|
|
|
}
|
2013-10-01 22:14:56 +00:00
|
|
|
if (Options & LLVMDisassembler_Option_SetInstrComments) {
|
|
|
|
LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
|
|
|
|
MCInstPrinter *IP = DC->getIP();
|
|
|
|
IP->setCommentStream(DC->CommentStream);
|
2013-10-02 22:07:57 +00:00
|
|
|
DC->addOptions(LLVMDisassembler_Option_SetInstrComments);
|
2013-10-01 22:14:56 +00:00
|
|
|
Options &= ~LLVMDisassembler_Option_SetInstrComments;
|
|
|
|
}
|
2013-10-02 22:07:57 +00:00
|
|
|
if (Options & LLVMDisassembler_Option_PrintLatency) {
|
|
|
|
LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
|
|
|
|
DC->addOptions(LLVMDisassembler_Option_PrintLatency);
|
|
|
|
Options &= ~LLVMDisassembler_Option_PrintLatency;
|
|
|
|
}
|
2012-10-22 22:31:46 +00:00
|
|
|
return (Options == 0);
|
|
|
|
}
|