MIR Serialization: Serialize the implicit register flag.
[oota-llvm.git] / lib / CodeGen / MIRParser / MILexer.h
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
43     // Identifier tokens
44     Identifier,
45     NamedRegister,
46     MachineBasicBlock,
47     NamedGlobalValue,
48     GlobalValue,
49
50     // Other tokens
51     IntegerLiteral
52   };
53
54 private:
55   TokenKind Kind;
56   unsigned StringOffset;
57   StringRef Range;
58   APSInt IntVal;
59
60 public:
61   MIToken(TokenKind Kind, StringRef Range, unsigned StringOffset = 0)
62       : Kind(Kind), StringOffset(StringOffset), Range(Range) {}
63
64   MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal,
65           unsigned StringOffset = 0)
66       : Kind(Kind), StringOffset(StringOffset), Range(Range), IntVal(IntVal) {}
67
68   TokenKind kind() const { return Kind; }
69
70   bool isError() const { return Kind == Error; }
71
72   bool isRegister() const {
73     return Kind == NamedRegister || Kind == underscore;
74   }
75
76   bool isRegisterFlag() const {
77     return Kind == kw_implicit || Kind == kw_implicit_define;
78   }
79
80   bool is(TokenKind K) const { return Kind == K; }
81
82   bool isNot(TokenKind K) const { return Kind != K; }
83
84   StringRef::iterator location() const { return Range.begin(); }
85
86   StringRef stringValue() const { return Range.drop_front(StringOffset); }
87
88   const APSInt &integerValue() const { return IntVal; }
89
90   bool hasIntegerValue() const {
91     return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
92            Kind == GlobalValue;
93   }
94 };
95
96 /// Consume a single machine instruction token in the given source and return
97 /// the remaining source string.
98 StringRef lexMIToken(
99     StringRef Source, MIToken &Token,
100     function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
101
102 } // end namespace llvm
103
104 #endif