Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

530 lines
17 KiB

  1. /*===-- llvm-c/EnhancedDisassembly.h - Disassembler 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 declares the C interface to EnhancedDisassembly.so, which *|
  11. |* implements a disassembler with the ability to extract operand values and *|
  12. |* individual tokens from assembly instructions. *|
  13. |* *|
  14. |* The header declares additional interfaces if the host compiler supports *|
  15. |* the blocks API. *|
  16. |* *|
  17. \*===----------------------------------------------------------------------===*/
  18. #ifndef LLVM_C_ENHANCEDDISASSEMBLY_H
  19. #define LLVM_C_ENHANCEDDISASSEMBLY_H
  20. #include "llvm/Support/DataTypes.h"
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /**
  25. * @defgroup LLVMCEnhancedDisassembly Enhanced Disassembly
  26. * @ingroup LLVMC
  27. * @deprecated
  28. *
  29. * This module contains an interface to the Enhanced Disassembly (edis)
  30. * library. The edis library is deprecated and will likely disappear in
  31. * the near future. You should use the @ref LLVMCDisassembler interface
  32. * instead.
  33. *
  34. * @{
  35. */
  36. /*!
  37. @typedef EDByteReaderCallback
  38. Interface to memory from which instructions may be read.
  39. @param byte A pointer whose target should be filled in with the data returned.
  40. @param address The address of the byte to be read.
  41. @param arg An anonymous argument for client use.
  42. @result 0 on success; -1 otherwise.
  43. */
  44. typedef int (*EDByteReaderCallback)(uint8_t *byte, uint64_t address, void *arg);
  45. /*!
  46. @typedef EDRegisterReaderCallback
  47. Interface to registers from which registers may be read.
  48. @param value A pointer whose target should be filled in with the value of the
  49. register.
  50. @param regID The LLVM register identifier for the register to read.
  51. @param arg An anonymous argument for client use.
  52. @result 0 if the register could be read; -1 otherwise.
  53. */
  54. typedef int (*EDRegisterReaderCallback)(uint64_t *value, unsigned regID,
  55. void* arg);
  56. /*!
  57. @typedef EDAssemblySyntax_t
  58. An assembly syntax for use in tokenizing instructions.
  59. */
  60. enum {
  61. /*! @constant kEDAssemblySyntaxX86Intel Intel syntax for i386 and x86_64. */
  62. kEDAssemblySyntaxX86Intel = 0,
  63. /*! @constant kEDAssemblySyntaxX86ATT AT&T syntax for i386 and x86_64. */
  64. kEDAssemblySyntaxX86ATT = 1,
  65. kEDAssemblySyntaxARMUAL = 2
  66. };
  67. typedef unsigned EDAssemblySyntax_t;
  68. /*!
  69. @typedef EDDisassemblerRef
  70. Encapsulates a disassembler for a single CPU architecture.
  71. */
  72. typedef void *EDDisassemblerRef;
  73. /*!
  74. @typedef EDInstRef
  75. Encapsulates a single disassembled instruction in one assembly syntax.
  76. */
  77. typedef void *EDInstRef;
  78. /*!
  79. @typedef EDTokenRef
  80. Encapsulates a token from the disassembly of an instruction.
  81. */
  82. typedef void *EDTokenRef;
  83. /*!
  84. @typedef EDOperandRef
  85. Encapsulates an operand of an instruction.
  86. */
  87. typedef void *EDOperandRef;
  88. /*!
  89. @functiongroup Getting a disassembler
  90. */
  91. /*!
  92. @function EDGetDisassembler
  93. Gets the disassembler for a given target.
  94. @param disassembler A pointer whose target will be filled in with the
  95. disassembler.
  96. @param triple Identifies the target. Example: "x86_64-apple-darwin10"
  97. @param syntax The assembly syntax to use when decoding instructions.
  98. @result 0 on success; -1 otherwise.
  99. */
  100. int EDGetDisassembler(EDDisassemblerRef *disassembler,
  101. const char *triple,
  102. EDAssemblySyntax_t syntax);
  103. /*!
  104. @functiongroup Generic architectural queries
  105. */
  106. /*!
  107. @function EDGetRegisterName
  108. Gets the human-readable name for a given register.
  109. @param regName A pointer whose target will be pointed at the name of the
  110. register. The name does not need to be deallocated and will be
  111. @param disassembler The disassembler to query for the name.
  112. @param regID The register identifier, as returned by EDRegisterTokenValue.
  113. @result 0 on success; -1 otherwise.
  114. */
  115. int EDGetRegisterName(const char** regName,
  116. EDDisassemblerRef disassembler,
  117. unsigned regID);
  118. /*!
  119. @function EDRegisterIsStackPointer
  120. Determines if a register is one of the platform's stack-pointer registers.
  121. @param disassembler The disassembler to query.
  122. @param regID The register identifier, as returned by EDRegisterTokenValue.
  123. @result 1 if true; 0 otherwise.
  124. */
  125. int EDRegisterIsStackPointer(EDDisassemblerRef disassembler,
  126. unsigned regID);
  127. /*!
  128. @function EDRegisterIsProgramCounter
  129. Determines if a register is one of the platform's stack-pointer registers.
  130. @param disassembler The disassembler to query.
  131. @param regID The register identifier, as returned by EDRegisterTokenValue.
  132. @result 1 if true; 0 otherwise.
  133. */
  134. int EDRegisterIsProgramCounter(EDDisassemblerRef disassembler,
  135. unsigned regID);
  136. /*!
  137. @functiongroup Creating and querying instructions
  138. */
  139. /*!
  140. @function EDCreateInst
  141. Gets a set of contiguous instructions from a disassembler.
  142. @param insts A pointer to an array that will be filled in with the
  143. instructions. Must have at least count entries. Entries not filled in will
  144. be set to NULL.
  145. @param count The maximum number of instructions to fill in.
  146. @param disassembler The disassembler to use when decoding the instructions.
  147. @param byteReader The function to use when reading the instruction's machine
  148. code.
  149. @param address The address of the first byte of the instruction.
  150. @param arg An anonymous argument to be passed to byteReader.
  151. @result The number of instructions read on success; 0 otherwise.
  152. */
  153. unsigned int EDCreateInsts(EDInstRef *insts,
  154. unsigned int count,
  155. EDDisassemblerRef disassembler,
  156. EDByteReaderCallback byteReader,
  157. uint64_t address,
  158. void *arg);
  159. /*!
  160. @function EDReleaseInst
  161. Frees the memory for an instruction. The instruction can no longer be accessed
  162. after this call.
  163. @param inst The instruction to be freed.
  164. */
  165. void EDReleaseInst(EDInstRef inst);
  166. /*!
  167. @function EDInstByteSize
  168. @param inst The instruction to be queried.
  169. @result The number of bytes in the instruction's machine-code representation.
  170. */
  171. int EDInstByteSize(EDInstRef inst);
  172. /*!
  173. @function EDGetInstString
  174. Gets the disassembled text equivalent of the instruction.
  175. @param buf A pointer whose target will be filled in with a pointer to the
  176. string. (The string becomes invalid when the instruction is released.)
  177. @param inst The instruction to be queried.
  178. @result 0 on success; -1 otherwise.
  179. */
  180. int EDGetInstString(const char **buf,
  181. EDInstRef inst);
  182. /*!
  183. @function EDInstID
  184. @param instID A pointer whose target will be filled in with the LLVM identifier
  185. for the instruction.
  186. @param inst The instruction to be queried.
  187. @result 0 on success; -1 otherwise.
  188. */
  189. int EDInstID(unsigned *instID, EDInstRef inst);
  190. /*!
  191. @function EDInstIsBranch
  192. @param inst The instruction to be queried.
  193. @result 1 if the instruction is a branch instruction; 0 if it is some other
  194. type of instruction; -1 if there was an error.
  195. */
  196. int EDInstIsBranch(EDInstRef inst);
  197. /*!
  198. @function EDInstIsMove
  199. @param inst The instruction to be queried.
  200. @result 1 if the instruction is a move instruction; 0 if it is some other
  201. type of instruction; -1 if there was an error.
  202. */
  203. int EDInstIsMove(EDInstRef inst);
  204. /*!
  205. @function EDBranchTargetID
  206. @param inst The instruction to be queried.
  207. @result The ID of the branch target operand, suitable for use with
  208. EDCopyOperand. -1 if no such operand exists.
  209. */
  210. int EDBranchTargetID(EDInstRef inst);
  211. /*!
  212. @function EDMoveSourceID
  213. @param inst The instruction to be queried.
  214. @result The ID of the move source operand, suitable for use with
  215. EDCopyOperand. -1 if no such operand exists.
  216. */
  217. int EDMoveSourceID(EDInstRef inst);
  218. /*!
  219. @function EDMoveTargetID
  220. @param inst The instruction to be queried.
  221. @result The ID of the move source operand, suitable for use with
  222. EDCopyOperand. -1 if no such operand exists.
  223. */
  224. int EDMoveTargetID(EDInstRef inst);
  225. /*!
  226. @functiongroup Creating and querying tokens
  227. */
  228. /*!
  229. @function EDNumTokens
  230. @param inst The instruction to be queried.
  231. @result The number of tokens in the instruction, or -1 on error.
  232. */
  233. int EDNumTokens(EDInstRef inst);
  234. /*!
  235. @function EDGetToken
  236. Retrieves a token from an instruction. The token is valid until the
  237. instruction is released.
  238. @param token A pointer to be filled in with the token.
  239. @param inst The instruction to be queried.
  240. @param index The index of the token in the instruction.
  241. @result 0 on success; -1 otherwise.
  242. */
  243. int EDGetToken(EDTokenRef *token,
  244. EDInstRef inst,
  245. int index);
  246. /*!
  247. @function EDGetTokenString
  248. Gets the disassembled text for a token.
  249. @param buf A pointer whose target will be filled in with a pointer to the
  250. string. (The string becomes invalid when the token is released.)
  251. @param token The token to be queried.
  252. @result 0 on success; -1 otherwise.
  253. */
  254. int EDGetTokenString(const char **buf,
  255. EDTokenRef token);
  256. /*!
  257. @function EDOperandIndexForToken
  258. Returns the index of the operand to which a token belongs.
  259. @param token The token to be queried.
  260. @result The operand index on success; -1 otherwise
  261. */
  262. int EDOperandIndexForToken(EDTokenRef token);
  263. /*!
  264. @function EDTokenIsWhitespace
  265. @param token The token to be queried.
  266. @result 1 if the token is whitespace; 0 if not; -1 on error.
  267. */
  268. int EDTokenIsWhitespace(EDTokenRef token);
  269. /*!
  270. @function EDTokenIsPunctuation
  271. @param token The token to be queried.
  272. @result 1 if the token is punctuation; 0 if not; -1 on error.
  273. */
  274. int EDTokenIsPunctuation(EDTokenRef token);
  275. /*!
  276. @function EDTokenIsOpcode
  277. @param token The token to be queried.
  278. @result 1 if the token is opcode; 0 if not; -1 on error.
  279. */
  280. int EDTokenIsOpcode(EDTokenRef token);
  281. /*!
  282. @function EDTokenIsLiteral
  283. @param token The token to be queried.
  284. @result 1 if the token is a numeric literal; 0 if not; -1 on error.
  285. */
  286. int EDTokenIsLiteral(EDTokenRef token);
  287. /*!
  288. @function EDTokenIsRegister
  289. @param token The token to be queried.
  290. @result 1 if the token identifies a register; 0 if not; -1 on error.
  291. */
  292. int EDTokenIsRegister(EDTokenRef token);
  293. /*!
  294. @function EDTokenIsNegativeLiteral
  295. @param token The token to be queried.
  296. @result 1 if the token is a negative signed literal; 0 if not; -1 on error.
  297. */
  298. int EDTokenIsNegativeLiteral(EDTokenRef token);
  299. /*!
  300. @function EDLiteralTokenAbsoluteValue
  301. @param value A pointer whose target will be filled in with the absolute value
  302. of the literal.
  303. @param token The token to be queried.
  304. @result 0 on success; -1 otherwise.
  305. */
  306. int EDLiteralTokenAbsoluteValue(uint64_t *value,
  307. EDTokenRef token);
  308. /*!
  309. @function EDRegisterTokenValue
  310. @param registerID A pointer whose target will be filled in with the LLVM
  311. register identifier for the token.
  312. @param token The token to be queried.
  313. @result 0 on success; -1 otherwise.
  314. */
  315. int EDRegisterTokenValue(unsigned *registerID,
  316. EDTokenRef token);
  317. /*!
  318. @functiongroup Creating and querying operands
  319. */
  320. /*!
  321. @function EDNumOperands
  322. @param inst The instruction to be queried.
  323. @result The number of operands in the instruction, or -1 on error.
  324. */
  325. int EDNumOperands(EDInstRef inst);
  326. /*!
  327. @function EDGetOperand
  328. Retrieves an operand from an instruction. The operand is valid until the
  329. instruction is released.
  330. @param operand A pointer to be filled in with the operand.
  331. @param inst The instruction to be queried.
  332. @param index The index of the operand in the instruction.
  333. @result 0 on success; -1 otherwise.
  334. */
  335. int EDGetOperand(EDOperandRef *operand,
  336. EDInstRef inst,
  337. int index);
  338. /*!
  339. @function EDOperandIsRegister
  340. @param operand The operand to be queried.
  341. @result 1 if the operand names a register; 0 if not; -1 on error.
  342. */
  343. int EDOperandIsRegister(EDOperandRef operand);
  344. /*!
  345. @function EDOperandIsImmediate
  346. @param operand The operand to be queried.
  347. @result 1 if the operand specifies an immediate value; 0 if not; -1 on error.
  348. */
  349. int EDOperandIsImmediate(EDOperandRef operand);
  350. /*!
  351. @function EDOperandIsMemory
  352. @param operand The operand to be queried.
  353. @result 1 if the operand specifies a location in memory; 0 if not; -1 on error.
  354. */
  355. int EDOperandIsMemory(EDOperandRef operand);
  356. /*!
  357. @function EDRegisterOperandValue
  358. @param value A pointer whose target will be filled in with the LLVM register ID
  359. of the register named by the operand.
  360. @param operand The operand to be queried.
  361. @result 0 on success; -1 otherwise.
  362. */
  363. int EDRegisterOperandValue(unsigned *value,
  364. EDOperandRef operand);
  365. /*!
  366. @function EDImmediateOperandValue
  367. @param value A pointer whose target will be filled in with the value of the
  368. immediate.
  369. @param operand The operand to be queried.
  370. @result 0 on success; -1 otherwise.
  371. */
  372. int EDImmediateOperandValue(uint64_t *value,
  373. EDOperandRef operand);
  374. /*!
  375. @function EDEvaluateOperand
  376. Evaluates an operand using a client-supplied register state accessor. Register
  377. operands are evaluated by reading the value of the register; immediate operands
  378. are evaluated by reporting the immediate value; memory operands are evaluated
  379. by computing the target address (with only those relocations applied that were
  380. already applied to the original bytes).
  381. @param result A pointer whose target is to be filled with the result of
  382. evaluating the operand.
  383. @param operand The operand to be evaluated.
  384. @param regReader The function to use when reading registers from the register
  385. state.
  386. @param arg An anonymous argument for client use.
  387. @result 0 if the operand could be evaluated; -1 otherwise.
  388. */
  389. int EDEvaluateOperand(uint64_t *result,
  390. EDOperandRef operand,
  391. EDRegisterReaderCallback regReader,
  392. void *arg);
  393. #ifdef __BLOCKS__
  394. /*!
  395. @typedef EDByteBlock_t
  396. Block-based interface to memory from which instructions may be read.
  397. @param byte A pointer whose target should be filled in with the data returned.
  398. @param address The address of the byte to be read.
  399. @result 0 on success; -1 otherwise.
  400. */
  401. typedef int (^EDByteBlock_t)(uint8_t *byte, uint64_t address);
  402. /*!
  403. @typedef EDRegisterBlock_t
  404. Block-based interface to registers from which registers may be read.
  405. @param value A pointer whose target should be filled in with the value of the
  406. register.
  407. @param regID The LLVM register identifier for the register to read.
  408. @result 0 if the register could be read; -1 otherwise.
  409. */
  410. typedef int (^EDRegisterBlock_t)(uint64_t *value, unsigned regID);
  411. /*!
  412. @typedef EDTokenVisitor_t
  413. Block-based handler for individual tokens.
  414. @param token The current token being read.
  415. @result 0 to continue; 1 to stop normally; -1 on error.
  416. */
  417. typedef int (^EDTokenVisitor_t)(EDTokenRef token);
  418. /*! @functiongroup Block-based interfaces */
  419. /*!
  420. @function EDBlockCreateInsts
  421. Gets a set of contiguous instructions from a disassembler, using a block to
  422. read memory.
  423. @param insts A pointer to an array that will be filled in with the
  424. instructions. Must have at least count entries. Entries not filled in will
  425. be set to NULL.
  426. @param count The maximum number of instructions to fill in.
  427. @param disassembler The disassembler to use when decoding the instructions.
  428. @param byteBlock The block to use when reading the instruction's machine
  429. code.
  430. @param address The address of the first byte of the instruction.
  431. @result The number of instructions read on success; 0 otherwise.
  432. */
  433. unsigned int EDBlockCreateInsts(EDInstRef *insts,
  434. int count,
  435. EDDisassemblerRef disassembler,
  436. EDByteBlock_t byteBlock,
  437. uint64_t address);
  438. /*!
  439. @function EDBlockEvaluateOperand
  440. Evaluates an operand using a block to read registers.
  441. @param result A pointer whose target is to be filled with the result of
  442. evaluating the operand.
  443. @param operand The operand to be evaluated.
  444. @param regBlock The block to use when reading registers from the register
  445. state.
  446. @result 0 if the operand could be evaluated; -1 otherwise.
  447. */
  448. int EDBlockEvaluateOperand(uint64_t *result,
  449. EDOperandRef operand,
  450. EDRegisterBlock_t regBlock);
  451. /*!
  452. @function EDBlockVisitTokens
  453. Visits every token with a visitor.
  454. @param inst The instruction with the tokens to be visited.
  455. @param visitor The visitor.
  456. @result 0 if the visit ended normally; -1 if the visitor encountered an error
  457. or there was some other error.
  458. */
  459. int EDBlockVisitTokens(EDInstRef inst,
  460. EDTokenVisitor_t visitor);
  461. /**
  462. * @}
  463. */
  464. #endif
  465. #ifdef __cplusplus
  466. }
  467. #endif
  468. #endif