blob: 8f31150ad91db6faec1004a3101b3f23a64a2310 [file] [log] [blame]
Kevin Enderbyf3070dc2011-03-28 18:25:07 +00001/*===-- 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|* *|
Chris Lattnere8bf2d52011-05-22 04:44:48 +000010|* This header provides a public interface to a disassembler library. *|
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000011|* LLVM provides an implementation of this interface. *|
12|* *|
13\*===----------------------------------------------------------------------===*/
14
15#ifndef LLVM_C_DISASSEMBLER_H
Chris Lattnere8bf2d52011-05-22 04:44:48 +000016#define LLVM_C_DISASSEMBLER_H
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000017
Daniel Dunbar30d0bdd2011-03-29 02:30:34 +000018#include "llvm/Support/DataTypes.h"
Chris Lattnere8bf2d52011-05-22 04:44:48 +000019#include <stddef.h>
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000020
21/**
Gregory Szorc34c863a2012-03-21 03:54:29 +000022 * @defgroup LLVMCDisassembler Disassembler
23 * @ingroup LLVMC
24 *
25 * @{
26 */
27
28/**
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000029 * An opaque reference to a disassembler context.
30 */
31typedef void *LLVMDisasmContextRef;
32
33/**
34 * The type for the operand information call back function. This is called to
35 * get the symbolic information for an operand of an instruction. Typically
36 * this is from the relocation information, symbol table, etc. That block of
37 * information is saved when the disassembler context is created and passed to
38 * the call back in the DisInfo parameter. The instruction containing operand
39 * is at the PC parameter. For some instruction sets, there can be more than
40 * one operand with symbolic information. To determine the symbolic operand
Chris Lattner0ab5e2c2011-04-15 05:18:47 +000041 * information for each operand, the bytes for the specific operand in the
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000042 * instruction are specified by the Offset parameter and its byte widith is the
43 * size parameter. For instructions sets with fixed widths and one symbolic
44 * operand per instruction, the Offset parameter will be zero and Size parameter
NAKAMURA Takumia3a81352013-10-23 17:56:29 +000045 * will be the instruction width. The information is returned in TagBuf and is
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000046 * Triple specific with its specific information defined by the value of
47 * TagType for that Triple. If symbolic information is returned the function
Chris Lattnere8bf2d52011-05-22 04:44:48 +000048 * returns 1, otherwise it returns 0.
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000049 */
Chris Lattnere8bf2d52011-05-22 04:44:48 +000050typedef int (*LLVMOpInfoCallback)(void *DisInfo, uint64_t PC,
51 uint64_t Offset, uint64_t Size,
52 int TagType, void *TagBuf);
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000053
54/**
Kevin Enderby9377a522011-04-11 18:08:50 +000055 * The initial support in LLVM MC for the most general form of a relocatable
56 * expression is "AddSymbol - SubtractSymbol + Offset". For some Darwin targets
57 * this full form is encoded in the relocation information so that AddSymbol and
58 * SubtractSymbol can be link edited independent of each other. Many other
59 * platforms only allow a relocatable expression of the form AddSymbol + Offset
60 * to be encoded.
NAKAMURA Takumia3a81352013-10-23 17:56:29 +000061 *
Kevin Enderby9377a522011-04-11 18:08:50 +000062 * The LLVMOpInfoCallback() for the TagType value of 1 uses the struct
63 * LLVMOpInfo1. The value of the relocatable expression for the operand,
64 * including any PC adjustment, is passed in to the call back in the Value
65 * field. The symbolic information about the operand is returned using all
66 * the fields of the structure with the Offset of the relocatable expression
67 * returned in the Value field. It is possible that some symbols in the
68 * relocatable expression were assembly temporary symbols, for example
69 * "Ldata - LpicBase + constant", and only the Values of the symbols without
70 * symbol names are present in the relocation information. The VariantKind
71 * type is one of the Target specific #defines below and is used to print
72 * operands like "_foo@GOT", ":lower16:_foo", etc.
73 */
74struct LLVMOpInfoSymbol1 {
Chris Lattnere8bf2d52011-05-22 04:44:48 +000075 uint64_t Present; /* 1 if this symbol is present */
Kevin Enderby5dcda642011-10-04 22:44:48 +000076 const char *Name; /* symbol name if not NULL */
Chris Lattnere8bf2d52011-05-22 04:44:48 +000077 uint64_t Value; /* symbol value if name is NULL */
Kevin Enderby9377a522011-04-11 18:08:50 +000078};
Chris Lattnere8bf2d52011-05-22 04:44:48 +000079
Kevin Enderby9377a522011-04-11 18:08:50 +000080struct LLVMOpInfo1 {
81 struct LLVMOpInfoSymbol1 AddSymbol;
82 struct LLVMOpInfoSymbol1 SubtractSymbol;
83 uint64_t Value;
84 uint64_t VariantKind;
85};
86
87/**
88 * The operand VariantKinds for symbolic disassembly.
89 */
90#define LLVMDisassembler_VariantKind_None 0 /* all targets */
91
92/**
93 * The ARM target VariantKinds.
94 */
95#define LLVMDisassembler_VariantKind_ARM_HI16 1 /* :upper16: */
96#define LLVMDisassembler_VariantKind_ARM_LO16 2 /* :lower16: */
97
98/**
Tim Northover00ed9962014-03-29 10:18:08 +000099 * The ARM64 target VariantKinds.
100 */
101#define LLVMDisassembler_VariantKind_ARM64_PAGE 1 /* @page */
102#define LLVMDisassembler_VariantKind_ARM64_PAGEOFF 2 /* @pageoff */
103#define LLVMDisassembler_VariantKind_ARM64_GOTPAGE 3 /* @gotpage */
104#define LLVMDisassembler_VariantKind_ARM64_GOTPAGEOFF 4 /* @gotpageoff */
105#define LLVMDisassembler_VariantKind_ARM64_TLVP 5 /* @tvlppage */
106#define LLVMDisassembler_VariantKind_ARM64_TLVOFF 6 /* @tvlppageoff */
107
108/**
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000109 * The type for the symbol lookup function. This may be called by the
Chris Lattnere8bf2d52011-05-22 04:44:48 +0000110 * disassembler for things like adding a comment for a PC plus a constant
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000111 * offset load instruction to use a symbol name instead of a load address value.
112 * It is passed the block information is saved when the disassembler context is
Kevin Enderby5dcda642011-10-04 22:44:48 +0000113 * created and the ReferenceValue to look up as a symbol. If no symbol is found
114 * for the ReferenceValue NULL is returned. The ReferenceType of the
115 * instruction is passed indirectly as is the PC of the instruction in
116 * ReferencePC. If the output reference can be determined its type is returned
117 * indirectly in ReferenceType along with ReferenceName if any, or that is set
118 * to NULL.
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000119 */
120typedef const char *(*LLVMSymbolLookupCallback)(void *DisInfo,
Kevin Enderby5dcda642011-10-04 22:44:48 +0000121 uint64_t ReferenceValue,
Bill Wendling0de59132012-07-19 00:01:33 +0000122 uint64_t *ReferenceType,
123 uint64_t ReferencePC,
124 const char **ReferenceName);
Kevin Enderby5dcda642011-10-04 22:44:48 +0000125/**
126 * The reference types on input and output.
127 */
128/* No input reference type or no output reference type. */
129#define LLVMDisassembler_ReferenceType_InOut_None 0
130
131/* The input reference is from a branch instruction. */
132#define LLVMDisassembler_ReferenceType_In_Branch 1
133/* The input reference is from a PC relative load instruction. */
134#define LLVMDisassembler_ReferenceType_In_PCrel_Load 2
135
Tim Northover00ed9962014-03-29 10:18:08 +0000136/* The input reference is from an ARM64::ADRP instruction. */
137#define LLVMDisassembler_ReferenceType_In_ARM64_ADRP 0x100000001
138/* The input reference is from an ARM64::ADDXri instruction. */
139#define LLVMDisassembler_ReferenceType_In_ARM64_ADDXri 0x100000002
140/* The input reference is from an ARM64::LDRXui instruction. */
141#define LLVMDisassembler_ReferenceType_In_ARM64_LDRXui 0x100000003
142/* The input reference is from an ARM64::LDRXl instruction. */
143#define LLVMDisassembler_ReferenceType_In_ARM64_LDRXl 0x100000004
144/* The input reference is from an ARM64::ADR instruction. */
145#define LLVMDisassembler_ReferenceType_In_ARM64_ADR 0x100000005
146
Kevin Enderby5dcda642011-10-04 22:44:48 +0000147/* The output reference is to as symbol stub. */
148#define LLVMDisassembler_ReferenceType_Out_SymbolStub 1
149/* The output reference is to a symbol address in a literal pool. */
150#define LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr 2
151/* The output reference is to a cstring address in a literal pool. */
152#define LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr 3
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000153
Kevin Enderby3c5ac812013-11-01 00:00:07 +0000154/* The output reference is to a Objective-C CoreFoundation string. */
155#define LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref 4
156/* The output reference is to a Objective-C message. */
157#define LLVMDisassembler_ReferenceType_Out_Objc_Message 5
158/* The output reference is to a Objective-C message ref. */
159#define LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref 6
160/* The output reference is to a Objective-C selector ref. */
161#define LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref 7
162/* The output reference is to a Objective-C class ref. */
163#define LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref 8
164
Kevin Enderbyf16c8c52014-01-06 22:08:08 +0000165/* The output reference is to a C++ symbol name. */
166#define LLVMDisassembler_ReferenceType_DeMangled_Name 9
167
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000168#ifdef __cplusplus
169extern "C" {
170#endif /* !defined(__cplusplus) */
171
172/**
173 * Create a disassembler for the TripleName. Symbolic disassembly is supported
Chris Lattnere8bf2d52011-05-22 04:44:48 +0000174 * by passing a block of information in the DisInfo parameter and specifying the
175 * TagType and callback functions as described above. These can all be passed
176 * as NULL. If successful, this returns a disassembler context. If not, it
Jim Grosbach0ca9d5b2012-12-07 23:53:27 +0000177 * returns NULL. This function is equivalent to calling LLVMCreateDisasmCPU()
178 * with an empty CPU name.
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000179 */
Chris Lattnere8bf2d52011-05-22 04:44:48 +0000180LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo,
181 int TagType, LLVMOpInfoCallback GetOpInfo,
182 LLVMSymbolLookupCallback SymbolLookUp);
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000183
184/**
Jim Grosbach0ca9d5b2012-12-07 23:53:27 +0000185 * Create a disassembler for the TripleName and a specific CPU. Symbolic
186 * disassembly is supported by passing a block of information in the DisInfo
187 * parameter and specifying the TagType and callback functions as described
188 * above. These can all be passed * as NULL. If successful, this returns a
189 * disassembler context. If not, it returns NULL.
190 */
191LLVMDisasmContextRef LLVMCreateDisasmCPU(const char *Triple, const char *CPU,
192 void *DisInfo, int TagType,
193 LLVMOpInfoCallback GetOpInfo,
194 LLVMSymbolLookupCallback SymbolLookUp);
195
196/**
Kevin Enderby62183c42012-10-22 22:31:46 +0000197 * Set the disassembler's options. Returns 1 if it can set the Options and 0
198 * otherwise.
199 */
200int LLVMSetDisasmOptions(LLVMDisasmContextRef DC, uint64_t Options);
201
202/* The option to produce marked up assembly. */
203#define LLVMDisassembler_Option_UseMarkup 1
Kevin Enderby168ffb32012-12-05 18:13:19 +0000204/* The option to print immediates as hex. */
205#define LLVMDisassembler_Option_PrintImmHex 2
Kevin Enderby85cf5312012-12-18 23:47:28 +0000206/* The option use the other assembler printer variant */
207#define LLVMDisassembler_Option_AsmPrinterVariant 4
Quentin Colombet93a98aa2013-10-01 22:14:56 +0000208/* The option to set comment on instructions */
209#define LLVMDisassembler_Option_SetInstrComments 8
Quentin Colombet5f09cb02013-10-02 22:07:57 +0000210 /* The option to print latency information alongside instructions */
211#define LLVMDisassembler_Option_PrintLatency 16
Kevin Enderby62183c42012-10-22 22:31:46 +0000212
213/**
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000214 * Dispose of a disassembler context.
215 */
Chris Lattnere8bf2d52011-05-22 04:44:48 +0000216void LLVMDisasmDispose(LLVMDisasmContextRef DC);
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000217
218/**
Chris Lattnere8bf2d52011-05-22 04:44:48 +0000219 * Disassemble a single instruction using the disassembler context specified in
220 * the parameter DC. The bytes of the instruction are specified in the
221 * parameter Bytes, and contains at least BytesSize number of bytes. The
222 * instruction is at the address specified by the PC parameter. If a valid
223 * instruction can be disassembled, its string is returned indirectly in
224 * OutString whose size is specified in the parameter OutStringSize. This
225 * function returns the number of bytes in the instruction or zero if there was
226 * no valid instruction.
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000227 */
Chris Lattnere8bf2d52011-05-22 04:44:48 +0000228size_t LLVMDisasmInstruction(LLVMDisasmContextRef DC, uint8_t *Bytes,
229 uint64_t BytesSize, uint64_t PC,
230 char *OutString, size_t OutStringSize);
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000231
Gregory Szorc34c863a2012-03-21 03:54:29 +0000232/**
233 * @}
234 */
235
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000236#ifdef __cplusplus
237}
238#endif /* !defined(__cplusplus) */
239
240#endif /* !defined(LLVM_C_DISASSEMBLER_H) */