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, |
Alex Lorenz | 5022f6b | 2015-08-13 23:10:16 +0000 | [diff] [blame^] | 33 | Newline, |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 34 | |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 35 | // Tokens with no info. |
| 36 | comma, |
| 37 | equal, |
Alex Lorenz | 12b554e | 2015-06-24 17:34:58 +0000 | [diff] [blame] | 38 | underscore, |
Alex Lorenz | 2eacca8 | 2015-07-13 23:24:34 +0000 | [diff] [blame] | 39 | colon, |
Alex Lorenz | 4af7e61 | 2015-08-03 23:08:19 +0000 | [diff] [blame] | 40 | coloncolon, |
Alex Lorenz | 35e4446 | 2015-07-22 17:58:46 +0000 | [diff] [blame] | 41 | exclaim, |
Alex Lorenz | deb5349 | 2015-07-28 17:28:03 +0000 | [diff] [blame] | 42 | lparen, |
| 43 | rparen, |
Alex Lorenz | 5672a89 | 2015-08-05 22:26:15 +0000 | [diff] [blame] | 44 | plus, |
| 45 | minus, |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 46 | |
Alex Lorenz | cb268d4 | 2015-07-06 23:07:26 +0000 | [diff] [blame] | 47 | // Keywords |
| 48 | kw_implicit, |
| 49 | kw_implicit_define, |
Alex Lorenz | cbbfd0b | 2015-07-07 20:34:53 +0000 | [diff] [blame] | 50 | kw_dead, |
Alex Lorenz | 495ad87 | 2015-07-08 21:23:34 +0000 | [diff] [blame] | 51 | kw_killed, |
Alex Lorenz | 4d026b89 | 2015-07-08 23:58:31 +0000 | [diff] [blame] | 52 | kw_undef, |
Alex Lorenz | 01c1a5e | 2015-08-05 17:49:03 +0000 | [diff] [blame] | 53 | kw_early_clobber, |
Alex Lorenz | 9075258 | 2015-08-05 17:41:17 +0000 | [diff] [blame] | 54 | kw_debug_use, |
Alex Lorenz | e5a4466 | 2015-07-17 00:24:15 +0000 | [diff] [blame] | 55 | kw_frame_setup, |
Alex Lorenz | 46d760d | 2015-07-22 21:15:11 +0000 | [diff] [blame] | 56 | kw_debug_location, |
Alex Lorenz | 8cfc686 | 2015-07-23 23:09:07 +0000 | [diff] [blame] | 57 | kw_cfi_offset, |
Alex Lorenz | 5b0d5f6 | 2015-07-27 20:39:03 +0000 | [diff] [blame] | 58 | kw_cfi_def_cfa_register, |
Alex Lorenz | f4baeb5 | 2015-07-21 22:28:27 +0000 | [diff] [blame] | 59 | kw_cfi_def_cfa_offset, |
Alex Lorenz | b139323 | 2015-07-29 18:57:23 +0000 | [diff] [blame] | 60 | kw_cfi_def_cfa, |
Alex Lorenz | deb5349 | 2015-07-28 17:28:03 +0000 | [diff] [blame] | 61 | kw_blockaddress, |
Alex Lorenz | ef5c196 | 2015-07-28 23:02:45 +0000 | [diff] [blame] | 62 | kw_target_index, |
Alex Lorenz | ad156fb | 2015-07-31 20:49:21 +0000 | [diff] [blame] | 63 | kw_half, |
| 64 | kw_float, |
| 65 | kw_double, |
| 66 | kw_x86_fp80, |
| 67 | kw_fp128, |
| 68 | kw_ppc_fp128, |
Alex Lorenz | 49873a8 | 2015-08-06 00:44:07 +0000 | [diff] [blame] | 69 | kw_target_flags, |
Alex Lorenz | a518b79 | 2015-08-04 00:24:45 +0000 | [diff] [blame] | 70 | kw_volatile, |
Alex Lorenz | 10fd038 | 2015-08-06 16:49:30 +0000 | [diff] [blame] | 71 | kw_non_temporal, |
Alex Lorenz | dc8de2a | 2015-08-06 16:55:53 +0000 | [diff] [blame] | 72 | kw_invariant, |
Alex Lorenz | 61420f7 | 2015-08-07 20:48:30 +0000 | [diff] [blame] | 73 | kw_align, |
Alex Lorenz | 46e9558 | 2015-08-12 20:44:16 +0000 | [diff] [blame] | 74 | kw_stack, |
Alex Lorenz | d858f87 | 2015-08-12 21:00:22 +0000 | [diff] [blame] | 75 | kw_got, |
Alex Lorenz | 4be56e9 | 2015-08-12 21:11:08 +0000 | [diff] [blame] | 76 | kw_jump_table, |
Alex Lorenz | 91097a3 | 2015-08-12 20:33:26 +0000 | [diff] [blame] | 77 | kw_constant_pool, |
Alex Lorenz | b97c9ef | 2015-08-10 23:24:42 +0000 | [diff] [blame] | 78 | kw_liveout, |
Alex Lorenz | 5022f6b | 2015-08-13 23:10:16 +0000 | [diff] [blame^] | 79 | kw_address_taken, |
| 80 | kw_landing_pad, |
| 81 | kw_liveins, |
| 82 | kw_successors, |
Alex Lorenz | cb268d4 | 2015-07-06 23:07:26 +0000 | [diff] [blame] | 83 | |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 84 | // Identifier tokens |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 85 | Identifier, |
Alex Lorenz | 05e3882 | 2015-08-05 18:52:21 +0000 | [diff] [blame] | 86 | IntegerType, |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 87 | NamedRegister, |
Alex Lorenz | 5022f6b | 2015-08-13 23:10:16 +0000 | [diff] [blame^] | 88 | MachineBasicBlockLabel, |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 89 | MachineBasicBlock, |
Alex Lorenz | 7feaf7c | 2015-07-16 23:37:45 +0000 | [diff] [blame] | 90 | StackObject, |
| 91 | FixedStackObject, |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 92 | NamedGlobalValue, |
| 93 | GlobalValue, |
Alex Lorenz | 6ede374 | 2015-07-21 16:59:53 +0000 | [diff] [blame] | 94 | ExternalSymbol, |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 95 | |
| 96 | // Other tokens |
Alex Lorenz | 5346451 | 2015-07-10 22:51:20 +0000 | [diff] [blame] | 97 | IntegerLiteral, |
Alex Lorenz | ad156fb | 2015-07-31 20:49:21 +0000 | [diff] [blame] | 98 | FloatingPointLiteral, |
Alex Lorenz | 31d7068 | 2015-07-15 23:38:35 +0000 | [diff] [blame] | 99 | VirtualRegister, |
Alex Lorenz | ab98049 | 2015-07-20 20:51:18 +0000 | [diff] [blame] | 100 | ConstantPoolItem, |
Alex Lorenz | 8a1915b | 2015-07-27 22:42:41 +0000 | [diff] [blame] | 101 | JumpTableIndex, |
Alex Lorenz | deb5349 | 2015-07-28 17:28:03 +0000 | [diff] [blame] | 102 | NamedIRBlock, |
Alex Lorenz | 8a1915b | 2015-07-27 22:42:41 +0000 | [diff] [blame] | 103 | IRBlock, |
Alex Lorenz | 4af7e61 | 2015-08-03 23:08:19 +0000 | [diff] [blame] | 104 | NamedIRValue, |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | private: |
| 108 | TokenKind Kind; |
| 109 | StringRef Range; |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 110 | StringRef StringValue; |
| 111 | std::string StringValueStorage; |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 112 | APSInt IntVal; |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 113 | |
| 114 | public: |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 115 | MIToken() : Kind(Error) {} |
Alex Lorenz | 970c12e | 2015-08-05 17:35:55 +0000 | [diff] [blame] | 116 | |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 117 | MIToken &reset(TokenKind Kind, StringRef Range); |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 118 | |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 119 | MIToken &setStringValue(StringRef StrVal); |
| 120 | MIToken &setOwnedStringValue(std::string StrVal); |
| 121 | MIToken &setIntegerValue(APSInt IntVal); |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 122 | |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 123 | TokenKind kind() const { return Kind; } |
| 124 | |
| 125 | bool isError() const { return Kind == Error; } |
| 126 | |
Alex Lorenz | 5022f6b | 2015-08-13 23:10:16 +0000 | [diff] [blame^] | 127 | bool isNewlineOrEOF() const { return Kind == Newline || Kind == Eof; } |
| 128 | |
| 129 | bool isErrorOrEOF() const { return Kind == Error || Kind == Eof; } |
| 130 | |
Alex Lorenz | 12b554e | 2015-06-24 17:34:58 +0000 | [diff] [blame] | 131 | bool isRegister() const { |
Alex Lorenz | 5346451 | 2015-07-10 22:51:20 +0000 | [diff] [blame] | 132 | return Kind == NamedRegister || Kind == underscore || |
| 133 | Kind == VirtualRegister; |
Alex Lorenz | 12b554e | 2015-06-24 17:34:58 +0000 | [diff] [blame] | 134 | } |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame] | 135 | |
Alex Lorenz | cb268d4 | 2015-07-06 23:07:26 +0000 | [diff] [blame] | 136 | bool isRegisterFlag() const { |
Alex Lorenz | 495ad87 | 2015-07-08 21:23:34 +0000 | [diff] [blame] | 137 | return Kind == kw_implicit || Kind == kw_implicit_define || |
Alex Lorenz | 9075258 | 2015-08-05 17:41:17 +0000 | [diff] [blame] | 138 | Kind == kw_dead || Kind == kw_killed || Kind == kw_undef || |
Alex Lorenz | 01c1a5e | 2015-08-05 17:49:03 +0000 | [diff] [blame] | 139 | Kind == kw_early_clobber || Kind == kw_debug_use; |
Alex Lorenz | cb268d4 | 2015-07-06 23:07:26 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Alex Lorenz | 10fd038 | 2015-08-06 16:49:30 +0000 | [diff] [blame] | 142 | bool isMemoryOperandFlag() const { |
Alex Lorenz | dc8de2a | 2015-08-06 16:55:53 +0000 | [diff] [blame] | 143 | return Kind == kw_volatile || Kind == kw_non_temporal || |
| 144 | Kind == kw_invariant; |
Alex Lorenz | 10fd038 | 2015-08-06 16:49:30 +0000 | [diff] [blame] | 145 | } |
Alex Lorenz | a518b79 | 2015-08-04 00:24:45 +0000 | [diff] [blame] | 146 | |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 147 | bool is(TokenKind K) const { return Kind == K; } |
| 148 | |
| 149 | bool isNot(TokenKind K) const { return Kind != K; } |
| 150 | |
| 151 | StringRef::iterator location() const { return Range.begin(); } |
| 152 | |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 153 | StringRef range() const { return Range; } |
Alex Lorenz | b29554d | 2015-07-20 20:31:01 +0000 | [diff] [blame] | 154 | |
Alex Lorenz | 970c12e | 2015-08-05 17:35:55 +0000 | [diff] [blame] | 155 | /// Return the token's string value. |
Alex Lorenz | 3fb7768 | 2015-08-06 23:17:42 +0000 | [diff] [blame] | 156 | StringRef stringValue() const { return StringValue; } |
Alex Lorenz | b29554d | 2015-07-20 20:31:01 +0000 | [diff] [blame] | 157 | |
Alex Lorenz | 240fc1e | 2015-06-23 23:42:28 +0000 | [diff] [blame] | 158 | const APSInt &integerValue() const { return IntVal; } |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 159 | |
| 160 | bool hasIntegerValue() const { |
Alex Lorenz | 5d6108e | 2015-06-26 22:56:48 +0000 | [diff] [blame] | 161 | return Kind == IntegerLiteral || Kind == MachineBasicBlock || |
Alex Lorenz | 5022f6b | 2015-08-13 23:10:16 +0000 | [diff] [blame^] | 162 | Kind == MachineBasicBlockLabel || Kind == StackObject || |
| 163 | Kind == FixedStackObject || Kind == GlobalValue || |
| 164 | Kind == VirtualRegister || Kind == ConstantPoolItem || |
| 165 | Kind == JumpTableIndex || Kind == IRBlock; |
Alex Lorenz | 33f0aef | 2015-06-26 16:46:11 +0000 | [diff] [blame] | 166 | } |
Alex Lorenz | 91370c5 | 2015-06-22 20:37:46 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | /// Consume a single machine instruction token in the given source and return |
| 170 | /// the remaining source string. |
| 171 | StringRef lexMIToken( |
| 172 | StringRef Source, MIToken &Token, |
| 173 | function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback); |
| 174 | |
| 175 | } // end namespace llvm |
| 176 | |
| 177 | #endif |