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