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