blob: bf2f2767cd34419a6e687166c9aeb44e428ce0f8 [file] [log] [blame]
Kevin Enderby93f79362011-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 Lattner1a55c412011-05-22 04:44:48 +000010|* This header provides a public interface to a disassembler library. *|
Kevin Enderby93f79362011-03-28 18:25:07 +000011|* LLVM provides an implementation of this interface. *|
12|* *|
13\*===----------------------------------------------------------------------===*/
14
15#ifndef LLVM_C_DISASSEMBLER_H
Chris Lattner1a55c412011-05-22 04:44:48 +000016#define LLVM_C_DISASSEMBLER_H
Kevin Enderby93f79362011-03-28 18:25:07 +000017
Daniel Dunbar84704752011-03-29 02:30:34 +000018#include "llvm/Support/DataTypes.h"
Chris Lattner1a55c412011-05-22 04:44:48 +000019#include <stddef.h>
Kevin Enderby93f79362011-03-28 18:25:07 +000020
21/**
22 * An opaque reference to a disassembler context.
23 */
24typedef void *LLVMDisasmContextRef;
25
26/**
27 * The type for the operand information call back function. This is called to
28 * get the symbolic information for an operand of an instruction. Typically
29 * this is from the relocation information, symbol table, etc. That block of
30 * information is saved when the disassembler context is created and passed to
31 * the call back in the DisInfo parameter. The instruction containing operand
32 * is at the PC parameter. For some instruction sets, there can be more than
33 * one operand with symbolic information. To determine the symbolic operand
Chris Lattner7a2bdde2011-04-15 05:18:47 +000034 * information for each operand, the bytes for the specific operand in the
Kevin Enderby93f79362011-03-28 18:25:07 +000035 * instruction are specified by the Offset parameter and its byte widith is the
36 * size parameter. For instructions sets with fixed widths and one symbolic
37 * operand per instruction, the Offset parameter will be zero and Size parameter
38 * will be the instruction width. The information is returned in TagBuf and is
39 * Triple specific with its specific information defined by the value of
40 * TagType for that Triple. If symbolic information is returned the function
Chris Lattner1a55c412011-05-22 04:44:48 +000041 * returns 1, otherwise it returns 0.
Kevin Enderby93f79362011-03-28 18:25:07 +000042 */
Chris Lattner1a55c412011-05-22 04:44:48 +000043typedef int (*LLVMOpInfoCallback)(void *DisInfo, uint64_t PC,
44 uint64_t Offset, uint64_t Size,
45 int TagType, void *TagBuf);
Kevin Enderby93f79362011-03-28 18:25:07 +000046
47/**
Kevin Enderbybd332762011-04-11 18:08:50 +000048 * The initial support in LLVM MC for the most general form of a relocatable
49 * expression is "AddSymbol - SubtractSymbol + Offset". For some Darwin targets
50 * this full form is encoded in the relocation information so that AddSymbol and
51 * SubtractSymbol can be link edited independent of each other. Many other
52 * platforms only allow a relocatable expression of the form AddSymbol + Offset
53 * to be encoded.
54 *
55 * The LLVMOpInfoCallback() for the TagType value of 1 uses the struct
56 * LLVMOpInfo1. The value of the relocatable expression for the operand,
57 * including any PC adjustment, is passed in to the call back in the Value
58 * field. The symbolic information about the operand is returned using all
59 * the fields of the structure with the Offset of the relocatable expression
60 * returned in the Value field. It is possible that some symbols in the
61 * relocatable expression were assembly temporary symbols, for example
62 * "Ldata - LpicBase + constant", and only the Values of the symbols without
63 * symbol names are present in the relocation information. The VariantKind
64 * type is one of the Target specific #defines below and is used to print
65 * operands like "_foo@GOT", ":lower16:_foo", etc.
66 */
67struct LLVMOpInfoSymbol1 {
Chris Lattner1a55c412011-05-22 04:44:48 +000068 uint64_t Present; /* 1 if this symbol is present */
Kevin Enderby9e5887b2011-10-04 22:44:48 +000069 const char *Name; /* symbol name if not NULL */
Chris Lattner1a55c412011-05-22 04:44:48 +000070 uint64_t Value; /* symbol value if name is NULL */
Kevin Enderbybd332762011-04-11 18:08:50 +000071};
Chris Lattner1a55c412011-05-22 04:44:48 +000072
Kevin Enderbybd332762011-04-11 18:08:50 +000073struct LLVMOpInfo1 {
74 struct LLVMOpInfoSymbol1 AddSymbol;
75 struct LLVMOpInfoSymbol1 SubtractSymbol;
76 uint64_t Value;
77 uint64_t VariantKind;
78};
79
80/**
81 * The operand VariantKinds for symbolic disassembly.
82 */
83#define LLVMDisassembler_VariantKind_None 0 /* all targets */
84
85/**
86 * The ARM target VariantKinds.
87 */
88#define LLVMDisassembler_VariantKind_ARM_HI16 1 /* :upper16: */
89#define LLVMDisassembler_VariantKind_ARM_LO16 2 /* :lower16: */
90
91/**
Kevin Enderby93f79362011-03-28 18:25:07 +000092 * The type for the symbol lookup function. This may be called by the
Chris Lattner1a55c412011-05-22 04:44:48 +000093 * disassembler for things like adding a comment for a PC plus a constant
Kevin Enderby93f79362011-03-28 18:25:07 +000094 * offset load instruction to use a symbol name instead of a load address value.
95 * It is passed the block information is saved when the disassembler context is
Kevin Enderby9e5887b2011-10-04 22:44:48 +000096 * created and the ReferenceValue to look up as a symbol. If no symbol is found
97 * for the ReferenceValue NULL is returned. The ReferenceType of the
98 * instruction is passed indirectly as is the PC of the instruction in
99 * ReferencePC. If the output reference can be determined its type is returned
100 * indirectly in ReferenceType along with ReferenceName if any, or that is set
101 * to NULL.
Kevin Enderby93f79362011-03-28 18:25:07 +0000102 */
103typedef const char *(*LLVMSymbolLookupCallback)(void *DisInfo,
Kevin Enderby9e5887b2011-10-04 22:44:48 +0000104 uint64_t ReferenceValue,
105 uint64_t *ReferenceType,
106 uint64_t ReferencePC,
107 const char **ReferenceName);
108/**
109 * The reference types on input and output.
110 */
111/* No input reference type or no output reference type. */
112#define LLVMDisassembler_ReferenceType_InOut_None 0
113
114/* The input reference is from a branch instruction. */
115#define LLVMDisassembler_ReferenceType_In_Branch 1
116/* The input reference is from a PC relative load instruction. */
117#define LLVMDisassembler_ReferenceType_In_PCrel_Load 2
118
119/* The output reference is to as symbol stub. */
120#define LLVMDisassembler_ReferenceType_Out_SymbolStub 1
121/* The output reference is to a symbol address in a literal pool. */
122#define LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr 2
123/* The output reference is to a cstring address in a literal pool. */
124#define LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr 3
Kevin Enderby93f79362011-03-28 18:25:07 +0000125
126#ifdef __cplusplus
127extern "C" {
128#endif /* !defined(__cplusplus) */
129
130/**
131 * Create a disassembler for the TripleName. Symbolic disassembly is supported
Chris Lattner1a55c412011-05-22 04:44:48 +0000132 * by passing a block of information in the DisInfo parameter and specifying the
133 * TagType and callback functions as described above. These can all be passed
134 * as NULL. If successful, this returns a disassembler context. If not, it
Kevin Enderby93f79362011-03-28 18:25:07 +0000135 * returns NULL.
136 */
Chris Lattner1a55c412011-05-22 04:44:48 +0000137LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo,
138 int TagType, LLVMOpInfoCallback GetOpInfo,
139 LLVMSymbolLookupCallback SymbolLookUp);
Kevin Enderby93f79362011-03-28 18:25:07 +0000140
141/**
142 * Dispose of a disassembler context.
143 */
Chris Lattner1a55c412011-05-22 04:44:48 +0000144void LLVMDisasmDispose(LLVMDisasmContextRef DC);
Kevin Enderby93f79362011-03-28 18:25:07 +0000145
146/**
Chris Lattner1a55c412011-05-22 04:44:48 +0000147 * Disassemble a single instruction using the disassembler context specified in
148 * the parameter DC. The bytes of the instruction are specified in the
149 * parameter Bytes, and contains at least BytesSize number of bytes. The
150 * instruction is at the address specified by the PC parameter. If a valid
151 * instruction can be disassembled, its string is returned indirectly in
152 * OutString whose size is specified in the parameter OutStringSize. This
153 * function returns the number of bytes in the instruction or zero if there was
154 * no valid instruction.
Kevin Enderby93f79362011-03-28 18:25:07 +0000155 */
Chris Lattner1a55c412011-05-22 04:44:48 +0000156size_t LLVMDisasmInstruction(LLVMDisasmContextRef DC, uint8_t *Bytes,
157 uint64_t BytesSize, uint64_t PC,
158 char *OutString, size_t OutStringSize);
Kevin Enderby93f79362011-03-28 18:25:07 +0000159
160#ifdef __cplusplus
161}
162#endif /* !defined(__cplusplus) */
163
164#endif /* !defined(LLVM_C_DISASSEMBLER_H) */