2011-09-02 05:09:04 +08:00
|
|
|
//===-- lib/MC/Disassembler.cpp - Disassembler Public C Interface ---------===//
|
2011-03-29 02:25:07 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-03-29 02:25:07 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2011-05-22 12:52:24 +08:00
|
|
|
|
2011-03-29 02:25:07 +08:00
|
|
|
#include "Disassembler.h"
|
|
|
|
#include "llvm-c/Disassembler.h"
|
2016-08-24 01:14:32 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Triple.h"
|
2011-03-29 02:25:07 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2011-07-20 14:54:19 +08:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2016-01-27 00:44:37 +08:00
|
|
|
#include "llvm/MC/MCDisassembler/MCDisassembler.h"
|
|
|
|
#include "llvm/MC/MCDisassembler/MCRelocationInfo.h"
|
|
|
|
#include "llvm/MC/MCDisassembler/MCSymbolizer.h"
|
2011-03-29 02:25:07 +08:00
|
|
|
#include "llvm/MC/MCInst.h"
|
|
|
|
#include "llvm/MC/MCInstPrinter.h"
|
2016-08-24 01:14:32 +08:00
|
|
|
#include "llvm/MC/MCInstrDesc.h"
|
2012-04-07 02:21:09 +08:00
|
|
|
#include "llvm/MC/MCInstrInfo.h"
|
2016-08-24 01:14:32 +08:00
|
|
|
#include "llvm/MC/MCInstrItineraries.h"
|
2011-07-20 14:54:19 +08:00
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
2016-08-24 01:14:32 +08:00
|
|
|
#include "llvm/MC/MCSchedule.h"
|
2012-04-07 02:21:09 +08:00
|
|
|
#include "llvm/MC/MCSubtargetInfo.h"
|
2019-10-23 18:24:35 +08:00
|
|
|
#include "llvm/MC/MCTargetOptions.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2013-10-02 06:14:56 +08:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2011-08-25 02:08:43 +08:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2016-08-24 01:14:32 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstring>
|
2011-03-29 02:25:07 +08: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 12:52:24 +08:00
|
|
|
// parameter and specifying the TagType and callback functions as described in
|
2018-07-31 03:41:25 +08:00
|
|
|
// the header llvm-c/Disassembler.h . The pointer to the block and the
|
2011-05-22 12:52:24 +08:00
|
|
|
// functions can all be passed as NULL. If successful, this returns a
|
|
|
|
// disassembler context. If not, it returns NULL.
|
2011-03-29 02:25:07 +08:00
|
|
|
//
|
2014-10-01 00:31:40 +08:00
|
|
|
LLVMDisasmContextRef
|
2015-03-31 06:31:14 +08:00
|
|
|
LLVMCreateDisasmCPUFeatures(const char *TT, const char *CPU,
|
2014-10-01 00:31:40 +08:00
|
|
|
const char *Features, void *DisInfo, int TagType,
|
|
|
|
LLVMOpInfoCallback GetOpInfo,
|
|
|
|
LLVMSymbolLookupCallback SymbolLookUp) {
|
2011-03-29 02:25:07 +08:00
|
|
|
// Get the target.
|
|
|
|
std::string Error;
|
2015-03-31 06:31:14 +08:00
|
|
|
const Target *TheTarget = TargetRegistry::lookupTarget(TT, Error);
|
2013-05-23 08:32:34 +08:00
|
|
|
if (!TheTarget)
|
2014-04-24 14:44:33 +08:00
|
|
|
return nullptr;
|
2011-03-29 02:25:07 +08:00
|
|
|
|
2019-06-27 00:13:17 +08:00
|
|
|
std::unique_ptr<const MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TT));
|
2013-05-13 09:16:13 +08:00
|
|
|
if (!MRI)
|
2014-04-24 14:44:33 +08:00
|
|
|
return nullptr;
|
2013-05-13 09:16:13 +08:00
|
|
|
|
2019-10-23 18:24:35 +08:00
|
|
|
MCTargetOptions MCOptions;
|
2011-03-29 02:25:07 +08:00
|
|
|
// Get the assembler info needed to setup the MCContext.
|
2019-10-23 18:24:35 +08:00
|
|
|
std::unique_ptr<const MCAsmInfo> MAI(
|
|
|
|
TheTarget->createMCAsmInfo(*MRI, TT, MCOptions));
|
2013-03-13 02:12:17 +08:00
|
|
|
if (!MAI)
|
2014-04-24 14:44:33 +08:00
|
|
|
return nullptr;
|
2011-03-29 02:25:07 +08:00
|
|
|
|
2019-06-27 00:13:17 +08:00
|
|
|
std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
|
2013-03-13 02:12:17 +08:00
|
|
|
if (!MII)
|
2014-04-24 14:44:33 +08:00
|
|
|
return nullptr;
|
2012-04-02 14:09:36 +08:00
|
|
|
|
2019-06-27 00:13:17 +08:00
|
|
|
std::unique_ptr<const MCSubtargetInfo> STI(
|
|
|
|
TheTarget->createMCSubtargetInfo(TT, CPU, Features));
|
2013-03-13 02:12:17 +08:00
|
|
|
if (!STI)
|
2014-04-24 14:44:33 +08:00
|
|
|
return nullptr;
|
2011-09-08 01:24:38 +08:00
|
|
|
|
2011-03-29 02:25:07 +08:00
|
|
|
// Set up the MCContext for creating symbols and MCExpr's.
|
2019-06-27 00:13:17 +08:00
|
|
|
std::unique_ptr<MCContext> Ctx(new MCContext(MAI.get(), MRI.get(), nullptr));
|
2013-03-13 02:12:17 +08:00
|
|
|
if (!Ctx)
|
2014-04-24 14:44:33 +08:00
|
|
|
return nullptr;
|
2011-03-29 02:25:07 +08:00
|
|
|
|
|
|
|
// Set up disassembler.
|
2019-06-27 00:13:17 +08:00
|
|
|
std::unique_ptr<MCDisassembler> DisAsm(
|
|
|
|
TheTarget->createMCDisassembler(*STI, *Ctx));
|
2013-03-13 02:12:17 +08:00
|
|
|
if (!DisAsm)
|
2014-04-24 14:44:33 +08: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).
llvm-svn: 182625
2013-05-24 08:39:57 +08:00
|
|
|
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<MCRelocationInfo> RelInfo(
|
2015-03-31 06:31:14 +08:00
|
|
|
TheTarget->createMCRelocationInfo(TT, *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).
llvm-svn: 182625
2013-05-24 08:39:57 +08:00
|
|
|
if (!RelInfo)
|
2014-04-24 14:44:33 +08: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).
llvm-svn: 182625
2013-05-24 08:39:57 +08:00
|
|
|
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<MCSymbolizer> Symbolizer(TheTarget->createMCSymbolizer(
|
2019-06-27 00:13:17 +08:00
|
|
|
TT, GetOpInfo, SymbolLookUp, DisInfo, Ctx.get(), std::move(RelInfo)));
|
2014-03-07 17:38:02 +08:00
|
|
|
DisAsm->setSymbolizer(std::move(Symbolizer));
|
2014-04-12 04:07:58 +08:00
|
|
|
|
2011-03-29 02:25:07 +08:00
|
|
|
// Set up the instruction printer.
|
|
|
|
int AsmPrinterVariant = MAI->getAssemblerDialect();
|
2019-06-27 00:13:17 +08:00
|
|
|
std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
|
|
|
|
Triple(TT), AsmPrinterVariant, *MAI, *MII, *MRI));
|
2013-03-13 02:12:17 +08:00
|
|
|
if (!IP)
|
2014-04-24 14:44:33 +08:00
|
|
|
return nullptr;
|
2011-03-29 02:25:07 +08:00
|
|
|
|
2019-06-27 00:13:17 +08:00
|
|
|
LLVMDisasmContext *DC = new LLVMDisasmContext(
|
|
|
|
TT, DisInfo, TagType, GetOpInfo, SymbolLookUp, TheTarget, std::move(MAI),
|
|
|
|
std::move(MRI), std::move(STI), std::move(MII), std::move(Ctx),
|
|
|
|
std::move(DisAsm), std::move(IP));
|
2013-03-13 02:12:17 +08:00
|
|
|
if (!DC)
|
2014-04-24 14:44:33 +08:00
|
|
|
return nullptr;
|
2011-09-16 02:37:20 +08:00
|
|
|
|
2013-10-04 01:51:49 +08:00
|
|
|
DC->setCPU(CPU);
|
2011-03-29 02:25:07 +08:00
|
|
|
return DC;
|
|
|
|
}
|
|
|
|
|
2015-03-31 06:31:14 +08:00
|
|
|
LLVMDisasmContextRef
|
|
|
|
LLVMCreateDisasmCPU(const char *TT, const char *CPU, void *DisInfo, int TagType,
|
|
|
|
LLVMOpInfoCallback GetOpInfo,
|
|
|
|
LLVMSymbolLookupCallback SymbolLookUp) {
|
|
|
|
return LLVMCreateDisasmCPUFeatures(TT, CPU, "", DisInfo, TagType, GetOpInfo,
|
|
|
|
SymbolLookUp);
|
2014-10-01 00:31:40 +08:00
|
|
|
}
|
|
|
|
|
2015-03-31 06:31:14 +08:00
|
|
|
LLVMDisasmContextRef LLVMCreateDisasm(const char *TT, void *DisInfo,
|
2012-12-08 07:53:27 +08:00
|
|
|
int TagType, LLVMOpInfoCallback GetOpInfo,
|
|
|
|
LLVMSymbolLookupCallback SymbolLookUp) {
|
2015-03-31 06:31:14 +08:00
|
|
|
return LLVMCreateDisasmCPUFeatures(TT, "", "", DisInfo, TagType, GetOpInfo,
|
|
|
|
SymbolLookUp);
|
2012-12-08 07:53:27 +08:00
|
|
|
}
|
|
|
|
|
2011-03-29 02:25:07 +08:00
|
|
|
//
|
|
|
|
// LLVMDisasmDispose() disposes of the disassembler specified by the context.
|
|
|
|
//
|
|
|
|
void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
|
2016-08-24 01:14:32 +08:00
|
|
|
LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
|
2011-03-29 02:25:07 +08:00
|
|
|
delete DC;
|
|
|
|
}
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Emits the comments that are stored in \p DC comment stream.
|
2013-10-02 06:14:56 +08:00
|
|
|
/// 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.
|
|
|
|
StringRef Comments = DC->CommentsToEmit.str();
|
|
|
|
// Get the default information for printing a comment.
|
|
|
|
const MCAsmInfo *MAI = DC->getAsmInfo();
|
2016-10-01 14:46:33 +08:00
|
|
|
StringRef CommentBegin = MAI->getCommentString();
|
2013-10-02 06:14:56 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Gets latency information for \p Inst from the itinerary
|
2013-10-04 01:51:49 +08:00
|
|
|
/// scheduling model, based on \p DC information.
|
|
|
|
/// \return The maximum expected latency over all the operands or -1
|
2015-01-13 08:21:14 +08:00
|
|
|
/// if no information is available.
|
2013-10-04 01:51:49 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Gets latency information for \p Inst, based on \p DC information.
|
2013-10-03 06:07:57 +08:00
|
|
|
/// \return The maximum expected latency over all the definitions or -1
|
2015-01-13 08:21:14 +08:00
|
|
|
/// if no information is available.
|
2013-10-03 06:07:57 +08:00
|
|
|
static int getLatency(LLVMDisasmContext *DC, const MCInst &Inst) {
|
|
|
|
// Try to compute scheduling information.
|
|
|
|
const MCSubtargetInfo *STI = DC->getSubtargetInfo();
|
2014-09-03 01:43:54 +08:00
|
|
|
const MCSchedModel SCModel = STI->getSchedModel();
|
2013-10-03 06:07:57 +08:00
|
|
|
const int NoInformationAvailable = -1;
|
|
|
|
|
|
|
|
// Check if we have a scheduling model for instructions.
|
2014-09-03 01:43:54 +08:00
|
|
|
if (!SCModel.hasInstrSchedModel())
|
|
|
|
// Try to fall back to the itinerary model if the scheduling model doesn't
|
|
|
|
// have a scheduling table. Note the default does not have a table.
|
2013-10-04 01:51:49 +08:00
|
|
|
return getItineraryLatency(DC, Inst);
|
2013-10-03 06:07:57 +08:00
|
|
|
|
|
|
|
// Get the scheduling class of the requested instruction.
|
|
|
|
const MCInstrDesc& Desc = DC->getInstrInfo()->get(Inst.getOpcode());
|
|
|
|
unsigned SCClass = Desc.getSchedClass();
|
2014-09-03 01:43:54 +08:00
|
|
|
const MCSchedClassDesc *SCDesc = SCModel.getSchedClassDesc(SCClass);
|
2013-10-03 07:11:47 +08:00
|
|
|
// Resolving the variant SchedClass requires an MI to pass to
|
|
|
|
// SubTargetInfo::resolveSchedClass.
|
|
|
|
if (!SCDesc || !SCDesc->isValid() || SCDesc->isVariant())
|
2013-10-03 06:07:57 +08:00
|
|
|
return NoInformationAvailable;
|
|
|
|
|
|
|
|
// Compute output latency.
|
2018-02-24 03:32:56 +08:00
|
|
|
int16_t Latency = 0;
|
2013-10-03 06:07:57 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Emits latency information in DC->CommentStream for \p Inst, based
|
2013-10-03 06:07:57 +08:00
|
|
|
/// on the information available in \p DC.
|
|
|
|
static void emitLatency(LLVMDisasmContext *DC, const MCInst &Inst) {
|
|
|
|
int Latency = getLatency(DC, Inst);
|
|
|
|
|
2015-01-13 08:21:14 +08:00
|
|
|
// Report only interesting latencies.
|
2013-10-03 06:07:57 +08:00
|
|
|
if (Latency < 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
DC->CommentStream << "Latency: " << Latency << '\n';
|
|
|
|
}
|
|
|
|
|
2011-03-29 02:25:07 +08:00
|
|
|
//
|
2011-04-09 22:06:12 +08:00
|
|
|
// LLVMDisasmInstruction() disassembles a single instruction using the
|
2011-03-29 02:25:07 +08:00
|
|
|
// disassembler context specified in the parameter DC. The bytes of the
|
2011-04-09 22:06:12 +08:00
|
|
|
// instruction are specified in the parameter Bytes, and contains at least
|
2011-03-29 02:25:07 +08: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){
|
2016-08-24 01:14:32 +08:00
|
|
|
LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
|
2011-03-29 02:25:07 +08:00
|
|
|
// Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
|
2014-11-12 10:04:27 +08:00
|
|
|
ArrayRef<uint8_t> Data(Bytes, BytesSize);
|
2011-03-29 02:25:07 +08:00
|
|
|
|
|
|
|
uint64_t Size;
|
|
|
|
MCInst Inst;
|
|
|
|
const MCDisassembler *DisAsm = DC->getDisAsm();
|
|
|
|
MCInstPrinter *IP = DC->getIP();
|
2011-09-02 06:01:14 +08:00
|
|
|
MCDisassembler::DecodeStatus S;
|
2014-06-27 06:52:05 +08:00
|
|
|
SmallVector<char, 64> InsnStr;
|
|
|
|
raw_svector_ostream Annotations(InsnStr);
|
2020-01-12 04:36:13 +08:00
|
|
|
S = DisAsm->getInstruction(Inst, Size, Data, PC, Annotations);
|
2011-09-02 06:01:14 +08:00
|
|
|
switch (S) {
|
|
|
|
case MCDisassembler::Fail:
|
|
|
|
case MCDisassembler::SoftFail:
|
2011-09-02 02:02:14 +08:00
|
|
|
// FIXME: Do something different for soft failure modes?
|
2011-03-29 02:25:07 +08:00
|
|
|
return 0;
|
2011-09-02 05:09:04 +08:00
|
|
|
|
2011-09-02 06:01:14 +08:00
|
|
|
case MCDisassembler::Success: {
|
2014-06-27 06:52:05 +08:00
|
|
|
StringRef AnnotationsStr = Annotations.str();
|
|
|
|
|
2011-09-16 07:38:46 +08:00
|
|
|
SmallVector<char, 64> InsnStr;
|
|
|
|
raw_svector_ostream OS(InsnStr);
|
2013-10-02 06:14:56 +08:00
|
|
|
formatted_raw_ostream FormattedOS(OS);
|
2020-01-04 02:55:30 +08:00
|
|
|
IP->printInst(&Inst, PC, AnnotationsStr, *DC->getSubtargetInfo(),
|
|
|
|
FormattedOS);
|
2011-09-16 02:37:20 +08:00
|
|
|
|
2013-10-03 06:07:57 +08:00
|
|
|
if (DC->getOptions() & LLVMDisassembler_Option_PrintLatency)
|
|
|
|
emitLatency(DC, Inst);
|
|
|
|
|
2013-10-02 06:14:56 +08:00
|
|
|
emitComments(DC, FormattedOS);
|
2011-09-16 02:37:20 +08:00
|
|
|
|
2011-09-02 06:01:14 +08: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-02 05:09:04 +08:00
|
|
|
|
2011-09-02 06:01:14 +08:00
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
}
|
2012-01-21 05:51:11 +08:00
|
|
|
llvm_unreachable("Invalid DecodeStatus!");
|
2011-03-29 02:25:07 +08:00
|
|
|
}
|
2012-10-23 06:31:46 +08: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){
|
2016-08-24 01:14:32 +08:00
|
|
|
LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
|
2012-10-23 06:31:46 +08:00
|
|
|
MCInstPrinter *IP = DC->getIP();
|
2016-08-24 01:14:32 +08:00
|
|
|
IP->setUseMarkup(true);
|
2013-10-03 06:07:57 +08:00
|
|
|
DC->addOptions(LLVMDisassembler_Option_UseMarkup);
|
2012-10-23 06:31:46 +08:00
|
|
|
Options &= ~LLVMDisassembler_Option_UseMarkup;
|
|
|
|
}
|
2012-12-06 02:13:19 +08:00
|
|
|
if (Options & LLVMDisassembler_Option_PrintImmHex){
|
2016-08-24 01:14:32 +08:00
|
|
|
LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
|
2012-12-06 02:13:19 +08:00
|
|
|
MCInstPrinter *IP = DC->getIP();
|
2016-08-24 01:14:32 +08:00
|
|
|
IP->setPrintImmHex(true);
|
2013-10-03 06:07:57 +08:00
|
|
|
DC->addOptions(LLVMDisassembler_Option_PrintImmHex);
|
2012-12-06 02:13:19 +08:00
|
|
|
Options &= ~LLVMDisassembler_Option_PrintImmHex;
|
|
|
|
}
|
2012-12-19 07:47:28 +08:00
|
|
|
if (Options & LLVMDisassembler_Option_AsmPrinterVariant){
|
2016-08-24 01:14:32 +08:00
|
|
|
LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
|
2012-12-19 07:47:28 +08:00
|
|
|
// Try to set up the new instruction printer.
|
|
|
|
const MCAsmInfo *MAI = DC->getAsmInfo();
|
|
|
|
const MCInstrInfo *MII = DC->getInstrInfo();
|
|
|
|
const MCRegisterInfo *MRI = DC->getRegisterInfo();
|
|
|
|
int AsmPrinterVariant = MAI->getAssemblerDialect();
|
|
|
|
AsmPrinterVariant = AsmPrinterVariant == 0 ? 1 : 0;
|
|
|
|
MCInstPrinter *IP = DC->getTarget()->createMCInstPrinter(
|
2015-09-16 00:17:27 +08:00
|
|
|
Triple(DC->getTripleName()), AsmPrinterVariant, *MAI, *MII, *MRI);
|
2012-12-19 07:47:28 +08:00
|
|
|
if (IP) {
|
|
|
|
DC->setIP(IP);
|
2013-10-03 06:07:57 +08:00
|
|
|
DC->addOptions(LLVMDisassembler_Option_AsmPrinterVariant);
|
2012-12-19 07:47:28 +08:00
|
|
|
Options &= ~LLVMDisassembler_Option_AsmPrinterVariant;
|
|
|
|
}
|
|
|
|
}
|
2013-10-02 06:14:56 +08:00
|
|
|
if (Options & LLVMDisassembler_Option_SetInstrComments) {
|
2016-08-24 01:14:32 +08:00
|
|
|
LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
|
2013-10-02 06:14:56 +08:00
|
|
|
MCInstPrinter *IP = DC->getIP();
|
|
|
|
IP->setCommentStream(DC->CommentStream);
|
2013-10-03 06:07:57 +08:00
|
|
|
DC->addOptions(LLVMDisassembler_Option_SetInstrComments);
|
2013-10-02 06:14:56 +08:00
|
|
|
Options &= ~LLVMDisassembler_Option_SetInstrComments;
|
|
|
|
}
|
2013-10-03 06:07:57 +08:00
|
|
|
if (Options & LLVMDisassembler_Option_PrintLatency) {
|
2016-08-24 01:14:32 +08:00
|
|
|
LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
|
2013-10-03 06:07:57 +08:00
|
|
|
DC->addOptions(LLVMDisassembler_Option_PrintLatency);
|
|
|
|
Options &= ~LLVMDisassembler_Option_PrintLatency;
|
|
|
|
}
|
2012-10-23 06:31:46 +08:00
|
|
|
return (Options == 0);
|
|
|
|
}
|