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_nuw,
73 kw_nsw,
74 kw_exact,
75 kw_nofpexcept,
76 kw_unpredictable,
77 kw_nneg,
78 kw_disjoint,
79 kw_debug_location,
80 kw_debug_instr_number,
81 kw_dbg_instr_ref,
82 kw_cfi_same_value,
83 kw_cfi_offset,
84 kw_cfi_rel_offset,
85 kw_cfi_def_cfa_register,
86 kw_cfi_def_cfa_offset,
87 kw_cfi_adjust_cfa_offset,
88 kw_cfi_escape,
89 kw_cfi_def_cfa,
90 kw_cfi_llvm_def_aspace_cfa,
91 kw_cfi_register,
92 kw_cfi_remember_state,
93 kw_cfi_restore,
94 kw_cfi_restore_state,
95 kw_cfi_undefined,
96 kw_cfi_window_save,
97 kw_cfi_aarch64_negate_ra_sign_state,
98 kw_blockaddress,
99 kw_intrinsic,
100 kw_target_index,
101 kw_half,
102 kw_float,
103 kw_double,
104 kw_x86_fp80,
105 kw_fp128,
106 kw_ppc_fp128,
107 kw_target_flags,
108 kw_volatile,
109 kw_non_temporal,
110 kw_invariant,
111 kw_align,
112 kw_basealign,
113 kw_addrspace,
114 kw_stack,
115 kw_got,
116 kw_jump_table,
117 kw_constant_pool,
118 kw_call_entry,
119 kw_custom,
120 kw_liveout,
121 kw_landing_pad,
122 kw_inlineasm_br_indirect_target,
123 kw_ehfunclet_entry,
124 kw_liveins,
125 kw_successors,
126 kw_floatpred,
127 kw_intpred,
128 kw_shufflemask,
129 kw_pre_instr_symbol,
130 kw_post_instr_symbol,
131 kw_heap_alloc_marker,
132 kw_pcsections,
133 kw_cfi_type,
134 kw_bbsections,
135 kw_bb_id,
136 kw_unknown_size,
137 kw_unknown_address,
138 kw_ir_block_address_taken,
139 kw_machine_block_address_taken,
140 kw_call_frame_size,
141 kw_noconvergent,
142
143 // Metadata types.
144 kw_distinct,
145
146 // Named metadata keywords
147 md_tbaa,
148 md_alias_scope,
149 md_noalias,
150 md_range,
151 md_diexpr,
152 md_dilocation,
153
154 // Identifier tokens
155 Identifier,
156 NamedRegister,
157 NamedVirtualRegister,
158 MachineBasicBlockLabel,
159 MachineBasicBlock,
160 StackObject,
161 FixedStackObject,
162 NamedGlobalValue,
163 GlobalValue,
164 ExternalSymbol,
165 MCSymbol,
166
167 // Other tokens
168 IntegerLiteral,
169 FloatingPointLiteral,
170 HexLiteral,
171 VectorLiteral,
172 VirtualRegister,
173 ConstantPoolItem,
174 JumpTableIndex,
175 NamedIRBlock,
176 IRBlock,
177 NamedIRValue,
178 IRValue,
179 QuotedIRValue, // `<constant value>`
180 SubRegisterIndex,
181 StringConstant
182 };
183
184private:
185 TokenKind Kind = Error;
186 StringRef Range;
187 StringRef StringValue;
188 std::string StringValueStorage;
189 APSInt IntVal;
190
191public:
192 MIToken() = default;
193
194 MIToken &reset(TokenKind Kind, StringRef Range);
195
196 MIToken &setStringValue(StringRef StrVal);
197 MIToken &setOwnedStringValue(std::string StrVal);
198 MIToken &setIntegerValue(APSInt IntVal);
199
200 TokenKind kind() const { return Kind; }
201
202 bool isError() const { return Kind == Error; }
203
204 bool isNewlineOrEOF() const { return Kind == Newline || Kind == Eof; }
205
206 bool isErrorOrEOF() const { return Kind == Error || Kind == Eof; }
207
208 bool isRegister() const {
209 return Kind == NamedRegister || Kind == underscore ||
210 Kind == NamedVirtualRegister || Kind == VirtualRegister;
211 }
212
213 bool isRegisterFlag() const {
214 return Kind == kw_implicit || Kind == kw_implicit_define ||
215 Kind == kw_def || Kind == kw_dead || Kind == kw_killed ||
216 Kind == kw_undef || Kind == kw_internal ||
217 Kind == kw_early_clobber || Kind == kw_debug_use ||
218 Kind == kw_renamable;
219 }
220
221 bool isMemoryOperandFlag() const {
222 return Kind == kw_volatile || Kind == kw_non_temporal ||
223 Kind == kw_dereferenceable || Kind == kw_invariant ||
224 Kind == StringConstant;
225 }
226
227 bool is(TokenKind K) const { return Kind == K; }
228
229 bool isNot(TokenKind K) const { return Kind != K; }
230
231 StringRef::iterator location() const { return Range.begin(); }
232
233 StringRef range() const { return Range; }
234
235 /// Return the token's string value.
236 StringRef stringValue() const { return StringValue; }
237
238 const APSInt &integerValue() const { return IntVal; }
239
240 bool hasIntegerValue() const {
241 return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
242 Kind == MachineBasicBlockLabel || Kind == StackObject ||
243 Kind == FixedStackObject || Kind == GlobalValue ||
244 Kind == VirtualRegister || Kind == ConstantPoolItem ||
245 Kind == JumpTableIndex || Kind == IRBlock || Kind == IRValue;
246 }
247};
248
249/// Consume a single machine instruction token in the given source and return
250/// the remaining source string.
251StringRef lexMIToken(
252 StringRef Source, MIToken &Token,
253 function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
254
255} // end namespace llvm
256
257#endif // LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
258

source code of llvm/lib/CodeGen/MIRParser/MILexer.h