LLVM  14.0.0git
MILexer.h
Go to the documentation of this file.
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 
21 namespace llvm {
22 
23 class Twine;
24 
25 /// A token produced by the machine instruction lexer.
26 struct MIToken {
27  enum TokenKind {
28  // Markers
29  Eof,
32 
33  // Tokens with no info.
39  dot,
49 
50  // Keywords
131 
132  // Metadata types.
134 
135  // Named metadata keywords
142 
143  // Identifier tokens
155 
156  // Other tokens
168  QuotedIRValue, // `<constant value>`
171  };
172 
173 private:
174  TokenKind Kind = Error;
175  StringRef Range;
176  StringRef StringValue;
177  std::string StringValueStorage;
178  APSInt IntVal;
179 
180 public:
181  MIToken() = default;
182 
183  MIToken &reset(TokenKind Kind, StringRef Range);
184 
186  MIToken &setOwnedStringValue(std::string StrVal);
187  MIToken &setIntegerValue(APSInt IntVal);
188 
189  TokenKind kind() const { return Kind; }
190 
191  bool isError() const { return Kind == Error; }
192 
193  bool isNewlineOrEOF() const { return Kind == Newline || Kind == Eof; }
194 
195  bool isErrorOrEOF() const { return Kind == Error || Kind == Eof; }
196 
197  bool isRegister() const {
198  return Kind == NamedRegister || Kind == underscore ||
199  Kind == NamedVirtualRegister || Kind == VirtualRegister;
200  }
201 
202  bool isRegisterFlag() const {
203  return Kind == kw_implicit || Kind == kw_implicit_define ||
204  Kind == kw_def || Kind == kw_dead || Kind == kw_killed ||
205  Kind == kw_undef || Kind == kw_internal ||
206  Kind == kw_early_clobber || Kind == kw_debug_use ||
207  Kind == kw_renamable;
208  }
209 
210  bool isMemoryOperandFlag() const {
211  return Kind == kw_volatile || Kind == kw_non_temporal ||
212  Kind == kw_dereferenceable || Kind == kw_invariant ||
213  Kind == StringConstant;
214  }
215 
216  bool is(TokenKind K) const { return Kind == K; }
217 
218  bool isNot(TokenKind K) const { return Kind != K; }
219 
220  StringRef::iterator location() const { return Range.begin(); }
221 
222  StringRef range() const { return Range; }
223 
224  /// Return the token's string value.
225  StringRef stringValue() const { return StringValue; }
226 
227  const APSInt &integerValue() const { return IntVal; }
228 
229  bool hasIntegerValue() const {
230  return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
231  Kind == MachineBasicBlockLabel || Kind == StackObject ||
232  Kind == FixedStackObject || Kind == GlobalValue ||
233  Kind == VirtualRegister || Kind == ConstantPoolItem ||
234  Kind == JumpTableIndex || Kind == IRBlock || Kind == IRValue;
235  }
236 };
237 
238 /// Consume a single machine instruction token in the given source and return
239 /// the remaining source string.
240 StringRef lexMIToken(
241  StringRef Source, MIToken &Token,
242  function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
243 
244 } // end namespace llvm
245 
246 #endif // LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
llvm::MIToken::kw_float
@ kw_float
Definition: MILexer.h:98
llvm::MIToken::NamedRegister
@ NamedRegister
Definition: MILexer.h:145
llvm::MIToken::kw_cfi_offset
@ kw_cfi_offset
Definition: MILexer.h:79
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::MIToken::kw_exact
@ kw_exact
Definition: MILexer.h:74
llvm::MIToken::kw_unknown_size
@ kw_unknown_size
Definition: MILexer.h:129
llvm::MIToken::colon
@ colon
Definition: MILexer.h:37
llvm::MIToken::FixedStackObject
@ FixedStackObject
Definition: MILexer.h:150
llvm::MIToken::kw_nsz
@ kw_nsz
Definition: MILexer.h:67
llvm::MIToken::GlobalValue
@ GlobalValue
Definition: MILexer.h:152
llvm::MIToken::VirtualRegister
@ VirtualRegister
Definition: MILexer.h:161
llvm::MIToken::kw_cfi_restore_state
@ kw_cfi_restore_state
Definition: MILexer.h:90
llvm::MIToken::md_noalias
@ md_noalias
Definition: MILexer.h:138
StringRef.h
llvm::MIToken::Error
@ Error
Definition: MILexer.h:30
llvm::MIToken::kw_call_entry
@ kw_call_entry
Definition: MILexer.h:114
llvm::MIToken::kw_early_clobber
@ kw_early_clobber
Definition: MILexer.h:59
llvm::MIToken::integerValue
const APSInt & integerValue() const
Definition: MILexer.h:227
llvm::MIToken::kw_cfi_def_cfa
@ kw_cfi_def_cfa
Definition: MILexer.h:85
llvm::MIToken::kw_double
@ kw_double
Definition: MILexer.h:99
llvm::MIToken::kw_post_instr_symbol
@ kw_post_instr_symbol
Definition: MILexer.h:126
llvm::MIToken::kw_afn
@ kw_afn
Definition: MILexer.h:70
llvm::MIToken::NamedGlobalValue
@ NamedGlobalValue
Definition: MILexer.h:151
llvm::MIToken::kw_renamable
@ kw_renamable
Definition: MILexer.h:61
llvm::MIToken::kw_nofpexcept
@ kw_nofpexcept
Definition: MILexer.h:75
llvm::MIToken::underscore
@ underscore
Definition: MILexer.h:36
llvm::MIToken::kw_frame_destroy
@ kw_frame_destroy
Definition: MILexer.h:64
llvm::MIToken::kw_ninf
@ kw_ninf
Definition: MILexer.h:66
llvm::MIToken::kw_addrspace
@ kw_addrspace
Definition: MILexer.h:109
llvm::MIToken::kw_cfi_register
@ kw_cfi_register
Definition: MILexer.h:87
llvm::MIToken::range
StringRef range() const
Definition: MILexer.h:222
llvm::MIToken::kw_internal
@ kw_internal
Definition: MILexer.h:58
llvm::MIToken
A token produced by the machine instruction lexer.
Definition: MILexer.h:26
llvm::MIToken::kw_cfi_def_cfa_register
@ kw_cfi_def_cfa_register
Definition: MILexer.h:81
llvm::MIToken::kw_killed
@ kw_killed
Definition: MILexer.h:56
llvm::MIToken::NamedIRValue
@ NamedIRValue
Definition: MILexer.h:166
llvm::MIToken::stringValue
StringRef stringValue() const
Return the token's string value.
Definition: MILexer.h:225
llvm::MIToken::kw_arcp
@ kw_arcp
Definition: MILexer.h:68
llvm::MIToken::kw_debug_use
@ kw_debug_use
Definition: MILexer.h:60
llvm::MIToken::MachineBasicBlockLabel
@ MachineBasicBlockLabel
Definition: MILexer.h:147
llvm::MIToken::kw_cfi_llvm_def_aspace_cfa
@ kw_cfi_llvm_def_aspace_cfa
Definition: MILexer.h:86
llvm::MIToken::isRegisterFlag
bool isRegisterFlag() const
Definition: MILexer.h:202
llvm::MIToken::kw_cfi_remember_state
@ kw_cfi_remember_state
Definition: MILexer.h:88
llvm::MIToken::kw_invariant
@ kw_invariant
Definition: MILexer.h:106
llvm::MIToken::kw_shufflemask
@ kw_shufflemask
Definition: MILexer.h:124
llvm::MIToken::equal
@ equal
Definition: MILexer.h:35
llvm::MIToken::kw_cfi_restore
@ kw_cfi_restore
Definition: MILexer.h:89
llvm::MIToken::kw_implicit
@ kw_implicit
Definition: MILexer.h:51
llvm::MIToken::FloatingPointLiteral
@ FloatingPointLiteral
Definition: MILexer.h:158
llvm::MIToken::md_tbaa
@ md_tbaa
Definition: MILexer.h:136
llvm::MIToken::kw_x86_fp80
@ kw_x86_fp80
Definition: MILexer.h:100
llvm::StringRef::iterator
const char * iterator
Definition: StringRef.h:62
llvm::MIToken::kw_cfi_undefined
@ kw_cfi_undefined
Definition: MILexer.h:91
llvm::MIToken::kw_distinct
@ kw_distinct
Definition: MILexer.h:133
llvm::MIToken::kw_tied_def
@ kw_tied_def
Definition: MILexer.h:62
llvm::MIToken::IRBlock
@ IRBlock
Definition: MILexer.h:165
llvm::APSInt
An arbitrary precision integer that knows its signedness.
Definition: APSInt.h:22
llvm::MIToken::setStringValue
MIToken & setStringValue(StringRef StrVal)
Definition: MILexer.cpp:70
llvm::MIToken::HexLiteral
@ HexLiteral
Definition: MILexer.h:159
llvm::MIToken::kw_cfi_escape
@ kw_cfi_escape
Definition: MILexer.h:84
llvm::MIToken::kw_heap_alloc_marker
@ kw_heap_alloc_marker
Definition: MILexer.h:127
llvm::MIToken::StringConstant
@ StringConstant
Definition: MILexer.h:170
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
llvm::MIToken::kw_liveout
@ kw_liveout
Definition: MILexer.h:116
llvm::MIToken::kw_reassoc
@ kw_reassoc
Definition: MILexer.h:71
llvm::MIToken::ConstantPoolItem
@ ConstantPoolItem
Definition: MILexer.h:162
llvm::MIToken::hasIntegerValue
bool hasIntegerValue() const
Definition: MILexer.h:229
llvm::MIToken::StackObject
@ StackObject
Definition: MILexer.h:149
llvm::MIToken::kw_dead
@ kw_dead
Definition: MILexer.h:54
llvm::MIToken::kw_cfi_adjust_cfa_offset
@ kw_cfi_adjust_cfa_offset
Definition: MILexer.h:83
llvm::MIToken::isRegister
bool isRegister() const
Definition: MILexer.h:197
llvm::GlobalValue
Definition: GlobalValue.h:44
llvm::MIToken::kw_cfi_window_save
@ kw_cfi_window_save
Definition: MILexer.h:92
llvm::MIToken::kw_pre_instr_symbol
@ kw_pre_instr_symbol
Definition: MILexer.h:125
llvm::MIToken::SubRegisterIndex
@ SubRegisterIndex
Definition: MILexer.h:169
llvm::MIToken::kw_nnan
@ kw_nnan
Definition: MILexer.h:65
llvm::MIToken::kw_target_index
@ kw_target_index
Definition: MILexer.h:96
llvm::MIToken::kw_fp128
@ kw_fp128
Definition: MILexer.h:101
llvm::MIToken::MachineBasicBlock
@ MachineBasicBlock
Definition: MILexer.h:148
llvm::MIToken::Newline
@ Newline
Definition: MILexer.h:31
llvm::MIToken::lbrace
@ lbrace
Definition: MILexer.h:43
llvm::MIToken::kw_half
@ kw_half
Definition: MILexer.h:97
llvm::MIToken::IRValue
@ IRValue
Definition: MILexer.h:167
IntegerLiteral
Definition: ItaniumDemangle.h:2107
llvm::MIToken::comma
@ comma
Definition: MILexer.h:34
llvm::MIToken::dot
@ dot
Definition: MILexer.h:39
APSInt.h
llvm::MIToken::JumpTableIndex
@ JumpTableIndex
Definition: MILexer.h:163
llvm::MIToken::kw_constant_pool
@ kw_constant_pool
Definition: MILexer.h:113
llvm::MIToken::Eof
@ Eof
Definition: MILexer.h:29
llvm::MIToken::isMemoryOperandFlag
bool isMemoryOperandFlag() const
Definition: MILexer.h:210
llvm::MIToken::kw_successors
@ kw_successors
Definition: MILexer.h:121
llvm::MIToken::md_dilocation
@ md_dilocation
Definition: MILexer.h:141
llvm::MIToken::kw_basealign
@ kw_basealign
Definition: MILexer.h:108
llvm::MIToken::kw_frame_setup
@ kw_frame_setup
Definition: MILexer.h:63
llvm::MIToken::kw_debug_location
@ kw_debug_location
Definition: MILexer.h:76
llvm::Sched::Source
@ Source
Definition: TargetLowering.h:99
llvm::MIToken::VectorLiteral
@ VectorLiteral
Definition: MILexer.h:160
llvm::MIToken::kw_nsw
@ kw_nsw
Definition: MILexer.h:73
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::MIToken::md_range
@ md_range
Definition: MILexer.h:139
llvm::MIToken::kw_cfi_aarch64_negate_ra_sign_state
@ kw_cfi_aarch64_negate_ra_sign_state
Definition: MILexer.h:93
llvm::MIToken::isNot
bool isNot(TokenKind K) const
Definition: MILexer.h:218
llvm::MIToken::kw_intrinsic
@ kw_intrinsic
Definition: MILexer.h:95
llvm::MIToken::kw_bbsections
@ kw_bbsections
Definition: MILexer.h:128
llvm::MIToken::kw_implicit_define
@ kw_implicit_define
Definition: MILexer.h:52
llvm::MIToken::kw_ppc_fp128
@ kw_ppc_fp128
Definition: MILexer.h:102
llvm::MIToken::kw_dereferenceable
@ kw_dereferenceable
Definition: MILexer.h:55
llvm::MIToken::kw_jump_table
@ kw_jump_table
Definition: MILexer.h:112
llvm::MIToken::kw_non_temporal
@ kw_non_temporal
Definition: MILexer.h:105
llvm::tgtok::StrVal
@ StrVal
Definition: TGLexer.h:71
llvm::MIToken::minus
@ minus
Definition: MILexer.h:46
llvm::MIToken::ExternalSymbol
@ ExternalSymbol
Definition: MILexer.h:153
llvm::MIToken::kw_target_flags
@ kw_target_flags
Definition: MILexer.h:103
llvm::MIToken::NamedIRBlock
@ NamedIRBlock
Definition: MILexer.h:164
llvm::MIToken::kw_floatpred
@ kw_floatpred
Definition: MILexer.h:122
llvm::MIToken::coloncolon
@ coloncolon
Definition: MILexer.h:38
llvm::MIToken::rparen
@ rparen
Definition: MILexer.h:42
llvm::MIToken::setIntegerValue
MIToken & setIntegerValue(APSInt IntVal)
Definition: MILexer.cpp:81
llvm::MIToken::kw_debug_instr_number
@ kw_debug_instr_number
Definition: MILexer.h:77
llvm::MIToken::isNewlineOrEOF
bool isNewlineOrEOF() const
Definition: MILexer.h:193
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::MIToken::kw_intpred
@ kw_intpred
Definition: MILexer.h:123
llvm::MIToken::MCSymbol
@ MCSymbol
Definition: MILexer.h:154
llvm::MIToken::greater
@ greater
Definition: MILexer.h:48
llvm::MIToken::exclaim
@ exclaim
Definition: MILexer.h:40
llvm::MIToken::IntegerLiteral
@ IntegerLiteral
Definition: MILexer.h:157
llvm::MIToken::kw_align
@ kw_align
Definition: MILexer.h:107
llvm::MIToken::MIToken
MIToken()=default
llvm::MIToken::TokenKind
TokenKind
Definition: MILexer.h:27
llvm::MIToken::kind
TokenKind kind() const
Definition: MILexer.h:189
llvm::MIToken::QuotedIRValue
@ QuotedIRValue
Definition: MILexer.h:168
llvm::MIToken::kw_custom
@ kw_custom
Definition: MILexer.h:115
llvm::MIToken::kw_stack
@ kw_stack
Definition: MILexer.h:110
llvm::MIToken::kw_got
@ kw_got
Definition: MILexer.h:111
llvm::MIToken::is
bool is(TokenKind K) const
Definition: MILexer.h:216
llvm::MIToken::location
StringRef::iterator location() const
Definition: MILexer.h:220
llvm::MIToken::Identifier
@ Identifier
Definition: MILexer.h:144
llvm::MIToken::isError
bool isError() const
Definition: MILexer.h:191
llvm::MIToken::rbrace
@ rbrace
Definition: MILexer.h:44
llvm::MIToken::kw_cfi_def_cfa_offset
@ kw_cfi_def_cfa_offset
Definition: MILexer.h:82
llvm::MIToken::less
@ less
Definition: MILexer.h:47
llvm::MIToken::kw_volatile
@ kw_volatile
Definition: MILexer.h:104
llvm::MIToken::kw_def
@ kw_def
Definition: MILexer.h:53
llvm::MIToken::kw_landing_pad
@ kw_landing_pad
Definition: MILexer.h:118
llvm::MIToken::reset
MIToken & reset(TokenKind Kind, StringRef Range)
Definition: MILexer.cpp:64
llvm::MIToken::setOwnedStringValue
MIToken & setOwnedStringValue(std::string StrVal)
Definition: MILexer.cpp:75
llvm::MIToken::kw_nuw
@ kw_nuw
Definition: MILexer.h:72
llvm::MIToken::plus
@ plus
Definition: MILexer.h:45
llvm::MIToken::md_alias_scope
@ md_alias_scope
Definition: MILexer.h:137
llvm::MIToken::kw_cfi_same_value
@ kw_cfi_same_value
Definition: MILexer.h:78
llvm::MIToken::NamedVirtualRegister
@ NamedVirtualRegister
Definition: MILexer.h:146
llvm::MIToken::kw_ehfunclet_entry
@ kw_ehfunclet_entry
Definition: MILexer.h:119
llvm::StringRef::begin
iterator begin() const
Definition: StringRef.h:128
llvm::MIToken::kw_liveins
@ kw_liveins
Definition: MILexer.h:120
llvm::MIToken::kw_contract
@ kw_contract
Definition: MILexer.h:69
llvm::lexMIToken
StringRef lexMIToken(StringRef Source, MIToken &Token, function_ref< void(StringRef::iterator, const Twine &)> ErrorCallback)
Consume a single machine instruction token in the given source and return the remaining source string...
llvm::MIToken::kw_cfi_rel_offset
@ kw_cfi_rel_offset
Definition: MILexer.h:80
llvm::MIToken::md_diexpr
@ md_diexpr
Definition: MILexer.h:140
llvm::MIToken::lparen
@ lparen
Definition: MILexer.h:41
llvm::MIToken::kw_address_taken
@ kw_address_taken
Definition: MILexer.h:117
llvm::MIToken::kw_undef
@ kw_undef
Definition: MILexer.h:57
llvm::MIToken::isErrorOrEOF
bool isErrorOrEOF() const
Definition: MILexer.h:195
llvm::MIToken::kw_blockaddress
@ kw_blockaddress
Definition: MILexer.h:94
llvm::MIToken::kw_unknown_address
@ kw_unknown_address
Definition: MILexer.h:130