1//===- MILexer.h - Lexer for machine instructions ---------------*- C++ -*-===//
2//
3// 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
6//
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
17#include "llvm/ADT/APSInt.h"
18#include "llvm/ADT/StringRef.h"
19#include <string>
20
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,
31 Newline,
32
33 // Tokens with no info.
34 comma,
35 equal,
36 underscore,
37 colon,
38 coloncolon,
39 dot,
40 exclaim,
41 lparen,
42 rparen,
43 lbrace,
44 rbrace,
45 plus,
46 minus,
47 less,
48 greater,
49
50 // Keywords
51 kw_implicit,
52 kw_implicit_define,
53 kw_def,
54 kw_dead,
55 kw_dereferenceable,
56 kw_killed,
57 kw_undef,
58 kw_internal,
59 kw_early_clobber,
60 kw_debug_use,
61 kw_renamable,
62 kw_tied_def,
63 kw_frame_setup,
64 kw_frame_destroy,
65 kw_nnan,
66 kw_ninf,
67 kw_nsz,
68 kw_arcp,
69 kw_contract,
70 kw_afn,
71 kw_reassoc,
72 kw_nusw,
73 kw_nuw,
74 kw_nsw,
75 kw_exact,
76 kw_nofpexcept,
77 kw_unpredictable,
78 kw_nneg,
79 kw_disjoint,
80 kw_samesign,
81 kw_inbounds,
82 kw_debug_location,
83 kw_debug_instr_number,
84 kw_dbg_instr_ref,
85 kw_cfi_same_value,
86 kw_cfi_offset,
87 kw_cfi_rel_offset,
88 kw_cfi_def_cfa_register,
89 kw_cfi_def_cfa_offset,
90 kw_cfi_adjust_cfa_offset,
91 kw_cfi_escape,
92 kw_cfi_def_cfa,
93 kw_cfi_llvm_def_aspace_cfa,
94 kw_cfi_register,
95 kw_cfi_remember_state,
96 kw_cfi_restore,
97 kw_cfi_restore_state,
98 kw_cfi_undefined,
99 kw_cfi_window_save,
100 kw_cfi_aarch64_negate_ra_sign_state,
101 kw_cfi_aarch64_negate_ra_sign_state_with_pc,
102 kw_cfi_llvm_register_pair,
103 kw_cfi_llvm_vector_registers,
104 kw_cfi_llvm_vector_offset,
105 kw_cfi_llvm_vector_register_mask,
106 kw_blockaddress,
107 kw_intrinsic,
108 kw_target_index,
109 kw_half,
110 kw_bfloat,
111 kw_float,
112 kw_double,
113 kw_x86_fp80,
114 kw_fp128,
115 kw_ppc_fp128,
116 kw_target_flags,
117 kw_volatile,
118 kw_non_temporal,
119 kw_invariant,
120 kw_align,
121 kw_basealign,
122 kw_addrspace,
123 kw_stack,
124 kw_got,
125 kw_jump_table,
126 kw_constant_pool,
127 kw_call_entry,
128 kw_custom,
129 kw_lanemask,
130 kw_liveout,
131 kw_landing_pad,
132 kw_inlineasm_br_indirect_target,
133 kw_ehscope_entry,
134 kw_ehfunclet_entry,
135 kw_liveins,
136 kw_successors,
137 kw_floatpred,
138 kw_intpred,
139 kw_shufflemask,
140 kw_pre_instr_symbol,
141 kw_post_instr_symbol,
142 kw_heap_alloc_marker,
143 kw_pcsections,
144 kw_cfi_type,
145 kw_deactivation_symbol,
146 kw_bbsections,
147 kw_bb_id,
148 kw_unknown_size,
149 kw_unknown_address,
150 kw_ir_block_address_taken,
151 kw_machine_block_address_taken,
152 kw_call_frame_size,
153 kw_noconvergent,
154 kw_mmra,
155 kw_lr_split,
156
157 // Metadata types.
158 kw_distinct,
159
160 // Named metadata keywords
161 md_tbaa,
162 md_alias_scope,
163 md_noalias,
164 md_noalias_addrspace,
165 md_range,
166 md_diexpr,
167 md_dilocation,
168
169 // Identifier tokens
170 Identifier,
171 NamedRegister,
172 NamedVirtualRegister,
173 MachineBasicBlockLabel,
174 MachineBasicBlock,
175 StackObject,
176 FixedStackObject,
177 NamedGlobalValue,
178 GlobalValue,
179 ExternalSymbol,
180 MCSymbol,
181
182 // Other tokens
183 IntegerLiteral,
184 FloatingPointLiteral,
185 HexLiteral,
186 VectorLiteral,
187 VirtualRegister,
188 ConstantPoolItem,
189 JumpTableIndex,
190 NamedIRBlock,
191 IRBlock,
192 NamedIRValue,
193 IRValue,
194 QuotedIRValue, // `<constant value>`
195 SubRegisterIndex,
196 StringConstant
197 };
198
199private:
200 TokenKind Kind = Error;
201 StringRef Range;
202 StringRef StringValue;
203 std::string StringValueStorage;
204 APSInt IntVal;
205
206public:
207 MIToken() = default;
208
209 MIToken &reset(TokenKind Kind, StringRef Range);
210
211 MIToken &setStringValue(StringRef StrVal);
212 MIToken &setOwnedStringValue(std::string StrVal);
213 MIToken &setIntegerValue(APSInt IntVal);
214
215 TokenKind kind() const { return Kind; }
216
217 bool isError() const { return Kind == Error; }
218
219 bool isNewlineOrEOF() const { return Kind == Newline || Kind == Eof; }
220
221 bool isErrorOrEOF() const { return Kind == Error || Kind == Eof; }
222
223 bool isRegister() const {
224 return Kind == NamedRegister || Kind == underscore ||
225 Kind == NamedVirtualRegister || Kind == VirtualRegister;
226 }
227
228 bool isRegisterFlag() const {
229 return Kind == kw_implicit || Kind == kw_implicit_define ||
230 Kind == kw_def || Kind == kw_dead || Kind == kw_killed ||
231 Kind == kw_undef || Kind == kw_internal ||
232 Kind == kw_early_clobber || Kind == kw_debug_use ||
233 Kind == kw_renamable;
234 }
235
236 bool isMemoryOperandFlag() const {
237 return Kind == kw_volatile || Kind == kw_non_temporal ||
238 Kind == kw_dereferenceable || Kind == kw_invariant ||
239 Kind == StringConstant;
240 }
241
242 bool is(TokenKind K) const { return Kind == K; }
243
244 bool isNot(TokenKind K) const { return Kind != K; }
245
246 StringRef::iterator location() const { return Range.begin(); }
247
248 StringRef range() const { return Range; }
249
250 /// Return the token's string value.
251 StringRef stringValue() const { return StringValue; }
252
253 const APSInt &integerValue() const { return IntVal; }
254
255 bool hasIntegerValue() const {
256 return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
257 Kind == MachineBasicBlockLabel || Kind == StackObject ||
258 Kind == FixedStackObject || Kind == GlobalValue ||
259 Kind == VirtualRegister || Kind == ConstantPoolItem ||
260 Kind == JumpTableIndex || Kind == IRBlock || Kind == IRValue;
261 }
262};
263
264/// Consume a single machine instruction token in the given source and return
265/// the remaining source string.
266StringRef lexMIToken(
267 StringRef Source, MIToken &Token,
268 function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
269
270} // end namespace llvm
271
272#endif // LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
273