Home | History | Annotate | Download | only in MCParser
      1 //===-- llvm/MC/MCAsmParser.h - Abstract Asm Parser Interface ---*- 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 #ifndef LLVM_MC_MCPARSER_MCASMPARSER_H
     11 #define LLVM_MC_MCPARSER_MCASMPARSER_H
     12 
     13 #include "llvm/ADT/ArrayRef.h"
     14 #include "llvm/ADT/StringRef.h"
     15 #include "llvm/MC/MCParser/AsmLexer.h"
     16 #include "llvm/Support/DataTypes.h"
     17 
     18 namespace llvm {
     19 class MCAsmInfo;
     20 class MCAsmLexer;
     21 class MCAsmParserExtension;
     22 class MCContext;
     23 class MCExpr;
     24 class MCInstPrinter;
     25 class MCInstrInfo;
     26 class MCStreamer;
     27 class MCTargetAsmParser;
     28 class SMLoc;
     29 class SMRange;
     30 class SourceMgr;
     31 class Twine;
     32 
     33 class InlineAsmIdentifierInfo {
     34 public:
     35   void *OpDecl;
     36   bool IsVarDecl;
     37   unsigned Length, Size, Type;
     38 
     39   void clear() {
     40     OpDecl = nullptr;
     41     IsVarDecl = false;
     42     Length = 1;
     43     Size = 0;
     44     Type = 0;
     45   }
     46 };
     47 
     48 /// Generic Sema callback for assembly parser.
     49 class MCAsmParserSemaCallback {
     50 public:
     51   virtual ~MCAsmParserSemaCallback();
     52   virtual void *LookupInlineAsmIdentifier(StringRef &LineBuf,
     53                                           InlineAsmIdentifierInfo &Info,
     54                                           bool IsUnevaluatedContext) = 0;
     55   virtual StringRef LookupInlineAsmLabel(StringRef Identifier, SourceMgr &SM,
     56                                          SMLoc Location, bool Create) = 0;
     57 
     58   virtual bool LookupInlineAsmField(StringRef Base, StringRef Member,
     59                                     unsigned &Offset) = 0;
     60 };
     61 
     62 /// Generic assembler parser interface, for use by target specific assembly
     63 /// parsers.
     64 class MCAsmParser {
     65 public:
     66   typedef bool (*DirectiveHandler)(MCAsmParserExtension*, StringRef, SMLoc);
     67   typedef std::pair<MCAsmParserExtension*, DirectiveHandler>
     68     ExtensionDirectiveHandler;
     69 
     70 private:
     71   MCAsmParser(const MCAsmParser &) = delete;
     72   void operator=(const MCAsmParser &) = delete;
     73 
     74   MCTargetAsmParser *TargetParser;
     75 
     76   unsigned ShowParsedOperands : 1;
     77 
     78 protected: // Can only create subclasses.
     79   MCAsmParser();
     80 
     81 public:
     82   virtual ~MCAsmParser();
     83 
     84   virtual void addDirectiveHandler(StringRef Directive,
     85                                    ExtensionDirectiveHandler Handler) = 0;
     86 
     87   virtual SourceMgr &getSourceManager() = 0;
     88 
     89   virtual MCAsmLexer &getLexer() = 0;
     90   const MCAsmLexer &getLexer() const {
     91     return const_cast<MCAsmParser*>(this)->getLexer();
     92   }
     93 
     94   virtual MCContext &getContext() = 0;
     95 
     96   /// Return the output streamer for the assembler.
     97   virtual MCStreamer &getStreamer() = 0;
     98 
     99   MCTargetAsmParser &getTargetParser() const { return *TargetParser; }
    100   void setTargetParser(MCTargetAsmParser &P);
    101 
    102   virtual unsigned getAssemblerDialect() { return 0;}
    103   virtual void setAssemblerDialect(unsigned i) { }
    104 
    105   bool getShowParsedOperands() const { return ShowParsedOperands; }
    106   void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; }
    107 
    108   /// Run the parser on the input source buffer.
    109   virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
    110 
    111   virtual void setParsingInlineAsm(bool V) = 0;
    112   virtual bool isParsingInlineAsm() = 0;
    113 
    114   /// Parse ms-style inline assembly.
    115   virtual bool parseMSInlineAsm(
    116       void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
    117       unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool>> &OpDecls,
    118       SmallVectorImpl<std::string> &Constraints,
    119       SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
    120       const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) = 0;
    121 
    122   /// Emit a note at the location \p L, with the message \p Msg.
    123   virtual void Note(SMLoc L, const Twine &Msg,
    124                     ArrayRef<SMRange> Ranges = None) = 0;
    125 
    126   /// Emit a warning at the location \p L, with the message \p Msg.
    127   ///
    128   /// \return The return value is true, if warnings are fatal.
    129   virtual bool Warning(SMLoc L, const Twine &Msg,
    130                        ArrayRef<SMRange> Ranges = None) = 0;
    131 
    132   /// Emit an error at the location \p L, with the message \p Msg.
    133   ///
    134   /// \return The return value is always true, as an idiomatic convenience to
    135   /// clients.
    136   virtual bool Error(SMLoc L, const Twine &Msg,
    137                      ArrayRef<SMRange> Ranges = None) = 0;
    138 
    139   /// Get the next AsmToken in the stream, possibly handling file inclusion
    140   /// first.
    141   virtual const AsmToken &Lex() = 0;
    142 
    143   /// Get the current AsmToken from the stream.
    144   const AsmToken &getTok() const;
    145 
    146   /// \brief Report an error at the current lexer location.
    147   bool TokError(const Twine &Msg, ArrayRef<SMRange> Ranges = None);
    148 
    149   /// Parse an identifier or string (as a quoted identifier) and set \p Res to
    150   /// the identifier contents.
    151   virtual bool parseIdentifier(StringRef &Res) = 0;
    152 
    153   /// \brief Parse up to the end of statement and return the contents from the
    154   /// current token until the end of the statement; the current token on exit
    155   /// will be either the EndOfStatement or EOF.
    156   virtual StringRef parseStringToEndOfStatement() = 0;
    157 
    158   /// Parse the current token as a string which may include escaped characters
    159   /// and return the string contents.
    160   virtual bool parseEscapedString(std::string &Data) = 0;
    161 
    162   /// Skip to the end of the current statement, for error recovery.
    163   virtual void eatToEndOfStatement() = 0;
    164 
    165   /// Parse an arbitrary expression.
    166   ///
    167   /// @param Res - The value of the expression. The result is undefined
    168   /// on error.
    169   /// @result - False on success.
    170   virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
    171   bool parseExpression(const MCExpr *&Res);
    172 
    173   /// Parse a primary expression.
    174   ///
    175   /// @param Res - The value of the expression. The result is undefined
    176   /// on error.
    177   /// @result - False on success.
    178   virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) = 0;
    179 
    180   /// Parse an arbitrary expression, assuming that an initial '(' has already
    181   /// been consumed.
    182   ///
    183   /// @param Res - The value of the expression. The result is undefined
    184   /// on error.
    185   /// @result - False on success.
    186   virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
    187 
    188   /// Parse an expression which must evaluate to an absolute value.
    189   ///
    190   /// @param Res - The value of the absolute expression. The result is undefined
    191   /// on error.
    192   /// @result - False on success.
    193   virtual bool parseAbsoluteExpression(int64_t &Res) = 0;
    194 
    195   /// Ensure that we have a valid section set in the streamer. Otherwise, report
    196   /// an error and switch to .text.
    197   virtual void checkForValidSection() = 0;
    198 };
    199 
    200 /// \brief Create an MCAsmParser instance.
    201 MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &,
    202                                MCStreamer &, const MCAsmInfo &);
    203 
    204 } // End llvm namespace
    205 
    206 #endif
    207