1//===-- lib/MC/Disassembler.cpp - Disassembler Public C Interface ---------===//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//===----------------------------------------------------------------------===//
35// LLVMCreateDisasm() creates a disassembler for the TripleName. Symbolic
36// disassembly is supported by passing a block of information in the DisInfo
37// parameter and specifying the TagType and callback functions as described in
38// the header llvm-c/Disassembler.h . The pointer to the block and the
39// functions can all be passed as NULL. If successful, this returns a
40// disassembler context. If not, it returns NULL.
44 const char *Features,
void *DisInfo,
int TagType,
55 std::unique_ptr<const MCRegisterInfo>
MRI(
61 // Get the assembler info needed to setup the MCContext.
62 std::unique_ptr<const MCAsmInfo> MAI(
71 std::unique_ptr<const MCSubtargetInfo> STI(
76 // Set up the MCContext for creating symbols and MCExpr's.
77 std::unique_ptr<MCContext> Ctx(
78 new MCContext(TheTriple, MAI.get(),
MRI.get(), STI.get()));
82 // Set up disassembler.
83 std::unique_ptr<MCDisassembler> DisAsm(
88 std::unique_ptr<MCRelocationInfo> RelInfo(
93 std::unique_ptr<MCSymbolizer> Symbolizer(
95 Ctx.get(), std::move(RelInfo)));
96 DisAsm->setSymbolizer(std::move(Symbolizer));
98 // Set up the instruction printer.
99 int AsmPrinterVariant = MAI->getAssemblerDialect();
101 Triple(TT), AsmPrinterVariant, *MAI, *MII, *
MRI));
106 TT, DisInfo, TagType, GetOpInfo, SymbolLookUp, TheTarget, std::move(MAI),
107 std::move(
MRI), std::move(STI), std::move(MII), std::move(Ctx),
108 std::move(DisAsm), std::move(IP));
132// LLVMDisasmDispose() disposes of the disassembler specified by the context.
139/// Emits the comments that are stored in \p DC comment stream.
140/// Each comment in the comment stream must end with a newline.
143 // Flush the stream before taking its content.
145 // Get the default information for printing a comment.
150 while (!Comments.
empty()) {
153 // Emit a line of comments.
155 size_t Position = Comments.
find(
'\n');
156 FormattedOS << CommentBegin <<
' ' << Comments.
substr(0, Position);
157 // Move after the newline character.
158 Comments = Comments.
substr(Position+1);
163 // Tell the comment stream that the vector changed underneath it.
167/// Emits latency information in DC->CommentStream for \p Inst, based
168/// on the information available in \p DC.
175 // Report only interesting latencies.
183// LLVMDisasmInstruction() disassembles a single instruction using the
184// disassembler context specified in the parameter DC. The bytes of the
185// instruction are specified in the parameter Bytes, and contains at least
186// BytesSize number of bytes. The instruction is at the address specified by
187// the PC parameter. If a valid instruction can be disassembled its string is
188// returned indirectly in OutString which whos size is specified in the
189// parameter OutStringSize. This function returns the number of bytes in the
190// instruction or zero if there was no valid instruction. If this function
191// returns zero the caller will have to pick how many bytes they want to step
192// over by printing a .byte, .long etc. to continue.
196 size_t OutStringSize){
198 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
212 // FIXME: Do something different for soft failure modes?
235 assert(OutStringSize != 0 &&
"Output buffer cannot be zero size");
236 size_t OutputSize = std::min(OutStringSize-1, InsnStr.
size());
237 std::memcpy(OutString, InsnStr.
data(), OutputSize);
238 OutString[OutputSize] =
'0円';
// Terminate string.
247// LLVMSetDisasmOptions() sets the disassembler's options. It returns 1 if it
248// can set all the Options and 0 otherwise.
267 // Try to set up the new instruction printer.
272 AsmPrinterVariant = AsmPrinterVariant == 0 ? 1 : 0;
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static void emitComments(LLVMDisasmContext *DC, formatted_raw_ostream &FormattedOS)
Emits the comments that are stored in DC comment stream.
static void emitLatency(LLVMDisasmContext *DC, const MCInst &Inst)
Emits latency information in DC->CommentStream for Inst, based on the information available in DC.
This file defines the SmallVector class.
Annotations lets you mark points and ranges inside source code, for tests:
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Lightweight error class with error context and mandatory checking.
const MCDisassembler * getDisAsm() const
void setCPU(const char *CPU)
void addOptions(uint64_t Options)
raw_svector_ostream CommentStream
void setIP(MCInstPrinter *NewIP)
const MCSubtargetInfo * getSubtargetInfo() const
const MCAsmInfo * getAsmInfo() const
uint64_t getOptions() const
const MCRegisterInfo * getRegisterInfo() const
SmallString< 128 > CommentsToEmit
StringRef getTripleName() const
const MCInstrInfo * getInstrInfo() const
const Target * getTarget() const
This class is intended to be used as a base class for asm properties and features specific to the tar...
unsigned getAssemblerDialect() const
StringRef getCommentString() const
unsigned getCommentColumn() const
Context object for machine code objects.
Superclass for all disassemblers.
DecodeStatus
Ternary decode status.
virtual DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, ArrayRef< uint8_t > Bytes, uint64_t Address, raw_ostream &CStream) const =0
Returns the disassembly of a single instruction.
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
void setCommentStream(raw_ostream &OS)
Specify a stream to emit comments to.
void setUseColor(bool Value)
void setPrintImmHex(bool Value)
void setUseMarkup(bool Value)
virtual void printInst(const MCInst *MI, uint64_t Address, StringRef Annot, const MCSubtargetInfo &STI, raw_ostream &OS)=0
Print the specified MCInst to the specified raw_ostream.
Instances of this class represent a single low-level machine instruction.
Interface to description of machine instruction set.
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Generic base class for all target subtargets.
const MCSchedModel & getSchedModel() const
Get the machine model for this subtarget's CPU.
StringRef str() const
Explicit conversion to StringRef.
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
constexpr bool empty() const
empty - Check if the string is empty.
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Target - Wrapper for Target specific information.
MCSubtargetInfo * createMCSubtargetInfo(StringRef TheTriple, StringRef CPU, StringRef Features) const
MCSymbolizer * createMCSymbolizer(StringRef TT, LLVMOpInfoCallback GetOpInfo, LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo, MCContext *Ctx, std::unique_ptr< MCRelocationInfo > &&RelInfo) const
MCRelocationInfo * createMCRelocationInfo(StringRef TT, MCContext &Ctx) const
MCRegisterInfo * createMCRegInfo(StringRef TT) const
MCDisassembler * createMCDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx) const
MCAsmInfo * createMCAsmInfo(const MCRegisterInfo &MRI, StringRef TheTriple, const MCTargetOptions &Options) const
MCInstPrinter * createMCInstPrinter(const Triple &T, unsigned SyntaxVariant, const MCAsmInfo &MAI, const MCInstrInfo &MII, const MCRegisterInfo &MRI) const
MCInstrInfo * createMCInstrInfo() const
createMCInstrInfo - Create a MCInstrInfo implementation.
Triple - Helper class for working with autoconf configuration names.
virtual void enable_colors(bool enable)
A raw_ostream that writes to an SmallVector or SmallString.
const char *(* LLVMSymbolLookupCallback)(void *DisInfo, uint64_t ReferenceValue, uint64_t *ReferenceType, uint64_t ReferencePC, const char **ReferenceName)
The type for the symbol lookup function.
#define LLVMDisassembler_Option_PrintImmHex
#define LLVMDisassembler_Option_UseMarkup
void LLVMDisasmDispose(LLVMDisasmContextRef DCR)
Dispose of a disassembler context.
int LLVMSetDisasmOptions(LLVMDisasmContextRef DCR, uint64_t Options)
Set the disassembler's options.
#define LLVMDisassembler_Option_PrintLatency
LLVMDisasmContextRef LLVMCreateDisasmCPUFeatures(const char *TT, const char *CPU, const char *Features, void *DisInfo, int TagType, LLVMOpInfoCallback GetOpInfo, LLVMSymbolLookupCallback SymbolLookUp)
Create a disassembler for the TripleName, a specific CPU and specific feature string.
#define LLVMDisassembler_Option_SetInstrComments
LLVMDisasmContextRef LLVMCreateDisasm(const char *TT, void *DisInfo, int TagType, LLVMOpInfoCallback GetOpInfo, LLVMSymbolLookupCallback SymbolLookUp)
Create a disassembler for the TripleName.
#define LLVMDisassembler_Option_Color
int(* LLVMOpInfoCallback)(void *DisInfo, uint64_t PC, uint64_t Offset, uint64_t OpSize, uint64_t InstSize, int TagType, void *TagBuf)
The type for the operand information call back function.
size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes, uint64_t BytesSize, uint64_t PC, char *OutString, size_t OutStringSize)
Disassemble a single instruction using the disassembler context specified in the parameter DC.
LLVMDisasmContextRef LLVMCreateDisasmCPU(const char *TT, const char *CPU, void *DisInfo, int TagType, LLVMOpInfoCallback GetOpInfo, LLVMSymbolLookupCallback SymbolLookUp)
Create a disassembler for the TripleName and a specific CPU.
#define LLVMDisassembler_Option_AsmPrinterVariant
void * LLVMDisasmContextRef
An opaque reference to a disassembler context.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Machine model for scheduling, bundling, and heuristics.
static LLVM_ABI int computeInstrLatency(const MCSubtargetInfo &STI, const MCSchedClassDesc &SCDesc)
Returns the latency value for the scheduling class.
static const Target * lookupTarget(StringRef TripleStr, std::string &Error)
lookupTarget - Lookup a target based on a target triple.