Home | History | Annotate | Download | only in MIRParser
      1 //===- MILexer.h - Lexer for machine instructions -------------------------===//
      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/StringRef.h"
     20 #include "llvm/ADT/STLExtras.h"
     21 #include <functional>
     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     exclaim,
     42     lparen,
     43     rparen,
     44     lbrace,
     45     rbrace,
     46     plus,
     47     minus,
     48 
     49     // Keywords
     50     kw_implicit,
     51     kw_implicit_define,
     52     kw_def,
     53     kw_dead,
     54     kw_killed,
     55     kw_undef,
     56     kw_internal,
     57     kw_early_clobber,
     58     kw_debug_use,
     59     kw_tied_def,
     60     kw_frame_setup,
     61     kw_debug_location,
     62     kw_cfi_same_value,
     63     kw_cfi_offset,
     64     kw_cfi_def_cfa_register,
     65     kw_cfi_def_cfa_offset,
     66     kw_cfi_def_cfa,
     67     kw_blockaddress,
     68     kw_target_index,
     69     kw_half,
     70     kw_float,
     71     kw_double,
     72     kw_x86_fp80,
     73     kw_fp128,
     74     kw_ppc_fp128,
     75     kw_target_flags,
     76     kw_volatile,
     77     kw_non_temporal,
     78     kw_invariant,
     79     kw_align,
     80     kw_stack,
     81     kw_got,
     82     kw_jump_table,
     83     kw_constant_pool,
     84     kw_call_entry,
     85     kw_liveout,
     86     kw_address_taken,
     87     kw_landing_pad,
     88     kw_liveins,
     89     kw_successors,
     90 
     91     // Named metadata keywords
     92     md_tbaa,
     93     md_alias_scope,
     94     md_noalias,
     95     md_range,
     96 
     97     // Identifier tokens
     98     Identifier,
     99     IntegerType,
    100     NamedRegister,
    101     MachineBasicBlockLabel,
    102     MachineBasicBlock,
    103     StackObject,
    104     FixedStackObject,
    105     NamedGlobalValue,
    106     GlobalValue,
    107     ExternalSymbol,
    108 
    109     // Other tokens
    110     IntegerLiteral,
    111     FloatingPointLiteral,
    112     VirtualRegister,
    113     ConstantPoolItem,
    114     JumpTableIndex,
    115     NamedIRBlock,
    116     IRBlock,
    117     NamedIRValue,
    118     IRValue,
    119     QuotedIRValue // `<constant value>`
    120   };
    121 
    122 private:
    123   TokenKind Kind;
    124   StringRef Range;
    125   StringRef StringValue;
    126   std::string StringValueStorage;
    127   APSInt IntVal;
    128 
    129 public:
    130   MIToken() : Kind(Error) {}
    131 
    132   MIToken &reset(TokenKind Kind, StringRef Range);
    133 
    134   MIToken &setStringValue(StringRef StrVal);
    135   MIToken &setOwnedStringValue(std::string StrVal);
    136   MIToken &setIntegerValue(APSInt IntVal);
    137 
    138   TokenKind kind() const { return Kind; }
    139 
    140   bool isError() const { return Kind == Error; }
    141 
    142   bool isNewlineOrEOF() const { return Kind == Newline || Kind == Eof; }
    143 
    144   bool isErrorOrEOF() const { return Kind == Error || Kind == Eof; }
    145 
    146   bool isRegister() const {
    147     return Kind == NamedRegister || Kind == underscore ||
    148            Kind == VirtualRegister;
    149   }
    150 
    151   bool isRegisterFlag() const {
    152     return Kind == kw_implicit || Kind == kw_implicit_define ||
    153            Kind == kw_def || Kind == kw_dead || Kind == kw_killed ||
    154            Kind == kw_undef || Kind == kw_internal ||
    155            Kind == kw_early_clobber || Kind == kw_debug_use;
    156   }
    157 
    158   bool isMemoryOperandFlag() const {
    159     return Kind == kw_volatile || Kind == kw_non_temporal ||
    160            Kind == kw_invariant;
    161   }
    162 
    163   bool is(TokenKind K) const { return Kind == K; }
    164 
    165   bool isNot(TokenKind K) const { return Kind != K; }
    166 
    167   StringRef::iterator location() const { return Range.begin(); }
    168 
    169   StringRef range() const { return Range; }
    170 
    171   /// Return the token's string value.
    172   StringRef stringValue() const { return StringValue; }
    173 
    174   const APSInt &integerValue() const { return IntVal; }
    175 
    176   bool hasIntegerValue() const {
    177     return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
    178            Kind == MachineBasicBlockLabel || Kind == StackObject ||
    179            Kind == FixedStackObject || Kind == GlobalValue ||
    180            Kind == VirtualRegister || Kind == ConstantPoolItem ||
    181            Kind == JumpTableIndex || Kind == IRBlock || Kind == IRValue;
    182   }
    183 };
    184 
    185 /// Consume a single machine instruction token in the given source and return
    186 /// the remaining source string.
    187 StringRef lexMIToken(
    188     StringRef Source, MIToken &Token,
    189     function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
    190 
    191 } // end namespace llvm
    192 
    193 #endif
    194