Disassembler.h 12 KB

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