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