MILexer.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //===- MILexer.h - Lexer for machine instructions ---------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file declares the function that lexes the machine instruction source
  10. // string.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
  14. #define LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
  15. #include "llvm/ADT/APSInt.h"
  16. #include "llvm/ADT/StringRef.h"
  17. #include <string>
  18. namespace llvm {
  19. class Twine;
  20. /// A token produced by the machine instruction lexer.
  21. struct MIToken {
  22. enum TokenKind {
  23. // Markers
  24. Eof,
  25. Error,
  26. Newline,
  27. // Tokens with no info.
  28. comma,
  29. equal,
  30. underscore,
  31. colon,
  32. coloncolon,
  33. dot,
  34. exclaim,
  35. lparen,
  36. rparen,
  37. lbrace,
  38. rbrace,
  39. plus,
  40. minus,
  41. less,
  42. greater,
  43. // Keywords
  44. kw_implicit,
  45. kw_implicit_define,
  46. kw_def,
  47. kw_dead,
  48. kw_dereferenceable,
  49. kw_killed,
  50. kw_undef,
  51. kw_internal,
  52. kw_early_clobber,
  53. kw_debug_use,
  54. kw_renamable,
  55. kw_tied_def,
  56. kw_frame_setup,
  57. kw_frame_destroy,
  58. kw_nnan,
  59. kw_ninf,
  60. kw_nsz,
  61. kw_arcp,
  62. kw_contract,
  63. kw_afn,
  64. kw_reassoc,
  65. kw_nuw,
  66. kw_nsw,
  67. kw_exact,
  68. kw_nofpexcept,
  69. kw_debug_location,
  70. kw_debug_instr_number,
  71. kw_dbg_instr_ref,
  72. kw_cfi_same_value,
  73. kw_cfi_offset,
  74. kw_cfi_rel_offset,
  75. kw_cfi_def_cfa_register,
  76. kw_cfi_def_cfa_offset,
  77. kw_cfi_adjust_cfa_offset,
  78. kw_cfi_escape,
  79. kw_cfi_def_cfa,
  80. kw_cfi_llvm_def_aspace_cfa,
  81. kw_cfi_register,
  82. kw_cfi_remember_state,
  83. kw_cfi_restore,
  84. kw_cfi_restore_state,
  85. kw_cfi_undefined,
  86. kw_cfi_window_save,
  87. kw_cfi_aarch64_negate_ra_sign_state,
  88. kw_blockaddress,
  89. kw_intrinsic,
  90. kw_target_index,
  91. kw_half,
  92. kw_float,
  93. kw_double,
  94. kw_x86_fp80,
  95. kw_fp128,
  96. kw_ppc_fp128,
  97. kw_target_flags,
  98. kw_volatile,
  99. kw_non_temporal,
  100. kw_invariant,
  101. kw_align,
  102. kw_basealign,
  103. kw_addrspace,
  104. kw_stack,
  105. kw_got,
  106. kw_jump_table,
  107. kw_constant_pool,
  108. kw_call_entry,
  109. kw_custom,
  110. kw_liveout,
  111. kw_landing_pad,
  112. kw_inlineasm_br_indirect_target,
  113. kw_ehfunclet_entry,
  114. kw_liveins,
  115. kw_successors,
  116. kw_floatpred,
  117. kw_intpred,
  118. kw_shufflemask,
  119. kw_pre_instr_symbol,
  120. kw_post_instr_symbol,
  121. kw_heap_alloc_marker,
  122. kw_pcsections,
  123. kw_cfi_type,
  124. kw_bbsections,
  125. kw_bb_id,
  126. kw_unknown_size,
  127. kw_unknown_address,
  128. kw_ir_block_address_taken,
  129. kw_machine_block_address_taken,
  130. // Metadata types.
  131. kw_distinct,
  132. // Named metadata keywords
  133. md_tbaa,
  134. md_alias_scope,
  135. md_noalias,
  136. md_range,
  137. md_diexpr,
  138. md_dilocation,
  139. // Identifier tokens
  140. Identifier,
  141. NamedRegister,
  142. NamedVirtualRegister,
  143. MachineBasicBlockLabel,
  144. MachineBasicBlock,
  145. StackObject,
  146. FixedStackObject,
  147. NamedGlobalValue,
  148. GlobalValue,
  149. ExternalSymbol,
  150. MCSymbol,
  151. // Other tokens
  152. IntegerLiteral,
  153. FloatingPointLiteral,
  154. HexLiteral,
  155. VectorLiteral,
  156. VirtualRegister,
  157. ConstantPoolItem,
  158. JumpTableIndex,
  159. NamedIRBlock,
  160. IRBlock,
  161. NamedIRValue,
  162. IRValue,
  163. QuotedIRValue, // `<constant value>`
  164. SubRegisterIndex,
  165. StringConstant
  166. };
  167. private:
  168. TokenKind Kind = Error;
  169. StringRef Range;
  170. StringRef StringValue;
  171. std::string StringValueStorage;
  172. APSInt IntVal;
  173. public:
  174. MIToken() = default;
  175. MIToken &reset(TokenKind Kind, StringRef Range);
  176. MIToken &setStringValue(StringRef StrVal);
  177. MIToken &setOwnedStringValue(std::string StrVal);
  178. MIToken &setIntegerValue(APSInt IntVal);
  179. TokenKind kind() const { return Kind; }
  180. bool isError() const { return Kind == Error; }
  181. bool isNewlineOrEOF() const { return Kind == Newline || Kind == Eof; }
  182. bool isErrorOrEOF() const { return Kind == Error || Kind == Eof; }
  183. bool isRegister() const {
  184. return Kind == NamedRegister || Kind == underscore ||
  185. Kind == NamedVirtualRegister || Kind == VirtualRegister;
  186. }
  187. bool isRegisterFlag() const {
  188. return Kind == kw_implicit || Kind == kw_implicit_define ||
  189. Kind == kw_def || Kind == kw_dead || Kind == kw_killed ||
  190. Kind == kw_undef || Kind == kw_internal ||
  191. Kind == kw_early_clobber || Kind == kw_debug_use ||
  192. Kind == kw_renamable;
  193. }
  194. bool isMemoryOperandFlag() const {
  195. return Kind == kw_volatile || Kind == kw_non_temporal ||
  196. Kind == kw_dereferenceable || Kind == kw_invariant ||
  197. Kind == StringConstant;
  198. }
  199. bool is(TokenKind K) const { return Kind == K; }
  200. bool isNot(TokenKind K) const { return Kind != K; }
  201. StringRef::iterator location() const { return Range.begin(); }
  202. StringRef range() const { return Range; }
  203. /// Return the token's string value.
  204. StringRef stringValue() const { return StringValue; }
  205. const APSInt &integerValue() const { return IntVal; }
  206. bool hasIntegerValue() const {
  207. return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
  208. Kind == MachineBasicBlockLabel || Kind == StackObject ||
  209. Kind == FixedStackObject || Kind == GlobalValue ||
  210. Kind == VirtualRegister || Kind == ConstantPoolItem ||
  211. Kind == JumpTableIndex || Kind == IRBlock || Kind == IRValue;
  212. }
  213. };
  214. /// Consume a single machine instruction token in the given source and return
  215. /// the remaining source string.
  216. StringRef lexMIToken(
  217. StringRef Source, MIToken &Token,
  218. function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
  219. } // end namespace llvm
  220. #endif // LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H