MILexer.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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_cfi_same_value,
  72. kw_cfi_offset,
  73. kw_cfi_rel_offset,
  74. kw_cfi_def_cfa_register,
  75. kw_cfi_def_cfa_offset,
  76. kw_cfi_adjust_cfa_offset,
  77. kw_cfi_escape,
  78. kw_cfi_def_cfa,
  79. kw_cfi_llvm_def_aspace_cfa,
  80. kw_cfi_register,
  81. kw_cfi_remember_state,
  82. kw_cfi_restore,
  83. kw_cfi_restore_state,
  84. kw_cfi_undefined,
  85. kw_cfi_window_save,
  86. kw_cfi_aarch64_negate_ra_sign_state,
  87. kw_blockaddress,
  88. kw_intrinsic,
  89. kw_target_index,
  90. kw_half,
  91. kw_float,
  92. kw_double,
  93. kw_x86_fp80,
  94. kw_fp128,
  95. kw_ppc_fp128,
  96. kw_target_flags,
  97. kw_volatile,
  98. kw_non_temporal,
  99. kw_invariant,
  100. kw_align,
  101. kw_basealign,
  102. kw_addrspace,
  103. kw_stack,
  104. kw_got,
  105. kw_jump_table,
  106. kw_constant_pool,
  107. kw_call_entry,
  108. kw_custom,
  109. kw_liveout,
  110. kw_address_taken,
  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_bbsections,
  123. kw_unknown_size,
  124. kw_unknown_address,
  125. // Metadata types.
  126. kw_distinct,
  127. // Named metadata keywords
  128. md_tbaa,
  129. md_alias_scope,
  130. md_noalias,
  131. md_range,
  132. md_diexpr,
  133. md_dilocation,
  134. // Identifier tokens
  135. Identifier,
  136. NamedRegister,
  137. NamedVirtualRegister,
  138. MachineBasicBlockLabel,
  139. MachineBasicBlock,
  140. StackObject,
  141. FixedStackObject,
  142. NamedGlobalValue,
  143. GlobalValue,
  144. ExternalSymbol,
  145. MCSymbol,
  146. // Other tokens
  147. IntegerLiteral,
  148. FloatingPointLiteral,
  149. HexLiteral,
  150. VectorLiteral,
  151. VirtualRegister,
  152. ConstantPoolItem,
  153. JumpTableIndex,
  154. NamedIRBlock,
  155. IRBlock,
  156. NamedIRValue,
  157. IRValue,
  158. QuotedIRValue, // `<constant value>`
  159. SubRegisterIndex,
  160. StringConstant
  161. };
  162. private:
  163. TokenKind Kind = Error;
  164. StringRef Range;
  165. StringRef StringValue;
  166. std::string StringValueStorage;
  167. APSInt IntVal;
  168. public:
  169. MIToken() = default;
  170. MIToken &reset(TokenKind Kind, StringRef Range);
  171. MIToken &setStringValue(StringRef StrVal);
  172. MIToken &setOwnedStringValue(std::string StrVal);
  173. MIToken &setIntegerValue(APSInt IntVal);
  174. TokenKind kind() const { return Kind; }
  175. bool isError() const { return Kind == Error; }
  176. bool isNewlineOrEOF() const { return Kind == Newline || Kind == Eof; }
  177. bool isErrorOrEOF() const { return Kind == Error || Kind == Eof; }
  178. bool isRegister() const {
  179. return Kind == NamedRegister || Kind == underscore ||
  180. Kind == NamedVirtualRegister || Kind == VirtualRegister;
  181. }
  182. bool isRegisterFlag() const {
  183. return Kind == kw_implicit || Kind == kw_implicit_define ||
  184. Kind == kw_def || Kind == kw_dead || Kind == kw_killed ||
  185. Kind == kw_undef || Kind == kw_internal ||
  186. Kind == kw_early_clobber || Kind == kw_debug_use ||
  187. Kind == kw_renamable;
  188. }
  189. bool isMemoryOperandFlag() const {
  190. return Kind == kw_volatile || Kind == kw_non_temporal ||
  191. Kind == kw_dereferenceable || Kind == kw_invariant ||
  192. Kind == StringConstant;
  193. }
  194. bool is(TokenKind K) const { return Kind == K; }
  195. bool isNot(TokenKind K) const { return Kind != K; }
  196. StringRef::iterator location() const { return Range.begin(); }
  197. StringRef range() const { return Range; }
  198. /// Return the token's string value.
  199. StringRef stringValue() const { return StringValue; }
  200. const APSInt &integerValue() const { return IntVal; }
  201. bool hasIntegerValue() const {
  202. return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
  203. Kind == MachineBasicBlockLabel || Kind == StackObject ||
  204. Kind == FixedStackObject || Kind == GlobalValue ||
  205. Kind == VirtualRegister || Kind == ConstantPoolItem ||
  206. Kind == JumpTableIndex || Kind == IRBlock || Kind == IRValue;
  207. }
  208. };
  209. /// Consume a single machine instruction token in the given source and return
  210. /// the remaining source string.
  211. StringRef lexMIToken(
  212. StringRef Source, MIToken &Token,
  213. function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
  214. } // end namespace llvm
  215. #endif // LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H