Adding a C API to the disassembler for use by such tools as Darwin's otool(1).
[oota-llvm.git] / include / llvm-c / Disassembler.h
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 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
18 #include <stdint.h>
19 #include <stddef.h>
20
21 /**
22  * An opaque reference to a disassembler context.
23  */
24 typedef 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
34  * infomation for each operand, the bytes for the specific operand in the
35  * 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  */
43 typedef int (*LLVMOpInfoCallback)(void *DisInfo,
44                                   uint64_t PC,
45                                   uint64_t Offset,
46                                   uint64_t Size,
47                                   int TagType,
48                                   void *TagBuf);
49
50 /**
51  * The type for the symbol lookup function.  This may be called by the
52  * disassembler for such things like adding a comment for a PC plus a constant
53  * offset load instruction to use a symbol name instead of a load address value.
54  * It is passed the block information is saved when the disassembler context is
55  * created and a value of a symbol to look up.  If no symbol is found NULL is
56  * to be returned.
57  */
58 typedef const char *(*LLVMSymbolLookupCallback)(void *DisInfo,
59                                                 uint64_t SymbolValue);
60
61 #ifdef __cplusplus
62 extern "C" {
63 #endif /* !defined(__cplusplus) */
64
65 /**
66  * Create a disassembler for the TripleName.  Symbolic disassembly is supported
67  * by passing a block of information in the DisInfo parameter and specifing the
68  * TagType and call back functions as described above.  These can all be passed
69  * as NULL.  If successfull this returns a disassembler context if not it
70  * returns NULL.
71  */
72 extern LLVMDisasmContextRef
73 LLVMCreateDisasm(const char *TripleName,
74                  void *DisInfo,
75                  int TagType,
76                  LLVMOpInfoCallback GetOpInfo,
77                  LLVMSymbolLookupCallback SymbolLookUp);
78
79 /**
80  * Dispose of a disassembler context.
81  */
82 extern void
83 LLVMDisasmDispose(LLVMDisasmContextRef DC);
84
85 /**
86  * Disassmble a single instruction using the disassembler context specified in
87  * the parameter DC.  The bytes of the instuction are specified in the parameter
88  * Bytes, and contains at least BytesSize number of bytes.  The instruction is
89  * at the address specified by the PC parameter.  If a valid instruction can be
90  * disassembled its string is returned indirectly in OutString which whos size
91  * is specified in the parameter OutStringSize.  This function returns the
92  * number of bytes in the instruction or zero if there was no valid instruction.
93  */
94 extern size_t
95 LLVMDisasmInstruction(LLVMDisasmContextRef DC,
96                       uint8_t *Bytes,
97                       uint64_t BytesSize,
98                       uint64_t PC,
99                       char *OutString,
100                       size_t OutStringSize);
101
102 #ifdef __cplusplus
103 }
104 #endif /* !defined(__cplusplus) */
105
106 #endif /* !defined(LLVM_C_DISASSEMBLER_H) */