LLVM  3.7.0
MILexer.h
Go to the documentation of this file.
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,
33 
34  // Tokens with no info.
39 
40  // Keywords
46 
47  // Identifier tokens
53 
54  // Other tokens
57  };
58 
59 private:
60  TokenKind Kind;
61  unsigned StringOffset;
62  StringRef Range;
63  APSInt IntVal;
64 
65 public:
66  MIToken(TokenKind Kind, StringRef Range, unsigned StringOffset = 0)
67  : Kind(Kind), StringOffset(StringOffset), Range(Range) {}
68 
69  MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal,
70  unsigned StringOffset = 0)
71  : Kind(Kind), StringOffset(StringOffset), Range(Range), IntVal(IntVal) {}
72 
73  TokenKind kind() const { return Kind; }
74 
75  bool isError() const { return Kind == Error; }
76 
77  bool isRegister() const {
78  return Kind == NamedRegister || Kind == underscore ||
79  Kind == VirtualRegister;
80  }
81 
82  bool isRegisterFlag() const {
83  return Kind == kw_implicit || Kind == kw_implicit_define ||
84  Kind == kw_dead || Kind == kw_killed || Kind == kw_undef;
85  }
86 
87  bool is(TokenKind K) const { return Kind == K; }
88 
89  bool isNot(TokenKind K) const { return Kind != K; }
90 
91  StringRef::iterator location() const { return Range.begin(); }
92 
93  StringRef stringValue() const { return Range.drop_front(StringOffset); }
94 
95  const APSInt &integerValue() const { return IntVal; }
96 
97  bool hasIntegerValue() const {
98  return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
99  Kind == GlobalValue || Kind == VirtualRegister;
100  }
101 };
102 
103 /// Consume a single machine instruction token in the given source and return
104 /// the remaining source string.
105 StringRef lexMIToken(
106  StringRef Source, MIToken &Token,
107  function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
108 
109 } // end namespace llvm
110 
111 #endif
StringRef lexMIToken(StringRef Source, MIToken &Token, function_ref< void(StringRef::iterator, const Twine &)> ErrorCallback)
Consume a single machine instruction token in the given source and return the remaining source string...
StringRef::iterator location() const
Definition: MILexer.h:91
MIToken(TokenKind Kind, StringRef Range, unsigned StringOffset=0)
Definition: MILexer.h:66
bool isNot(TokenKind K) const
Definition: MILexer.h:89
MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal, unsigned StringOffset=0)
Definition: MILexer.h:69
iterator begin() const
Definition: StringRef.h:90
bool isError() const
Definition: MILexer.h:75
bool isRegisterFlag() const
Definition: MILexer.h:82
bool hasIntegerValue() const
Definition: MILexer.h:97
StringRef stringValue() const
Definition: MILexer.h:93
const char * iterator
Definition: StringRef.h:42
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition: StringRef.h:412
TokenKind kind() const
Definition: MILexer.h:73
bool is(TokenKind K) const
Definition: MILexer.h:87
const APSInt & integerValue() const
Definition: MILexer.h:95
bool isRegister() const
Definition: MILexer.h:77
A token produced by the machine instruction lexer.
Definition: MILexer.h:28
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40