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