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 \*===----------------------------------------------------------------------===*/
14 
15 module deimos.llvm.c.disassembler;
16 
17 import core.stdc.stdint : uint8_t, uint64_t;
18 import core.stdc.stddef;
19 
20 extern(C) nothrow:
21 
22 /**
23  * @defgroup LLVMCDisassembler Disassembler
24  * @ingroup LLVMC
25  *
26  * @{
27  */
28 
29 /**
30  * An opaque reference to a disassembler context.
31  */
32 alias void *LLVMDisasmContextRef;
33 
34 /**
35  * The type for the operand information call back function.  This is called to
36  * get the symbolic information for an operand of an instruction.  Typically
37  * this is from the relocation information, symbol table, etc.  That block of
38  * information is saved when the disassembler context is created and passed to
39  * the call back in the DisInfo parameter.  The instruction containing operand
40  * is at the PC parameter.  For some instruction sets, there can be more than
41  * one operand with symbolic information.  To determine the symbolic operand
42  * information for each operand, the bytes for the specific operand in the
43  * instruction are specified by the Offset parameter and its byte widith is the
44  * size parameter.  For instructions sets with fixed widths and one symbolic
45  * operand per instruction, the Offset parameter will be zero and Size parameter
46  * will be the instruction width.  The information is returned in TagBuf and is 
47  * Triple specific with its specific information defined by the value of
48  * TagType for that Triple.  If symbolic information is returned the function
49  * returns 1, otherwise it returns 0.
50  */
51 alias int function(void *DisInfo, uint64_t PC,
52                    uint64_t Offset, uint64_t Size,
53                    int TagType, void *TagBuf) LLVMOpInfoCallback;
54 
55 /**
56  * The initial support in LLVM MC for the most general form of a relocatable
57  * expression is "AddSymbol - SubtractSymbol + Offset".  For some Darwin targets
58  * this full form is encoded in the relocation information so that AddSymbol and
59  * SubtractSymbol can be link edited independent of each other.  Many other
60  * platforms only allow a relocatable expression of the form AddSymbol + Offset
61  * to be encoded.
62  * 
63  * The LLVMOpInfoCallback() for the TagType value of 1 uses the struct
64  * LLVMOpInfo1.  The value of the relocatable expression for the operand,
65  * including any PC adjustment, is passed in to the call back in the Value
66  * field.  The symbolic information about the operand is returned using all
67  * the fields of the structure with the Offset of the relocatable expression
68  * returned in the Value field.  It is possible that some symbols in the
69  * relocatable expression were assembly temporary symbols, for example
70  * "Ldata - LpicBase + constant", and only the Values of the symbols without
71  * symbol names are present in the relocation information.  The VariantKind
72  * type is one of the Target specific #defines below and is used to print
73  * operands like "_foo@GOT", ":lower16:_foo", etc.
74  */
75 struct LLVMOpInfoSymbol1 {
76   uint64_t Present;  /* 1 if this symbol is present */
77   const(char) *Name;  /* symbol name if not NULL */
78   uint64_t Value;    /* symbol value if name is NULL */
79 };
80 
81 struct LLVMOpInfo1 {
82   LLVMOpInfoSymbol1 AddSymbol;
83   LLVMOpInfoSymbol1 SubtractSymbol;
84   uint64_t Value;
85   uint64_t VariantKind;
86 };
87 
88 /**
89  * The operand VariantKinds for symbolic disassembly.
90  */
91 enum LLVMDisassembler_VariantKind_None = 0; /* all targets */
92 
93 /**
94  * The ARM target VariantKinds.
95  */
96 enum LLVMDisassembler_VariantKind_ARM_HI16 = 1; /* :upper16: */
97 enum LLVMDisassembler_VariantKind_ARM_LO16 = 2; /* :lower16: */
98 
99 /**
100  * The type for the symbol lookup function.  This may be called by the
101  * disassembler for things like adding a comment for a PC plus a constant
102  * offset load instruction to use a symbol name instead of a load address value.
103  * It is passed the block information is saved when the disassembler context is
104  * created and the ReferenceValue to look up as a symbol.  If no symbol is found
105  * for the ReferenceValue NULL is returned.  The ReferenceType of the
106  * instruction is passed indirectly as is the PC of the instruction in
107  * ReferencePC.  If the output reference can be determined its type is returned
108  * indirectly in ReferenceType along with ReferenceName if any, or that is set
109  * to NULL.
110  */
111 alias const(char) *function(void *DisInfo,
112                             uint64_t ReferenceValue,
113                             uint64_t *ReferenceType,
114                             uint64_t ReferencePC,
115                             const(char) **ReferenceName) LLVMSymbolLookupCallback;
116 /**
117  * The reference types on input and output.
118  */
119 /* No input reference type or no output reference type. */
120 enum LLVMDisassembler_ReferenceType_InOut_None = 0;
121 
122 /* The input reference is from a branch instruction. */
123 enum LLVMDisassembler_ReferenceType_In_Branch = 1;
124 /* The input reference is from a PC relative load instruction. */
125 enum LLVMDisassembler_ReferenceType_In_PCrel_Load = 2;
126 
127 /* The output reference is to as symbol stub. */
128 enum LLVMDisassembler_ReferenceType_Out_SymbolStub = 1;
129 /* The output reference is to a symbol address in a literal pool. */
130 enum LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr = 2;
131 /* The output reference is to a cstring address in a literal pool. */
132 enum LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr = 3;
133 
134 /**
135  * Create a disassembler for the TripleName.  Symbolic disassembly is supported
136  * by passing a block of information in the DisInfo parameter and specifying the
137  * TagType and callback functions as described above.  These can all be passed
138  * as NULL.  If successful, this returns a disassembler context.  If not, it
139  * returns NULL. This function is equivalent to calling LLVMCreateDisasmCPU()
140  * with an empty CPU name.
141  */
142 LLVMDisasmContextRef LLVMCreateDisasm(const(char) *TripleName, void *DisInfo,
143                                       int TagType, LLVMOpInfoCallback GetOpInfo,
144                                       LLVMSymbolLookupCallback SymbolLookUp);
145 
146 /**
147  * Create a disassembler for the TripleName and a specific CPU.  Symbolic
148  * disassembly is supported by passing a block of information in the DisInfo
149  * parameter and specifying the TagType and callback functions as described
150  * above.  These can all be passed * as NULL.  If successful, this returns a
151  * disassembler context.  If not, it returns NULL.
152  */
153 LLVMDisasmContextRef LLVMCreateDisasmCPU(const(char) *Triple, const(char) *CPU,
154                                          void *DisInfo, int TagType,
155                                          LLVMOpInfoCallback GetOpInfo,
156                                          LLVMSymbolLookupCallback SymbolLookUp);
157 
158 /**
159  * Set the disassembler's options.  Returns 1 if it can set the Options and 0
160  * otherwise.
161  */
162 int LLVMSetDisasmOptions(LLVMDisasmContextRef DC, uint64_t Options);
163 
164 /* The option to produce marked up assembly. */
165 enum LLVMDisassembler_Option_UseMarkup = 1;
166 /* The option to print immediates as hex. */
167 enum LLVMDisassembler_Option_PrintImmHex = 2;
168 /* The option use the other assembler printer variant */
169 enum LLVMDisassembler_Option_AsmPrinterVariant = 4;
170 
171 /**
172  * Dispose of a disassembler context.
173  */
174 void LLVMDisasmDispose(LLVMDisasmContextRef DC);
175 
176 /**
177  * Disassemble a single instruction using the disassembler context specified in
178  * the parameter DC.  The bytes of the instruction are specified in the
179  * parameter Bytes, and contains at least BytesSize number of bytes.  The
180  * instruction is at the address specified by the PC parameter.  If a valid
181  * instruction can be disassembled, its string is returned indirectly in
182  * OutString whose size is specified in the parameter OutStringSize.  This
183  * function returns the number of bytes in the instruction or zero if there was
184  * no valid instruction.
185  */
186 size_t LLVMDisasmInstruction(LLVMDisasmContextRef DC, uint8_t *Bytes,
187                              uint64_t BytesSize, uint64_t PC,
188                              char *OutString, size_t OutStringSize);
189 
190 /**
191  * @}
192  */