862
[darwin-xtools.git] / cctools / include / llvm-c / Disassembler.h
blob5fa79fa52653887c82eea129fd969d821e52f409
1 /*===-- llvm-c/Disassembler.h - Disassembler Public C Interface ---*- C -*-===*\
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 |*===----------------------------------------------------------------------===*|
9 |* *|
10 |* This header provides a public interface to a disassembler library. *|
11 |* LLVM provides an implementation of this interface. *|
12 |* *|
13 \*===----------------------------------------------------------------------===*/
15 #ifndef LLVM_C_DISASSEMBLER_H
16 #define LLVM_C_DISASSEMBLER_H
18 #include <stddef.h>
20 /**
21 * @defgroup LLVMCDisassembler Disassembler
22 * @ingroup LLVMC
24 * @{
27 /**
28 * An opaque reference to a disassembler context.
30 typedef void *LLVMDisasmContextRef;
32 /**
33 * The type for the operand information call back function. This is called to
34 * get the symbolic information for an operand of an instruction. Typically
35 * this is from the relocation information, symbol table, etc. That block of
36 * information is saved when the disassembler context is created and passed to
37 * the call back in the DisInfo parameter. The instruction containing operand
38 * is at the PC parameter. For some instruction sets, there can be more than
39 * one operand with symbolic information. To determine the symbolic operand
40 * information for each operand, the bytes for the specific operand in the
41 * instruction are specified by the Offset parameter and its byte widith is the
42 * size parameter. For instructions sets with fixed widths and one symbolic
43 * operand per instruction, the Offset parameter will be zero and Size parameter
44 * will be the instruction width. The information is returned in TagBuf and is
45 * Triple specific with its specific information defined by the value of
46 * TagType for that Triple. If symbolic information is returned the function
47 * returns 1, otherwise it returns 0.
49 typedef int (*LLVMOpInfoCallback)(void *DisInfo, uint64_t Pc,
50 uint64_t Offset, uint64_t Size,
51 int TagType, void *TagBuf);
53 /**
54 * The initial support in LLVM MC for the most general form of a relocatable
55 * expression is "AddSymbol - SubtractSymbol + Offset". For some Darwin targets
56 * this full form is encoded in the relocation information so that AddSymbol and
57 * SubtractSymbol can be link edited independent of each other. Many other
58 * platforms only allow a relocatable expression of the form AddSymbol + Offset
59 * to be encoded.
61 * The LLVMOpInfoCallback() for the TagType value of 1 uses the struct
62 * LLVMOpInfo1. The value of the relocatable expression for the operand,
63 * including any PC adjustment, is passed in to the call back in the Value
64 * field. The symbolic information about the operand is returned using all
65 * the fields of the structure with the Offset of the relocatable expression
66 * returned in the Value field. It is possible that some symbols in the
67 * relocatable expression were assembly temporary symbols, for example
68 * "Ldata - LpicBase + constant", and only the Values of the symbols without
69 * symbol names are present in the relocation information. The VariantKind
70 * type is one of the Target specific #defines below and is used to print
71 * operands like "_foo@GOT", ":lower16:_foo", etc.
73 struct LLVMOpInfoSymbol1 {
74 uint64_t Present; /* 1 if this symbol is present */
75 const char *Name; /* symbol name if not NULL */
76 uint64_t Value; /* symbol value if name is NULL */
79 struct LLVMOpInfo1 {
80 struct LLVMOpInfoSymbol1 AddSymbol;
81 struct LLVMOpInfoSymbol1 SubtractSymbol;
82 uint64_t Value;
83 uint64_t VariantKind;
86 /**
87 * The operand VariantKinds for symbolic disassembly.
89 #define LLVMDisassembler_VariantKind_None 0 /* all targets */
91 /**
92 * The ARM target VariantKinds.
94 #define LLVMDisassembler_VariantKind_ARM_HI16 1 /* :upper16: */
95 #define LLVMDisassembler_VariantKind_ARM_LO16 2 /* :lower16: */
97 /**
98 * The ARM64 target VariantKinds.
100 #define LLVMDisassembler_VariantKind_ARM64_PAGE 1 /* @page */
101 #define LLVMDisassembler_VariantKind_ARM64_PAGEOFF 2 /* @pageoff */
102 #define LLVMDisassembler_VariantKind_ARM64_GOTPAGE 3 /* @gotpage */
103 #define LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF 4 /* @gotpageoff */
104 #define LLVMDisassembler_VariantKind_ARM64_TLVP 5 /* @tvlppage */
105 #define LLVMDisassembler_VariantKind_ARM64_TLVOFF 6 /* @tvlppageoff */
108 * The type for the symbol lookup function. This may be called by the
109 * disassembler for things like adding a comment for a PC plus a constant
110 * offset load instruction to use a symbol name instead of a load address value.
111 * It is passed the block information is saved when the disassembler context is
112 * created and the ReferenceValue to look up as a symbol. If no symbol is found
113 * for the ReferenceValue NULL is returned. The ReferenceType of the
114 * instruction is passed indirectly as is the PC of the instruction in
115 * ReferencePC. If the output reference can be determined its type is returned
116 * indirectly in ReferenceType along with ReferenceName if any, or that is set
117 * to NULL.
119 typedef const char *(*LLVMSymbolLookupCallback)(void *DisInfo,
120 uint64_t ReferenceValue,
121 uint64_t *ReferenceType,
122 uint64_t ReferencePC,
123 const char **ReferenceName);
125 * The reference types on input and output.
127 /* No input reference type or no output reference type. */
128 #define LLVMDisassembler_ReferenceType_InOut_None 0
130 /* The input reference is from a branch instruction. */
131 #define LLVMDisassembler_ReferenceType_In_Branch 1
132 /* The input reference is from a PC relative load instruction. */
133 #define LLVMDisassembler_ReferenceType_In_PCrel_Load 2
135 /* The input reference is from an ARM64::ADRP instruction. */
136 #define LLVMDisassembler_ReferenceType_In_ARM64_ADRP 0x100000001
137 /* The input reference is from an ARM64::ADDXri instruction. */
138 #define LLVMDisassembler_ReferenceType_In_ARM64_ADDXri 0x100000002
139 /* The input reference is from an ARM64::LDRXui instruction. */
140 #define LLVMDisassembler_ReferenceType_In_ARM64_LDRXui 0x100000003
141 /* The input reference is from an ARM64::LDRXl instruction. */
142 #define LLVMDisassembler_ReferenceType_In_ARM64_LDRXl 0x100000004
143 /* The input reference is from an ARM64::ADR instruction. */
144 #define LLVMDisassembler_ReferenceType_In_ARM64_ADR 0x100000005
146 /* The output reference is to as symbol stub. */
147 #define LLVMDisassembler_ReferenceType_Out_SymbolStub 1
148 /* The output reference is to a symbol address in a literal pool. */
149 #define LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr 2
150 /* The output reference is to a cstring address in a literal pool. */
151 #define LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr 3
153 /* The output reference is to a Objective-C CoreFoundation string. */
154 #define LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref 4
155 /* The output reference is to a Objective-C message. */
156 #define LLVMDisassembler_ReferenceType_Out_Objc_Message 5
157 /* The output reference is to a Objective-C message ref. */
158 #define LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref 6
159 /* The output reference is to a Objective-C selector ref. */
160 #define LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref 7
161 /* The output reference is to a Objective-C class ref. */
162 #define LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref 8
164 /* The output reference is to a C++ symbol name. */
165 #define LLVMDisassembler_ReferenceType_DeMangled_Name 9
167 #ifdef __cplusplus
168 extern "C" {
169 #endif /* !defined(__cplusplus) */
172 * Create a disassembler for the TripleName. Symbolic disassembly is supported
173 * by passing a block of information in the DisInfo parameter and specifying the
174 * TagType and callback functions as described above. These can all be passed
175 * as NULL. If successful, this returns a disassembler context. If not, it
176 * returns NULL. This function is equivalent to calling LLVMCreateDisasmCPU()
177 * with an empty CPU name.
179 LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo,
180 int TagType, LLVMOpInfoCallback GetOpInfo,
181 LLVMSymbolLookupCallback SymbolLookUp);
184 * Create a disassembler for the TripleName and a specific CPU. Symbolic
185 * disassembly is supported by passing a block of information in the DisInfo
186 * parameter and specifying the TagType and callback functions as described
187 * above. These can all be passed * as NULL. If successful, this returns a
188 * disassembler context. If not, it returns NULL.
190 LLVMDisasmContextRef LLVMCreateDisasmCPU(const char *Triple, const char *CPU,
191 void *DisInfo, int TagType,
192 LLVMOpInfoCallback GetOpInfo,
193 LLVMSymbolLookupCallback SymbolLookUp);
196 * Set the disassembler's options. Returns 1 if it can set the Options and 0
197 * otherwise.
199 int LLVMSetDisasmOptions(LLVMDisasmContextRef DC, uint64_t Options);
201 /* The option to produce marked up assembly. */
202 #define LLVMDisassembler_Option_UseMarkup 1
203 /* The option to print immediates as hex. */
204 #define LLVMDisassembler_Option_PrintImmHex 2
205 /* The option use the other assembler printer variant */
206 #define LLVMDisassembler_Option_AsmPrinterVariant 4
207 /* The option to set comment on instructions */
208 #define LLVMDisassembler_Option_SetInstrComments 8
209 /* The option to print latency information alongside instructions */
210 #define LLVMDisassembler_Option_PrintLatency 16
213 * Dispose of a disassembler context.
215 void LLVMDisasmDispose(LLVMDisasmContextRef DC);
218 * Disassemble a single instruction using the disassembler context specified in
219 * the parameter DC. The bytes of the instruction are specified in the
220 * parameter Bytes, and contains at least BytesSize number of bytes. The
221 * instruction is at the address specified by the PC parameter. If a valid
222 * instruction can be disassembled, its string is returned indirectly in
223 * OutString whose size is specified in the parameter OutStringSize. This
224 * function returns the number of bytes in the instruction or zero if there was
225 * no valid instruction.
227 size_t LLVMDisasmInstruction(LLVMDisasmContextRef DC, uint8_t *Bytes,
228 uint64_t BytesSize, uint64_t Pc,
229 char *OutString, size_t OutStringSize);
232 * @}
235 #ifdef __cplusplus
237 #endif /* !defined(__cplusplus) */
239 #endif /* !defined(LLVM_C_DISASSEMBLER_H) */