Home | History | Annotate | Download | only in MIRParser
      1 //===- MILexer.h - Lexer for machine instructions ---------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file declares the function that lexes the machine instruction source
     11 // string.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
     16 #define LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
     17 
     18 #include "llvm/ADT/APSInt.h"
     19 #include "llvm/ADT/STLExtras.h"
     20 #include "llvm/ADT/StringRef.h"
     21 #include <string>
     22 
     23 namespace llvm {
     24 
     25 class Twine;
     26 
     27 /// A token produced by the machine instruction lexer.
     28 struct MIToken {
     29   enum TokenKind {
     30     // Markers
     31     Eof,
     32     Error,
     33     Newline,
     34 
     35     // Tokens with no info.
     36     comma,
     37     equal,
     38     underscore,
     39     colon,
     40     coloncolon,
     41     dot,
     42     exclaim,
     43     lparen,
     44     rparen,
     45     lbrace,
     46     rbrace,
     47     plus,
     48     minus,
     49     less,
     50     greater,
     51 
     52     // Keywords
     53     kw_implicit,
     54     kw_implicit_define,
     55     kw_def,
     56     kw_dead,
     57     kw_dereferenceable,
     58     kw_killed,
     59     kw_undef,
     60     kw_internal,
     61     kw_early_clobber,
     62     kw_debug_use,
     63     kw_renamable,
     64     kw_tied_def,
     65     kw_frame_setup,
     66     kw_frame_destroy,
     67     kw_nnan,
     68     kw_ninf,
     69     kw_nsz,
     70     kw_arcp,
     71     kw_contract,
     72     kw_afn,
     73     kw_reassoc,
     74     kw_debug_location,
     75     kw_cfi_same_value,
     76     kw_cfi_offset,
     77     kw_cfi_rel_offset,
     78     kw_cfi_def_cfa_register,
     79     kw_cfi_def_cfa_offset,
     80     kw_cfi_adjust_cfa_offset,
     81     kw_cfi_escape,
     82     kw_cfi_def_cfa,
     83     kw_cfi_register,
     84     kw_cfi_remember_state,
     85     kw_cfi_restore,
     86     kw_cfi_restore_state,
     87     kw_cfi_undefined,
     88     kw_cfi_window_save,
     89     kw_blockaddress,
     90     kw_intrinsic,
     91     kw_target_index,
     92     kw_half,
     93     kw_float,
     94     kw_double,
     95     kw_x86_fp80,
     96     kw_fp128,
     97     kw_ppc_fp128,
     98     kw_target_flags,
     99     kw_volatile,
    100     kw_non_temporal,
    101     kw_invariant,
    102     kw_align,
    103     kw_addrspace,
    104     kw_stack,
    105     kw_got,
    106     kw_jump_table,
    107     kw_constant_pool,
    108     kw_call_entry,
    109     kw_liveout,
    110     kw_address_taken,
    111     kw_landing_pad,
    112     kw_liveins,
    113     kw_successors,
    114     kw_floatpred,
    115     kw_intpred,
    116 
    117     // Named metadata keywords
    118     md_tbaa,
    119     md_alias_scope,
    120     md_noalias,
    121     md_range,
    122     md_diexpr,
    123 
    124     // Identifier tokens
    125     Identifier,
    126     NamedRegister,
    127     NamedVirtualRegister,
    128     MachineBasicBlockLabel,
    129     MachineBasicBlock,
    130     StackObject,
    131     FixedStackObject,
    132     NamedGlobalValue,
    133     GlobalValue,
    134     ExternalSymbol,
    135 
    136     // Other tokens
    137     IntegerLiteral,
    138     FloatingPointLiteral,
    139     HexLiteral,
    140     VirtualRegister,
    141     ConstantPoolItem,
    142     JumpTableIndex,
    143     NamedIRBlock,
    144     IRBlock,
    145     NamedIRValue,
    146     IRValue,
    147     QuotedIRValue, // `<constant value>`
    148     SubRegisterIndex,
    149     StringConstant
    150   };
    151 
    152 private:
    153   TokenKind Kind = Error;
    154   StringRef Range;
    155   StringRef StringValue;
    156   std::string StringValueStorage;
    157   APSInt IntVal;
    158 
    159 public:
    160   MIToken() = default;
    161 
    162   MIToken &reset(TokenKind Kind, StringRef Range);
    163 
    164   MIToken &setStringValue(StringRef StrVal);
    165   MIToken &setOwnedStringValue(std::string StrVal);
    166   MIToken &setIntegerValue(APSInt IntVal);
    167 
    168   TokenKind kind() const { return Kind; }
    169 
    170   bool isError() const { return Kind == Error; }
    171 
    172   bool isNewlineOrEOF() const { return Kind == Newline || Kind == Eof; }
    173 
    174   bool isErrorOrEOF() const { return Kind == Error || Kind == Eof; }
    175 
    176   bool isRegister() const {
    177     return Kind == NamedRegister || Kind == underscore ||
    178            Kind == NamedVirtualRegister || Kind == VirtualRegister;
    179   }
    180 
    181   bool isRegisterFlag() const {
    182     return Kind == kw_implicit || Kind == kw_implicit_define ||
    183            Kind == kw_def || Kind == kw_dead || Kind == kw_killed ||
    184            Kind == kw_undef || Kind == kw_internal ||
    185            Kind == kw_early_clobber || Kind == kw_debug_use ||
    186            Kind == kw_renamable;
    187   }
    188 
    189   bool isMemoryOperandFlag() const {
    190     return Kind == kw_volatile || Kind == kw_non_temporal ||
    191            Kind == kw_dereferenceable || Kind == kw_invariant ||
    192            Kind == StringConstant;
    193   }
    194 
    195   bool is(TokenKind K) const { return Kind == K; }
    196 
    197   bool isNot(TokenKind K) const { return Kind != K; }
    198 
    199   StringRef::iterator location() const { return Range.begin(); }
    200 
    201   StringRef range() const { return Range; }
    202 
    203   /// Return the token's string value.
    204   StringRef stringValue() const { return StringValue; }
    205 
    206   const APSInt &integerValue() const { return IntVal; }
    207 
    208   bool hasIntegerValue() const {
    209     return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
    210            Kind == MachineBasicBlockLabel || Kind == StackObject ||
    211            Kind == FixedStackObject || Kind == GlobalValue ||
    212            Kind == VirtualRegister || Kind == ConstantPoolItem ||
    213            Kind == JumpTableIndex || Kind == IRBlock || Kind == IRValue;
    214   }
    215 };
    216 
    217 /// Consume a single machine instruction token in the given source and return
    218 /// the remaining source string.
    219 StringRef lexMIToken(
    220     StringRef Source, MIToken &Token,
    221     function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
    222 
    223 } // end namespace llvm
    224 
    225 #endif // LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
    226