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