[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / llvm / lib / CodeGen / MIRParser / MILexer.h
blob9ba0e7c720169a41fc59c9779f8a27a797d996b5
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.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
15 #define LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
17 #include "llvm/ADT/APSInt.h"
18 #include "llvm/ADT/StringRef.h"
19 #include <string>
21 namespace llvm {
23 class Twine;
25 /// A token produced by the machine instruction lexer.
26 struct MIToken {
27 enum TokenKind {
28 // Markers
29 Eof,
30 Error,
31 Newline,
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,
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_debug_location,
77 kw_debug_instr_number,
78 kw_cfi_same_value,
79 kw_cfi_offset,
80 kw_cfi_rel_offset,
81 kw_cfi_def_cfa_register,
82 kw_cfi_def_cfa_offset,
83 kw_cfi_adjust_cfa_offset,
84 kw_cfi_escape,
85 kw_cfi_def_cfa,
86 kw_cfi_llvm_def_aspace_cfa,
87 kw_cfi_register,
88 kw_cfi_remember_state,
89 kw_cfi_restore,
90 kw_cfi_restore_state,
91 kw_cfi_undefined,
92 kw_cfi_window_save,
93 kw_cfi_aarch64_negate_ra_sign_state,
94 kw_blockaddress,
95 kw_intrinsic,
96 kw_target_index,
97 kw_half,
98 kw_float,
99 kw_double,
100 kw_x86_fp80,
101 kw_fp128,
102 kw_ppc_fp128,
103 kw_target_flags,
104 kw_volatile,
105 kw_non_temporal,
106 kw_invariant,
107 kw_align,
108 kw_basealign,
109 kw_addrspace,
110 kw_stack,
111 kw_got,
112 kw_jump_table,
113 kw_constant_pool,
114 kw_call_entry,
115 kw_custom,
116 kw_liveout,
117 kw_landing_pad,
118 kw_inlineasm_br_indirect_target,
119 kw_ehfunclet_entry,
120 kw_liveins,
121 kw_successors,
122 kw_floatpred,
123 kw_intpred,
124 kw_shufflemask,
125 kw_pre_instr_symbol,
126 kw_post_instr_symbol,
127 kw_heap_alloc_marker,
128 kw_bbsections,
129 kw_unknown_size,
130 kw_unknown_address,
131 kw_ir_block_address_taken,
132 kw_machine_block_address_taken,
134 // Metadata types.
135 kw_distinct,
137 // Named metadata keywords
138 md_tbaa,
139 md_alias_scope,
140 md_noalias,
141 md_range,
142 md_diexpr,
143 md_dilocation,
145 // Identifier tokens
146 Identifier,
147 NamedRegister,
148 NamedVirtualRegister,
149 MachineBasicBlockLabel,
150 MachineBasicBlock,
151 StackObject,
152 FixedStackObject,
153 NamedGlobalValue,
154 GlobalValue,
155 ExternalSymbol,
156 MCSymbol,
158 // Other tokens
159 IntegerLiteral,
160 FloatingPointLiteral,
161 HexLiteral,
162 VectorLiteral,
163 VirtualRegister,
164 ConstantPoolItem,
165 JumpTableIndex,
166 NamedIRBlock,
167 IRBlock,
168 NamedIRValue,
169 IRValue,
170 QuotedIRValue, // `<constant value>`
171 SubRegisterIndex,
172 StringConstant
175 private:
176 TokenKind Kind = Error;
177 StringRef Range;
178 StringRef StringValue;
179 std::string StringValueStorage;
180 APSInt IntVal;
182 public:
183 MIToken() = default;
185 MIToken &reset(TokenKind Kind, StringRef Range);
187 MIToken &setStringValue(StringRef StrVal);
188 MIToken &setOwnedStringValue(std::string StrVal);
189 MIToken &setIntegerValue(APSInt IntVal);
191 TokenKind kind() const { return Kind; }
193 bool isError() const { return Kind == Error; }
195 bool isNewlineOrEOF() const { return Kind == Newline || Kind == Eof; }
197 bool isErrorOrEOF() const { return Kind == Error || Kind == Eof; }
199 bool isRegister() const {
200 return Kind == NamedRegister || Kind == underscore ||
201 Kind == NamedVirtualRegister || Kind == VirtualRegister;
204 bool isRegisterFlag() const {
205 return Kind == kw_implicit || Kind == kw_implicit_define ||
206 Kind == kw_def || Kind == kw_dead || Kind == kw_killed ||
207 Kind == kw_undef || Kind == kw_internal ||
208 Kind == kw_early_clobber || Kind == kw_debug_use ||
209 Kind == kw_renamable;
212 bool isMemoryOperandFlag() const {
213 return Kind == kw_volatile || Kind == kw_non_temporal ||
214 Kind == kw_dereferenceable || Kind == kw_invariant ||
215 Kind == StringConstant;
218 bool is(TokenKind K) const { return Kind == K; }
220 bool isNot(TokenKind K) const { return Kind != K; }
222 StringRef::iterator location() const { return Range.begin(); }
224 StringRef range() const { return Range; }
226 /// Return the token's string value.
227 StringRef stringValue() const { return StringValue; }
229 const APSInt &integerValue() const { return IntVal; }
231 bool hasIntegerValue() const {
232 return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
233 Kind == MachineBasicBlockLabel || Kind == StackObject ||
234 Kind == FixedStackObject || Kind == GlobalValue ||
235 Kind == VirtualRegister || Kind == ConstantPoolItem ||
236 Kind == JumpTableIndex || Kind == IRBlock || Kind == IRValue;
240 /// Consume a single machine instruction token in the given source and return
241 /// the remaining source string.
242 StringRef lexMIToken(
243 StringRef Source, MIToken &Token,
244 function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
246 } // end namespace llvm
248 #endif // LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H