Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 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 | |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/APSInt.h" |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 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 | |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 34 | // Tokens with no info. |
| 35 | comma, |
| 36 | equal, |
Alex Lorenz | 12b554e | 2015-06-24 17:34:58 +0000 | [diff] [blame] | 37 | underscore, |
Alex Lorenz | 2eacca8 | 2015-07-13 23:24:34 +0000 | [diff] [blame] | 38 | colon, |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 39 | |
Alex Lorenz | cb268d4 | 2015-07-06 23:07:26 +0000 | [diff] [blame] | 40 | // Keywords |
| 41 | kw_implicit, |
| 42 | kw_implicit_define, |
Alex Lorenz | cbbfd0b | 2015-07-07 20:34:53 +0000 | [diff] [blame] | 43 | kw_dead, |
Alex Lorenz | 495ad87 | 2015-07-08 21:23:34 +0000 | [diff] [blame] | 44 | kw_killed, |
Alex Lorenz | 4d026b89 | 2015-07-08 23:58:31 +0000 | [diff] [blame] | 45 | kw_undef, |
Alex Lorenz | cb268d4 | 2015-07-06 23:07:26 +0000 | [diff] [blame] | 46 | |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 47 | // Identifier tokens |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 48 | Identifier, |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 49 | NamedRegister, |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 50 | MachineBasicBlock, |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame^] | 51 | StackObject, |
| 52 | FixedStackObject, |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 53 | NamedGlobalValue, |
| 54 | GlobalValue, |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 55 | |
| 56 | // Other tokens |
Alex Lorenz | 5346451 | 2015-07-10 22:51:20 +0000 | [diff] [blame] | 57 | IntegerLiteral, |
Alex Lorenz | 31d7068 | 2015-07-15 23:38:35 +0000 | [diff] [blame] | 58 | VirtualRegister, |
| 59 | JumpTableIndex |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | private: |
| 63 | TokenKind Kind; |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 64 | unsigned StringOffset; |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 65 | StringRef Range; |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 66 | APSInt IntVal; |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 67 | |
| 68 | public: |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 69 | MIToken(TokenKind Kind, StringRef Range, unsigned StringOffset = 0) |
| 70 | : Kind(Kind), StringOffset(StringOffset), Range(Range) {} |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 71 | |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 72 | MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal, |
| 73 | unsigned StringOffset = 0) |
| 74 | : Kind(Kind), StringOffset(StringOffset), Range(Range), IntVal(IntVal) {} |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 75 | |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 76 | TokenKind kind() const { return Kind; } |
| 77 | |
| 78 | bool isError() const { return Kind == Error; } |
| 79 | |
Alex Lorenz | 12b554e | 2015-06-24 17:34:58 +0000 | [diff] [blame] | 80 | bool isRegister() const { |
Alex Lorenz | 5346451 | 2015-07-10 22:51:20 +0000 | [diff] [blame] | 81 | return Kind == NamedRegister || Kind == underscore || |
| 82 | Kind == VirtualRegister; |
Alex Lorenz | 12b554e | 2015-06-24 17:34:58 +0000 | [diff] [blame] | 83 | } |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 84 | |
Alex Lorenz | cb268d4 | 2015-07-06 23:07:26 +0000 | [diff] [blame] | 85 | bool isRegisterFlag() const { |
Alex Lorenz | 495ad87 | 2015-07-08 21:23:34 +0000 | [diff] [blame] | 86 | return Kind == kw_implicit || Kind == kw_implicit_define || |
Alex Lorenz | 4d026b89 | 2015-07-08 23:58:31 +0000 | [diff] [blame] | 87 | Kind == kw_dead || Kind == kw_killed || Kind == kw_undef; |
Alex Lorenz | cb268d4 | 2015-07-06 23:07:26 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 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 | |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 96 | StringRef stringValue() const { return Range.drop_front(StringOffset); } |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 97 | |
| 98 | const APSInt &integerValue() const { return IntVal; } |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 99 | |
| 100 | bool hasIntegerValue() const { |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 101 | return Kind == IntegerLiteral || Kind == MachineBasicBlock || |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame^] | 102 | Kind == StackObject || Kind == FixedStackObject || |
Alex Lorenz | 31d7068 | 2015-07-15 23:38:35 +0000 | [diff] [blame] | 103 | Kind == GlobalValue || Kind == VirtualRegister || |
| 104 | Kind == JumpTableIndex; |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 105 | } |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 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 |