265 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8//9// This file declares the function that lexes the machine instruction source10// string.11//12//===----------------------------------------------------------------------===//13 14#ifndef LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H15#define LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H16 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 // Markers29 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 // Keywords51 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_blockaddress,103 kw_intrinsic,104 kw_target_index,105 kw_half,106 kw_bfloat,107 kw_float,108 kw_double,109 kw_x86_fp80,110 kw_fp128,111 kw_ppc_fp128,112 kw_target_flags,113 kw_volatile,114 kw_non_temporal,115 kw_invariant,116 kw_align,117 kw_basealign,118 kw_addrspace,119 kw_stack,120 kw_got,121 kw_jump_table,122 kw_constant_pool,123 kw_call_entry,124 kw_custom,125 kw_liveout,126 kw_landing_pad,127 kw_inlineasm_br_indirect_target,128 kw_ehfunclet_entry,129 kw_liveins,130 kw_successors,131 kw_floatpred,132 kw_intpred,133 kw_shufflemask,134 kw_pre_instr_symbol,135 kw_post_instr_symbol,136 kw_heap_alloc_marker,137 kw_pcsections,138 kw_cfi_type,139 kw_deactivation_symbol,140 kw_bbsections,141 kw_bb_id,142 kw_unknown_size,143 kw_unknown_address,144 kw_ir_block_address_taken,145 kw_machine_block_address_taken,146 kw_call_frame_size,147 kw_noconvergent,148 149 // Metadata types.150 kw_distinct,151 152 // Named metadata keywords153 md_tbaa,154 md_alias_scope,155 md_noalias,156 md_noalias_addrspace,157 md_range,158 md_diexpr,159 md_dilocation,160 161 // Identifier tokens162 Identifier,163 NamedRegister,164 NamedVirtualRegister,165 MachineBasicBlockLabel,166 MachineBasicBlock,167 StackObject,168 FixedStackObject,169 NamedGlobalValue,170 GlobalValue,171 ExternalSymbol,172 MCSymbol,173 174 // Other tokens175 IntegerLiteral,176 FloatingPointLiteral,177 HexLiteral,178 VectorLiteral,179 VirtualRegister,180 ConstantPoolItem,181 JumpTableIndex,182 NamedIRBlock,183 IRBlock,184 NamedIRValue,185 IRValue,186 QuotedIRValue, // `<constant value>`187 SubRegisterIndex,188 StringConstant189 };190 191private:192 TokenKind Kind = Error;193 StringRef Range;194 StringRef StringValue;195 std::string StringValueStorage;196 APSInt IntVal;197 198public:199 MIToken() = default;200 201 MIToken &reset(TokenKind Kind, StringRef Range);202 203 MIToken &setStringValue(StringRef StrVal);204 MIToken &setOwnedStringValue(std::string StrVal);205 MIToken &setIntegerValue(APSInt IntVal);206 207 TokenKind kind() const { return Kind; }208 209 bool isError() const { return Kind == Error; }210 211 bool isNewlineOrEOF() const { return Kind == Newline || Kind == Eof; }212 213 bool isErrorOrEOF() const { return Kind == Error || Kind == Eof; }214 215 bool isRegister() const {216 return Kind == NamedRegister || Kind == underscore ||217 Kind == NamedVirtualRegister || Kind == VirtualRegister;218 }219 220 bool isRegisterFlag() const {221 return Kind == kw_implicit || Kind == kw_implicit_define ||222 Kind == kw_def || Kind == kw_dead || Kind == kw_killed ||223 Kind == kw_undef || Kind == kw_internal ||224 Kind == kw_early_clobber || Kind == kw_debug_use ||225 Kind == kw_renamable;226 }227 228 bool isMemoryOperandFlag() const {229 return Kind == kw_volatile || Kind == kw_non_temporal ||230 Kind == kw_dereferenceable || Kind == kw_invariant ||231 Kind == StringConstant;232 }233 234 bool is(TokenKind K) const { return Kind == K; }235 236 bool isNot(TokenKind K) const { return Kind != K; }237 238 StringRef::iterator location() const { return Range.begin(); }239 240 StringRef range() const { return Range; }241 242 /// Return the token's string value.243 StringRef stringValue() const { return StringValue; }244 245 const APSInt &integerValue() const { return IntVal; }246 247 bool hasIntegerValue() const {248 return Kind == IntegerLiteral || Kind == MachineBasicBlock ||249 Kind == MachineBasicBlockLabel || Kind == StackObject ||250 Kind == FixedStackObject || Kind == GlobalValue ||251 Kind == VirtualRegister || Kind == ConstantPoolItem ||252 Kind == JumpTableIndex || Kind == IRBlock || Kind == IRValue;253 }254};255 256/// Consume a single machine instruction token in the given source and return257/// the remaining source string.258StringRef lexMIToken(259 StringRef Source, MIToken &Token,260 function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);261 262} // end namespace llvm263 264#endif // LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H265