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 | 4af7e61 | 2015-08-03 23:08:19 +0000 | [diff] [blame] | 39 | coloncolon, |
Alex Lorenz | 35e4446 | 2015-07-22 17:58:46 +0000 | [diff] [blame] | 40 | exclaim, |
Alex Lorenz | deb5349 | 2015-07-28 17:28:03 +0000 | [diff] [blame] | 41 | lparen, |
| 42 | rparen, |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 43 | |
Alex Lorenz | cb268d4 | 2015-07-06 23:07:26 +0000 | [diff] [blame] | 44 | // Keywords |
| 45 | kw_implicit, |
| 46 | kw_implicit_define, |
Alex Lorenz | cbbfd0b | 2015-07-07 20:34:53 +0000 | [diff] [blame] | 47 | kw_dead, |
Alex Lorenz | 495ad87 | 2015-07-08 21:23:34 +0000 | [diff] [blame] | 48 | kw_killed, |
Alex Lorenz | 4d026b89 | 2015-07-08 23:58:31 +0000 | [diff] [blame] | 49 | kw_undef, |
Alex Lorenz | e5a4466 | 2015-07-17 00:24:15 +0000 | [diff] [blame] | 50 | kw_frame_setup, |
Alex Lorenz | 46d760d | 2015-07-22 21:15:11 +0000 | [diff] [blame] | 51 | kw_debug_location, |
Alex Lorenz | 8cfc686 | 2015-07-23 23:09:07 +0000 | [diff] [blame] | 52 | kw_cfi_offset, |
Alex Lorenz | 5b0d5f6 | 2015-07-27 20:39:03 +0000 | [diff] [blame] | 53 | kw_cfi_def_cfa_register, |
Alex Lorenz | f4baeb5 | 2015-07-21 22:28:27 +0000 | [diff] [blame] | 54 | kw_cfi_def_cfa_offset, |
Alex Lorenz | b139323 | 2015-07-29 18:57:23 +0000 | [diff] [blame] | 55 | kw_cfi_def_cfa, |
Alex Lorenz | deb5349 | 2015-07-28 17:28:03 +0000 | [diff] [blame] | 56 | kw_blockaddress, |
Alex Lorenz | ef5c196 | 2015-07-28 23:02:45 +0000 | [diff] [blame] | 57 | kw_target_index, |
Alex Lorenz | ad156fb | 2015-07-31 20:49:21 +0000 | [diff] [blame] | 58 | kw_half, |
| 59 | kw_float, |
| 60 | kw_double, |
| 61 | kw_x86_fp80, |
| 62 | kw_fp128, |
| 63 | kw_ppc_fp128, |
Alex Lorenz | a518b79 | 2015-08-04 00:24:45 +0000 | [diff] [blame] | 64 | kw_volatile, |
Alex Lorenz | cb268d4 | 2015-07-06 23:07:26 +0000 | [diff] [blame] | 65 | |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 66 | // Identifier tokens |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 67 | Identifier, |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 68 | NamedRegister, |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 69 | MachineBasicBlock, |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame] | 70 | StackObject, |
| 71 | FixedStackObject, |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 72 | NamedGlobalValue, |
| 73 | GlobalValue, |
Alex Lorenz | 6ede374 | 2015-07-21 16:59:53 +0000 | [diff] [blame] | 74 | ExternalSymbol, |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 75 | |
| 76 | // Other tokens |
Alex Lorenz | 5346451 | 2015-07-10 22:51:20 +0000 | [diff] [blame] | 77 | IntegerLiteral, |
Alex Lorenz | ad156fb | 2015-07-31 20:49:21 +0000 | [diff] [blame] | 78 | FloatingPointLiteral, |
Alex Lorenz | 31d7068 | 2015-07-15 23:38:35 +0000 | [diff] [blame] | 79 | VirtualRegister, |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 80 | ConstantPoolItem, |
Alex Lorenz | 8a1915b | 2015-07-27 22:42:41 +0000 | [diff] [blame] | 81 | JumpTableIndex, |
Alex Lorenz | deb5349 | 2015-07-28 17:28:03 +0000 | [diff] [blame] | 82 | NamedIRBlock, |
Alex Lorenz | 8a1915b | 2015-07-27 22:42:41 +0000 | [diff] [blame] | 83 | IRBlock, |
Alex Lorenz | 4af7e61 | 2015-08-03 23:08:19 +0000 | [diff] [blame] | 84 | NamedIRValue, |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | private: |
| 88 | TokenKind Kind; |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 89 | unsigned StringOffset; |
Alex Lorenz | 970c12e | 2015-08-05 17:35:55 +0000 | [diff] [blame^] | 90 | bool HasStringValue; |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 91 | StringRef Range; |
Alex Lorenz | 970c12e | 2015-08-05 17:35:55 +0000 | [diff] [blame^] | 92 | std::string StringValue; |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 93 | APSInt IntVal; |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 94 | |
| 95 | public: |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 96 | MIToken(TokenKind Kind, StringRef Range, unsigned StringOffset = 0) |
Alex Lorenz | 970c12e | 2015-08-05 17:35:55 +0000 | [diff] [blame^] | 97 | : Kind(Kind), StringOffset(StringOffset), HasStringValue(false), |
| 98 | Range(Range) {} |
| 99 | |
| 100 | MIToken(TokenKind Kind, StringRef Range, std::string StringValue, |
| 101 | unsigned StringOffset = 0) |
| 102 | : Kind(Kind), StringOffset(StringOffset), HasStringValue(true), |
| 103 | Range(Range), StringValue(std::move(StringValue)) {} |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 104 | |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 105 | MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal, |
| 106 | unsigned StringOffset = 0) |
Alex Lorenz | 970c12e | 2015-08-05 17:35:55 +0000 | [diff] [blame^] | 107 | : Kind(Kind), StringOffset(StringOffset), HasStringValue(false), |
| 108 | Range(Range), IntVal(IntVal) {} |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 109 | |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 110 | TokenKind kind() const { return Kind; } |
| 111 | |
| 112 | bool isError() const { return Kind == Error; } |
| 113 | |
Alex Lorenz | 12b554e | 2015-06-24 17:34:58 +0000 | [diff] [blame] | 114 | bool isRegister() const { |
Alex Lorenz | 5346451 | 2015-07-10 22:51:20 +0000 | [diff] [blame] | 115 | return Kind == NamedRegister || Kind == underscore || |
| 116 | Kind == VirtualRegister; |
Alex Lorenz | 12b554e | 2015-06-24 17:34:58 +0000 | [diff] [blame] | 117 | } |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 118 | |
Alex Lorenz | cb268d4 | 2015-07-06 23:07:26 +0000 | [diff] [blame] | 119 | bool isRegisterFlag() const { |
Alex Lorenz | 495ad87 | 2015-07-08 21:23:34 +0000 | [diff] [blame] | 120 | return Kind == kw_implicit || Kind == kw_implicit_define || |
Alex Lorenz | 4d026b89 | 2015-07-08 23:58:31 +0000 | [diff] [blame] | 121 | Kind == kw_dead || Kind == kw_killed || Kind == kw_undef; |
Alex Lorenz | cb268d4 | 2015-07-06 23:07:26 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Alex Lorenz | a518b79 | 2015-08-04 00:24:45 +0000 | [diff] [blame] | 124 | bool isMemoryOperandFlag() const { return Kind == kw_volatile; } |
| 125 | |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 126 | bool is(TokenKind K) const { return Kind == K; } |
| 127 | |
| 128 | bool isNot(TokenKind K) const { return Kind != K; } |
| 129 | |
| 130 | StringRef::iterator location() const { return Range.begin(); } |
| 131 | |
Alex Lorenz | b29554d | 2015-07-20 20:31:01 +0000 | [diff] [blame] | 132 | /// Return the token's raw string value. |
| 133 | /// |
| 134 | /// If the string value is quoted, this method returns that quoted string as |
| 135 | /// it is, without unescaping the string value. |
| 136 | StringRef rawStringValue() const { return Range.drop_front(StringOffset); } |
| 137 | |
Alex Lorenz | 970c12e | 2015-08-05 17:35:55 +0000 | [diff] [blame^] | 138 | /// Return the token's string value. |
Alex Lorenz | b29554d | 2015-07-20 20:31:01 +0000 | [diff] [blame] | 139 | StringRef stringValue() const { |
Alex Lorenz | 970c12e | 2015-08-05 17:35:55 +0000 | [diff] [blame^] | 140 | return HasStringValue ? StringRef(StringValue) |
| 141 | : Range.drop_front(StringOffset); |
Alex Lorenz | b29554d | 2015-07-20 20:31:01 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 144 | const APSInt &integerValue() const { return IntVal; } |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 145 | |
| 146 | bool hasIntegerValue() const { |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 147 | return Kind == IntegerLiteral || Kind == MachineBasicBlock || |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame] | 148 | Kind == StackObject || Kind == FixedStackObject || |
Alex Lorenz | 31d7068 | 2015-07-15 23:38:35 +0000 | [diff] [blame] | 149 | Kind == GlobalValue || Kind == VirtualRegister || |
Alex Lorenz | 8a1915b | 2015-07-27 22:42:41 +0000 | [diff] [blame] | 150 | Kind == ConstantPoolItem || Kind == JumpTableIndex || |
| 151 | Kind == IRBlock; |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 152 | } |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | /// Consume a single machine instruction token in the given source and return |
| 156 | /// the remaining source string. |
| 157 | StringRef lexMIToken( |
| 158 | StringRef Source, MIToken &Token, |
| 159 | function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback); |
| 160 | |
| 161 | } // end namespace llvm |
| 162 | |
| 163 | #endif |