MILexer.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //===- MILexer.h - Lexer for machine instructions -------------------------===//
  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 file declares the function that lexes the machine instruction source
  11. // string.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
  15. #define LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
  16. #include "llvm/ADT/APSInt.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include "llvm/ADT/STLExtras.h"
  19. #include <functional>
  20. namespace llvm {
  21. class Twine;
  22. /// A token produced by the machine instruction lexer.
  23. struct MIToken {
  24. enum TokenKind {
  25. // Markers
  26. Eof,
  27. Error,
  28. // Tokens with no info.
  29. comma,
  30. equal,
  31. underscore,
  32. colon,
  33. // Keywords
  34. kw_implicit,
  35. kw_implicit_define,
  36. kw_dead,
  37. kw_killed,
  38. kw_undef,
  39. // Identifier tokens
  40. Identifier,
  41. NamedRegister,
  42. MachineBasicBlock,
  43. NamedGlobalValue,
  44. GlobalValue,
  45. // Other tokens
  46. IntegerLiteral,
  47. VirtualRegister
  48. };
  49. private:
  50. TokenKind Kind;
  51. unsigned StringOffset;
  52. StringRef Range;
  53. APSInt IntVal;
  54. public:
  55. MIToken(TokenKind Kind, StringRef Range, unsigned StringOffset = 0)
  56. : Kind(Kind), StringOffset(StringOffset), Range(Range) {}
  57. MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal,
  58. unsigned StringOffset = 0)
  59. : Kind(Kind), StringOffset(StringOffset), Range(Range), IntVal(IntVal) {}
  60. TokenKind kind() const { return Kind; }
  61. bool isError() const { return Kind == Error; }
  62. bool isRegister() const {
  63. return Kind == NamedRegister || Kind == underscore ||
  64. Kind == VirtualRegister;
  65. }
  66. bool isRegisterFlag() const {
  67. return Kind == kw_implicit || Kind == kw_implicit_define ||
  68. Kind == kw_dead || Kind == kw_killed || Kind == kw_undef;
  69. }
  70. bool is(TokenKind K) const { return Kind == K; }
  71. bool isNot(TokenKind K) const { return Kind != K; }
  72. StringRef::iterator location() const { return Range.begin(); }
  73. StringRef stringValue() const { return Range.drop_front(StringOffset); }
  74. const APSInt &integerValue() const { return IntVal; }
  75. bool hasIntegerValue() const {
  76. return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
  77. Kind == GlobalValue || Kind == VirtualRegister;
  78. }
  79. };
  80. /// Consume a single machine instruction token in the given source and return
  81. /// the remaining source string.
  82. StringRef lexMIToken(
  83. StringRef Source, MIToken &Token,
  84. function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
  85. } // end namespace llvm
  86. #endif