blob: 9f10973404b7941eb48d446f89056ae03d0ea9b1 [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|* *|
10|* This header provides public interface to a disassembler library. *|
11|* LLVM provides an implementation of this interface. *|
12|* *|
13\*===----------------------------------------------------------------------===*/
14
15#ifndef LLVM_C_DISASSEMBLER_H
16#define LLVM_C_DISASSEMBLER_H 1
17
Kevin Enderby93f79362011-03-28 18:25:07 +000018#include <stddef.h>
Daniel Dunbar84704752011-03-29 02:30:34 +000019#include "llvm/Support/DataTypes.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
41 * returns 1 else it returns 0.
42 */
43typedef int (*LLVMOpInfoCallback)(void *DisInfo,
44 uint64_t PC,
45 uint64_t Offset,
46 uint64_t Size,
47 int TagType,
48 void *TagBuf);
49
50/**
Kevin Enderbybd332762011-04-11 18:08:50 +000051 * The initial support in LLVM MC for the most general form of a relocatable
52 * expression is "AddSymbol - SubtractSymbol + Offset". For some Darwin targets
53 * this full form is encoded in the relocation information so that AddSymbol and
54 * SubtractSymbol can be link edited independent of each other. Many other
55 * platforms only allow a relocatable expression of the form AddSymbol + Offset
56 * to be encoded.
57 *
58 * The LLVMOpInfoCallback() for the TagType value of 1 uses the struct
59 * LLVMOpInfo1. The value of the relocatable expression for the operand,
60 * including any PC adjustment, is passed in to the call back in the Value
61 * field. The symbolic information about the operand is returned using all
62 * the fields of the structure with the Offset of the relocatable expression
63 * returned in the Value field. It is possible that some symbols in the
64 * relocatable expression were assembly temporary symbols, for example
65 * "Ldata - LpicBase + constant", and only the Values of the symbols without
66 * symbol names are present in the relocation information. The VariantKind
67 * type is one of the Target specific #defines below and is used to print
68 * operands like "_foo@GOT", ":lower16:_foo", etc.
69 */
70struct LLVMOpInfoSymbol1 {
71 uint64_t Present; /* 1 if this symbol is present */
72 char *Name; /* symbol name if not NULL */
73 uint64_t Value; /* symbol value if name is NULL */
74};
75struct LLVMOpInfo1 {
76 struct LLVMOpInfoSymbol1 AddSymbol;
77 struct LLVMOpInfoSymbol1 SubtractSymbol;
78 uint64_t Value;
79 uint64_t VariantKind;
80};
81
82/**
83 * The operand VariantKinds for symbolic disassembly.
84 */
85#define LLVMDisassembler_VariantKind_None 0 /* all targets */
86
87/**
88 * The ARM target VariantKinds.
89 */
90#define LLVMDisassembler_VariantKind_ARM_HI16 1 /* :upper16: */
91#define LLVMDisassembler_VariantKind_ARM_LO16 2 /* :lower16: */
92
93/**
Kevin Enderby93f79362011-03-28 18:25:07 +000094 * The type for the symbol lookup function. This may be called by the
95 * disassembler for such things like adding a comment for a PC plus a constant
96 * offset load instruction to use a symbol name instead of a load address value.
97 * It is passed the block information is saved when the disassembler context is
98 * created and a value of a symbol to look up. If no symbol is found NULL is
99 * to be returned.
100 */
101typedef const char *(*LLVMSymbolLookupCallback)(void *DisInfo,
102 uint64_t SymbolValue);
103
104#ifdef __cplusplus
105extern "C" {
106#endif /* !defined(__cplusplus) */
107
108/**
109 * Create a disassembler for the TripleName. Symbolic disassembly is supported
110 * by passing a block of information in the DisInfo parameter and specifing the
111 * TagType and call back functions as described above. These can all be passed
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000112 * as NULL. If successful this returns a disassembler context if not it
Kevin Enderby93f79362011-03-28 18:25:07 +0000113 * returns NULL.
114 */
115extern LLVMDisasmContextRef
116LLVMCreateDisasm(const char *TripleName,
117 void *DisInfo,
118 int TagType,
119 LLVMOpInfoCallback GetOpInfo,
120 LLVMSymbolLookupCallback SymbolLookUp);
121
122/**
123 * Dispose of a disassembler context.
124 */
125extern void
126LLVMDisasmDispose(LLVMDisasmContextRef DC);
127
128/**
129 * Disassmble a single instruction using the disassembler context specified in
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000130 * the parameter DC. The bytes of the instruction are specified in the parameter
Kevin Enderby93f79362011-03-28 18:25:07 +0000131 * Bytes, and contains at least BytesSize number of bytes. The instruction is
132 * at the address specified by the PC parameter. If a valid instruction can be
133 * disassembled its string is returned indirectly in OutString which whos size
134 * is specified in the parameter OutStringSize. This function returns the
135 * number of bytes in the instruction or zero if there was no valid instruction.
136 */
137extern size_t
138LLVMDisasmInstruction(LLVMDisasmContextRef DC,
139 uint8_t *Bytes,
140 uint64_t BytesSize,
141 uint64_t PC,
142 char *OutString,
143 size_t OutStringSize);
144
145#ifdef __cplusplus
146}
147#endif /* !defined(__cplusplus) */
148
149#endif /* !defined(LLVM_C_DISASSEMBLER_H) */