Disassembler.h 12 KB

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