blob: ef16da94d21b35b9e1e335b6c4e52e168ab23fa6 [file] [log] [blame]
Eugene Zelenkofa57bd02017-09-27 23:26:01 +00001//===- MILexer.h - Lexer for machine instructions ---------------*- C++ -*-===//
Alex Lorenz91370c52015-06-22 20:37:46 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alex Lorenz91370c52015-06-22 20:37:46 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file declares the function that lexes the machine instruction source
10// string.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
15#define LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
16
Alex Lorenz240fc1e2015-06-23 23:42:28 +000017#include "llvm/ADT/APSInt.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000018#include "llvm/ADT/StringRef.h"
Eugene Zelenkofa57bd02017-09-27 23:26:01 +000019#include <string>
Alex Lorenz91370c52015-06-22 20:37:46 +000020
21namespace llvm {
22
23class Twine;
24
25/// A token produced by the machine instruction lexer.
26struct MIToken {
27 enum TokenKind {
28 // Markers
29 Eof,
30 Error,
Alex Lorenz5022f6b2015-08-13 23:10:16 +000031 Newline,
Alex Lorenz91370c52015-06-22 20:37:46 +000032
Alex Lorenzf3db51de2015-06-23 16:35:26 +000033 // Tokens with no info.
34 comma,
35 equal,
Alex Lorenz12b554e2015-06-24 17:34:58 +000036 underscore,
Alex Lorenz2eacca82015-07-13 23:24:34 +000037 colon,
Alex Lorenz4af7e612015-08-03 23:08:19 +000038 coloncolon,
Matthias Braun333e4682016-07-26 21:49:34 +000039 dot,
Alex Lorenz35e44462015-07-22 17:58:46 +000040 exclaim,
Alex Lorenzdeb53492015-07-28 17:28:03 +000041 lparen,
42 rparen,
Alex Lorenzf9a2b122015-08-14 18:57:24 +000043 lbrace,
44 rbrace,
Alex Lorenz5672a892015-08-05 22:26:15 +000045 plus,
46 minus,
Quentin Colombet5e63e782016-03-08 02:00:43 +000047 less,
48 greater,
Alex Lorenzf3db51de2015-06-23 16:35:26 +000049
Alex Lorenzcb268d42015-07-06 23:07:26 +000050 // Keywords
51 kw_implicit,
52 kw_implicit_define,
Alex Lorenze66a7cc2015-08-19 18:55:47 +000053 kw_def,
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +000054 kw_dead,
Justin Lebaradbf09e2016-09-11 01:38:58 +000055 kw_dereferenceable,
Alex Lorenz495ad872015-07-08 21:23:34 +000056 kw_killed,
Alex Lorenz4d026b892015-07-08 23:58:31 +000057 kw_undef,
Alex Lorenz1039fd12015-08-14 19:07:07 +000058 kw_internal,
Alex Lorenz01c1a5e2015-08-05 17:49:03 +000059 kw_early_clobber,
Alex Lorenz90752582015-08-05 17:41:17 +000060 kw_debug_use,
Geoff Berry60c43102017-12-12 17:53:59 +000061 kw_renamable,
Alex Lorenz5ef93b02015-08-19 19:05:34 +000062 kw_tied_def,
Alex Lorenze5a44662015-07-17 00:24:15 +000063 kw_frame_setup,
Francis Visoiu Mistrihdbf2c482018-01-09 11:33:22 +000064 kw_frame_destroy,
Michael Berg7d1b25d2018-05-03 00:07:56 +000065 kw_nnan,
66 kw_ninf,
67 kw_nsz,
68 kw_arcp,
69 kw_contract,
70 kw_afn,
71 kw_reassoc,
Michael Bergc72a7252018-09-11 21:35:32 +000072 kw_nuw,
73 kw_nsw,
74 kw_exact,
Ulrich Weigandf0fd11d2020-01-10 15:31:10 +010075 kw_nofpexcept,
Alex Lorenz46d760d2015-07-22 21:15:11 +000076 kw_debug_location,
Alex Lorenz577d2712015-08-14 21:55:58 +000077 kw_cfi_same_value,
Alex Lorenz8cfc6862015-07-23 23:09:07 +000078 kw_cfi_offset,
Francis Visoiu Mistrih5de20e02017-12-15 15:17:18 +000079 kw_cfi_rel_offset,
Alex Lorenz5b0d5f62015-07-27 20:39:03 +000080 kw_cfi_def_cfa_register,
Alex Lorenzf4baeb52015-07-21 22:28:27 +000081 kw_cfi_def_cfa_offset,
Francis Visoiu Mistrih5de20e02017-12-15 15:17:18 +000082 kw_cfi_adjust_cfa_offset,
83 kw_cfi_escape,
Alex Lorenzb1393232015-07-29 18:57:23 +000084 kw_cfi_def_cfa,
Francis Visoiu Mistrih5de20e02017-12-15 15:17:18 +000085 kw_cfi_register,
86 kw_cfi_remember_state,
Francis Visoiu Mistrih66d2c262017-11-02 12:00:58 +000087 kw_cfi_restore,
Francis Visoiu Mistrih5de20e02017-12-15 15:17:18 +000088 kw_cfi_restore_state,
89 kw_cfi_undefined,
90 kw_cfi_window_save,
Luke Cheesemanf57d7d82018-12-18 10:37:42 +000091 kw_cfi_aarch64_negate_ra_sign_state,
Alex Lorenzdeb53492015-07-28 17:28:03 +000092 kw_blockaddress,
Tim Northover6b3bd612016-07-29 20:32:59 +000093 kw_intrinsic,
Alex Lorenzef5c1962015-07-28 23:02:45 +000094 kw_target_index,
Alex Lorenzad156fb2015-07-31 20:49:21 +000095 kw_half,
96 kw_float,
97 kw_double,
98 kw_x86_fp80,
99 kw_fp128,
100 kw_ppc_fp128,
Alex Lorenz49873a82015-08-06 00:44:07 +0000101 kw_target_flags,
Alex Lorenza518b792015-08-04 00:24:45 +0000102 kw_volatile,
Alex Lorenz10fd0382015-08-06 16:49:30 +0000103 kw_non_temporal,
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000104 kw_invariant,
Alex Lorenz61420f72015-08-07 20:48:30 +0000105 kw_align,
Francis Visoiu Mistrihe4718e82018-01-26 11:47:28 +0000106 kw_addrspace,
Alex Lorenz46e95582015-08-12 20:44:16 +0000107 kw_stack,
Alex Lorenzd858f872015-08-12 21:00:22 +0000108 kw_got,
Alex Lorenz4be56e92015-08-12 21:11:08 +0000109 kw_jump_table,
Alex Lorenz91097a32015-08-12 20:33:26 +0000110 kw_constant_pool,
Alex Lorenz0d009642015-08-20 00:12:57 +0000111 kw_call_entry,
Daniel Sandersde3d0ee2020-01-08 20:02:37 -0800112 kw_custom,
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000113 kw_liveout,
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000114 kw_address_taken,
115 kw_landing_pad,
Reid Kleckner5ff5ddd2020-03-20 14:06:27 -0700116 kw_ehfunclet_entry,
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000117 kw_liveins,
118 kw_successors,
Tim Northoverde3aea0412016-08-17 20:25:25 +0000119 kw_floatpred,
120 kw_intpred,
Matt Arsenault5af9cf02019-08-13 15:34:38 +0000121 kw_shufflemask,
Chandler Carruth75ca6be2018-08-16 23:11:05 +0000122 kw_pre_instr_symbol,
123 kw_post_instr_symbol,
Amy Huanga078c772019-11-05 10:54:50 -0800124 kw_heap_alloc_marker,
Sriraman Tallamdf082ac2020-03-16 15:56:02 -0700125 kw_bbsections,
Krzysztof Parzyszekcc3f6302018-08-20 20:37:57 +0000126 kw_unknown_size,
Alex Lorenzcb268d42015-07-06 23:07:26 +0000127
Alex Lorenza617c912015-08-17 22:05:15 +0000128 // Named metadata keywords
129 md_tbaa,
Alex Lorenza16f6242015-08-17 22:06:40 +0000130 md_alias_scope,
Alex Lorenz03e940d2015-08-17 22:08:02 +0000131 md_noalias,
Alex Lorenzeb625682015-08-17 22:09:52 +0000132 md_range,
Reid Kleckner6d353342017-08-23 20:31:27 +0000133 md_diexpr,
Daniel Sanders9f3cf552018-12-13 14:25:27 +0000134 md_dilocation,
Alex Lorenza617c912015-08-17 22:05:15 +0000135
Alex Lorenz91370c52015-06-22 20:37:46 +0000136 // Identifier tokens
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000137 Identifier,
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000138 NamedRegister,
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000139 NamedVirtualRegister,
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000140 MachineBasicBlockLabel,
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000141 MachineBasicBlock,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000142 StackObject,
143 FixedStackObject,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000144 NamedGlobalValue,
145 GlobalValue,
Alex Lorenz6ede3742015-07-21 16:59:53 +0000146 ExternalSymbol,
Chandler Carruth75ca6be2018-08-16 23:11:05 +0000147 MCSymbol,
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000148
149 // Other tokens
Alex Lorenz53464512015-07-10 22:51:20 +0000150 IntegerLiteral,
Alex Lorenzad156fb2015-07-31 20:49:21 +0000151 FloatingPointLiteral,
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +0000152 HexLiteral,
Matt Arsenault5af9cf02019-08-13 15:34:38 +0000153 VectorLiteral,
Alex Lorenz31d70682015-07-15 23:38:35 +0000154 VirtualRegister,
Alex Lorenzab980492015-07-20 20:51:18 +0000155 ConstantPoolItem,
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000156 JumpTableIndex,
Alex Lorenzdeb53492015-07-28 17:28:03 +0000157 NamedIRBlock,
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000158 IRBlock,
Alex Lorenz4af7e612015-08-03 23:08:19 +0000159 NamedIRValue,
Alex Lorenzc1136ef32015-08-21 21:54:12 +0000160 IRValue,
Matthias Braunb74eb412016-03-28 18:18:46 +0000161 QuotedIRValue, // `<constant value>`
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000162 SubRegisterIndex,
163 StringConstant
Alex Lorenz91370c52015-06-22 20:37:46 +0000164 };
165
166private:
Eugene Zelenkofa57bd02017-09-27 23:26:01 +0000167 TokenKind Kind = Error;
Alex Lorenz91370c52015-06-22 20:37:46 +0000168 StringRef Range;
Alex Lorenz3fb77682015-08-06 23:17:42 +0000169 StringRef StringValue;
170 std::string StringValueStorage;
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000171 APSInt IntVal;
Alex Lorenz91370c52015-06-22 20:37:46 +0000172
173public:
Eugene Zelenkofa57bd02017-09-27 23:26:01 +0000174 MIToken() = default;
Alex Lorenz970c12e2015-08-05 17:35:55 +0000175
Alex Lorenz3fb77682015-08-06 23:17:42 +0000176 MIToken &reset(TokenKind Kind, StringRef Range);
Alex Lorenz91370c52015-06-22 20:37:46 +0000177
Alex Lorenz3fb77682015-08-06 23:17:42 +0000178 MIToken &setStringValue(StringRef StrVal);
179 MIToken &setOwnedStringValue(std::string StrVal);
180 MIToken &setIntegerValue(APSInt IntVal);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000181
Alex Lorenz91370c52015-06-22 20:37:46 +0000182 TokenKind kind() const { return Kind; }
183
184 bool isError() const { return Kind == Error; }
185
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000186 bool isNewlineOrEOF() const { return Kind == Newline || Kind == Eof; }
187
188 bool isErrorOrEOF() const { return Kind == Error || Kind == Eof; }
189
Alex Lorenz12b554e2015-06-24 17:34:58 +0000190 bool isRegister() const {
Alex Lorenz53464512015-07-10 22:51:20 +0000191 return Kind == NamedRegister || Kind == underscore ||
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000192 Kind == NamedVirtualRegister || Kind == VirtualRegister;
Alex Lorenz12b554e2015-06-24 17:34:58 +0000193 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000194
Alex Lorenzcb268d42015-07-06 23:07:26 +0000195 bool isRegisterFlag() const {
Alex Lorenz495ad872015-07-08 21:23:34 +0000196 return Kind == kw_implicit || Kind == kw_implicit_define ||
Alex Lorenze66a7cc2015-08-19 18:55:47 +0000197 Kind == kw_def || Kind == kw_dead || Kind == kw_killed ||
198 Kind == kw_undef || Kind == kw_internal ||
Geoff Berry60c43102017-12-12 17:53:59 +0000199 Kind == kw_early_clobber || Kind == kw_debug_use ||
200 Kind == kw_renamable;
Alex Lorenzcb268d42015-07-06 23:07:26 +0000201 }
202
Alex Lorenz10fd0382015-08-06 16:49:30 +0000203 bool isMemoryOperandFlag() const {
Alex Lorenzdc8de2a2015-08-06 16:55:53 +0000204 return Kind == kw_volatile || Kind == kw_non_temporal ||
Geoff Berry6748abe2017-07-13 02:28:54 +0000205 Kind == kw_dereferenceable || Kind == kw_invariant ||
206 Kind == StringConstant;
Alex Lorenz10fd0382015-08-06 16:49:30 +0000207 }
Alex Lorenza518b792015-08-04 00:24:45 +0000208
Alex Lorenz91370c52015-06-22 20:37:46 +0000209 bool is(TokenKind K) const { return Kind == K; }
210
211 bool isNot(TokenKind K) const { return Kind != K; }
212
213 StringRef::iterator location() const { return Range.begin(); }
214
Alex Lorenz3fb77682015-08-06 23:17:42 +0000215 StringRef range() const { return Range; }
Alex Lorenzb29554d2015-07-20 20:31:01 +0000216
Alex Lorenz970c12e2015-08-05 17:35:55 +0000217 /// Return the token's string value.
Alex Lorenz3fb77682015-08-06 23:17:42 +0000218 StringRef stringValue() const { return StringValue; }
Alex Lorenzb29554d2015-07-20 20:31:01 +0000219
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000220 const APSInt &integerValue() const { return IntVal; }
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000221
222 bool hasIntegerValue() const {
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000223 return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000224 Kind == MachineBasicBlockLabel || Kind == StackObject ||
225 Kind == FixedStackObject || Kind == GlobalValue ||
226 Kind == VirtualRegister || Kind == ConstantPoolItem ||
Alex Lorenzdd13be02015-08-19 23:31:05 +0000227 Kind == JumpTableIndex || Kind == IRBlock || Kind == IRValue;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000228 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000229};
230
231/// Consume a single machine instruction token in the given source and return
232/// the remaining source string.
233StringRef lexMIToken(
234 StringRef Source, MIToken &Token,
235 function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
236
237} // end namespace llvm
238
Eugene Zelenkofa57bd02017-09-27 23:26:01 +0000239#endif // LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H