Home | History | Annotate | Download | only in MCParser
      1 //===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
      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 class implements the parser for assembly files.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "llvm/ADT/APFloat.h"
     15 #include "llvm/ADT/STLExtras.h"
     16 #include "llvm/ADT/SmallString.h"
     17 #include "llvm/ADT/StringMap.h"
     18 #include "llvm/ADT/Twine.h"
     19 #include "llvm/MC/MCAsmInfo.h"
     20 #include "llvm/MC/MCContext.h"
     21 #include "llvm/MC/MCDwarf.h"
     22 #include "llvm/MC/MCExpr.h"
     23 #include "llvm/MC/MCInstPrinter.h"
     24 #include "llvm/MC/MCInstrInfo.h"
     25 #include "llvm/MC/MCObjectFileInfo.h"
     26 #include "llvm/MC/MCParser/AsmCond.h"
     27 #include "llvm/MC/MCParser/AsmLexer.h"
     28 #include "llvm/MC/MCParser/MCAsmParser.h"
     29 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
     30 #include "llvm/MC/MCRegisterInfo.h"
     31 #include "llvm/MC/MCSectionMachO.h"
     32 #include "llvm/MC/MCStreamer.h"
     33 #include "llvm/MC/MCSymbol.h"
     34 #include "llvm/MC/MCTargetAsmParser.h"
     35 #include "llvm/Support/CommandLine.h"
     36 #include "llvm/Support/ErrorHandling.h"
     37 #include "llvm/Support/MathExtras.h"
     38 #include "llvm/Support/MemoryBuffer.h"
     39 #include "llvm/Support/SourceMgr.h"
     40 #include "llvm/Support/raw_ostream.h"
     41 #include <cctype>
     42 #include <deque>
     43 #include <set>
     44 #include <string>
     45 #include <vector>
     46 using namespace llvm;
     47 
     48 MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
     49 
     50 namespace {
     51 /// \brief Helper types for tracking macro definitions.
     52 typedef std::vector<AsmToken> MCAsmMacroArgument;
     53 typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
     54 
     55 struct MCAsmMacroParameter {
     56   StringRef Name;
     57   MCAsmMacroArgument Value;
     58   bool Required;
     59   bool Vararg;
     60 
     61   MCAsmMacroParameter() : Required(false), Vararg(false) {}
     62 };
     63 
     64 typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
     65 
     66 struct MCAsmMacro {
     67   StringRef Name;
     68   StringRef Body;
     69   MCAsmMacroParameters Parameters;
     70 
     71 public:
     72   MCAsmMacro(StringRef N, StringRef B, MCAsmMacroParameters P)
     73       : Name(N), Body(B), Parameters(std::move(P)) {}
     74 };
     75 
     76 /// \brief Helper class for storing information about an active macro
     77 /// instantiation.
     78 struct MacroInstantiation {
     79   /// The location of the instantiation.
     80   SMLoc InstantiationLoc;
     81 
     82   /// The buffer where parsing should resume upon instantiation completion.
     83   int ExitBuffer;
     84 
     85   /// The location where parsing should resume upon instantiation completion.
     86   SMLoc ExitLoc;
     87 
     88   /// The depth of TheCondStack at the start of the instantiation.
     89   size_t CondStackDepth;
     90 
     91 public:
     92   MacroInstantiation(SMLoc IL, int EB, SMLoc EL, size_t CondStackDepth);
     93 };
     94 
     95 struct ParseStatementInfo {
     96   /// \brief The parsed operands from the last parsed statement.
     97   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands;
     98 
     99   /// \brief The opcode from the last parsed instruction.
    100   unsigned Opcode;
    101 
    102   /// \brief Was there an error parsing the inline assembly?
    103   bool ParseError;
    104 
    105   SmallVectorImpl<AsmRewrite> *AsmRewrites;
    106 
    107   ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(nullptr) {}
    108   ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
    109     : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
    110 };
    111 
    112 /// \brief The concrete assembly parser instance.
    113 class AsmParser : public MCAsmParser {
    114   AsmParser(const AsmParser &) = delete;
    115   void operator=(const AsmParser &) = delete;
    116 private:
    117   AsmLexer Lexer;
    118   MCContext &Ctx;
    119   MCStreamer &Out;
    120   const MCAsmInfo &MAI;
    121   SourceMgr &SrcMgr;
    122   SourceMgr::DiagHandlerTy SavedDiagHandler;
    123   void *SavedDiagContext;
    124   std::unique_ptr<MCAsmParserExtension> PlatformParser;
    125 
    126   /// This is the current buffer index we're lexing from as managed by the
    127   /// SourceMgr object.
    128   unsigned CurBuffer;
    129 
    130   AsmCond TheCondState;
    131   std::vector<AsmCond> TheCondStack;
    132 
    133   /// \brief maps directive names to handler methods in parser
    134   /// extensions. Extensions register themselves in this map by calling
    135   /// addDirectiveHandler.
    136   StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
    137 
    138   /// \brief Map of currently defined macros.
    139   StringMap<MCAsmMacro> MacroMap;
    140 
    141   /// \brief Stack of active macro instantiations.
    142   std::vector<MacroInstantiation*> ActiveMacros;
    143 
    144   /// \brief List of bodies of anonymous macros.
    145   std::deque<MCAsmMacro> MacroLikeBodies;
    146 
    147   /// Boolean tracking whether macro substitution is enabled.
    148   unsigned MacrosEnabledFlag : 1;
    149 
    150   /// Flag tracking whether any errors have been encountered.
    151   unsigned HadError : 1;
    152 
    153   /// The values from the last parsed cpp hash file line comment if any.
    154   StringRef CppHashFilename;
    155   int64_t CppHashLineNumber;
    156   SMLoc CppHashLoc;
    157   unsigned CppHashBuf;
    158   /// When generating dwarf for assembly source files we need to calculate the
    159   /// logical line number based on the last parsed cpp hash file line comment
    160   /// and current line. Since this is slow and messes up the SourceMgr's
    161   /// cache we save the last info we queried with SrcMgr.FindLineNumber().
    162   SMLoc LastQueryIDLoc;
    163   unsigned LastQueryBuffer;
    164   unsigned LastQueryLine;
    165 
    166   /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
    167   unsigned AssemblerDialect;
    168 
    169   /// \brief is Darwin compatibility enabled?
    170   bool IsDarwin;
    171 
    172   /// \brief Are we parsing ms-style inline assembly?
    173   bool ParsingInlineAsm;
    174 
    175 public:
    176   AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
    177             const MCAsmInfo &MAI);
    178   ~AsmParser() override;
    179 
    180   bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
    181 
    182   void addDirectiveHandler(StringRef Directive,
    183                            ExtensionDirectiveHandler Handler) override {
    184     ExtensionDirectiveMap[Directive] = Handler;
    185   }
    186 
    187 public:
    188   /// @name MCAsmParser Interface
    189   /// {
    190 
    191   SourceMgr &getSourceManager() override { return SrcMgr; }
    192   MCAsmLexer &getLexer() override { return Lexer; }
    193   MCContext &getContext() override { return Ctx; }
    194   MCStreamer &getStreamer() override { return Out; }
    195   unsigned getAssemblerDialect() override {
    196     if (AssemblerDialect == ~0U)
    197       return MAI.getAssemblerDialect();
    198     else
    199       return AssemblerDialect;
    200   }
    201   void setAssemblerDialect(unsigned i) override {
    202     AssemblerDialect = i;
    203   }
    204 
    205   void Note(SMLoc L, const Twine &Msg,
    206             ArrayRef<SMRange> Ranges = None) override;
    207   bool Warning(SMLoc L, const Twine &Msg,
    208                ArrayRef<SMRange> Ranges = None) override;
    209   bool Error(SMLoc L, const Twine &Msg,
    210              ArrayRef<SMRange> Ranges = None) override;
    211 
    212   const AsmToken &Lex() override;
    213 
    214   void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
    215   bool isParsingInlineAsm() override { return ParsingInlineAsm; }
    216 
    217   bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
    218                         unsigned &NumOutputs, unsigned &NumInputs,
    219                         SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
    220                         SmallVectorImpl<std::string> &Constraints,
    221                         SmallVectorImpl<std::string> &Clobbers,
    222                         const MCInstrInfo *MII, const MCInstPrinter *IP,
    223                         MCAsmParserSemaCallback &SI) override;
    224 
    225   bool parseExpression(const MCExpr *&Res);
    226   bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
    227   bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
    228   bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
    229   bool parseAbsoluteExpression(int64_t &Res) override;
    230 
    231   /// \brief Parse an identifier or string (as a quoted identifier)
    232   /// and set \p Res to the identifier contents.
    233   bool parseIdentifier(StringRef &Res) override;
    234   void eatToEndOfStatement() override;
    235 
    236   void checkForValidSection() override;
    237   /// }
    238 
    239 private:
    240 
    241   bool parseStatement(ParseStatementInfo &Info,
    242                       MCAsmParserSemaCallback *SI);
    243   void eatToEndOfLine();
    244   bool parseCppHashLineFilenameComment(const SMLoc &L);
    245 
    246   void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
    247                         ArrayRef<MCAsmMacroParameter> Parameters);
    248   bool expandMacro(raw_svector_ostream &OS, StringRef Body,
    249                    ArrayRef<MCAsmMacroParameter> Parameters,
    250                    ArrayRef<MCAsmMacroArgument> A,
    251                    const SMLoc &L);
    252 
    253   /// \brief Are macros enabled in the parser?
    254   bool areMacrosEnabled() {return MacrosEnabledFlag;}
    255 
    256   /// \brief Control a flag in the parser that enables or disables macros.
    257   void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
    258 
    259   /// \brief Lookup a previously defined macro.
    260   /// \param Name Macro name.
    261   /// \returns Pointer to macro. NULL if no such macro was defined.
    262   const MCAsmMacro* lookupMacro(StringRef Name);
    263 
    264   /// \brief Define a new macro with the given name and information.
    265   void defineMacro(StringRef Name, MCAsmMacro Macro);
    266 
    267   /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
    268   void undefineMacro(StringRef Name);
    269 
    270   /// \brief Are we inside a macro instantiation?
    271   bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
    272 
    273   /// \brief Handle entry to macro instantiation.
    274   ///
    275   /// \param M The macro.
    276   /// \param NameLoc Instantiation location.
    277   bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
    278 
    279   /// \brief Handle exit from macro instantiation.
    280   void handleMacroExit();
    281 
    282   /// \brief Extract AsmTokens for a macro argument.
    283   bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
    284 
    285   /// \brief Parse all macro arguments for a given macro.
    286   bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
    287 
    288   void printMacroInstantiations();
    289   void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
    290                     ArrayRef<SMRange> Ranges = None) const {
    291     SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
    292   }
    293   static void DiagHandler(const SMDiagnostic &Diag, void *Context);
    294 
    295   /// \brief Enter the specified file. This returns true on failure.
    296   bool enterIncludeFile(const std::string &Filename);
    297 
    298   /// \brief Process the specified file for the .incbin directive.
    299   /// This returns true on failure.
    300   bool processIncbinFile(const std::string &Filename);
    301 
    302   /// \brief Reset the current lexer position to that given by \p Loc. The
    303   /// current token is not set; clients should ensure Lex() is called
    304   /// subsequently.
    305   ///
    306   /// \param InBuffer If not 0, should be the known buffer id that contains the
    307   /// location.
    308   void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
    309 
    310   /// \brief Parse up to the end of statement and a return the contents from the
    311   /// current token until the end of the statement; the current token on exit
    312   /// will be either the EndOfStatement or EOF.
    313   StringRef parseStringToEndOfStatement() override;
    314 
    315   /// \brief Parse until the end of a statement or a comma is encountered,
    316   /// return the contents from the current token up to the end or comma.
    317   StringRef parseStringToComma();
    318 
    319   bool parseAssignment(StringRef Name, bool allow_redef,
    320                        bool NoDeadStrip = false);
    321 
    322   bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
    323   bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
    324   bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
    325 
    326   bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
    327 
    328   // Generic (target and platform independent) directive parsing.
    329   enum DirectiveKind {
    330     DK_NO_DIRECTIVE, // Placeholder
    331     DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
    332     DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
    333     DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
    334     DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
    335     DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
    336     DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
    337     DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
    338     DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
    339     DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
    340     DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
    341     DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
    342     DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFNES, DK_IFDEF, DK_IFNDEF,
    343     DK_IFNOTDEF, DK_ELSEIF, DK_ELSE, DK_ENDIF,
    344     DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
    345     DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
    346     DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
    347     DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
    348     DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
    349     DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
    350     DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
    351     DK_MACROS_ON, DK_MACROS_OFF,
    352     DK_MACRO, DK_EXITM, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
    353     DK_SLEB128, DK_ULEB128,
    354     DK_ERR, DK_ERROR, DK_WARNING,
    355     DK_END
    356   };
    357 
    358   /// \brief Maps directive name --> DirectiveKind enum, for
    359   /// directives parsed by this class.
    360   StringMap<DirectiveKind> DirectiveKindMap;
    361 
    362   // ".ascii", ".asciz", ".string"
    363   bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
    364   bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
    365   bool parseDirectiveOctaValue(); // ".octa"
    366   bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
    367   bool parseDirectiveFill(); // ".fill"
    368   bool parseDirectiveZero(); // ".zero"
    369   // ".set", ".equ", ".equiv"
    370   bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
    371   bool parseDirectiveOrg(); // ".org"
    372   // ".align{,32}", ".p2align{,w,l}"
    373   bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
    374 
    375   // ".file", ".line", ".loc", ".stabs"
    376   bool parseDirectiveFile(SMLoc DirectiveLoc);
    377   bool parseDirectiveLine();
    378   bool parseDirectiveLoc();
    379   bool parseDirectiveStabs();
    380 
    381   // .cfi directives
    382   bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
    383   bool parseDirectiveCFIWindowSave();
    384   bool parseDirectiveCFISections();
    385   bool parseDirectiveCFIStartProc();
    386   bool parseDirectiveCFIEndProc();
    387   bool parseDirectiveCFIDefCfaOffset();
    388   bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
    389   bool parseDirectiveCFIAdjustCfaOffset();
    390   bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
    391   bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
    392   bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
    393   bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
    394   bool parseDirectiveCFIRememberState();
    395   bool parseDirectiveCFIRestoreState();
    396   bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
    397   bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
    398   bool parseDirectiveCFIEscape();
    399   bool parseDirectiveCFISignalFrame();
    400   bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
    401 
    402   // macro directives
    403   bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
    404   bool parseDirectiveExitMacro(StringRef Directive);
    405   bool parseDirectiveEndMacro(StringRef Directive);
    406   bool parseDirectiveMacro(SMLoc DirectiveLoc);
    407   bool parseDirectiveMacrosOnOff(StringRef Directive);
    408 
    409   // ".bundle_align_mode"
    410   bool parseDirectiveBundleAlignMode();
    411   // ".bundle_lock"
    412   bool parseDirectiveBundleLock();
    413   // ".bundle_unlock"
    414   bool parseDirectiveBundleUnlock();
    415 
    416   // ".space", ".skip"
    417   bool parseDirectiveSpace(StringRef IDVal);
    418 
    419   // .sleb128 (Signed=true) and .uleb128 (Signed=false)
    420   bool parseDirectiveLEB128(bool Signed);
    421 
    422   /// \brief Parse a directive like ".globl" which
    423   /// accepts a single symbol (which should be a label or an external).
    424   bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
    425 
    426   bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
    427 
    428   bool parseDirectiveAbort(); // ".abort"
    429   bool parseDirectiveInclude(); // ".include"
    430   bool parseDirectiveIncbin(); // ".incbin"
    431 
    432   // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
    433   bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
    434   // ".ifb" or ".ifnb", depending on ExpectBlank.
    435   bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
    436   // ".ifc" or ".ifnc", depending on ExpectEqual.
    437   bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
    438   // ".ifeqs" or ".ifnes", depending on ExpectEqual.
    439   bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual);
    440   // ".ifdef" or ".ifndef", depending on expect_defined
    441   bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
    442   bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
    443   bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
    444   bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
    445   bool parseEscapedString(std::string &Data) override;
    446 
    447   const MCExpr *applyModifierToExpr(const MCExpr *E,
    448                                     MCSymbolRefExpr::VariantKind Variant);
    449 
    450   // Macro-like directives
    451   MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
    452   void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
    453                                 raw_svector_ostream &OS);
    454   bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
    455   bool parseDirectiveIrp(SMLoc DirectiveLoc);  // ".irp"
    456   bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
    457   bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
    458 
    459   // "_emit" or "__emit"
    460   bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
    461                             size_t Len);
    462 
    463   // "align"
    464   bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
    465 
    466   // "end"
    467   bool parseDirectiveEnd(SMLoc DirectiveLoc);
    468 
    469   // ".err" or ".error"
    470   bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
    471 
    472   // ".warning"
    473   bool parseDirectiveWarning(SMLoc DirectiveLoc);
    474 
    475   void initializeDirectiveKindMap();
    476 };
    477 }
    478 
    479 namespace llvm {
    480 
    481 extern MCAsmParserExtension *createDarwinAsmParser();
    482 extern MCAsmParserExtension *createELFAsmParser();
    483 extern MCAsmParserExtension *createCOFFAsmParser();
    484 
    485 }
    486 
    487 enum { DEFAULT_ADDRSPACE = 0 };
    488 
    489 AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
    490                      const MCAsmInfo &MAI)
    491     : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM),
    492       PlatformParser(nullptr), CurBuffer(SM.getMainFileID()),
    493       MacrosEnabledFlag(true), HadError(false), CppHashLineNumber(0),
    494       AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
    495   // Save the old handler.
    496   SavedDiagHandler = SrcMgr.getDiagHandler();
    497   SavedDiagContext = SrcMgr.getDiagContext();
    498   // Set our own handler which calls the saved handler.
    499   SrcMgr.setDiagHandler(DiagHandler, this);
    500   Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
    501 
    502   // Initialize the platform / file format parser.
    503   switch (Ctx.getObjectFileInfo()->getObjectFileType()) {
    504   case MCObjectFileInfo::IsCOFF:
    505     PlatformParser.reset(createCOFFAsmParser());
    506     break;
    507   case MCObjectFileInfo::IsMachO:
    508     PlatformParser.reset(createDarwinAsmParser());
    509     IsDarwin = true;
    510     break;
    511   case MCObjectFileInfo::IsELF:
    512     PlatformParser.reset(createELFAsmParser());
    513     break;
    514   }
    515 
    516   PlatformParser->Initialize(*this);
    517   initializeDirectiveKindMap();
    518 }
    519 
    520 AsmParser::~AsmParser() {
    521   assert((HadError || ActiveMacros.empty()) &&
    522          "Unexpected active macro instantiation!");
    523 }
    524 
    525 void AsmParser::printMacroInstantiations() {
    526   // Print the active macro instantiation stack.
    527   for (std::vector<MacroInstantiation *>::const_reverse_iterator
    528            it = ActiveMacros.rbegin(),
    529            ie = ActiveMacros.rend();
    530        it != ie; ++it)
    531     printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
    532                  "while in macro instantiation");
    533 }
    534 
    535 void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
    536   printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
    537   printMacroInstantiations();
    538 }
    539 
    540 bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
    541   if (getTargetParser().getTargetOptions().MCFatalWarnings)
    542     return Error(L, Msg, Ranges);
    543   printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
    544   printMacroInstantiations();
    545   return false;
    546 }
    547 
    548 bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
    549   HadError = true;
    550   printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
    551   printMacroInstantiations();
    552   return true;
    553 }
    554 
    555 bool AsmParser::enterIncludeFile(const std::string &Filename) {
    556   std::string IncludedFile;
    557   unsigned NewBuf =
    558       SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
    559   if (!NewBuf)
    560     return true;
    561 
    562   CurBuffer = NewBuf;
    563   Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
    564   return false;
    565 }
    566 
    567 /// Process the specified .incbin file by searching for it in the include paths
    568 /// then just emitting the byte contents of the file to the streamer. This
    569 /// returns true on failure.
    570 bool AsmParser::processIncbinFile(const std::string &Filename) {
    571   std::string IncludedFile;
    572   unsigned NewBuf =
    573       SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
    574   if (!NewBuf)
    575     return true;
    576 
    577   // Pick up the bytes from the file and emit them.
    578   getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
    579   return false;
    580 }
    581 
    582 void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
    583   CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
    584   Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
    585                   Loc.getPointer());
    586 }
    587 
    588 const AsmToken &AsmParser::Lex() {
    589   const AsmToken *tok = &Lexer.Lex();
    590 
    591   if (tok->is(AsmToken::Eof)) {
    592     // If this is the end of an included file, pop the parent file off the
    593     // include stack.
    594     SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
    595     if (ParentIncludeLoc != SMLoc()) {
    596       jumpToLoc(ParentIncludeLoc);
    597       tok = &Lexer.Lex();
    598     }
    599   }
    600 
    601   if (tok->is(AsmToken::Error))
    602     Error(Lexer.getErrLoc(), Lexer.getErr());
    603 
    604   return *tok;
    605 }
    606 
    607 bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
    608   // Create the initial section, if requested.
    609   if (!NoInitialTextSection)
    610     Out.InitSections(false);
    611 
    612   // Prime the lexer.
    613   Lex();
    614 
    615   HadError = false;
    616   AsmCond StartingCondState = TheCondState;
    617 
    618   // If we are generating dwarf for assembly source files save the initial text
    619   // section and generate a .file directive.
    620   if (getContext().getGenDwarfForAssembly()) {
    621     MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
    622     getStreamer().EmitLabel(SectionStartSym);
    623     auto InsertResult = getContext().addGenDwarfSection(
    624         getStreamer().getCurrentSection().first);
    625     assert(InsertResult.second && ".text section should not have debug info yet");
    626     InsertResult.first->second.first = SectionStartSym;
    627     getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
    628         0, StringRef(), getContext().getMainFileName()));
    629   }
    630 
    631   // While we have input, parse each statement.
    632   while (Lexer.isNot(AsmToken::Eof)) {
    633     ParseStatementInfo Info;
    634     if (!parseStatement(Info, nullptr))
    635       continue;
    636 
    637     // We had an error, validate that one was emitted and recover by skipping to
    638     // the next line.
    639     assert(HadError && "Parse statement returned an error, but none emitted!");
    640     eatToEndOfStatement();
    641   }
    642 
    643   if (TheCondState.TheCond != StartingCondState.TheCond ||
    644       TheCondState.Ignore != StartingCondState.Ignore)
    645     return TokError("unmatched .ifs or .elses");
    646 
    647   // Check to see there are no empty DwarfFile slots.
    648   const auto &LineTables = getContext().getMCDwarfLineTables();
    649   if (!LineTables.empty()) {
    650     unsigned Index = 0;
    651     for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
    652       if (File.Name.empty() && Index != 0)
    653         TokError("unassigned file number: " + Twine(Index) +
    654                  " for .file directives");
    655       ++Index;
    656     }
    657   }
    658 
    659   // Check to see that all assembler local symbols were actually defined.
    660   // Targets that don't do subsections via symbols may not want this, though,
    661   // so conservatively exclude them. Only do this if we're finalizing, though,
    662   // as otherwise we won't necessarilly have seen everything yet.
    663   if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
    664     const MCContext::SymbolTable &Symbols = getContext().getSymbols();
    665     for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
    666                                                 e = Symbols.end();
    667          i != e; ++i) {
    668       MCSymbol *Sym = i->getValue();
    669       // Variable symbols may not be marked as defined, so check those
    670       // explicitly. If we know it's a variable, we have a definition for
    671       // the purposes of this check.
    672       if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
    673         // FIXME: We would really like to refer back to where the symbol was
    674         // first referenced for a source location. We need to add something
    675         // to track that. Currently, we just point to the end of the file.
    676         printMessage(
    677             getLexer().getLoc(), SourceMgr::DK_Error,
    678             "assembler local symbol '" + Sym->getName() + "' not defined");
    679     }
    680   }
    681 
    682   // Finalize the output stream if there are no errors and if the client wants
    683   // us to.
    684   if (!HadError && !NoFinalize)
    685     Out.Finish();
    686 
    687   return HadError;
    688 }
    689 
    690 void AsmParser::checkForValidSection() {
    691   if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
    692     TokError("expected section directive before assembly directive");
    693     Out.InitSections(false);
    694   }
    695 }
    696 
    697 /// \brief Throw away the rest of the line for testing purposes.
    698 void AsmParser::eatToEndOfStatement() {
    699   while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
    700     Lex();
    701 
    702   // Eat EOL.
    703   if (Lexer.is(AsmToken::EndOfStatement))
    704     Lex();
    705 }
    706 
    707 StringRef AsmParser::parseStringToEndOfStatement() {
    708   const char *Start = getTok().getLoc().getPointer();
    709 
    710   while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
    711     Lex();
    712 
    713   const char *End = getTok().getLoc().getPointer();
    714   return StringRef(Start, End - Start);
    715 }
    716 
    717 StringRef AsmParser::parseStringToComma() {
    718   const char *Start = getTok().getLoc().getPointer();
    719 
    720   while (Lexer.isNot(AsmToken::EndOfStatement) &&
    721          Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
    722     Lex();
    723 
    724   const char *End = getTok().getLoc().getPointer();
    725   return StringRef(Start, End - Start);
    726 }
    727 
    728 /// \brief Parse a paren expression and return it.
    729 /// NOTE: This assumes the leading '(' has already been consumed.
    730 ///
    731 /// parenexpr ::= expr)
    732 ///
    733 bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
    734   if (parseExpression(Res))
    735     return true;
    736   if (Lexer.isNot(AsmToken::RParen))
    737     return TokError("expected ')' in parentheses expression");
    738   EndLoc = Lexer.getTok().getEndLoc();
    739   Lex();
    740   return false;
    741 }
    742 
    743 /// \brief Parse a bracket expression and return it.
    744 /// NOTE: This assumes the leading '[' has already been consumed.
    745 ///
    746 /// bracketexpr ::= expr]
    747 ///
    748 bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
    749   if (parseExpression(Res))
    750     return true;
    751   if (Lexer.isNot(AsmToken::RBrac))
    752     return TokError("expected ']' in brackets expression");
    753   EndLoc = Lexer.getTok().getEndLoc();
    754   Lex();
    755   return false;
    756 }
    757 
    758 /// \brief Parse a primary expression and return it.
    759 ///  primaryexpr ::= (parenexpr
    760 ///  primaryexpr ::= symbol
    761 ///  primaryexpr ::= number
    762 ///  primaryexpr ::= '.'
    763 ///  primaryexpr ::= ~,+,- primaryexpr
    764 bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
    765   SMLoc FirstTokenLoc = getLexer().getLoc();
    766   AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
    767   switch (FirstTokenKind) {
    768   default:
    769     return TokError("unknown token in expression");
    770   // If we have an error assume that we've already handled it.
    771   case AsmToken::Error:
    772     return true;
    773   case AsmToken::Exclaim:
    774     Lex(); // Eat the operator.
    775     if (parsePrimaryExpr(Res, EndLoc))
    776       return true;
    777     Res = MCUnaryExpr::CreateLNot(Res, getContext());
    778     return false;
    779   case AsmToken::Dollar:
    780   case AsmToken::At:
    781   case AsmToken::String:
    782   case AsmToken::Identifier: {
    783     StringRef Identifier;
    784     if (parseIdentifier(Identifier)) {
    785       if (FirstTokenKind == AsmToken::Dollar) {
    786         if (Lexer.getMAI().getDollarIsPC()) {
    787           // This is a '$' reference, which references the current PC.  Emit a
    788           // temporary label to the streamer and refer to it.
    789           MCSymbol *Sym = Ctx.CreateTempSymbol();
    790           Out.EmitLabel(Sym);
    791           Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
    792                                         getContext());
    793           EndLoc = FirstTokenLoc;
    794           return false;
    795         }
    796         return Error(FirstTokenLoc, "invalid token in expression");
    797       }
    798     }
    799     // Parse symbol variant
    800     std::pair<StringRef, StringRef> Split;
    801     if (!MAI.useParensForSymbolVariant()) {
    802       if (FirstTokenKind == AsmToken::String) {
    803         if (Lexer.is(AsmToken::At)) {
    804           Lexer.Lex(); // eat @
    805           SMLoc AtLoc = getLexer().getLoc();
    806           StringRef VName;
    807           if (parseIdentifier(VName))
    808             return Error(AtLoc, "expected symbol variant after '@'");
    809 
    810           Split = std::make_pair(Identifier, VName);
    811         }
    812       } else {
    813         Split = Identifier.split('@');
    814       }
    815     } else if (Lexer.is(AsmToken::LParen)) {
    816       Lexer.Lex(); // eat (
    817       StringRef VName;
    818       parseIdentifier(VName);
    819       if (Lexer.isNot(AsmToken::RParen)) {
    820           return Error(Lexer.getTok().getLoc(),
    821                        "unexpected token in variant, expected ')'");
    822       }
    823       Lexer.Lex(); // eat )
    824       Split = std::make_pair(Identifier, VName);
    825     }
    826 
    827     EndLoc = SMLoc::getFromPointer(Identifier.end());
    828 
    829     // This is a symbol reference.
    830     StringRef SymbolName = Identifier;
    831     MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
    832 
    833     // Lookup the symbol variant if used.
    834     if (Split.second.size()) {
    835       Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
    836       if (Variant != MCSymbolRefExpr::VK_Invalid) {
    837         SymbolName = Split.first;
    838       } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
    839         Variant = MCSymbolRefExpr::VK_None;
    840       } else {
    841         return Error(SMLoc::getFromPointer(Split.second.begin()),
    842                      "invalid variant '" + Split.second + "'");
    843       }
    844     }
    845 
    846     MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
    847 
    848     // If this is an absolute variable reference, substitute it now to preserve
    849     // semantics in the face of reassignment.
    850     if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
    851       if (Variant)
    852         return Error(EndLoc, "unexpected modifier on variable reference");
    853 
    854       Res = Sym->getVariableValue();
    855       return false;
    856     }
    857 
    858     // Otherwise create a symbol ref.
    859     Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
    860     return false;
    861   }
    862   case AsmToken::BigNum:
    863     return TokError("literal value out of range for directive");
    864   case AsmToken::Integer: {
    865     SMLoc Loc = getTok().getLoc();
    866     int64_t IntVal = getTok().getIntVal();
    867     Res = MCConstantExpr::Create(IntVal, getContext());
    868     EndLoc = Lexer.getTok().getEndLoc();
    869     Lex(); // Eat token.
    870     // Look for 'b' or 'f' following an Integer as a directional label
    871     if (Lexer.getKind() == AsmToken::Identifier) {
    872       StringRef IDVal = getTok().getString();
    873       // Lookup the symbol variant if used.
    874       std::pair<StringRef, StringRef> Split = IDVal.split('@');
    875       MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
    876       if (Split.first.size() != IDVal.size()) {
    877         Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
    878         if (Variant == MCSymbolRefExpr::VK_Invalid)
    879           return TokError("invalid variant '" + Split.second + "'");
    880         IDVal = Split.first;
    881       }
    882       if (IDVal == "f" || IDVal == "b") {
    883         MCSymbol *Sym =
    884             Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "b");
    885         Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
    886         if (IDVal == "b" && Sym->isUndefined())
    887           return Error(Loc, "invalid reference to undefined symbol");
    888         EndLoc = Lexer.getTok().getEndLoc();
    889         Lex(); // Eat identifier.
    890       }
    891     }
    892     return false;
    893   }
    894   case AsmToken::Real: {
    895     APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
    896     uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
    897     Res = MCConstantExpr::Create(IntVal, getContext());
    898     EndLoc = Lexer.getTok().getEndLoc();
    899     Lex(); // Eat token.
    900     return false;
    901   }
    902   case AsmToken::Dot: {
    903     // This is a '.' reference, which references the current PC.  Emit a
    904     // temporary label to the streamer and refer to it.
    905     MCSymbol *Sym = Ctx.CreateTempSymbol();
    906     Out.EmitLabel(Sym);
    907     Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
    908     EndLoc = Lexer.getTok().getEndLoc();
    909     Lex(); // Eat identifier.
    910     return false;
    911   }
    912   case AsmToken::LParen:
    913     Lex(); // Eat the '('.
    914     return parseParenExpr(Res, EndLoc);
    915   case AsmToken::LBrac:
    916     if (!PlatformParser->HasBracketExpressions())
    917       return TokError("brackets expression not supported on this target");
    918     Lex(); // Eat the '['.
    919     return parseBracketExpr(Res, EndLoc);
    920   case AsmToken::Minus:
    921     Lex(); // Eat the operator.
    922     if (parsePrimaryExpr(Res, EndLoc))
    923       return true;
    924     Res = MCUnaryExpr::CreateMinus(Res, getContext());
    925     return false;
    926   case AsmToken::Plus:
    927     Lex(); // Eat the operator.
    928     if (parsePrimaryExpr(Res, EndLoc))
    929       return true;
    930     Res = MCUnaryExpr::CreatePlus(Res, getContext());
    931     return false;
    932   case AsmToken::Tilde:
    933     Lex(); // Eat the operator.
    934     if (parsePrimaryExpr(Res, EndLoc))
    935       return true;
    936     Res = MCUnaryExpr::CreateNot(Res, getContext());
    937     return false;
    938   }
    939 }
    940 
    941 bool AsmParser::parseExpression(const MCExpr *&Res) {
    942   SMLoc EndLoc;
    943   return parseExpression(Res, EndLoc);
    944 }
    945 
    946 const MCExpr *
    947 AsmParser::applyModifierToExpr(const MCExpr *E,
    948                                MCSymbolRefExpr::VariantKind Variant) {
    949   // Ask the target implementation about this expression first.
    950   const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
    951   if (NewE)
    952     return NewE;
    953   // Recurse over the given expression, rebuilding it to apply the given variant
    954   // if there is exactly one symbol.
    955   switch (E->getKind()) {
    956   case MCExpr::Target:
    957   case MCExpr::Constant:
    958     return nullptr;
    959 
    960   case MCExpr::SymbolRef: {
    961     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
    962 
    963     if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
    964       TokError("invalid variant on expression '" + getTok().getIdentifier() +
    965                "' (already modified)");
    966       return E;
    967     }
    968 
    969     return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
    970   }
    971 
    972   case MCExpr::Unary: {
    973     const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
    974     const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
    975     if (!Sub)
    976       return nullptr;
    977     return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
    978   }
    979 
    980   case MCExpr::Binary: {
    981     const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
    982     const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
    983     const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
    984 
    985     if (!LHS && !RHS)
    986       return nullptr;
    987 
    988     if (!LHS)
    989       LHS = BE->getLHS();
    990     if (!RHS)
    991       RHS = BE->getRHS();
    992 
    993     return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
    994   }
    995   }
    996 
    997   llvm_unreachable("Invalid expression kind!");
    998 }
    999 
   1000 /// \brief Parse an expression and return it.
   1001 ///
   1002 ///  expr ::= expr &&,|| expr               -> lowest.
   1003 ///  expr ::= expr |,^,&,! expr
   1004 ///  expr ::= expr ==,!=,<>,<,<=,>,>= expr
   1005 ///  expr ::= expr <<,>> expr
   1006 ///  expr ::= expr +,- expr
   1007 ///  expr ::= expr *,/,% expr               -> highest.
   1008 ///  expr ::= primaryexpr
   1009 ///
   1010 bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
   1011   // Parse the expression.
   1012   Res = nullptr;
   1013   if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
   1014     return true;
   1015 
   1016   // As a special case, we support 'a op b @ modifier' by rewriting the
   1017   // expression to include the modifier. This is inefficient, but in general we
   1018   // expect users to use 'a@modifier op b'.
   1019   if (Lexer.getKind() == AsmToken::At) {
   1020     Lex();
   1021 
   1022     if (Lexer.isNot(AsmToken::Identifier))
   1023       return TokError("unexpected symbol modifier following '@'");
   1024 
   1025     MCSymbolRefExpr::VariantKind Variant =
   1026         MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
   1027     if (Variant == MCSymbolRefExpr::VK_Invalid)
   1028       return TokError("invalid variant '" + getTok().getIdentifier() + "'");
   1029 
   1030     const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
   1031     if (!ModifiedRes) {
   1032       return TokError("invalid modifier '" + getTok().getIdentifier() +
   1033                       "' (no symbols present)");
   1034     }
   1035 
   1036     Res = ModifiedRes;
   1037     Lex();
   1038   }
   1039 
   1040   // Try to constant fold it up front, if possible.
   1041   int64_t Value;
   1042   if (Res->EvaluateAsAbsolute(Value))
   1043     Res = MCConstantExpr::Create(Value, getContext());
   1044 
   1045   return false;
   1046 }
   1047 
   1048 bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
   1049   Res = nullptr;
   1050   return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
   1051 }
   1052 
   1053 bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
   1054   const MCExpr *Expr;
   1055 
   1056   SMLoc StartLoc = Lexer.getLoc();
   1057   if (parseExpression(Expr))
   1058     return true;
   1059 
   1060   if (!Expr->EvaluateAsAbsolute(Res))
   1061     return Error(StartLoc, "expected absolute expression");
   1062 
   1063   return false;
   1064 }
   1065 
   1066 static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
   1067                                    MCBinaryExpr::Opcode &Kind) {
   1068   switch (K) {
   1069   default:
   1070     return 0; // not a binop.
   1071 
   1072   // Lowest Precedence: &&, ||
   1073   case AsmToken::AmpAmp:
   1074     Kind = MCBinaryExpr::LAnd;
   1075     return 1;
   1076   case AsmToken::PipePipe:
   1077     Kind = MCBinaryExpr::LOr;
   1078     return 1;
   1079 
   1080   // Low Precedence: |, &, ^
   1081   //
   1082   // FIXME: gas seems to support '!' as an infix operator?
   1083   case AsmToken::Pipe:
   1084     Kind = MCBinaryExpr::Or;
   1085     return 2;
   1086   case AsmToken::Caret:
   1087     Kind = MCBinaryExpr::Xor;
   1088     return 2;
   1089   case AsmToken::Amp:
   1090     Kind = MCBinaryExpr::And;
   1091     return 2;
   1092 
   1093   // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
   1094   case AsmToken::EqualEqual:
   1095     Kind = MCBinaryExpr::EQ;
   1096     return 3;
   1097   case AsmToken::ExclaimEqual:
   1098   case AsmToken::LessGreater:
   1099     Kind = MCBinaryExpr::NE;
   1100     return 3;
   1101   case AsmToken::Less:
   1102     Kind = MCBinaryExpr::LT;
   1103     return 3;
   1104   case AsmToken::LessEqual:
   1105     Kind = MCBinaryExpr::LTE;
   1106     return 3;
   1107   case AsmToken::Greater:
   1108     Kind = MCBinaryExpr::GT;
   1109     return 3;
   1110   case AsmToken::GreaterEqual:
   1111     Kind = MCBinaryExpr::GTE;
   1112     return 3;
   1113 
   1114   // Intermediate Precedence: <<, >>
   1115   case AsmToken::LessLess:
   1116     Kind = MCBinaryExpr::Shl;
   1117     return 4;
   1118   case AsmToken::GreaterGreater:
   1119     Kind = MCBinaryExpr::Shr;
   1120     return 4;
   1121 
   1122   // High Intermediate Precedence: +, -
   1123   case AsmToken::Plus:
   1124     Kind = MCBinaryExpr::Add;
   1125     return 5;
   1126   case AsmToken::Minus:
   1127     Kind = MCBinaryExpr::Sub;
   1128     return 5;
   1129 
   1130   // Highest Precedence: *, /, %
   1131   case AsmToken::Star:
   1132     Kind = MCBinaryExpr::Mul;
   1133     return 6;
   1134   case AsmToken::Slash:
   1135     Kind = MCBinaryExpr::Div;
   1136     return 6;
   1137   case AsmToken::Percent:
   1138     Kind = MCBinaryExpr::Mod;
   1139     return 6;
   1140   }
   1141 }
   1142 
   1143 /// \brief Parse all binary operators with precedence >= 'Precedence'.
   1144 /// Res contains the LHS of the expression on input.
   1145 bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
   1146                               SMLoc &EndLoc) {
   1147   while (1) {
   1148     MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
   1149     unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
   1150 
   1151     // If the next token is lower precedence than we are allowed to eat, return
   1152     // successfully with what we ate already.
   1153     if (TokPrec < Precedence)
   1154       return false;
   1155 
   1156     Lex();
   1157 
   1158     // Eat the next primary expression.
   1159     const MCExpr *RHS;
   1160     if (parsePrimaryExpr(RHS, EndLoc))
   1161       return true;
   1162 
   1163     // If BinOp binds less tightly with RHS than the operator after RHS, let
   1164     // the pending operator take RHS as its LHS.
   1165     MCBinaryExpr::Opcode Dummy;
   1166     unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
   1167     if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
   1168       return true;
   1169 
   1170     // Merge LHS and RHS according to operator.
   1171     Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
   1172   }
   1173 }
   1174 
   1175 /// ParseStatement:
   1176 ///   ::= EndOfStatement
   1177 ///   ::= Label* Directive ...Operands... EndOfStatement
   1178 ///   ::= Label* Identifier OperandList* EndOfStatement
   1179 bool AsmParser::parseStatement(ParseStatementInfo &Info,
   1180                                MCAsmParserSemaCallback *SI) {
   1181   if (Lexer.is(AsmToken::EndOfStatement)) {
   1182     Out.AddBlankLine();
   1183     Lex();
   1184     return false;
   1185   }
   1186 
   1187   // Statements always start with an identifier or are a full line comment.
   1188   AsmToken ID = getTok();
   1189   SMLoc IDLoc = ID.getLoc();
   1190   StringRef IDVal;
   1191   int64_t LocalLabelVal = -1;
   1192   // A full line comment is a '#' as the first token.
   1193   if (Lexer.is(AsmToken::Hash))
   1194     return parseCppHashLineFilenameComment(IDLoc);
   1195 
   1196   // Allow an integer followed by a ':' as a directional local label.
   1197   if (Lexer.is(AsmToken::Integer)) {
   1198     LocalLabelVal = getTok().getIntVal();
   1199     if (LocalLabelVal < 0) {
   1200       if (!TheCondState.Ignore)
   1201         return TokError("unexpected token at start of statement");
   1202       IDVal = "";
   1203     } else {
   1204       IDVal = getTok().getString();
   1205       Lex(); // Consume the integer token to be used as an identifier token.
   1206       if (Lexer.getKind() != AsmToken::Colon) {
   1207         if (!TheCondState.Ignore)
   1208           return TokError("unexpected token at start of statement");
   1209       }
   1210     }
   1211   } else if (Lexer.is(AsmToken::Dot)) {
   1212     // Treat '.' as a valid identifier in this context.
   1213     Lex();
   1214     IDVal = ".";
   1215   } else if (parseIdentifier(IDVal)) {
   1216     if (!TheCondState.Ignore)
   1217       return TokError("unexpected token at start of statement");
   1218     IDVal = "";
   1219   }
   1220 
   1221   // Handle conditional assembly here before checking for skipping.  We
   1222   // have to do this so that .endif isn't skipped in a ".if 0" block for
   1223   // example.
   1224   StringMap<DirectiveKind>::const_iterator DirKindIt =
   1225       DirectiveKindMap.find(IDVal);
   1226   DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
   1227                               ? DK_NO_DIRECTIVE
   1228                               : DirKindIt->getValue();
   1229   switch (DirKind) {
   1230   default:
   1231     break;
   1232   case DK_IF:
   1233   case DK_IFEQ:
   1234   case DK_IFGE:
   1235   case DK_IFGT:
   1236   case DK_IFLE:
   1237   case DK_IFLT:
   1238   case DK_IFNE:
   1239     return parseDirectiveIf(IDLoc, DirKind);
   1240   case DK_IFB:
   1241     return parseDirectiveIfb(IDLoc, true);
   1242   case DK_IFNB:
   1243     return parseDirectiveIfb(IDLoc, false);
   1244   case DK_IFC:
   1245     return parseDirectiveIfc(IDLoc, true);
   1246   case DK_IFEQS:
   1247     return parseDirectiveIfeqs(IDLoc, true);
   1248   case DK_IFNC:
   1249     return parseDirectiveIfc(IDLoc, false);
   1250   case DK_IFNES:
   1251     return parseDirectiveIfeqs(IDLoc, false);
   1252   case DK_IFDEF:
   1253     return parseDirectiveIfdef(IDLoc, true);
   1254   case DK_IFNDEF:
   1255   case DK_IFNOTDEF:
   1256     return parseDirectiveIfdef(IDLoc, false);
   1257   case DK_ELSEIF:
   1258     return parseDirectiveElseIf(IDLoc);
   1259   case DK_ELSE:
   1260     return parseDirectiveElse(IDLoc);
   1261   case DK_ENDIF:
   1262     return parseDirectiveEndIf(IDLoc);
   1263   }
   1264 
   1265   // Ignore the statement if in the middle of inactive conditional
   1266   // (e.g. ".if 0").
   1267   if (TheCondState.Ignore) {
   1268     eatToEndOfStatement();
   1269     return false;
   1270   }
   1271 
   1272   // FIXME: Recurse on local labels?
   1273 
   1274   // See what kind of statement we have.
   1275   switch (Lexer.getKind()) {
   1276   case AsmToken::Colon: {
   1277     checkForValidSection();
   1278 
   1279     // identifier ':'   -> Label.
   1280     Lex();
   1281 
   1282     // Diagnose attempt to use '.' as a label.
   1283     if (IDVal == ".")
   1284       return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
   1285 
   1286     // Diagnose attempt to use a variable as a label.
   1287     //
   1288     // FIXME: Diagnostics. Note the location of the definition as a label.
   1289     // FIXME: This doesn't diagnose assignment to a symbol which has been
   1290     // implicitly marked as external.
   1291     MCSymbol *Sym;
   1292     if (LocalLabelVal == -1) {
   1293       if (ParsingInlineAsm && SI) {
   1294         StringRef RewrittenLabel = SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
   1295         assert(RewrittenLabel.size() && "We should have an internal name here.");
   1296         Info.AsmRewrites->push_back(AsmRewrite(AOK_Label, IDLoc,
   1297                                                IDVal.size(), RewrittenLabel));
   1298         IDVal = RewrittenLabel;
   1299       }
   1300       Sym = getContext().GetOrCreateSymbol(IDVal);
   1301     } else
   1302       Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
   1303 
   1304     Sym->redefineIfPossible();
   1305 
   1306     if (!Sym->isUndefined() || Sym->isVariable())
   1307       return Error(IDLoc, "invalid symbol redefinition");
   1308 
   1309     // Emit the label.
   1310     if (!ParsingInlineAsm)
   1311       Out.EmitLabel(Sym);
   1312 
   1313     // If we are generating dwarf for assembly source files then gather the
   1314     // info to make a dwarf label entry for this label if needed.
   1315     if (getContext().getGenDwarfForAssembly())
   1316       MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
   1317                                  IDLoc);
   1318 
   1319     getTargetParser().onLabelParsed(Sym);
   1320 
   1321     // Consume any end of statement token, if present, to avoid spurious
   1322     // AddBlankLine calls().
   1323     if (Lexer.is(AsmToken::EndOfStatement)) {
   1324       Lex();
   1325       if (Lexer.is(AsmToken::Eof))
   1326         return false;
   1327     }
   1328 
   1329     return false;
   1330   }
   1331 
   1332   case AsmToken::Equal:
   1333     // identifier '=' ... -> assignment statement
   1334     Lex();
   1335 
   1336     return parseAssignment(IDVal, true);
   1337 
   1338   default: // Normal instruction or directive.
   1339     break;
   1340   }
   1341 
   1342   // If macros are enabled, check to see if this is a macro instantiation.
   1343   if (areMacrosEnabled())
   1344     if (const MCAsmMacro *M = lookupMacro(IDVal)) {
   1345       return handleMacroEntry(M, IDLoc);
   1346     }
   1347 
   1348   // Otherwise, we have a normal instruction or directive.
   1349 
   1350   // Directives start with "."
   1351   if (IDVal[0] == '.' && IDVal != ".") {
   1352     // There are several entities interested in parsing directives:
   1353     //
   1354     // 1. The target-specific assembly parser. Some directives are target
   1355     //    specific or may potentially behave differently on certain targets.
   1356     // 2. Asm parser extensions. For example, platform-specific parsers
   1357     //    (like the ELF parser) register themselves as extensions.
   1358     // 3. The generic directive parser implemented by this class. These are
   1359     //    all the directives that behave in a target and platform independent
   1360     //    manner, or at least have a default behavior that's shared between
   1361     //    all targets and platforms.
   1362 
   1363     // First query the target-specific parser. It will return 'true' if it
   1364     // isn't interested in this directive.
   1365     if (!getTargetParser().ParseDirective(ID))
   1366       return false;
   1367 
   1368     // Next, check the extension directive map to see if any extension has
   1369     // registered itself to parse this directive.
   1370     std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
   1371         ExtensionDirectiveMap.lookup(IDVal);
   1372     if (Handler.first)
   1373       return (*Handler.second)(Handler.first, IDVal, IDLoc);
   1374 
   1375     // Finally, if no one else is interested in this directive, it must be
   1376     // generic and familiar to this class.
   1377     switch (DirKind) {
   1378     default:
   1379       break;
   1380     case DK_SET:
   1381     case DK_EQU:
   1382       return parseDirectiveSet(IDVal, true);
   1383     case DK_EQUIV:
   1384       return parseDirectiveSet(IDVal, false);
   1385     case DK_ASCII:
   1386       return parseDirectiveAscii(IDVal, false);
   1387     case DK_ASCIZ:
   1388     case DK_STRING:
   1389       return parseDirectiveAscii(IDVal, true);
   1390     case DK_BYTE:
   1391       return parseDirectiveValue(1);
   1392     case DK_SHORT:
   1393     case DK_VALUE:
   1394     case DK_2BYTE:
   1395       return parseDirectiveValue(2);
   1396     case DK_LONG:
   1397     case DK_INT:
   1398     case DK_4BYTE:
   1399       return parseDirectiveValue(4);
   1400     case DK_QUAD:
   1401     case DK_8BYTE:
   1402       return parseDirectiveValue(8);
   1403     case DK_OCTA:
   1404       return parseDirectiveOctaValue();
   1405     case DK_SINGLE:
   1406     case DK_FLOAT:
   1407       return parseDirectiveRealValue(APFloat::IEEEsingle);
   1408     case DK_DOUBLE:
   1409       return parseDirectiveRealValue(APFloat::IEEEdouble);
   1410     case DK_ALIGN: {
   1411       bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
   1412       return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
   1413     }
   1414     case DK_ALIGN32: {
   1415       bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
   1416       return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
   1417     }
   1418     case DK_BALIGN:
   1419       return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
   1420     case DK_BALIGNW:
   1421       return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
   1422     case DK_BALIGNL:
   1423       return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
   1424     case DK_P2ALIGN:
   1425       return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
   1426     case DK_P2ALIGNW:
   1427       return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
   1428     case DK_P2ALIGNL:
   1429       return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
   1430     case DK_ORG:
   1431       return parseDirectiveOrg();
   1432     case DK_FILL:
   1433       return parseDirectiveFill();
   1434     case DK_ZERO:
   1435       return parseDirectiveZero();
   1436     case DK_EXTERN:
   1437       eatToEndOfStatement(); // .extern is the default, ignore it.
   1438       return false;
   1439     case DK_GLOBL:
   1440     case DK_GLOBAL:
   1441       return parseDirectiveSymbolAttribute(MCSA_Global);
   1442     case DK_LAZY_REFERENCE:
   1443       return parseDirectiveSymbolAttribute(MCSA_LazyReference);
   1444     case DK_NO_DEAD_STRIP:
   1445       return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
   1446     case DK_SYMBOL_RESOLVER:
   1447       return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
   1448     case DK_PRIVATE_EXTERN:
   1449       return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
   1450     case DK_REFERENCE:
   1451       return parseDirectiveSymbolAttribute(MCSA_Reference);
   1452     case DK_WEAK_DEFINITION:
   1453       return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
   1454     case DK_WEAK_REFERENCE:
   1455       return parseDirectiveSymbolAttribute(MCSA_WeakReference);
   1456     case DK_WEAK_DEF_CAN_BE_HIDDEN:
   1457       return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
   1458     case DK_COMM:
   1459     case DK_COMMON:
   1460       return parseDirectiveComm(/*IsLocal=*/false);
   1461     case DK_LCOMM:
   1462       return parseDirectiveComm(/*IsLocal=*/true);
   1463     case DK_ABORT:
   1464       return parseDirectiveAbort();
   1465     case DK_INCLUDE:
   1466       return parseDirectiveInclude();
   1467     case DK_INCBIN:
   1468       return parseDirectiveIncbin();
   1469     case DK_CODE16:
   1470     case DK_CODE16GCC:
   1471       return TokError(Twine(IDVal) + " not supported yet");
   1472     case DK_REPT:
   1473       return parseDirectiveRept(IDLoc, IDVal);
   1474     case DK_IRP:
   1475       return parseDirectiveIrp(IDLoc);
   1476     case DK_IRPC:
   1477       return parseDirectiveIrpc(IDLoc);
   1478     case DK_ENDR:
   1479       return parseDirectiveEndr(IDLoc);
   1480     case DK_BUNDLE_ALIGN_MODE:
   1481       return parseDirectiveBundleAlignMode();
   1482     case DK_BUNDLE_LOCK:
   1483       return parseDirectiveBundleLock();
   1484     case DK_BUNDLE_UNLOCK:
   1485       return parseDirectiveBundleUnlock();
   1486     case DK_SLEB128:
   1487       return parseDirectiveLEB128(true);
   1488     case DK_ULEB128:
   1489       return parseDirectiveLEB128(false);
   1490     case DK_SPACE:
   1491     case DK_SKIP:
   1492       return parseDirectiveSpace(IDVal);
   1493     case DK_FILE:
   1494       return parseDirectiveFile(IDLoc);
   1495     case DK_LINE:
   1496       return parseDirectiveLine();
   1497     case DK_LOC:
   1498       return parseDirectiveLoc();
   1499     case DK_STABS:
   1500       return parseDirectiveStabs();
   1501     case DK_CFI_SECTIONS:
   1502       return parseDirectiveCFISections();
   1503     case DK_CFI_STARTPROC:
   1504       return parseDirectiveCFIStartProc();
   1505     case DK_CFI_ENDPROC:
   1506       return parseDirectiveCFIEndProc();
   1507     case DK_CFI_DEF_CFA:
   1508       return parseDirectiveCFIDefCfa(IDLoc);
   1509     case DK_CFI_DEF_CFA_OFFSET:
   1510       return parseDirectiveCFIDefCfaOffset();
   1511     case DK_CFI_ADJUST_CFA_OFFSET:
   1512       return parseDirectiveCFIAdjustCfaOffset();
   1513     case DK_CFI_DEF_CFA_REGISTER:
   1514       return parseDirectiveCFIDefCfaRegister(IDLoc);
   1515     case DK_CFI_OFFSET:
   1516       return parseDirectiveCFIOffset(IDLoc);
   1517     case DK_CFI_REL_OFFSET:
   1518       return parseDirectiveCFIRelOffset(IDLoc);
   1519     case DK_CFI_PERSONALITY:
   1520       return parseDirectiveCFIPersonalityOrLsda(true);
   1521     case DK_CFI_LSDA:
   1522       return parseDirectiveCFIPersonalityOrLsda(false);
   1523     case DK_CFI_REMEMBER_STATE:
   1524       return parseDirectiveCFIRememberState();
   1525     case DK_CFI_RESTORE_STATE:
   1526       return parseDirectiveCFIRestoreState();
   1527     case DK_CFI_SAME_VALUE:
   1528       return parseDirectiveCFISameValue(IDLoc);
   1529     case DK_CFI_RESTORE:
   1530       return parseDirectiveCFIRestore(IDLoc);
   1531     case DK_CFI_ESCAPE:
   1532       return parseDirectiveCFIEscape();
   1533     case DK_CFI_SIGNAL_FRAME:
   1534       return parseDirectiveCFISignalFrame();
   1535     case DK_CFI_UNDEFINED:
   1536       return parseDirectiveCFIUndefined(IDLoc);
   1537     case DK_CFI_REGISTER:
   1538       return parseDirectiveCFIRegister(IDLoc);
   1539     case DK_CFI_WINDOW_SAVE:
   1540       return parseDirectiveCFIWindowSave();
   1541     case DK_MACROS_ON:
   1542     case DK_MACROS_OFF:
   1543       return parseDirectiveMacrosOnOff(IDVal);
   1544     case DK_MACRO:
   1545       return parseDirectiveMacro(IDLoc);
   1546     case DK_EXITM:
   1547       return parseDirectiveExitMacro(IDVal);
   1548     case DK_ENDM:
   1549     case DK_ENDMACRO:
   1550       return parseDirectiveEndMacro(IDVal);
   1551     case DK_PURGEM:
   1552       return parseDirectivePurgeMacro(IDLoc);
   1553     case DK_END:
   1554       return parseDirectiveEnd(IDLoc);
   1555     case DK_ERR:
   1556       return parseDirectiveError(IDLoc, false);
   1557     case DK_ERROR:
   1558       return parseDirectiveError(IDLoc, true);
   1559     case DK_WARNING:
   1560       return parseDirectiveWarning(IDLoc);
   1561     }
   1562 
   1563     return Error(IDLoc, "unknown directive");
   1564   }
   1565 
   1566   // __asm _emit or __asm __emit
   1567   if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
   1568                            IDVal == "_EMIT" || IDVal == "__EMIT"))
   1569     return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
   1570 
   1571   // __asm align
   1572   if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
   1573     return parseDirectiveMSAlign(IDLoc, Info);
   1574 
   1575   checkForValidSection();
   1576 
   1577   // Canonicalize the opcode to lower case.
   1578   std::string OpcodeStr = IDVal.lower();
   1579   ParseInstructionInfo IInfo(Info.AsmRewrites);
   1580   bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
   1581                                                      Info.ParsedOperands);
   1582   Info.ParseError = HadError;
   1583 
   1584   // Dump the parsed representation, if requested.
   1585   if (getShowParsedOperands()) {
   1586     SmallString<256> Str;
   1587     raw_svector_ostream OS(Str);
   1588     OS << "parsed instruction: [";
   1589     for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
   1590       if (i != 0)
   1591         OS << ", ";
   1592       Info.ParsedOperands[i]->print(OS);
   1593     }
   1594     OS << "]";
   1595 
   1596     printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
   1597   }
   1598 
   1599   // If we are generating dwarf for the current section then generate a .loc
   1600   // directive for the instruction.
   1601   if (!HadError && getContext().getGenDwarfForAssembly() &&
   1602       getContext().getGenDwarfSectionSyms().count(
   1603           getStreamer().getCurrentSection().first)) {
   1604     unsigned Line;
   1605     if (ActiveMacros.empty())
   1606       Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
   1607     else
   1608       Line = SrcMgr.FindLineNumber(ActiveMacros.back()->InstantiationLoc,
   1609                                    ActiveMacros.back()->ExitBuffer);
   1610 
   1611     // If we previously parsed a cpp hash file line comment then make sure the
   1612     // current Dwarf File is for the CppHashFilename if not then emit the
   1613     // Dwarf File table for it and adjust the line number for the .loc.
   1614     if (CppHashFilename.size()) {
   1615       unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
   1616           0, StringRef(), CppHashFilename);
   1617       getContext().setGenDwarfFileNumber(FileNumber);
   1618 
   1619       // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
   1620       // cache with the different Loc from the call above we save the last
   1621       // info we queried here with SrcMgr.FindLineNumber().
   1622       unsigned CppHashLocLineNo;
   1623       if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
   1624         CppHashLocLineNo = LastQueryLine;
   1625       else {
   1626         CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
   1627         LastQueryLine = CppHashLocLineNo;
   1628         LastQueryIDLoc = CppHashLoc;
   1629         LastQueryBuffer = CppHashBuf;
   1630       }
   1631       Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
   1632     }
   1633 
   1634     getStreamer().EmitDwarfLocDirective(
   1635         getContext().getGenDwarfFileNumber(), Line, 0,
   1636         DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
   1637         StringRef());
   1638   }
   1639 
   1640   // If parsing succeeded, match the instruction.
   1641   if (!HadError) {
   1642     uint64_t ErrorInfo;
   1643     getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
   1644                                               Info.ParsedOperands, Out,
   1645                                               ErrorInfo, ParsingInlineAsm);
   1646   }
   1647 
   1648   // Don't skip the rest of the line, the instruction parser is responsible for
   1649   // that.
   1650   return false;
   1651 }
   1652 
   1653 /// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
   1654 /// since they may not be able to be tokenized to get to the end of line token.
   1655 void AsmParser::eatToEndOfLine() {
   1656   if (!Lexer.is(AsmToken::EndOfStatement))
   1657     Lexer.LexUntilEndOfLine();
   1658   // Eat EOL.
   1659   Lex();
   1660 }
   1661 
   1662 /// parseCppHashLineFilenameComment as this:
   1663 ///   ::= # number "filename"
   1664 /// or just as a full line comment if it doesn't have a number and a string.
   1665 bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
   1666   Lex(); // Eat the hash token.
   1667 
   1668   if (getLexer().isNot(AsmToken::Integer)) {
   1669     // Consume the line since in cases it is not a well-formed line directive,
   1670     // as if were simply a full line comment.
   1671     eatToEndOfLine();
   1672     return false;
   1673   }
   1674 
   1675   int64_t LineNumber = getTok().getIntVal();
   1676   Lex();
   1677 
   1678   if (getLexer().isNot(AsmToken::String)) {
   1679     eatToEndOfLine();
   1680     return false;
   1681   }
   1682 
   1683   StringRef Filename = getTok().getString();
   1684   // Get rid of the enclosing quotes.
   1685   Filename = Filename.substr(1, Filename.size() - 2);
   1686 
   1687   // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
   1688   CppHashLoc = L;
   1689   CppHashFilename = Filename;
   1690   CppHashLineNumber = LineNumber;
   1691   CppHashBuf = CurBuffer;
   1692 
   1693   // Ignore any trailing characters, they're just comment.
   1694   eatToEndOfLine();
   1695   return false;
   1696 }
   1697 
   1698 /// \brief will use the last parsed cpp hash line filename comment
   1699 /// for the Filename and LineNo if any in the diagnostic.
   1700 void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
   1701   const AsmParser *Parser = static_cast<const AsmParser *>(Context);
   1702   raw_ostream &OS = errs();
   1703 
   1704   const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
   1705   const SMLoc &DiagLoc = Diag.getLoc();
   1706   unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
   1707   unsigned CppHashBuf =
   1708       Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
   1709 
   1710   // Like SourceMgr::printMessage() we need to print the include stack if any
   1711   // before printing the message.
   1712   unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
   1713   if (!Parser->SavedDiagHandler && DiagCurBuffer &&
   1714       DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
   1715     SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
   1716     DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
   1717   }
   1718 
   1719   // If we have not parsed a cpp hash line filename comment or the source
   1720   // manager changed or buffer changed (like in a nested include) then just
   1721   // print the normal diagnostic using its Filename and LineNo.
   1722   if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
   1723       DiagBuf != CppHashBuf) {
   1724     if (Parser->SavedDiagHandler)
   1725       Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
   1726     else
   1727       Diag.print(nullptr, OS);
   1728     return;
   1729   }
   1730 
   1731   // Use the CppHashFilename and calculate a line number based on the
   1732   // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
   1733   // the diagnostic.
   1734   const std::string &Filename = Parser->CppHashFilename;
   1735 
   1736   int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
   1737   int CppHashLocLineNo =
   1738       Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
   1739   int LineNo =
   1740       Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
   1741 
   1742   SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
   1743                        Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
   1744                        Diag.getLineContents(), Diag.getRanges());
   1745 
   1746   if (Parser->SavedDiagHandler)
   1747     Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
   1748   else
   1749     NewDiag.print(nullptr, OS);
   1750 }
   1751 
   1752 // FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
   1753 // difference being that that function accepts '@' as part of identifiers and
   1754 // we can't do that. AsmLexer.cpp should probably be changed to handle
   1755 // '@' as a special case when needed.
   1756 static bool isIdentifierChar(char c) {
   1757   return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
   1758          c == '.';
   1759 }
   1760 
   1761 bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
   1762                             ArrayRef<MCAsmMacroParameter> Parameters,
   1763                             ArrayRef<MCAsmMacroArgument> A, const SMLoc &L) {
   1764   unsigned NParameters = Parameters.size();
   1765   bool HasVararg = NParameters ? Parameters.back().Vararg : false;
   1766   if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
   1767     return Error(L, "Wrong number of arguments");
   1768 
   1769   // A macro without parameters is handled differently on Darwin:
   1770   // gas accepts no arguments and does no substitutions
   1771   while (!Body.empty()) {
   1772     // Scan for the next substitution.
   1773     std::size_t End = Body.size(), Pos = 0;
   1774     for (; Pos != End; ++Pos) {
   1775       // Check for a substitution or escape.
   1776       if (IsDarwin && !NParameters) {
   1777         // This macro has no parameters, look for $0, $1, etc.
   1778         if (Body[Pos] != '$' || Pos + 1 == End)
   1779           continue;
   1780 
   1781         char Next = Body[Pos + 1];
   1782         if (Next == '$' || Next == 'n' ||
   1783             isdigit(static_cast<unsigned char>(Next)))
   1784           break;
   1785       } else {
   1786         // This macro has parameters, look for \foo, \bar, etc.
   1787         if (Body[Pos] == '\\' && Pos + 1 != End)
   1788           break;
   1789       }
   1790     }
   1791 
   1792     // Add the prefix.
   1793     OS << Body.slice(0, Pos);
   1794 
   1795     // Check if we reached the end.
   1796     if (Pos == End)
   1797       break;
   1798 
   1799     if (IsDarwin && !NParameters) {
   1800       switch (Body[Pos + 1]) {
   1801       // $$ => $
   1802       case '$':
   1803         OS << '$';
   1804         break;
   1805 
   1806       // $n => number of arguments
   1807       case 'n':
   1808         OS << A.size();
   1809         break;
   1810 
   1811       // $[0-9] => argument
   1812       default: {
   1813         // Missing arguments are ignored.
   1814         unsigned Index = Body[Pos + 1] - '0';
   1815         if (Index >= A.size())
   1816           break;
   1817 
   1818         // Otherwise substitute with the token values, with spaces eliminated.
   1819         for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
   1820                                                 ie = A[Index].end();
   1821              it != ie; ++it)
   1822           OS << it->getString();
   1823         break;
   1824       }
   1825       }
   1826       Pos += 2;
   1827     } else {
   1828       unsigned I = Pos + 1;
   1829       while (isIdentifierChar(Body[I]) && I + 1 != End)
   1830         ++I;
   1831 
   1832       const char *Begin = Body.data() + Pos + 1;
   1833       StringRef Argument(Begin, I - (Pos + 1));
   1834       unsigned Index = 0;
   1835       for (; Index < NParameters; ++Index)
   1836         if (Parameters[Index].Name == Argument)
   1837           break;
   1838 
   1839       if (Index == NParameters) {
   1840         if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
   1841           Pos += 3;
   1842         else {
   1843           OS << '\\' << Argument;
   1844           Pos = I;
   1845         }
   1846       } else {
   1847         bool VarargParameter = HasVararg && Index == (NParameters - 1);
   1848         for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
   1849                                                 ie = A[Index].end();
   1850              it != ie; ++it)
   1851           // We expect no quotes around the string's contents when
   1852           // parsing for varargs.
   1853           if (it->getKind() != AsmToken::String || VarargParameter)
   1854             OS << it->getString();
   1855           else
   1856             OS << it->getStringContents();
   1857 
   1858         Pos += 1 + Argument.size();
   1859       }
   1860     }
   1861     // Update the scan point.
   1862     Body = Body.substr(Pos);
   1863   }
   1864 
   1865   return false;
   1866 }
   1867 
   1868 MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL,
   1869                                        size_t CondStackDepth)
   1870     : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL),
   1871       CondStackDepth(CondStackDepth) {}
   1872 
   1873 static bool isOperator(AsmToken::TokenKind kind) {
   1874   switch (kind) {
   1875   default:
   1876     return false;
   1877   case AsmToken::Plus:
   1878   case AsmToken::Minus:
   1879   case AsmToken::Tilde:
   1880   case AsmToken::Slash:
   1881   case AsmToken::Star:
   1882   case AsmToken::Dot:
   1883   case AsmToken::Equal:
   1884   case AsmToken::EqualEqual:
   1885   case AsmToken::Pipe:
   1886   case AsmToken::PipePipe:
   1887   case AsmToken::Caret:
   1888   case AsmToken::Amp:
   1889   case AsmToken::AmpAmp:
   1890   case AsmToken::Exclaim:
   1891   case AsmToken::ExclaimEqual:
   1892   case AsmToken::Percent:
   1893   case AsmToken::Less:
   1894   case AsmToken::LessEqual:
   1895   case AsmToken::LessLess:
   1896   case AsmToken::LessGreater:
   1897   case AsmToken::Greater:
   1898   case AsmToken::GreaterEqual:
   1899   case AsmToken::GreaterGreater:
   1900     return true;
   1901   }
   1902 }
   1903 
   1904 namespace {
   1905 class AsmLexerSkipSpaceRAII {
   1906 public:
   1907   AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
   1908     Lexer.setSkipSpace(SkipSpace);
   1909   }
   1910 
   1911   ~AsmLexerSkipSpaceRAII() {
   1912     Lexer.setSkipSpace(true);
   1913   }
   1914 
   1915 private:
   1916   AsmLexer &Lexer;
   1917 };
   1918 }
   1919 
   1920 bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
   1921 
   1922   if (Vararg) {
   1923     if (Lexer.isNot(AsmToken::EndOfStatement)) {
   1924       StringRef Str = parseStringToEndOfStatement();
   1925       MA.push_back(AsmToken(AsmToken::String, Str));
   1926     }
   1927     return false;
   1928   }
   1929 
   1930   unsigned ParenLevel = 0;
   1931   unsigned AddTokens = 0;
   1932 
   1933   // Darwin doesn't use spaces to delmit arguments.
   1934   AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
   1935 
   1936   for (;;) {
   1937     if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
   1938       return TokError("unexpected token in macro instantiation");
   1939 
   1940     if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
   1941       break;
   1942 
   1943     if (Lexer.is(AsmToken::Space)) {
   1944       Lex(); // Eat spaces
   1945 
   1946       // Spaces can delimit parameters, but could also be part an expression.
   1947       // If the token after a space is an operator, add the token and the next
   1948       // one into this argument
   1949       if (!IsDarwin) {
   1950         if (isOperator(Lexer.getKind())) {
   1951           // Check to see whether the token is used as an operator,
   1952           // or part of an identifier
   1953           const char *NextChar = getTok().getEndLoc().getPointer();
   1954           if (*NextChar == ' ')
   1955             AddTokens = 2;
   1956         }
   1957 
   1958         if (!AddTokens && ParenLevel == 0) {
   1959           break;
   1960         }
   1961       }
   1962     }
   1963 
   1964     // handleMacroEntry relies on not advancing the lexer here
   1965     // to be able to fill in the remaining default parameter values
   1966     if (Lexer.is(AsmToken::EndOfStatement))
   1967       break;
   1968 
   1969     // Adjust the current parentheses level.
   1970     if (Lexer.is(AsmToken::LParen))
   1971       ++ParenLevel;
   1972     else if (Lexer.is(AsmToken::RParen) && ParenLevel)
   1973       --ParenLevel;
   1974 
   1975     // Append the token to the current argument list.
   1976     MA.push_back(getTok());
   1977     if (AddTokens)
   1978       AddTokens--;
   1979     Lex();
   1980   }
   1981 
   1982   if (ParenLevel != 0)
   1983     return TokError("unbalanced parentheses in macro argument");
   1984   return false;
   1985 }
   1986 
   1987 // Parse the macro instantiation arguments.
   1988 bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
   1989                                     MCAsmMacroArguments &A) {
   1990   const unsigned NParameters = M ? M->Parameters.size() : 0;
   1991   bool NamedParametersFound = false;
   1992   SmallVector<SMLoc, 4> FALocs;
   1993 
   1994   A.resize(NParameters);
   1995   FALocs.resize(NParameters);
   1996 
   1997   // Parse two kinds of macro invocations:
   1998   // - macros defined without any parameters accept an arbitrary number of them
   1999   // - macros defined with parameters accept at most that many of them
   2000   bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
   2001   for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
   2002        ++Parameter) {
   2003     SMLoc IDLoc = Lexer.getLoc();
   2004     MCAsmMacroParameter FA;
   2005 
   2006     if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
   2007       if (parseIdentifier(FA.Name)) {
   2008         Error(IDLoc, "invalid argument identifier for formal argument");
   2009         eatToEndOfStatement();
   2010         return true;
   2011       }
   2012 
   2013       if (!Lexer.is(AsmToken::Equal)) {
   2014         TokError("expected '=' after formal parameter identifier");
   2015         eatToEndOfStatement();
   2016         return true;
   2017       }
   2018       Lex();
   2019 
   2020       NamedParametersFound = true;
   2021     }
   2022 
   2023     if (NamedParametersFound && FA.Name.empty()) {
   2024       Error(IDLoc, "cannot mix positional and keyword arguments");
   2025       eatToEndOfStatement();
   2026       return true;
   2027     }
   2028 
   2029     bool Vararg = HasVararg && Parameter == (NParameters - 1);
   2030     if (parseMacroArgument(FA.Value, Vararg))
   2031       return true;
   2032 
   2033     unsigned PI = Parameter;
   2034     if (!FA.Name.empty()) {
   2035       unsigned FAI = 0;
   2036       for (FAI = 0; FAI < NParameters; ++FAI)
   2037         if (M->Parameters[FAI].Name == FA.Name)
   2038           break;
   2039 
   2040       if (FAI >= NParameters) {
   2041     assert(M && "expected macro to be defined");
   2042         Error(IDLoc,
   2043               "parameter named '" + FA.Name + "' does not exist for macro '" +
   2044               M->Name + "'");
   2045         return true;
   2046       }
   2047       PI = FAI;
   2048     }
   2049 
   2050     if (!FA.Value.empty()) {
   2051       if (A.size() <= PI)
   2052         A.resize(PI + 1);
   2053       A[PI] = FA.Value;
   2054 
   2055       if (FALocs.size() <= PI)
   2056         FALocs.resize(PI + 1);
   2057 
   2058       FALocs[PI] = Lexer.getLoc();
   2059     }
   2060 
   2061     // At the end of the statement, fill in remaining arguments that have
   2062     // default values. If there aren't any, then the next argument is
   2063     // required but missing
   2064     if (Lexer.is(AsmToken::EndOfStatement)) {
   2065       bool Failure = false;
   2066       for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
   2067         if (A[FAI].empty()) {
   2068           if (M->Parameters[FAI].Required) {
   2069             Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
   2070                   "missing value for required parameter "
   2071                   "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
   2072             Failure = true;
   2073           }
   2074 
   2075           if (!M->Parameters[FAI].Value.empty())
   2076             A[FAI] = M->Parameters[FAI].Value;
   2077         }
   2078       }
   2079       return Failure;
   2080     }
   2081 
   2082     if (Lexer.is(AsmToken::Comma))
   2083       Lex();
   2084   }
   2085 
   2086   return TokError("too many positional arguments");
   2087 }
   2088 
   2089 const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
   2090   StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name);
   2091   return (I == MacroMap.end()) ? nullptr : &I->getValue();
   2092 }
   2093 
   2094 void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) {
   2095   MacroMap.insert(std::make_pair(Name, std::move(Macro)));
   2096 }
   2097 
   2098 void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); }
   2099 
   2100 bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
   2101   // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
   2102   // this, although we should protect against infinite loops.
   2103   if (ActiveMacros.size() == 20)
   2104     return TokError("macros cannot be nested more than 20 levels deep");
   2105 
   2106   MCAsmMacroArguments A;
   2107   if (parseMacroArguments(M, A))
   2108     return true;
   2109 
   2110   // Macro instantiation is lexical, unfortunately. We construct a new buffer
   2111   // to hold the macro body with substitutions.
   2112   SmallString<256> Buf;
   2113   StringRef Body = M->Body;
   2114   raw_svector_ostream OS(Buf);
   2115 
   2116   if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
   2117     return true;
   2118 
   2119   // We include the .endmacro in the buffer as our cue to exit the macro
   2120   // instantiation.
   2121   OS << ".endmacro\n";
   2122 
   2123   std::unique_ptr<MemoryBuffer> Instantiation =
   2124       MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
   2125 
   2126   // Create the macro instantiation object and add to the current macro
   2127   // instantiation stack.
   2128   MacroInstantiation *MI = new MacroInstantiation(
   2129       NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
   2130   ActiveMacros.push_back(MI);
   2131 
   2132   // Jump to the macro instantiation and prime the lexer.
   2133   CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
   2134   Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
   2135   Lex();
   2136 
   2137   return false;
   2138 }
   2139 
   2140 void AsmParser::handleMacroExit() {
   2141   // Jump to the EndOfStatement we should return to, and consume it.
   2142   jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
   2143   Lex();
   2144 
   2145   // Pop the instantiation entry.
   2146   delete ActiveMacros.back();
   2147   ActiveMacros.pop_back();
   2148 }
   2149 
   2150 static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
   2151   switch (Value->getKind()) {
   2152   case MCExpr::Binary: {
   2153     const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
   2154     return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
   2155   }
   2156   case MCExpr::Target:
   2157   case MCExpr::Constant:
   2158     return false;
   2159   case MCExpr::SymbolRef: {
   2160     const MCSymbol &S =
   2161         static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
   2162     if (S.isVariable())
   2163       return isUsedIn(Sym, S.getVariableValue());
   2164     return &S == Sym;
   2165   }
   2166   case MCExpr::Unary:
   2167     return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
   2168   }
   2169 
   2170   llvm_unreachable("Unknown expr kind!");
   2171 }
   2172 
   2173 bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
   2174                                 bool NoDeadStrip) {
   2175   // FIXME: Use better location, we should use proper tokens.
   2176   SMLoc EqualLoc = Lexer.getLoc();
   2177 
   2178   const MCExpr *Value;
   2179   if (parseExpression(Value))
   2180     return true;
   2181 
   2182   // Note: we don't count b as used in "a = b". This is to allow
   2183   // a = b
   2184   // b = c
   2185 
   2186   if (Lexer.isNot(AsmToken::EndOfStatement))
   2187     return TokError("unexpected token in assignment");
   2188 
   2189   // Eat the end of statement marker.
   2190   Lex();
   2191 
   2192   // Validate that the LHS is allowed to be a variable (either it has not been
   2193   // used as a symbol, or it is an absolute symbol).
   2194   MCSymbol *Sym = getContext().LookupSymbol(Name);
   2195   if (Sym) {
   2196     // Diagnose assignment to a label.
   2197     //
   2198     // FIXME: Diagnostics. Note the location of the definition as a label.
   2199     // FIXME: Diagnose assignment to protected identifier (e.g., register name).
   2200     if (isUsedIn(Sym, Value))
   2201       return Error(EqualLoc, "Recursive use of '" + Name + "'");
   2202     else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
   2203       ; // Allow redefinitions of undefined symbols only used in directives.
   2204     else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
   2205       ; // Allow redefinitions of variables that haven't yet been used.
   2206     else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
   2207       return Error(EqualLoc, "redefinition of '" + Name + "'");
   2208     else if (!Sym->isVariable())
   2209       return Error(EqualLoc, "invalid assignment to '" + Name + "'");
   2210     else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
   2211       return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
   2212                                  Name + "'");
   2213 
   2214     // Don't count these checks as uses.
   2215     Sym->setUsed(false);
   2216   } else if (Name == ".") {
   2217     if (Out.EmitValueToOffset(Value, 0)) {
   2218       Error(EqualLoc, "expected absolute expression");
   2219       eatToEndOfStatement();
   2220     }
   2221     return false;
   2222   } else
   2223     Sym = getContext().GetOrCreateSymbol(Name);
   2224 
   2225   Sym->setRedefinable(allow_redef);
   2226 
   2227   // Do the assignment.
   2228   Out.EmitAssignment(Sym, Value);
   2229   if (NoDeadStrip)
   2230     Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
   2231 
   2232   return false;
   2233 }
   2234 
   2235 /// parseIdentifier:
   2236 ///   ::= identifier
   2237 ///   ::= string
   2238 bool AsmParser::parseIdentifier(StringRef &Res) {
   2239   // The assembler has relaxed rules for accepting identifiers, in particular we
   2240   // allow things like '.globl $foo' and '.def @feat.00', which would normally be
   2241   // separate tokens. At this level, we have already lexed so we cannot (currently)
   2242   // handle this as a context dependent token, instead we detect adjacent tokens
   2243   // and return the combined identifier.
   2244   if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
   2245     SMLoc PrefixLoc = getLexer().getLoc();
   2246 
   2247     // Consume the prefix character, and check for a following identifier.
   2248     Lex();
   2249     if (Lexer.isNot(AsmToken::Identifier))
   2250       return true;
   2251 
   2252     // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
   2253     if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
   2254       return true;
   2255 
   2256     // Construct the joined identifier and consume the token.
   2257     Res =
   2258         StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
   2259     Lex();
   2260     return false;
   2261   }
   2262 
   2263   if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
   2264     return true;
   2265 
   2266   Res = getTok().getIdentifier();
   2267 
   2268   Lex(); // Consume the identifier token.
   2269 
   2270   return false;
   2271 }
   2272 
   2273 /// parseDirectiveSet:
   2274 ///   ::= .equ identifier ',' expression
   2275 ///   ::= .equiv identifier ',' expression
   2276 ///   ::= .set identifier ',' expression
   2277 bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
   2278   StringRef Name;
   2279 
   2280   if (parseIdentifier(Name))
   2281     return TokError("expected identifier after '" + Twine(IDVal) + "'");
   2282 
   2283   if (getLexer().isNot(AsmToken::Comma))
   2284     return TokError("unexpected token in '" + Twine(IDVal) + "'");
   2285   Lex();
   2286 
   2287   return parseAssignment(Name, allow_redef, true);
   2288 }
   2289 
   2290 bool AsmParser::parseEscapedString(std::string &Data) {
   2291   assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
   2292 
   2293   Data = "";
   2294   StringRef Str = getTok().getStringContents();
   2295   for (unsigned i = 0, e = Str.size(); i != e; ++i) {
   2296     if (Str[i] != '\\') {
   2297       Data += Str[i];
   2298       continue;
   2299     }
   2300 
   2301     // Recognize escaped characters. Note that this escape semantics currently
   2302     // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
   2303     ++i;
   2304     if (i == e)
   2305       return TokError("unexpected backslash at end of string");
   2306 
   2307     // Recognize octal sequences.
   2308     if ((unsigned)(Str[i] - '0') <= 7) {
   2309       // Consume up to three octal characters.
   2310       unsigned Value = Str[i] - '0';
   2311 
   2312       if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
   2313         ++i;
   2314         Value = Value * 8 + (Str[i] - '0');
   2315 
   2316         if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
   2317           ++i;
   2318           Value = Value * 8 + (Str[i] - '0');
   2319         }
   2320       }
   2321 
   2322       if (Value > 255)
   2323         return TokError("invalid octal escape sequence (out of range)");
   2324 
   2325       Data += (unsigned char)Value;
   2326       continue;
   2327     }
   2328 
   2329     // Otherwise recognize individual escapes.
   2330     switch (Str[i]) {
   2331     default:
   2332       // Just reject invalid escape sequences for now.
   2333       return TokError("invalid escape sequence (unrecognized character)");
   2334 
   2335     case 'b': Data += '\b'; break;
   2336     case 'f': Data += '\f'; break;
   2337     case 'n': Data += '\n'; break;
   2338     case 'r': Data += '\r'; break;
   2339     case 't': Data += '\t'; break;
   2340     case '"': Data += '"'; break;
   2341     case '\\': Data += '\\'; break;
   2342     }
   2343   }
   2344 
   2345   return false;
   2346 }
   2347 
   2348 /// parseDirectiveAscii:
   2349 ///   ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
   2350 bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
   2351   if (getLexer().isNot(AsmToken::EndOfStatement)) {
   2352     checkForValidSection();
   2353 
   2354     for (;;) {
   2355       if (getLexer().isNot(AsmToken::String))
   2356         return TokError("expected string in '" + Twine(IDVal) + "' directive");
   2357 
   2358       std::string Data;
   2359       if (parseEscapedString(Data))
   2360         return true;
   2361 
   2362       getStreamer().EmitBytes(Data);
   2363       if (ZeroTerminated)
   2364         getStreamer().EmitBytes(StringRef("\0", 1));
   2365 
   2366       Lex();
   2367 
   2368       if (getLexer().is(AsmToken::EndOfStatement))
   2369         break;
   2370 
   2371       if (getLexer().isNot(AsmToken::Comma))
   2372         return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
   2373       Lex();
   2374     }
   2375   }
   2376 
   2377   Lex();
   2378   return false;
   2379 }
   2380 
   2381 /// parseDirectiveValue
   2382 ///  ::= (.byte | .short | ... ) [ expression (, expression)* ]
   2383 bool AsmParser::parseDirectiveValue(unsigned Size) {
   2384   if (getLexer().isNot(AsmToken::EndOfStatement)) {
   2385     checkForValidSection();
   2386 
   2387     for (;;) {
   2388       const MCExpr *Value;
   2389       SMLoc ExprLoc = getLexer().getLoc();
   2390       if (parseExpression(Value))
   2391         return true;
   2392 
   2393       // Special case constant expressions to match code generator.
   2394       if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
   2395         assert(Size <= 8 && "Invalid size");
   2396         uint64_t IntValue = MCE->getValue();
   2397         if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
   2398           return Error(ExprLoc, "literal value out of range for directive");
   2399         getStreamer().EmitIntValue(IntValue, Size);
   2400       } else
   2401         getStreamer().EmitValue(Value, Size, ExprLoc);
   2402 
   2403       if (getLexer().is(AsmToken::EndOfStatement))
   2404         break;
   2405 
   2406       // FIXME: Improve diagnostic.
   2407       if (getLexer().isNot(AsmToken::Comma))
   2408         return TokError("unexpected token in directive");
   2409       Lex();
   2410     }
   2411   }
   2412 
   2413   Lex();
   2414   return false;
   2415 }
   2416 
   2417 /// ParseDirectiveOctaValue
   2418 ///  ::= .octa [ hexconstant (, hexconstant)* ]
   2419 bool AsmParser::parseDirectiveOctaValue() {
   2420   if (getLexer().isNot(AsmToken::EndOfStatement)) {
   2421     checkForValidSection();
   2422 
   2423     for (;;) {
   2424       if (Lexer.getKind() == AsmToken::Error)
   2425         return true;
   2426       if (Lexer.getKind() != AsmToken::Integer &&
   2427           Lexer.getKind() != AsmToken::BigNum)
   2428         return TokError("unknown token in expression");
   2429 
   2430       SMLoc ExprLoc = getLexer().getLoc();
   2431       APInt IntValue = getTok().getAPIntVal();
   2432       Lex();
   2433 
   2434       uint64_t hi, lo;
   2435       if (IntValue.isIntN(64)) {
   2436         hi = 0;
   2437         lo = IntValue.getZExtValue();
   2438       } else if (IntValue.isIntN(128)) {
   2439         // It might actually have more than 128 bits, but the top ones are zero.
   2440         hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
   2441         lo = IntValue.getLoBits(64).getZExtValue();
   2442       } else
   2443         return Error(ExprLoc, "literal value out of range for directive");
   2444 
   2445       if (MAI.isLittleEndian()) {
   2446         getStreamer().EmitIntValue(lo, 8);
   2447         getStreamer().EmitIntValue(hi, 8);
   2448       } else {
   2449         getStreamer().EmitIntValue(hi, 8);
   2450         getStreamer().EmitIntValue(lo, 8);
   2451       }
   2452 
   2453       if (getLexer().is(AsmToken::EndOfStatement))
   2454         break;
   2455 
   2456       // FIXME: Improve diagnostic.
   2457       if (getLexer().isNot(AsmToken::Comma))
   2458         return TokError("unexpected token in directive");
   2459       Lex();
   2460     }
   2461   }
   2462 
   2463   Lex();
   2464   return false;
   2465 }
   2466 
   2467 /// parseDirectiveRealValue
   2468 ///  ::= (.single | .double) [ expression (, expression)* ]
   2469 bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
   2470   if (getLexer().isNot(AsmToken::EndOfStatement)) {
   2471     checkForValidSection();
   2472 
   2473     for (;;) {
   2474       // We don't truly support arithmetic on floating point expressions, so we
   2475       // have to manually parse unary prefixes.
   2476       bool IsNeg = false;
   2477       if (getLexer().is(AsmToken::Minus)) {
   2478         Lex();
   2479         IsNeg = true;
   2480       } else if (getLexer().is(AsmToken::Plus))
   2481         Lex();
   2482 
   2483       if (getLexer().isNot(AsmToken::Integer) &&
   2484           getLexer().isNot(AsmToken::Real) &&
   2485           getLexer().isNot(AsmToken::Identifier))
   2486         return TokError("unexpected token in directive");
   2487 
   2488       // Convert to an APFloat.
   2489       APFloat Value(Semantics);
   2490       StringRef IDVal = getTok().getString();
   2491       if (getLexer().is(AsmToken::Identifier)) {
   2492         if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
   2493           Value = APFloat::getInf(Semantics);
   2494         else if (!IDVal.compare_lower("nan"))
   2495           Value = APFloat::getNaN(Semantics, false, ~0);
   2496         else
   2497           return TokError("invalid floating point literal");
   2498       } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
   2499                  APFloat::opInvalidOp)
   2500         return TokError("invalid floating point literal");
   2501       if (IsNeg)
   2502         Value.changeSign();
   2503 
   2504       // Consume the numeric token.
   2505       Lex();
   2506 
   2507       // Emit the value as an integer.
   2508       APInt AsInt = Value.bitcastToAPInt();
   2509       getStreamer().EmitIntValue(AsInt.getLimitedValue(),
   2510                                  AsInt.getBitWidth() / 8);
   2511 
   2512       if (getLexer().is(AsmToken::EndOfStatement))
   2513         break;
   2514 
   2515       if (getLexer().isNot(AsmToken::Comma))
   2516         return TokError("unexpected token in directive");
   2517       Lex();
   2518     }
   2519   }
   2520 
   2521   Lex();
   2522   return false;
   2523 }
   2524 
   2525 /// parseDirectiveZero
   2526 ///  ::= .zero expression
   2527 bool AsmParser::parseDirectiveZero() {
   2528   checkForValidSection();
   2529 
   2530   int64_t NumBytes;
   2531   if (parseAbsoluteExpression(NumBytes))
   2532     return true;
   2533 
   2534   int64_t Val = 0;
   2535   if (getLexer().is(AsmToken::Comma)) {
   2536     Lex();
   2537     if (parseAbsoluteExpression(Val))
   2538       return true;
   2539   }
   2540 
   2541   if (getLexer().isNot(AsmToken::EndOfStatement))
   2542     return TokError("unexpected token in '.zero' directive");
   2543 
   2544   Lex();
   2545 
   2546   getStreamer().EmitFill(NumBytes, Val);
   2547 
   2548   return false;
   2549 }
   2550 
   2551 /// parseDirectiveFill
   2552 ///  ::= .fill expression [ , expression [ , expression ] ]
   2553 bool AsmParser::parseDirectiveFill() {
   2554   checkForValidSection();
   2555 
   2556   SMLoc RepeatLoc = getLexer().getLoc();
   2557   int64_t NumValues;
   2558   if (parseAbsoluteExpression(NumValues))
   2559     return true;
   2560 
   2561   if (NumValues < 0) {
   2562     Warning(RepeatLoc,
   2563             "'.fill' directive with negative repeat count has no effect");
   2564     NumValues = 0;
   2565   }
   2566 
   2567   int64_t FillSize = 1;
   2568   int64_t FillExpr = 0;
   2569 
   2570   SMLoc SizeLoc, ExprLoc;
   2571   if (getLexer().isNot(AsmToken::EndOfStatement)) {
   2572     if (getLexer().isNot(AsmToken::Comma))
   2573       return TokError("unexpected token in '.fill' directive");
   2574     Lex();
   2575 
   2576     SizeLoc = getLexer().getLoc();
   2577     if (parseAbsoluteExpression(FillSize))
   2578       return true;
   2579 
   2580     if (getLexer().isNot(AsmToken::EndOfStatement)) {
   2581       if (getLexer().isNot(AsmToken::Comma))
   2582         return TokError("unexpected token in '.fill' directive");
   2583       Lex();
   2584 
   2585       ExprLoc = getLexer().getLoc();
   2586       if (parseAbsoluteExpression(FillExpr))
   2587         return true;
   2588 
   2589       if (getLexer().isNot(AsmToken::EndOfStatement))
   2590         return TokError("unexpected token in '.fill' directive");
   2591 
   2592       Lex();
   2593     }
   2594   }
   2595 
   2596   if (FillSize < 0) {
   2597     Warning(SizeLoc, "'.fill' directive with negative size has no effect");
   2598     NumValues = 0;
   2599   }
   2600   if (FillSize > 8) {
   2601     Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
   2602     FillSize = 8;
   2603   }
   2604 
   2605   if (!isUInt<32>(FillExpr) && FillSize > 4)
   2606     Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
   2607 
   2608   if (NumValues > 0) {
   2609     int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
   2610     FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
   2611     for (uint64_t i = 0, e = NumValues; i != e; ++i) {
   2612       getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
   2613       if (NonZeroFillSize < FillSize)
   2614         getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
   2615     }
   2616   }
   2617 
   2618   return false;
   2619 }
   2620 
   2621 /// parseDirectiveOrg
   2622 ///  ::= .org expression [ , expression ]
   2623 bool AsmParser::parseDirectiveOrg() {
   2624   checkForValidSection();
   2625 
   2626   const MCExpr *Offset;
   2627   SMLoc Loc = getTok().getLoc();
   2628   if (parseExpression(Offset))
   2629     return true;
   2630 
   2631   // Parse optional fill expression.
   2632   int64_t FillExpr = 0;
   2633   if (getLexer().isNot(AsmToken::EndOfStatement)) {
   2634     if (getLexer().isNot(AsmToken::Comma))
   2635       return TokError("unexpected token in '.org' directive");
   2636     Lex();
   2637 
   2638     if (parseAbsoluteExpression(FillExpr))
   2639       return true;
   2640 
   2641     if (getLexer().isNot(AsmToken::EndOfStatement))
   2642       return TokError("unexpected token in '.org' directive");
   2643   }
   2644 
   2645   Lex();
   2646 
   2647   // Only limited forms of relocatable expressions are accepted here, it
   2648   // has to be relative to the current section. The streamer will return
   2649   // 'true' if the expression wasn't evaluatable.
   2650   if (getStreamer().EmitValueToOffset(Offset, FillExpr))
   2651     return Error(Loc, "expected assembly-time absolute expression");
   2652 
   2653   return false;
   2654 }
   2655 
   2656 /// parseDirectiveAlign
   2657 ///  ::= {.align, ...} expression [ , expression [ , expression ]]
   2658 bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
   2659   checkForValidSection();
   2660 
   2661   SMLoc AlignmentLoc = getLexer().getLoc();
   2662   int64_t Alignment;
   2663   if (parseAbsoluteExpression(Alignment))
   2664     return true;
   2665 
   2666   SMLoc MaxBytesLoc;
   2667   bool HasFillExpr = false;
   2668   int64_t FillExpr = 0;
   2669   int64_t MaxBytesToFill = 0;
   2670   if (getLexer().isNot(AsmToken::EndOfStatement)) {
   2671     if (getLexer().isNot(AsmToken::Comma))
   2672       return TokError("unexpected token in directive");
   2673     Lex();
   2674 
   2675     // The fill expression can be omitted while specifying a maximum number of
   2676     // alignment bytes, e.g:
   2677     //  .align 3,,4
   2678     if (getLexer().isNot(AsmToken::Comma)) {
   2679       HasFillExpr = true;
   2680       if (parseAbsoluteExpression(FillExpr))
   2681         return true;
   2682     }
   2683 
   2684     if (getLexer().isNot(AsmToken::EndOfStatement)) {
   2685       if (getLexer().isNot(AsmToken::Comma))
   2686         return TokError("unexpected token in directive");
   2687       Lex();
   2688 
   2689       MaxBytesLoc = getLexer().getLoc();
   2690       if (parseAbsoluteExpression(MaxBytesToFill))
   2691         return true;
   2692 
   2693       if (getLexer().isNot(AsmToken::EndOfStatement))
   2694         return TokError("unexpected token in directive");
   2695     }
   2696   }
   2697 
   2698   Lex();
   2699 
   2700   if (!HasFillExpr)
   2701     FillExpr = 0;
   2702 
   2703   // Compute alignment in bytes.
   2704   if (IsPow2) {
   2705     // FIXME: Diagnose overflow.
   2706     if (Alignment >= 32) {
   2707       Error(AlignmentLoc, "invalid alignment value");
   2708       Alignment = 31;
   2709     }
   2710 
   2711     Alignment = 1ULL << Alignment;
   2712   } else {
   2713     // Reject alignments that aren't a power of two, for gas compatibility.
   2714     if (!isPowerOf2_64(Alignment))
   2715       Error(AlignmentLoc, "alignment must be a power of 2");
   2716   }
   2717 
   2718   // Diagnose non-sensical max bytes to align.
   2719   if (MaxBytesLoc.isValid()) {
   2720     if (MaxBytesToFill < 1) {
   2721       Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
   2722                          "many bytes, ignoring maximum bytes expression");
   2723       MaxBytesToFill = 0;
   2724     }
   2725 
   2726     if (MaxBytesToFill >= Alignment) {
   2727       Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
   2728                            "has no effect");
   2729       MaxBytesToFill = 0;
   2730     }
   2731   }
   2732 
   2733   // Check whether we should use optimal code alignment for this .align
   2734   // directive.
   2735   const MCSection *Section = getStreamer().getCurrentSection().first;
   2736   assert(Section && "must have section to emit alignment");
   2737   bool UseCodeAlign = Section->UseCodeAlign();
   2738   if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
   2739       ValueSize == 1 && UseCodeAlign) {
   2740     getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
   2741   } else {
   2742     // FIXME: Target specific behavior about how the "extra" bytes are filled.
   2743     getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
   2744                                        MaxBytesToFill);
   2745   }
   2746 
   2747   return false;
   2748 }
   2749 
   2750 /// parseDirectiveFile
   2751 /// ::= .file [number] filename
   2752 /// ::= .file number directory filename
   2753 bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
   2754   // FIXME: I'm not sure what this is.
   2755   int64_t FileNumber = -1;
   2756   SMLoc FileNumberLoc = getLexer().getLoc();
   2757   if (getLexer().is(AsmToken::Integer)) {
   2758     FileNumber = getTok().getIntVal();
   2759     Lex();
   2760 
   2761     if (FileNumber < 1)
   2762       return TokError("file number less than one");
   2763   }
   2764 
   2765   if (getLexer().isNot(AsmToken::String))
   2766     return TokError("unexpected token in '.file' directive");
   2767 
   2768   // Usually the directory and filename together, otherwise just the directory.
   2769   // Allow the strings to have escaped octal character sequence.
   2770   std::string Path = getTok().getString();
   2771   if (parseEscapedString(Path))
   2772     return true;
   2773   Lex();
   2774 
   2775   StringRef Directory;
   2776   StringRef Filename;
   2777   std::string FilenameData;
   2778   if (getLexer().is(AsmToken::String)) {
   2779     if (FileNumber == -1)
   2780       return TokError("explicit path specified, but no file number");
   2781     if (parseEscapedString(FilenameData))
   2782       return true;
   2783     Filename = FilenameData;
   2784     Directory = Path;
   2785     Lex();
   2786   } else {
   2787     Filename = Path;
   2788   }
   2789 
   2790   if (getLexer().isNot(AsmToken::EndOfStatement))
   2791     return TokError("unexpected token in '.file' directive");
   2792 
   2793   if (FileNumber == -1)
   2794     getStreamer().EmitFileDirective(Filename);
   2795   else {
   2796     if (getContext().getGenDwarfForAssembly())
   2797       Error(DirectiveLoc,
   2798             "input can't have .file dwarf directives when -g is "
   2799             "used to generate dwarf debug info for assembly code");
   2800 
   2801     if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
   2802         0)
   2803       Error(FileNumberLoc, "file number already allocated");
   2804   }
   2805 
   2806   return false;
   2807 }
   2808 
   2809 /// parseDirectiveLine
   2810 /// ::= .line [number]
   2811 bool AsmParser::parseDirectiveLine() {
   2812   if (getLexer().isNot(AsmToken::EndOfStatement)) {
   2813     if (getLexer().isNot(AsmToken::Integer))
   2814       return TokError("unexpected token in '.line' directive");
   2815 
   2816     int64_t LineNumber = getTok().getIntVal();
   2817     (void)LineNumber;
   2818     Lex();
   2819 
   2820     // FIXME: Do something with the .line.
   2821   }
   2822 
   2823   if (getLexer().isNot(AsmToken::EndOfStatement))
   2824     return TokError("unexpected token in '.line' directive");
   2825 
   2826   return false;
   2827 }
   2828 
   2829 /// parseDirectiveLoc
   2830 /// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
   2831 ///                                [epilogue_begin] [is_stmt VALUE] [isa VALUE]
   2832 /// The first number is a file number, must have been previously assigned with
   2833 /// a .file directive, the second number is the line number and optionally the
   2834 /// third number is a column position (zero if not specified).  The remaining
   2835 /// optional items are .loc sub-directives.
   2836 bool AsmParser::parseDirectiveLoc() {
   2837   if (getLexer().isNot(AsmToken::Integer))
   2838     return TokError("unexpected token in '.loc' directive");
   2839   int64_t FileNumber = getTok().getIntVal();
   2840   if (FileNumber < 1)
   2841     return TokError("file number less than one in '.loc' directive");
   2842   if (!getContext().isValidDwarfFileNumber(FileNumber))
   2843     return TokError("unassigned file number in '.loc' directive");
   2844   Lex();
   2845 
   2846   int64_t LineNumber = 0;
   2847   if (getLexer().is(AsmToken::Integer)) {
   2848     LineNumber = getTok().getIntVal();
   2849     if (LineNumber < 0)
   2850       return TokError("line number less than zero in '.loc' directive");
   2851     Lex();
   2852   }
   2853 
   2854   int64_t ColumnPos = 0;
   2855   if (getLexer().is(AsmToken::Integer)) {
   2856     ColumnPos = getTok().getIntVal();
   2857     if (ColumnPos < 0)
   2858       return TokError("column position less than zero in '.loc' directive");
   2859     Lex();
   2860   }
   2861 
   2862   unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
   2863   unsigned Isa = 0;
   2864   int64_t Discriminator = 0;
   2865   if (getLexer().isNot(AsmToken::EndOfStatement)) {
   2866     for (;;) {
   2867       if (getLexer().is(AsmToken::EndOfStatement))
   2868         break;
   2869 
   2870       StringRef Name;
   2871       SMLoc Loc = getTok().getLoc();
   2872       if (parseIdentifier(Name))
   2873         return TokError("unexpected token in '.loc' directive");
   2874 
   2875       if (Name == "basic_block")
   2876         Flags |= DWARF2_FLAG_BASIC_BLOCK;
   2877       else if (Name == "prologue_end")
   2878         Flags |= DWARF2_FLAG_PROLOGUE_END;
   2879       else if (Name == "epilogue_begin")
   2880         Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
   2881       else if (Name == "is_stmt") {
   2882         Loc = getTok().getLoc();
   2883         const MCExpr *Value;
   2884         if (parseExpression(Value))
   2885           return true;
   2886         // The expression must be the constant 0 or 1.
   2887         if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
   2888           int Value = MCE->getValue();
   2889           if (Value == 0)
   2890             Flags &= ~DWARF2_FLAG_IS_STMT;
   2891           else if (Value == 1)
   2892             Flags |= DWARF2_FLAG_IS_STMT;
   2893           else
   2894             return Error(Loc, "is_stmt value not 0 or 1");
   2895         } else {
   2896           return Error(Loc, "is_stmt value not the constant value of 0 or 1");
   2897         }
   2898       } else if (Name == "isa") {
   2899         Loc = getTok().getLoc();
   2900         const MCExpr *Value;
   2901         if (parseExpression(Value))
   2902           return true;
   2903         // The expression must be a constant greater or equal to 0.
   2904         if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
   2905           int Value = MCE->getValue();
   2906           if (Value < 0)
   2907             return Error(Loc, "isa number less than zero");
   2908           Isa = Value;
   2909         } else {
   2910           return Error(Loc, "isa number not a constant value");
   2911         }
   2912       } else if (Name == "discriminator") {
   2913         if (parseAbsoluteExpression(Discriminator))
   2914           return true;
   2915       } else {
   2916         return Error(Loc, "unknown sub-directive in '.loc' directive");
   2917       }
   2918 
   2919       if (getLexer().is(AsmToken::EndOfStatement))
   2920         break;
   2921     }
   2922   }
   2923 
   2924   getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
   2925                                       Isa, Discriminator, StringRef());
   2926 
   2927   return false;
   2928 }
   2929 
   2930 /// parseDirectiveStabs
   2931 /// ::= .stabs string, number, number, number
   2932 bool AsmParser::parseDirectiveStabs() {
   2933   return TokError("unsupported directive '.stabs'");
   2934 }
   2935 
   2936 /// parseDirectiveCFISections
   2937 /// ::= .cfi_sections section [, section]
   2938 bool AsmParser::parseDirectiveCFISections() {
   2939   StringRef Name;
   2940   bool EH = false;
   2941   bool Debug = false;
   2942 
   2943   if (parseIdentifier(Name))
   2944     return TokError("Expected an identifier");
   2945 
   2946   if (Name == ".eh_frame")
   2947     EH = true;
   2948   else if (Name == ".debug_frame")
   2949     Debug = true;
   2950 
   2951   if (getLexer().is(AsmToken::Comma)) {
   2952     Lex();
   2953 
   2954     if (parseIdentifier(Name))
   2955       return TokError("Expected an identifier");
   2956 
   2957     if (Name == ".eh_frame")
   2958       EH = true;
   2959     else if (Name == ".debug_frame")
   2960       Debug = true;
   2961   }
   2962 
   2963   getStreamer().EmitCFISections(EH, Debug);
   2964   return false;
   2965 }
   2966 
   2967 /// parseDirectiveCFIStartProc
   2968 /// ::= .cfi_startproc [simple]
   2969 bool AsmParser::parseDirectiveCFIStartProc() {
   2970   StringRef Simple;
   2971   if (getLexer().isNot(AsmToken::EndOfStatement))
   2972     if (parseIdentifier(Simple) || Simple != "simple")
   2973       return TokError("unexpected token in .cfi_startproc directive");
   2974 
   2975   getStreamer().EmitCFIStartProc(!Simple.empty());
   2976   return false;
   2977 }
   2978 
   2979 /// parseDirectiveCFIEndProc
   2980 /// ::= .cfi_endproc
   2981 bool AsmParser::parseDirectiveCFIEndProc() {
   2982   getStreamer().EmitCFIEndProc();
   2983   return false;
   2984 }
   2985 
   2986 /// \brief parse register name or number.
   2987 bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
   2988                                               SMLoc DirectiveLoc) {
   2989   unsigned RegNo;
   2990 
   2991   if (getLexer().isNot(AsmToken::Integer)) {
   2992     if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
   2993       return true;
   2994     Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
   2995   } else
   2996     return parseAbsoluteExpression(Register);
   2997 
   2998   return false;
   2999 }
   3000 
   3001 /// parseDirectiveCFIDefCfa
   3002 /// ::= .cfi_def_cfa register,  offset
   3003 bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
   3004   int64_t Register = 0;
   3005   if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
   3006     return true;
   3007 
   3008   if (getLexer().isNot(AsmToken::Comma))
   3009     return TokError("unexpected token in directive");
   3010   Lex();
   3011 
   3012   int64_t Offset = 0;
   3013   if (parseAbsoluteExpression(Offset))
   3014     return true;
   3015 
   3016   getStreamer().EmitCFIDefCfa(Register, Offset);
   3017   return false;
   3018 }
   3019 
   3020 /// parseDirectiveCFIDefCfaOffset
   3021 /// ::= .cfi_def_cfa_offset offset
   3022 bool AsmParser::parseDirectiveCFIDefCfaOffset() {
   3023   int64_t Offset = 0;
   3024   if (parseAbsoluteExpression(Offset))
   3025     return true;
   3026 
   3027   getStreamer().EmitCFIDefCfaOffset(Offset);
   3028   return false;
   3029 }
   3030 
   3031 /// parseDirectiveCFIRegister
   3032 /// ::= .cfi_register register, register
   3033 bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
   3034   int64_t Register1 = 0;
   3035   if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
   3036     return true;
   3037 
   3038   if (getLexer().isNot(AsmToken::Comma))
   3039     return TokError("unexpected token in directive");
   3040   Lex();
   3041 
   3042   int64_t Register2 = 0;
   3043   if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
   3044     return true;
   3045 
   3046   getStreamer().EmitCFIRegister(Register1, Register2);
   3047   return false;
   3048 }
   3049 
   3050 /// parseDirectiveCFIWindowSave
   3051 /// ::= .cfi_window_save
   3052 bool AsmParser::parseDirectiveCFIWindowSave() {
   3053   getStreamer().EmitCFIWindowSave();
   3054   return false;
   3055 }
   3056 
   3057 /// parseDirectiveCFIAdjustCfaOffset
   3058 /// ::= .cfi_adjust_cfa_offset adjustment
   3059 bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
   3060   int64_t Adjustment = 0;
   3061   if (parseAbsoluteExpression(Adjustment))
   3062     return true;
   3063 
   3064   getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
   3065   return false;
   3066 }
   3067 
   3068 /// parseDirectiveCFIDefCfaRegister
   3069 /// ::= .cfi_def_cfa_register register
   3070 bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
   3071   int64_t Register = 0;
   3072   if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
   3073     return true;
   3074 
   3075   getStreamer().EmitCFIDefCfaRegister(Register);
   3076   return false;
   3077 }
   3078 
   3079 /// parseDirectiveCFIOffset
   3080 /// ::= .cfi_offset register, offset
   3081 bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
   3082   int64_t Register = 0;
   3083   int64_t Offset = 0;
   3084 
   3085   if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
   3086     return true;
   3087 
   3088   if (getLexer().isNot(AsmToken::Comma))
   3089     return TokError("unexpected token in directive");
   3090   Lex();
   3091 
   3092   if (parseAbsoluteExpression(Offset))
   3093     return true;
   3094 
   3095   getStreamer().EmitCFIOffset(Register, Offset);
   3096   return false;
   3097 }
   3098 
   3099 /// parseDirectiveCFIRelOffset
   3100 /// ::= .cfi_rel_offset register, offset
   3101 bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
   3102   int64_t Register = 0;
   3103 
   3104   if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
   3105     return true;
   3106 
   3107   if (getLexer().isNot(AsmToken::Comma))
   3108     return TokError("unexpected token in directive");
   3109   Lex();
   3110 
   3111   int64_t Offset = 0;
   3112   if (parseAbsoluteExpression(Offset))
   3113     return true;
   3114 
   3115   getStreamer().EmitCFIRelOffset(Register, Offset);
   3116   return false;
   3117 }
   3118 
   3119 static bool isValidEncoding(int64_t Encoding) {
   3120   if (Encoding & ~0xff)
   3121     return false;
   3122 
   3123   if (Encoding == dwarf::DW_EH_PE_omit)
   3124     return true;
   3125 
   3126   const unsigned Format = Encoding & 0xf;
   3127   if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
   3128       Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
   3129       Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
   3130       Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
   3131     return false;
   3132 
   3133   const unsigned Application = Encoding & 0x70;
   3134   if (Application != dwarf::DW_EH_PE_absptr &&
   3135       Application != dwarf::DW_EH_PE_pcrel)
   3136     return false;
   3137 
   3138   return true;
   3139 }
   3140 
   3141 /// parseDirectiveCFIPersonalityOrLsda
   3142 /// IsPersonality true for cfi_personality, false for cfi_lsda
   3143 /// ::= .cfi_personality encoding, [symbol_name]
   3144 /// ::= .cfi_lsda encoding, [symbol_name]
   3145 bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
   3146   int64_t Encoding = 0;
   3147   if (parseAbsoluteExpression(Encoding))
   3148     return true;
   3149   if (Encoding == dwarf::DW_EH_PE_omit)
   3150     return false;
   3151 
   3152   if (!isValidEncoding(Encoding))
   3153     return TokError("unsupported encoding.");
   3154 
   3155   if (getLexer().isNot(AsmToken::Comma))
   3156     return TokError("unexpected token in directive");
   3157   Lex();
   3158 
   3159   StringRef Name;
   3160   if (parseIdentifier(Name))
   3161     return TokError("expected identifier in directive");
   3162 
   3163   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
   3164 
   3165   if (IsPersonality)
   3166     getStreamer().EmitCFIPersonality(Sym, Encoding);
   3167   else
   3168     getStreamer().EmitCFILsda(Sym, Encoding);
   3169   return false;
   3170 }
   3171 
   3172 /// parseDirectiveCFIRememberState
   3173 /// ::= .cfi_remember_state
   3174 bool AsmParser::parseDirectiveCFIRememberState() {
   3175   getStreamer().EmitCFIRememberState();
   3176   return false;
   3177 }
   3178 
   3179 /// parseDirectiveCFIRestoreState
   3180 /// ::= .cfi_remember_state
   3181 bool AsmParser::parseDirectiveCFIRestoreState() {
   3182   getStreamer().EmitCFIRestoreState();
   3183   return false;
   3184 }
   3185 
   3186 /// parseDirectiveCFISameValue
   3187 /// ::= .cfi_same_value register
   3188 bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
   3189   int64_t Register = 0;
   3190 
   3191   if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
   3192     return true;
   3193 
   3194   getStreamer().EmitCFISameValue(Register);
   3195   return false;
   3196 }
   3197 
   3198 /// parseDirectiveCFIRestore
   3199 /// ::= .cfi_restore register
   3200 bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
   3201   int64_t Register = 0;
   3202   if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
   3203     return true;
   3204 
   3205   getStreamer().EmitCFIRestore(Register);
   3206   return false;
   3207 }
   3208 
   3209 /// parseDirectiveCFIEscape
   3210 /// ::= .cfi_escape expression[,...]
   3211 bool AsmParser::parseDirectiveCFIEscape() {
   3212   std::string Values;
   3213   int64_t CurrValue;
   3214   if (parseAbsoluteExpression(CurrValue))
   3215     return true;
   3216 
   3217   Values.push_back((uint8_t)CurrValue);
   3218 
   3219   while (getLexer().is(AsmToken::Comma)) {
   3220     Lex();
   3221 
   3222     if (parseAbsoluteExpression(CurrValue))
   3223       return true;
   3224 
   3225     Values.push_back((uint8_t)CurrValue);
   3226   }
   3227 
   3228   getStreamer().EmitCFIEscape(Values);
   3229   return false;
   3230 }
   3231 
   3232 /// parseDirectiveCFISignalFrame
   3233 /// ::= .cfi_signal_frame
   3234 bool AsmParser::parseDirectiveCFISignalFrame() {
   3235   if (getLexer().isNot(AsmToken::EndOfStatement))
   3236     return Error(getLexer().getLoc(),
   3237                  "unexpected token in '.cfi_signal_frame'");
   3238 
   3239   getStreamer().EmitCFISignalFrame();
   3240   return false;
   3241 }
   3242 
   3243 /// parseDirectiveCFIUndefined
   3244 /// ::= .cfi_undefined register
   3245 bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
   3246   int64_t Register = 0;
   3247 
   3248   if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
   3249     return true;
   3250 
   3251   getStreamer().EmitCFIUndefined(Register);
   3252   return false;
   3253 }
   3254 
   3255 /// parseDirectiveMacrosOnOff
   3256 /// ::= .macros_on
   3257 /// ::= .macros_off
   3258 bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
   3259   if (getLexer().isNot(AsmToken::EndOfStatement))
   3260     return Error(getLexer().getLoc(),
   3261                  "unexpected token in '" + Directive + "' directive");
   3262 
   3263   setMacrosEnabled(Directive == ".macros_on");
   3264   return false;
   3265 }
   3266 
   3267 /// parseDirectiveMacro
   3268 /// ::= .macro name[,] [parameters]
   3269 bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
   3270   StringRef Name;
   3271   if (parseIdentifier(Name))
   3272     return TokError("expected identifier in '.macro' directive");
   3273 
   3274   if (getLexer().is(AsmToken::Comma))
   3275     Lex();
   3276 
   3277   MCAsmMacroParameters Parameters;
   3278   while (getLexer().isNot(AsmToken::EndOfStatement)) {
   3279 
   3280     if (!Parameters.empty() && Parameters.back().Vararg)
   3281       return Error(Lexer.getLoc(),
   3282                    "Vararg parameter '" + Parameters.back().Name +
   3283                    "' should be last one in the list of parameters.");
   3284 
   3285     MCAsmMacroParameter Parameter;
   3286     if (parseIdentifier(Parameter.Name))
   3287       return TokError("expected identifier in '.macro' directive");
   3288 
   3289     if (Lexer.is(AsmToken::Colon)) {
   3290       Lex();  // consume ':'
   3291 
   3292       SMLoc QualLoc;
   3293       StringRef Qualifier;
   3294 
   3295       QualLoc = Lexer.getLoc();
   3296       if (parseIdentifier(Qualifier))
   3297         return Error(QualLoc, "missing parameter qualifier for "
   3298                      "'" + Parameter.Name + "' in macro '" + Name + "'");
   3299 
   3300       if (Qualifier == "req")
   3301         Parameter.Required = true;
   3302       else if (Qualifier == "vararg")
   3303         Parameter.Vararg = true;
   3304       else
   3305         return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
   3306                      "for '" + Parameter.Name + "' in macro '" + Name + "'");
   3307     }
   3308 
   3309     if (getLexer().is(AsmToken::Equal)) {
   3310       Lex();
   3311 
   3312       SMLoc ParamLoc;
   3313 
   3314       ParamLoc = Lexer.getLoc();
   3315       if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
   3316         return true;
   3317 
   3318       if (Parameter.Required)
   3319         Warning(ParamLoc, "pointless default value for required parameter "
   3320                 "'" + Parameter.Name + "' in macro '" + Name + "'");
   3321     }
   3322 
   3323     Parameters.push_back(std::move(Parameter));
   3324 
   3325     if (getLexer().is(AsmToken::Comma))
   3326       Lex();
   3327   }
   3328 
   3329   // Eat the end of statement.
   3330   Lex();
   3331 
   3332   AsmToken EndToken, StartToken = getTok();
   3333   unsigned MacroDepth = 0;
   3334 
   3335   // Lex the macro definition.
   3336   for (;;) {
   3337     // Check whether we have reached the end of the file.
   3338     if (getLexer().is(AsmToken::Eof))
   3339       return Error(DirectiveLoc, "no matching '.endmacro' in definition");
   3340 
   3341     // Otherwise, check whether we have reach the .endmacro.
   3342     if (getLexer().is(AsmToken::Identifier)) {
   3343       if (getTok().getIdentifier() == ".endm" ||
   3344           getTok().getIdentifier() == ".endmacro") {
   3345         if (MacroDepth == 0) { // Outermost macro.
   3346           EndToken = getTok();
   3347           Lex();
   3348           if (getLexer().isNot(AsmToken::EndOfStatement))
   3349             return TokError("unexpected token in '" + EndToken.getIdentifier() +
   3350                             "' directive");
   3351           break;
   3352         } else {
   3353           // Otherwise we just found the end of an inner macro.
   3354           --MacroDepth;
   3355         }
   3356       } else if (getTok().getIdentifier() == ".macro") {
   3357         // We allow nested macros. Those aren't instantiated until the outermost
   3358         // macro is expanded so just ignore them for now.
   3359         ++MacroDepth;
   3360       }
   3361     }
   3362 
   3363     // Otherwise, scan til the end of the statement.
   3364     eatToEndOfStatement();
   3365   }
   3366 
   3367   if (lookupMacro(Name)) {
   3368     return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
   3369   }
   3370 
   3371   const char *BodyStart = StartToken.getLoc().getPointer();
   3372   const char *BodyEnd = EndToken.getLoc().getPointer();
   3373   StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
   3374   checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
   3375   defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters)));
   3376   return false;
   3377 }
   3378 
   3379 /// checkForBadMacro
   3380 ///
   3381 /// With the support added for named parameters there may be code out there that
   3382 /// is transitioning from positional parameters.  In versions of gas that did
   3383 /// not support named parameters they would be ignored on the macro definition.
   3384 /// But to support both styles of parameters this is not possible so if a macro
   3385 /// definition has named parameters but does not use them and has what appears
   3386 /// to be positional parameters, strings like $1, $2, ... and $n, then issue a
   3387 /// warning that the positional parameter found in body which have no effect.
   3388 /// Hoping the developer will either remove the named parameters from the macro
   3389 /// definition so the positional parameters get used if that was what was
   3390 /// intended or change the macro to use the named parameters.  It is possible
   3391 /// this warning will trigger when the none of the named parameters are used
   3392 /// and the strings like $1 are infact to simply to be passed trough unchanged.
   3393 void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
   3394                                  StringRef Body,
   3395                                  ArrayRef<MCAsmMacroParameter> Parameters) {
   3396   // If this macro is not defined with named parameters the warning we are
   3397   // checking for here doesn't apply.
   3398   unsigned NParameters = Parameters.size();
   3399   if (NParameters == 0)
   3400     return;
   3401 
   3402   bool NamedParametersFound = false;
   3403   bool PositionalParametersFound = false;
   3404 
   3405   // Look at the body of the macro for use of both the named parameters and what
   3406   // are likely to be positional parameters.  This is what expandMacro() is
   3407   // doing when it finds the parameters in the body.
   3408   while (!Body.empty()) {
   3409     // Scan for the next possible parameter.
   3410     std::size_t End = Body.size(), Pos = 0;
   3411     for (; Pos != End; ++Pos) {
   3412       // Check for a substitution or escape.
   3413       // This macro is defined with parameters, look for \foo, \bar, etc.
   3414       if (Body[Pos] == '\\' && Pos + 1 != End)
   3415         break;
   3416 
   3417       // This macro should have parameters, but look for $0, $1, ..., $n too.
   3418       if (Body[Pos] != '$' || Pos + 1 == End)
   3419         continue;
   3420       char Next = Body[Pos + 1];
   3421       if (Next == '$' || Next == 'n' ||
   3422           isdigit(static_cast<unsigned char>(Next)))
   3423         break;
   3424     }
   3425 
   3426     // Check if we reached the end.
   3427     if (Pos == End)
   3428       break;
   3429 
   3430     if (Body[Pos] == '$') {
   3431       switch (Body[Pos + 1]) {
   3432       // $$ => $
   3433       case '$':
   3434         break;
   3435 
   3436       // $n => number of arguments
   3437       case 'n':
   3438         PositionalParametersFound = true;
   3439         break;
   3440 
   3441       // $[0-9] => argument
   3442       default: {
   3443         PositionalParametersFound = true;
   3444         break;
   3445       }
   3446       }
   3447       Pos += 2;
   3448     } else {
   3449       unsigned I = Pos + 1;
   3450       while (isIdentifierChar(Body[I]) && I + 1 != End)
   3451         ++I;
   3452 
   3453       const char *Begin = Body.data() + Pos + 1;
   3454       StringRef Argument(Begin, I - (Pos + 1));
   3455       unsigned Index = 0;
   3456       for (; Index < NParameters; ++Index)
   3457         if (Parameters[Index].Name == Argument)
   3458           break;
   3459 
   3460       if (Index == NParameters) {
   3461         if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
   3462           Pos += 3;
   3463         else {
   3464           Pos = I;
   3465         }
   3466       } else {
   3467         NamedParametersFound = true;
   3468         Pos += 1 + Argument.size();
   3469       }
   3470     }
   3471     // Update the scan point.
   3472     Body = Body.substr(Pos);
   3473   }
   3474 
   3475   if (!NamedParametersFound && PositionalParametersFound)
   3476     Warning(DirectiveLoc, "macro defined with named parameters which are not "
   3477                           "used in macro body, possible positional parameter "
   3478                           "found in body which will have no effect");
   3479 }
   3480 
   3481 /// parseDirectiveExitMacro
   3482 /// ::= .exitm
   3483 bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
   3484   if (getLexer().isNot(AsmToken::EndOfStatement))
   3485     return TokError("unexpected token in '" + Directive + "' directive");
   3486 
   3487   if (!isInsideMacroInstantiation())
   3488     return TokError("unexpected '" + Directive + "' in file, "
   3489                                                  "no current macro definition");
   3490 
   3491   // Exit all conditionals that are active in the current macro.
   3492   while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
   3493     TheCondState = TheCondStack.back();
   3494     TheCondStack.pop_back();
   3495   }
   3496 
   3497   handleMacroExit();
   3498   return false;
   3499 }
   3500 
   3501 /// parseDirectiveEndMacro
   3502 /// ::= .endm
   3503 /// ::= .endmacro
   3504 bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
   3505   if (getLexer().isNot(AsmToken::EndOfStatement))
   3506     return TokError("unexpected token in '" + Directive + "' directive");
   3507 
   3508   // If we are inside a macro instantiation, terminate the current
   3509   // instantiation.
   3510   if (isInsideMacroInstantiation()) {
   3511     handleMacroExit();
   3512     return false;
   3513   }
   3514 
   3515   // Otherwise, this .endmacro is a stray entry in the file; well formed
   3516   // .endmacro directives are handled during the macro definition parsing.
   3517   return TokError("unexpected '" + Directive + "' in file, "
   3518                                                "no current macro definition");
   3519 }
   3520 
   3521 /// parseDirectivePurgeMacro
   3522 /// ::= .purgem
   3523 bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
   3524   StringRef Name;
   3525   if (parseIdentifier(Name))
   3526     return TokError("expected identifier in '.purgem' directive");
   3527 
   3528   if (getLexer().isNot(AsmToken::EndOfStatement))
   3529     return TokError("unexpected token in '.purgem' directive");
   3530 
   3531   if (!lookupMacro(Name))
   3532     return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
   3533 
   3534   undefineMacro(Name);
   3535   return false;
   3536 }
   3537 
   3538 /// parseDirectiveBundleAlignMode
   3539 /// ::= {.bundle_align_mode} expression
   3540 bool AsmParser::parseDirectiveBundleAlignMode() {
   3541   checkForValidSection();
   3542 
   3543   // Expect a single argument: an expression that evaluates to a constant
   3544   // in the inclusive range 0-30.
   3545   SMLoc ExprLoc = getLexer().getLoc();
   3546   int64_t AlignSizePow2;
   3547   if (parseAbsoluteExpression(AlignSizePow2))
   3548     return true;
   3549   else if (getLexer().isNot(AsmToken::EndOfStatement))
   3550     return TokError("unexpected token after expression in"
   3551                     " '.bundle_align_mode' directive");
   3552   else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
   3553     return Error(ExprLoc,
   3554                  "invalid bundle alignment size (expected between 0 and 30)");
   3555 
   3556   Lex();
   3557 
   3558   // Because of AlignSizePow2's verified range we can safely truncate it to
   3559   // unsigned.
   3560   getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
   3561   return false;
   3562 }
   3563 
   3564 /// parseDirectiveBundleLock
   3565 /// ::= {.bundle_lock} [align_to_end]
   3566 bool AsmParser::parseDirectiveBundleLock() {
   3567   checkForValidSection();
   3568   bool AlignToEnd = false;
   3569 
   3570   if (getLexer().isNot(AsmToken::EndOfStatement)) {
   3571     StringRef Option;
   3572     SMLoc Loc = getTok().getLoc();
   3573     const char *kInvalidOptionError =
   3574         "invalid option for '.bundle_lock' directive";
   3575 
   3576     if (parseIdentifier(Option))
   3577       return Error(Loc, kInvalidOptionError);
   3578 
   3579     if (Option != "align_to_end")
   3580       return Error(Loc, kInvalidOptionError);
   3581     else if (getLexer().isNot(AsmToken::EndOfStatement))
   3582       return Error(Loc,
   3583                    "unexpected token after '.bundle_lock' directive option");
   3584     AlignToEnd = true;
   3585   }
   3586 
   3587   Lex();
   3588 
   3589   getStreamer().EmitBundleLock(AlignToEnd);
   3590   return false;
   3591 }
   3592 
   3593 /// parseDirectiveBundleLock
   3594 /// ::= {.bundle_lock}
   3595 bool AsmParser::parseDirectiveBundleUnlock() {
   3596   checkForValidSection();
   3597 
   3598   if (getLexer().isNot(AsmToken::EndOfStatement))
   3599     return TokError("unexpected token in '.bundle_unlock' directive");
   3600   Lex();
   3601 
   3602   getStreamer().EmitBundleUnlock();
   3603   return false;
   3604 }
   3605 
   3606 /// parseDirectiveSpace
   3607 /// ::= (.skip | .space) expression [ , expression ]
   3608 bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
   3609   checkForValidSection();
   3610 
   3611   int64_t NumBytes;
   3612   if (parseAbsoluteExpression(NumBytes))
   3613     return true;
   3614 
   3615   int64_t FillExpr = 0;
   3616   if (getLexer().isNot(AsmToken::EndOfStatement)) {
   3617     if (getLexer().isNot(AsmToken::Comma))
   3618       return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
   3619     Lex();
   3620 
   3621     if (parseAbsoluteExpression(FillExpr))
   3622       return true;
   3623 
   3624     if (getLexer().isNot(AsmToken::EndOfStatement))
   3625       return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
   3626   }
   3627 
   3628   Lex();
   3629 
   3630   if (NumBytes <= 0)
   3631     return TokError("invalid number of bytes in '" + Twine(IDVal) +
   3632                     "' directive");
   3633 
   3634   // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
   3635   getStreamer().EmitFill(NumBytes, FillExpr);
   3636 
   3637   return false;
   3638 }
   3639 
   3640 /// parseDirectiveLEB128
   3641 /// ::= (.sleb128 | .uleb128) [ expression (, expression)* ]
   3642 bool AsmParser::parseDirectiveLEB128(bool Signed) {
   3643   checkForValidSection();
   3644   const MCExpr *Value;
   3645 
   3646   for (;;) {
   3647     if (parseExpression(Value))
   3648       return true;
   3649 
   3650     if (Signed)
   3651       getStreamer().EmitSLEB128Value(Value);
   3652     else
   3653       getStreamer().EmitULEB128Value(Value);
   3654 
   3655     if (getLexer().is(AsmToken::EndOfStatement))
   3656       break;
   3657 
   3658     if (getLexer().isNot(AsmToken::Comma))
   3659       return TokError("unexpected token in directive");
   3660     Lex();
   3661   }
   3662 
   3663   return false;
   3664 }
   3665 
   3666 /// parseDirectiveSymbolAttribute
   3667 ///  ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
   3668 bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
   3669   if (getLexer().isNot(AsmToken::EndOfStatement)) {
   3670     for (;;) {
   3671       StringRef Name;
   3672       SMLoc Loc = getTok().getLoc();
   3673 
   3674       if (parseIdentifier(Name))
   3675         return Error(Loc, "expected identifier in directive");
   3676 
   3677       MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
   3678 
   3679       // Assembler local symbols don't make any sense here. Complain loudly.
   3680       if (Sym->isTemporary())
   3681         return Error(Loc, "non-local symbol required in directive");
   3682 
   3683       if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
   3684         return Error(Loc, "unable to emit symbol attribute");
   3685 
   3686       if (getLexer().is(AsmToken::EndOfStatement))
   3687         break;
   3688 
   3689       if (getLexer().isNot(AsmToken::Comma))
   3690         return TokError("unexpected token in directive");
   3691       Lex();
   3692     }
   3693   }
   3694 
   3695   Lex();
   3696   return false;
   3697 }
   3698 
   3699 /// parseDirectiveComm
   3700 ///  ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
   3701 bool AsmParser::parseDirectiveComm(bool IsLocal) {
   3702   checkForValidSection();
   3703 
   3704   SMLoc IDLoc = getLexer().getLoc();
   3705   StringRef Name;
   3706   if (parseIdentifier(Name))
   3707     return TokError("expected identifier in directive");
   3708 
   3709   // Handle the identifier as the key symbol.
   3710   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
   3711 
   3712   if (getLexer().isNot(AsmToken::Comma))
   3713     return TokError("unexpected token in directive");
   3714   Lex();
   3715 
   3716   int64_t Size;
   3717   SMLoc SizeLoc = getLexer().getLoc();
   3718   if (parseAbsoluteExpression(Size))
   3719     return true;
   3720 
   3721   int64_t Pow2Alignment = 0;
   3722   SMLoc Pow2AlignmentLoc;
   3723   if (getLexer().is(AsmToken::Comma)) {
   3724     Lex();
   3725     Pow2AlignmentLoc = getLexer().getLoc();
   3726     if (parseAbsoluteExpression(Pow2Alignment))
   3727       return true;
   3728 
   3729     LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
   3730     if (IsLocal && LCOMM == LCOMM::NoAlignment)
   3731       return Error(Pow2AlignmentLoc, "alignment not supported on this target");
   3732 
   3733     // If this target takes alignments in bytes (not log) validate and convert.
   3734     if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
   3735         (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
   3736       if (!isPowerOf2_64(Pow2Alignment))
   3737         return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
   3738       Pow2Alignment = Log2_64(Pow2Alignment);
   3739     }
   3740   }
   3741 
   3742   if (getLexer().isNot(AsmToken::EndOfStatement))
   3743     return TokError("unexpected token in '.comm' or '.lcomm' directive");
   3744 
   3745   Lex();
   3746 
   3747   // NOTE: a size of zero for a .comm should create a undefined symbol
   3748   // but a size of .lcomm creates a bss symbol of size zero.
   3749   if (Size < 0)
   3750     return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
   3751                           "be less than zero");
   3752 
   3753   // NOTE: The alignment in the directive is a power of 2 value, the assembler
   3754   // may internally end up wanting an alignment in bytes.
   3755   // FIXME: Diagnose overflow.
   3756   if (Pow2Alignment < 0)
   3757     return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
   3758                                    "alignment, can't be less than zero");
   3759 
   3760   if (!Sym->isUndefined())
   3761     return Error(IDLoc, "invalid symbol redefinition");
   3762 
   3763   // Create the Symbol as a common or local common with Size and Pow2Alignment
   3764   if (IsLocal) {
   3765     getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
   3766     return false;
   3767   }
   3768 
   3769   getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
   3770   return false;
   3771 }
   3772 
   3773 /// parseDirectiveAbort
   3774 ///  ::= .abort [... message ...]
   3775 bool AsmParser::parseDirectiveAbort() {
   3776   // FIXME: Use loc from directive.
   3777   SMLoc Loc = getLexer().getLoc();
   3778 
   3779   StringRef Str = parseStringToEndOfStatement();
   3780   if (getLexer().isNot(AsmToken::EndOfStatement))
   3781     return TokError("unexpected token in '.abort' directive");
   3782 
   3783   Lex();
   3784 
   3785   if (Str.empty())
   3786     Error(Loc, ".abort detected. Assembly stopping.");
   3787   else
   3788     Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
   3789   // FIXME: Actually abort assembly here.
   3790 
   3791   return false;
   3792 }
   3793 
   3794 /// parseDirectiveInclude
   3795 ///  ::= .include "filename"
   3796 bool AsmParser::parseDirectiveInclude() {
   3797   if (getLexer().isNot(AsmToken::String))
   3798     return TokError("expected string in '.include' directive");
   3799 
   3800   // Allow the strings to have escaped octal character sequence.
   3801   std::string Filename;
   3802   if (parseEscapedString(Filename))
   3803     return true;
   3804   SMLoc IncludeLoc = getLexer().getLoc();
   3805   Lex();
   3806 
   3807   if (getLexer().isNot(AsmToken::EndOfStatement))
   3808     return TokError("unexpected token in '.include' directive");
   3809 
   3810   // Attempt to switch the lexer to the included file before consuming the end
   3811   // of statement to avoid losing it when we switch.
   3812   if (enterIncludeFile(Filename)) {
   3813     Error(IncludeLoc, "Could not find include file '" + Filename + "'");
   3814     return true;
   3815   }
   3816 
   3817   return false;
   3818 }
   3819 
   3820 /// parseDirectiveIncbin
   3821 ///  ::= .incbin "filename"
   3822 bool AsmParser::parseDirectiveIncbin() {
   3823   if (getLexer().isNot(AsmToken::String))
   3824     return TokError("expected string in '.incbin' directive");
   3825 
   3826   // Allow the strings to have escaped octal character sequence.
   3827   std::string Filename;
   3828   if (parseEscapedString(Filename))
   3829     return true;
   3830   SMLoc IncbinLoc = getLexer().getLoc();
   3831   Lex();
   3832 
   3833   if (getLexer().isNot(AsmToken::EndOfStatement))
   3834     return TokError("unexpected token in '.incbin' directive");
   3835 
   3836   // Attempt to process the included file.
   3837   if (processIncbinFile(Filename)) {
   3838     Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
   3839     return true;
   3840   }
   3841 
   3842   return false;
   3843 }
   3844 
   3845 /// parseDirectiveIf
   3846 /// ::= .if{,eq,ge,gt,le,lt,ne} expression
   3847 bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
   3848   TheCondStack.push_back(TheCondState);
   3849   TheCondState.TheCond = AsmCond::IfCond;
   3850   if (TheCondState.Ignore) {
   3851     eatToEndOfStatement();
   3852   } else {
   3853     int64_t ExprValue;
   3854     if (parseAbsoluteExpression(ExprValue))
   3855       return true;
   3856 
   3857     if (getLexer().isNot(AsmToken::EndOfStatement))
   3858       return TokError("unexpected token in '.if' directive");
   3859 
   3860     Lex();
   3861 
   3862     switch (DirKind) {
   3863     default:
   3864       llvm_unreachable("unsupported directive");
   3865     case DK_IF:
   3866     case DK_IFNE:
   3867       break;
   3868     case DK_IFEQ:
   3869       ExprValue = ExprValue == 0;
   3870       break;
   3871     case DK_IFGE:
   3872       ExprValue = ExprValue >= 0;
   3873       break;
   3874     case DK_IFGT:
   3875       ExprValue = ExprValue > 0;
   3876       break;
   3877     case DK_IFLE:
   3878       ExprValue = ExprValue <= 0;
   3879       break;
   3880     case DK_IFLT:
   3881       ExprValue = ExprValue < 0;
   3882       break;
   3883     }
   3884 
   3885     TheCondState.CondMet = ExprValue;
   3886     TheCondState.Ignore = !TheCondState.CondMet;
   3887   }
   3888 
   3889   return false;
   3890 }
   3891 
   3892 /// parseDirectiveIfb
   3893 /// ::= .ifb string
   3894 bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
   3895   TheCondStack.push_back(TheCondState);
   3896   TheCondState.TheCond = AsmCond::IfCond;
   3897 
   3898   if (TheCondState.Ignore) {
   3899     eatToEndOfStatement();
   3900   } else {
   3901     StringRef Str = parseStringToEndOfStatement();
   3902 
   3903     if (getLexer().isNot(AsmToken::EndOfStatement))
   3904       return TokError("unexpected token in '.ifb' directive");
   3905 
   3906     Lex();
   3907 
   3908     TheCondState.CondMet = ExpectBlank == Str.empty();
   3909     TheCondState.Ignore = !TheCondState.CondMet;
   3910   }
   3911 
   3912   return false;
   3913 }
   3914 
   3915 /// parseDirectiveIfc
   3916 /// ::= .ifc string1, string2
   3917 /// ::= .ifnc string1, string2
   3918 bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
   3919   TheCondStack.push_back(TheCondState);
   3920   TheCondState.TheCond = AsmCond::IfCond;
   3921 
   3922   if (TheCondState.Ignore) {
   3923     eatToEndOfStatement();
   3924   } else {
   3925     StringRef Str1 = parseStringToComma();
   3926 
   3927     if (getLexer().isNot(AsmToken::Comma))
   3928       return TokError("unexpected token in '.ifc' directive");
   3929 
   3930     Lex();
   3931 
   3932     StringRef Str2 = parseStringToEndOfStatement();
   3933 
   3934     if (getLexer().isNot(AsmToken::EndOfStatement))
   3935       return TokError("unexpected token in '.ifc' directive");
   3936 
   3937     Lex();
   3938 
   3939     TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
   3940     TheCondState.Ignore = !TheCondState.CondMet;
   3941   }
   3942 
   3943   return false;
   3944 }
   3945 
   3946 /// parseDirectiveIfeqs
   3947 ///   ::= .ifeqs string1, string2
   3948 bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) {
   3949   if (Lexer.isNot(AsmToken::String)) {
   3950     if (ExpectEqual)
   3951       TokError("expected string parameter for '.ifeqs' directive");
   3952     else
   3953       TokError("expected string parameter for '.ifnes' directive");
   3954     eatToEndOfStatement();
   3955     return true;
   3956   }
   3957 
   3958   StringRef String1 = getTok().getStringContents();
   3959   Lex();
   3960 
   3961   if (Lexer.isNot(AsmToken::Comma)) {
   3962     if (ExpectEqual)
   3963       TokError("expected comma after first string for '.ifeqs' directive");
   3964     else
   3965       TokError("expected comma after first string for '.ifnes' directive");
   3966     eatToEndOfStatement();
   3967     return true;
   3968   }
   3969 
   3970   Lex();
   3971 
   3972   if (Lexer.isNot(AsmToken::String)) {
   3973     if (ExpectEqual)
   3974       TokError("expected string parameter for '.ifeqs' directive");
   3975     else
   3976       TokError("expected string parameter for '.ifnes' directive");
   3977     eatToEndOfStatement();
   3978     return true;
   3979   }
   3980 
   3981   StringRef String2 = getTok().getStringContents();
   3982   Lex();
   3983 
   3984   TheCondStack.push_back(TheCondState);
   3985   TheCondState.TheCond = AsmCond::IfCond;
   3986   TheCondState.CondMet = ExpectEqual == (String1 == String2);
   3987   TheCondState.Ignore = !TheCondState.CondMet;
   3988 
   3989   return false;
   3990 }
   3991 
   3992 /// parseDirectiveIfdef
   3993 /// ::= .ifdef symbol
   3994 bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
   3995   StringRef Name;
   3996   TheCondStack.push_back(TheCondState);
   3997   TheCondState.TheCond = AsmCond::IfCond;
   3998 
   3999   if (TheCondState.Ignore) {
   4000     eatToEndOfStatement();
   4001   } else {
   4002     if (parseIdentifier(Name))
   4003       return TokError("expected identifier after '.ifdef'");
   4004 
   4005     Lex();
   4006 
   4007     MCSymbol *Sym = getContext().LookupSymbol(Name);
   4008 
   4009     if (expect_defined)
   4010       TheCondState.CondMet = (Sym && !Sym->isUndefined());
   4011     else
   4012       TheCondState.CondMet = (!Sym || Sym->isUndefined());
   4013     TheCondState.Ignore = !TheCondState.CondMet;
   4014   }
   4015 
   4016   return false;
   4017 }
   4018 
   4019 /// parseDirectiveElseIf
   4020 /// ::= .elseif expression
   4021 bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
   4022   if (TheCondState.TheCond != AsmCond::IfCond &&
   4023       TheCondState.TheCond != AsmCond::ElseIfCond)
   4024     Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
   4025                         " an .elseif");
   4026   TheCondState.TheCond = AsmCond::ElseIfCond;
   4027 
   4028   bool LastIgnoreState = false;
   4029   if (!TheCondStack.empty())
   4030     LastIgnoreState = TheCondStack.back().Ignore;
   4031   if (LastIgnoreState || TheCondState.CondMet) {
   4032     TheCondState.Ignore = true;
   4033     eatToEndOfStatement();
   4034   } else {
   4035     int64_t ExprValue;
   4036     if (parseAbsoluteExpression(ExprValue))
   4037       return true;
   4038 
   4039     if (getLexer().isNot(AsmToken::EndOfStatement))
   4040       return TokError("unexpected token in '.elseif' directive");
   4041 
   4042     Lex();
   4043     TheCondState.CondMet = ExprValue;
   4044     TheCondState.Ignore = !TheCondState.CondMet;
   4045   }
   4046 
   4047   return false;
   4048 }
   4049 
   4050 /// parseDirectiveElse
   4051 /// ::= .else
   4052 bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
   4053   if (getLexer().isNot(AsmToken::EndOfStatement))
   4054     return TokError("unexpected token in '.else' directive");
   4055 
   4056   Lex();
   4057 
   4058   if (TheCondState.TheCond != AsmCond::IfCond &&
   4059       TheCondState.TheCond != AsmCond::ElseIfCond)
   4060     Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
   4061                         ".elseif");
   4062   TheCondState.TheCond = AsmCond::ElseCond;
   4063   bool LastIgnoreState = false;
   4064   if (!TheCondStack.empty())
   4065     LastIgnoreState = TheCondStack.back().Ignore;
   4066   if (LastIgnoreState || TheCondState.CondMet)
   4067     TheCondState.Ignore = true;
   4068   else
   4069     TheCondState.Ignore = false;
   4070 
   4071   return false;
   4072 }
   4073 
   4074 /// parseDirectiveEnd
   4075 /// ::= .end
   4076 bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
   4077   if (getLexer().isNot(AsmToken::EndOfStatement))
   4078     return TokError("unexpected token in '.end' directive");
   4079 
   4080   Lex();
   4081 
   4082   while (Lexer.isNot(AsmToken::Eof))
   4083     Lex();
   4084 
   4085   return false;
   4086 }
   4087 
   4088 /// parseDirectiveError
   4089 ///   ::= .err
   4090 ///   ::= .error [string]
   4091 bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
   4092   if (!TheCondStack.empty()) {
   4093     if (TheCondStack.back().Ignore) {
   4094       eatToEndOfStatement();
   4095       return false;
   4096     }
   4097   }
   4098 
   4099   if (!WithMessage)
   4100     return Error(L, ".err encountered");
   4101 
   4102   StringRef Message = ".error directive invoked in source file";
   4103   if (Lexer.isNot(AsmToken::EndOfStatement)) {
   4104     if (Lexer.isNot(AsmToken::String)) {
   4105       TokError(".error argument must be a string");
   4106       eatToEndOfStatement();
   4107       return true;
   4108     }
   4109 
   4110     Message = getTok().getStringContents();
   4111     Lex();
   4112   }
   4113 
   4114   Error(L, Message);
   4115   return true;
   4116 }
   4117 
   4118 /// parseDirectiveWarning
   4119 ///   ::= .warning [string]
   4120 bool AsmParser::parseDirectiveWarning(SMLoc L) {
   4121   if (!TheCondStack.empty()) {
   4122     if (TheCondStack.back().Ignore) {
   4123       eatToEndOfStatement();
   4124       return false;
   4125     }
   4126   }
   4127 
   4128   StringRef Message = ".warning directive invoked in source file";
   4129   if (Lexer.isNot(AsmToken::EndOfStatement)) {
   4130     if (Lexer.isNot(AsmToken::String)) {
   4131       TokError(".warning argument must be a string");
   4132       eatToEndOfStatement();
   4133       return true;
   4134     }
   4135 
   4136     Message = getTok().getStringContents();
   4137     Lex();
   4138   }
   4139 
   4140   Warning(L, Message);
   4141   return false;
   4142 }
   4143 
   4144 /// parseDirectiveEndIf
   4145 /// ::= .endif
   4146 bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
   4147   if (getLexer().isNot(AsmToken::EndOfStatement))
   4148     return TokError("unexpected token in '.endif' directive");
   4149 
   4150   Lex();
   4151 
   4152   if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
   4153     Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
   4154                         ".else");
   4155   if (!TheCondStack.empty()) {
   4156     TheCondState = TheCondStack.back();
   4157     TheCondStack.pop_back();
   4158   }
   4159 
   4160   return false;
   4161 }
   4162 
   4163 void AsmParser::initializeDirectiveKindMap() {
   4164   DirectiveKindMap[".set"] = DK_SET;
   4165   DirectiveKindMap[".equ"] = DK_EQU;
   4166   DirectiveKindMap[".equiv"] = DK_EQUIV;
   4167   DirectiveKindMap[".ascii"] = DK_ASCII;
   4168   DirectiveKindMap[".asciz"] = DK_ASCIZ;
   4169   DirectiveKindMap[".string"] = DK_STRING;
   4170   DirectiveKindMap[".byte"] = DK_BYTE;
   4171   DirectiveKindMap[".short"] = DK_SHORT;
   4172   DirectiveKindMap[".value"] = DK_VALUE;
   4173   DirectiveKindMap[".2byte"] = DK_2BYTE;
   4174   DirectiveKindMap[".long"] = DK_LONG;
   4175   DirectiveKindMap[".int"] = DK_INT;
   4176   DirectiveKindMap[".4byte"] = DK_4BYTE;
   4177   DirectiveKindMap[".quad"] = DK_QUAD;
   4178   DirectiveKindMap[".8byte"] = DK_8BYTE;
   4179   DirectiveKindMap[".octa"] = DK_OCTA;
   4180   DirectiveKindMap[".single"] = DK_SINGLE;
   4181   DirectiveKindMap[".float"] = DK_FLOAT;
   4182   DirectiveKindMap[".double"] = DK_DOUBLE;
   4183   DirectiveKindMap[".align"] = DK_ALIGN;
   4184   DirectiveKindMap[".align32"] = DK_ALIGN32;
   4185   DirectiveKindMap[".balign"] = DK_BALIGN;
   4186   DirectiveKindMap[".balignw"] = DK_BALIGNW;
   4187   DirectiveKindMap[".balignl"] = DK_BALIGNL;
   4188   DirectiveKindMap[".p2align"] = DK_P2ALIGN;
   4189   DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
   4190   DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
   4191   DirectiveKindMap[".org"] = DK_ORG;
   4192   DirectiveKindMap[".fill"] = DK_FILL;
   4193   DirectiveKindMap[".zero"] = DK_ZERO;
   4194   DirectiveKindMap[".extern"] = DK_EXTERN;
   4195   DirectiveKindMap[".globl"] = DK_GLOBL;
   4196   DirectiveKindMap[".global"] = DK_GLOBAL;
   4197   DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
   4198   DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
   4199   DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
   4200   DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
   4201   DirectiveKindMap[".reference"] = DK_REFERENCE;
   4202   DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
   4203   DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
   4204   DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
   4205   DirectiveKindMap[".comm"] = DK_COMM;
   4206   DirectiveKindMap[".common"] = DK_COMMON;
   4207   DirectiveKindMap[".lcomm"] = DK_LCOMM;
   4208   DirectiveKindMap[".abort"] = DK_ABORT;
   4209   DirectiveKindMap[".include"] = DK_INCLUDE;
   4210   DirectiveKindMap[".incbin"] = DK_INCBIN;
   4211   DirectiveKindMap[".code16"] = DK_CODE16;
   4212   DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
   4213   DirectiveKindMap[".rept"] = DK_REPT;
   4214   DirectiveKindMap[".rep"] = DK_REPT;
   4215   DirectiveKindMap[".irp"] = DK_IRP;
   4216   DirectiveKindMap[".irpc"] = DK_IRPC;
   4217   DirectiveKindMap[".endr"] = DK_ENDR;
   4218   DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
   4219   DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
   4220   DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
   4221   DirectiveKindMap[".if"] = DK_IF;
   4222   DirectiveKindMap[".ifeq"] = DK_IFEQ;
   4223   DirectiveKindMap[".ifge"] = DK_IFGE;
   4224   DirectiveKindMap[".ifgt"] = DK_IFGT;
   4225   DirectiveKindMap[".ifle"] = DK_IFLE;
   4226   DirectiveKindMap[".iflt"] = DK_IFLT;
   4227   DirectiveKindMap[".ifne"] = DK_IFNE;
   4228   DirectiveKindMap[".ifb"] = DK_IFB;
   4229   DirectiveKindMap[".ifnb"] = DK_IFNB;
   4230   DirectiveKindMap[".ifc"] = DK_IFC;
   4231   DirectiveKindMap[".ifeqs"] = DK_IFEQS;
   4232   DirectiveKindMap[".ifnc"] = DK_IFNC;
   4233   DirectiveKindMap[".ifnes"] = DK_IFNES;
   4234   DirectiveKindMap[".ifdef"] = DK_IFDEF;
   4235   DirectiveKindMap[".ifndef"] = DK_IFNDEF;
   4236   DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
   4237   DirectiveKindMap[".elseif"] = DK_ELSEIF;
   4238   DirectiveKindMap[".else"] = DK_ELSE;
   4239   DirectiveKindMap[".end"] = DK_END;
   4240   DirectiveKindMap[".endif"] = DK_ENDIF;
   4241   DirectiveKindMap[".skip"] = DK_SKIP;
   4242   DirectiveKindMap[".space"] = DK_SPACE;
   4243   DirectiveKindMap[".file"] = DK_FILE;
   4244   DirectiveKindMap[".line"] = DK_LINE;
   4245   DirectiveKindMap[".loc"] = DK_LOC;
   4246   DirectiveKindMap[".stabs"] = DK_STABS;
   4247   DirectiveKindMap[".sleb128"] = DK_SLEB128;
   4248   DirectiveKindMap[".uleb128"] = DK_ULEB128;
   4249   DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
   4250   DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
   4251   DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
   4252   DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
   4253   DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
   4254   DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
   4255   DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
   4256   DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
   4257   DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
   4258   DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
   4259   DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
   4260   DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
   4261   DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
   4262   DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
   4263   DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
   4264   DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
   4265   DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
   4266   DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
   4267   DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
   4268   DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
   4269   DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
   4270   DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
   4271   DirectiveKindMap[".macro"] = DK_MACRO;
   4272   DirectiveKindMap[".exitm"] = DK_EXITM;
   4273   DirectiveKindMap[".endm"] = DK_ENDM;
   4274   DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
   4275   DirectiveKindMap[".purgem"] = DK_PURGEM;
   4276   DirectiveKindMap[".err"] = DK_ERR;
   4277   DirectiveKindMap[".error"] = DK_ERROR;
   4278   DirectiveKindMap[".warning"] = DK_WARNING;
   4279 }
   4280 
   4281 MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
   4282   AsmToken EndToken, StartToken = getTok();
   4283 
   4284   unsigned NestLevel = 0;
   4285   for (;;) {
   4286     // Check whether we have reached the end of the file.
   4287     if (getLexer().is(AsmToken::Eof)) {
   4288       Error(DirectiveLoc, "no matching '.endr' in definition");
   4289       return nullptr;
   4290     }
   4291 
   4292     if (Lexer.is(AsmToken::Identifier) &&
   4293         (getTok().getIdentifier() == ".rept")) {
   4294       ++NestLevel;
   4295     }
   4296 
   4297     // Otherwise, check whether we have reached the .endr.
   4298     if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
   4299       if (NestLevel == 0) {
   4300         EndToken = getTok();
   4301         Lex();
   4302         if (Lexer.isNot(AsmToken::EndOfStatement)) {
   4303           TokError("unexpected token in '.endr' directive");
   4304           return nullptr;
   4305         }
   4306         break;
   4307       }
   4308       --NestLevel;
   4309     }
   4310 
   4311     // Otherwise, scan till the end of the statement.
   4312     eatToEndOfStatement();
   4313   }
   4314 
   4315   const char *BodyStart = StartToken.getLoc().getPointer();
   4316   const char *BodyEnd = EndToken.getLoc().getPointer();
   4317   StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
   4318 
   4319   // We Are Anonymous.
   4320   MacroLikeBodies.push_back(
   4321       MCAsmMacro(StringRef(), Body, MCAsmMacroParameters()));
   4322   return &MacroLikeBodies.back();
   4323 }
   4324 
   4325 void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
   4326                                          raw_svector_ostream &OS) {
   4327   OS << ".endr\n";
   4328 
   4329   std::unique_ptr<MemoryBuffer> Instantiation =
   4330       MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
   4331 
   4332   // Create the macro instantiation object and add to the current macro
   4333   // instantiation stack.
   4334   MacroInstantiation *MI = new MacroInstantiation(
   4335       DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
   4336   ActiveMacros.push_back(MI);
   4337 
   4338   // Jump to the macro instantiation and prime the lexer.
   4339   CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
   4340   Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
   4341   Lex();
   4342 }
   4343 
   4344 /// parseDirectiveRept
   4345 ///   ::= .rep | .rept count
   4346 bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
   4347   const MCExpr *CountExpr;
   4348   SMLoc CountLoc = getTok().getLoc();
   4349   if (parseExpression(CountExpr))
   4350     return true;
   4351 
   4352   int64_t Count;
   4353   if (!CountExpr->EvaluateAsAbsolute(Count)) {
   4354     eatToEndOfStatement();
   4355     return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
   4356   }
   4357 
   4358   if (Count < 0)
   4359     return Error(CountLoc, "Count is negative");
   4360 
   4361   if (Lexer.isNot(AsmToken::EndOfStatement))
   4362     return TokError("unexpected token in '" + Dir + "' directive");
   4363 
   4364   // Eat the end of statement.
   4365   Lex();
   4366 
   4367   // Lex the rept definition.
   4368   MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
   4369   if (!M)
   4370     return true;
   4371 
   4372   // Macro instantiation is lexical, unfortunately. We construct a new buffer
   4373   // to hold the macro body with substitutions.
   4374   SmallString<256> Buf;
   4375   raw_svector_ostream OS(Buf);
   4376   while (Count--) {
   4377     if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
   4378       return true;
   4379   }
   4380   instantiateMacroLikeBody(M, DirectiveLoc, OS);
   4381 
   4382   return false;
   4383 }
   4384 
   4385 /// parseDirectiveIrp
   4386 /// ::= .irp symbol,values
   4387 bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
   4388   MCAsmMacroParameter Parameter;
   4389 
   4390   if (parseIdentifier(Parameter.Name))
   4391     return TokError("expected identifier in '.irp' directive");
   4392 
   4393   if (Lexer.isNot(AsmToken::Comma))
   4394     return TokError("expected comma in '.irp' directive");
   4395 
   4396   Lex();
   4397 
   4398   MCAsmMacroArguments A;
   4399   if (parseMacroArguments(nullptr, A))
   4400     return true;
   4401 
   4402   // Eat the end of statement.
   4403   Lex();
   4404 
   4405   // Lex the irp definition.
   4406   MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
   4407   if (!M)
   4408     return true;
   4409 
   4410   // Macro instantiation is lexical, unfortunately. We construct a new buffer
   4411   // to hold the macro body with substitutions.
   4412   SmallString<256> Buf;
   4413   raw_svector_ostream OS(Buf);
   4414 
   4415   for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
   4416     if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
   4417       return true;
   4418   }
   4419 
   4420   instantiateMacroLikeBody(M, DirectiveLoc, OS);
   4421 
   4422   return false;
   4423 }
   4424 
   4425 /// parseDirectiveIrpc
   4426 /// ::= .irpc symbol,values
   4427 bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
   4428   MCAsmMacroParameter Parameter;
   4429 
   4430   if (parseIdentifier(Parameter.Name))
   4431     return TokError("expected identifier in '.irpc' directive");
   4432 
   4433   if (Lexer.isNot(AsmToken::Comma))
   4434     return TokError("expected comma in '.irpc' directive");
   4435 
   4436   Lex();
   4437 
   4438   MCAsmMacroArguments A;
   4439   if (parseMacroArguments(nullptr, A))
   4440     return true;
   4441 
   4442   if (A.size() != 1 || A.front().size() != 1)
   4443     return TokError("unexpected token in '.irpc' directive");
   4444 
   4445   // Eat the end of statement.
   4446   Lex();
   4447 
   4448   // Lex the irpc definition.
   4449   MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
   4450   if (!M)
   4451     return true;
   4452 
   4453   // Macro instantiation is lexical, unfortunately. We construct a new buffer
   4454   // to hold the macro body with substitutions.
   4455   SmallString<256> Buf;
   4456   raw_svector_ostream OS(Buf);
   4457 
   4458   StringRef Values = A.front().front().getString();
   4459   for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
   4460     MCAsmMacroArgument Arg;
   4461     Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
   4462 
   4463     if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
   4464       return true;
   4465   }
   4466 
   4467   instantiateMacroLikeBody(M, DirectiveLoc, OS);
   4468 
   4469   return false;
   4470 }
   4471 
   4472 bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
   4473   if (ActiveMacros.empty())
   4474     return TokError("unmatched '.endr' directive");
   4475 
   4476   // The only .repl that should get here are the ones created by
   4477   // instantiateMacroLikeBody.
   4478   assert(getLexer().is(AsmToken::EndOfStatement));
   4479 
   4480   handleMacroExit();
   4481   return false;
   4482 }
   4483 
   4484 bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
   4485                                      size_t Len) {
   4486   const MCExpr *Value;
   4487   SMLoc ExprLoc = getLexer().getLoc();
   4488   if (parseExpression(Value))
   4489     return true;
   4490   const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
   4491   if (!MCE)
   4492     return Error(ExprLoc, "unexpected expression in _emit");
   4493   uint64_t IntValue = MCE->getValue();
   4494   if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
   4495     return Error(ExprLoc, "literal value out of range for directive");
   4496 
   4497   Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
   4498   return false;
   4499 }
   4500 
   4501 bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
   4502   const MCExpr *Value;
   4503   SMLoc ExprLoc = getLexer().getLoc();
   4504   if (parseExpression(Value))
   4505     return true;
   4506   const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
   4507   if (!MCE)
   4508     return Error(ExprLoc, "unexpected expression in align");
   4509   uint64_t IntValue = MCE->getValue();
   4510   if (!isPowerOf2_64(IntValue))
   4511     return Error(ExprLoc, "literal value not a power of two greater then zero");
   4512 
   4513   Info.AsmRewrites->push_back(
   4514       AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
   4515   return false;
   4516 }
   4517 
   4518 // We are comparing pointers, but the pointers are relative to a single string.
   4519 // Thus, this should always be deterministic.
   4520 static int rewritesSort(const AsmRewrite *AsmRewriteA,
   4521                         const AsmRewrite *AsmRewriteB) {
   4522   if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
   4523     return -1;
   4524   if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
   4525     return 1;
   4526 
   4527   // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
   4528   // rewrite to the same location.  Make sure the SizeDirective rewrite is
   4529   // performed first, then the Imm/ImmPrefix and finally the Input/Output.  This
   4530   // ensures the sort algorithm is stable.
   4531   if (AsmRewritePrecedence[AsmRewriteA->Kind] >
   4532       AsmRewritePrecedence[AsmRewriteB->Kind])
   4533     return -1;
   4534 
   4535   if (AsmRewritePrecedence[AsmRewriteA->Kind] <
   4536       AsmRewritePrecedence[AsmRewriteB->Kind])
   4537     return 1;
   4538   llvm_unreachable("Unstable rewrite sort.");
   4539 }
   4540 
   4541 bool AsmParser::parseMSInlineAsm(
   4542     void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
   4543     unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
   4544     SmallVectorImpl<std::string> &Constraints,
   4545     SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
   4546     const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
   4547   SmallVector<void *, 4> InputDecls;
   4548   SmallVector<void *, 4> OutputDecls;
   4549   SmallVector<bool, 4> InputDeclsAddressOf;
   4550   SmallVector<bool, 4> OutputDeclsAddressOf;
   4551   SmallVector<std::string, 4> InputConstraints;
   4552   SmallVector<std::string, 4> OutputConstraints;
   4553   SmallVector<unsigned, 4> ClobberRegs;
   4554 
   4555   SmallVector<AsmRewrite, 4> AsmStrRewrites;
   4556 
   4557   // Prime the lexer.
   4558   Lex();
   4559 
   4560   // While we have input, parse each statement.
   4561   unsigned InputIdx = 0;
   4562   unsigned OutputIdx = 0;
   4563   while (getLexer().isNot(AsmToken::Eof)) {
   4564     ParseStatementInfo Info(&AsmStrRewrites);
   4565     if (parseStatement(Info, &SI))
   4566       return true;
   4567 
   4568     if (Info.ParseError)
   4569       return true;
   4570 
   4571     if (Info.Opcode == ~0U)
   4572       continue;
   4573 
   4574     const MCInstrDesc &Desc = MII->get(Info.Opcode);
   4575 
   4576     // Build the list of clobbers, outputs and inputs.
   4577     for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
   4578       MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
   4579 
   4580       // Immediate.
   4581       if (Operand.isImm())
   4582         continue;
   4583 
   4584       // Register operand.
   4585       if (Operand.isReg() && !Operand.needAddressOf() &&
   4586           !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
   4587         unsigned NumDefs = Desc.getNumDefs();
   4588         // Clobber.
   4589         if (NumDefs && Operand.getMCOperandNum() < NumDefs)
   4590           ClobberRegs.push_back(Operand.getReg());
   4591         continue;
   4592       }
   4593 
   4594       // Expr/Input or Output.
   4595       StringRef SymName = Operand.getSymName();
   4596       if (SymName.empty())
   4597         continue;
   4598 
   4599       void *OpDecl = Operand.getOpDecl();
   4600       if (!OpDecl)
   4601         continue;
   4602 
   4603       bool isOutput = (i == 1) && Desc.mayStore();
   4604       SMLoc Start = SMLoc::getFromPointer(SymName.data());
   4605       if (isOutput) {
   4606         ++InputIdx;
   4607         OutputDecls.push_back(OpDecl);
   4608         OutputDeclsAddressOf.push_back(Operand.needAddressOf());
   4609         OutputConstraints.push_back(("=" + Operand.getConstraint()).str());
   4610         AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
   4611       } else {
   4612         InputDecls.push_back(OpDecl);
   4613         InputDeclsAddressOf.push_back(Operand.needAddressOf());
   4614         InputConstraints.push_back(Operand.getConstraint().str());
   4615         AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
   4616       }
   4617     }
   4618 
   4619     // Consider implicit defs to be clobbers.  Think of cpuid and push.
   4620     ArrayRef<uint16_t> ImpDefs(Desc.getImplicitDefs(),
   4621                                Desc.getNumImplicitDefs());
   4622     ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
   4623   }
   4624 
   4625   // Set the number of Outputs and Inputs.
   4626   NumOutputs = OutputDecls.size();
   4627   NumInputs = InputDecls.size();
   4628 
   4629   // Set the unique clobbers.
   4630   array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
   4631   ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
   4632                     ClobberRegs.end());
   4633   Clobbers.assign(ClobberRegs.size(), std::string());
   4634   for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
   4635     raw_string_ostream OS(Clobbers[I]);
   4636     IP->printRegName(OS, ClobberRegs[I]);
   4637   }
   4638 
   4639   // Merge the various outputs and inputs.  Output are expected first.
   4640   if (NumOutputs || NumInputs) {
   4641     unsigned NumExprs = NumOutputs + NumInputs;
   4642     OpDecls.resize(NumExprs);
   4643     Constraints.resize(NumExprs);
   4644     for (unsigned i = 0; i < NumOutputs; ++i) {
   4645       OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
   4646       Constraints[i] = OutputConstraints[i];
   4647     }
   4648     for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
   4649       OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
   4650       Constraints[j] = InputConstraints[i];
   4651     }
   4652   }
   4653 
   4654   // Build the IR assembly string.
   4655   std::string AsmStringIR;
   4656   raw_string_ostream OS(AsmStringIR);
   4657   StringRef ASMString =
   4658       SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
   4659   const char *AsmStart = ASMString.begin();
   4660   const char *AsmEnd = ASMString.end();
   4661   array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
   4662   for (const AsmRewrite &AR : AsmStrRewrites) {
   4663     AsmRewriteKind Kind = AR.Kind;
   4664     if (Kind == AOK_Delete)
   4665       continue;
   4666 
   4667     const char *Loc = AR.Loc.getPointer();
   4668     assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
   4669 
   4670     // Emit everything up to the immediate/expression.
   4671     if (unsigned Len = Loc - AsmStart)
   4672       OS << StringRef(AsmStart, Len);
   4673 
   4674     // Skip the original expression.
   4675     if (Kind == AOK_Skip) {
   4676       AsmStart = Loc + AR.Len;
   4677       continue;
   4678     }
   4679 
   4680     unsigned AdditionalSkip = 0;
   4681     // Rewrite expressions in $N notation.
   4682     switch (Kind) {
   4683     default:
   4684       break;
   4685     case AOK_Imm:
   4686       OS << "$$" << AR.Val;
   4687       break;
   4688     case AOK_ImmPrefix:
   4689       OS << "$$";
   4690       break;
   4691     case AOK_Label:
   4692       OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
   4693       break;
   4694     case AOK_Input:
   4695       OS << '$' << InputIdx++;
   4696       break;
   4697     case AOK_Output:
   4698       OS << '$' << OutputIdx++;
   4699       break;
   4700     case AOK_SizeDirective:
   4701       switch (AR.Val) {
   4702       default: break;
   4703       case 8:  OS << "byte ptr "; break;
   4704       case 16: OS << "word ptr "; break;
   4705       case 32: OS << "dword ptr "; break;
   4706       case 64: OS << "qword ptr "; break;
   4707       case 80: OS << "xword ptr "; break;
   4708       case 128: OS << "xmmword ptr "; break;
   4709       case 256: OS << "ymmword ptr "; break;
   4710       }
   4711       break;
   4712     case AOK_Emit:
   4713       OS << ".byte";
   4714       break;
   4715     case AOK_Align: {
   4716       unsigned Val = AR.Val;
   4717       OS << ".align " << Val;
   4718 
   4719       // Skip the original immediate.
   4720       assert(Val < 10 && "Expected alignment less then 2^10.");
   4721       AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
   4722       break;
   4723     }
   4724     case AOK_DotOperator:
   4725       // Insert the dot if the user omitted it.
   4726       OS.flush();
   4727       if (AsmStringIR.back() != '.')
   4728         OS << '.';
   4729       OS << AR.Val;
   4730       break;
   4731     }
   4732 
   4733     // Skip the original expression.
   4734     AsmStart = Loc + AR.Len + AdditionalSkip;
   4735   }
   4736 
   4737   // Emit the remainder of the asm string.
   4738   if (AsmStart != AsmEnd)
   4739     OS << StringRef(AsmStart, AsmEnd - AsmStart);
   4740 
   4741   AsmString = OS.str();
   4742   return false;
   4743 }
   4744 
   4745 /// \brief Create an MCAsmParser instance.
   4746 MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
   4747                                      MCStreamer &Out, const MCAsmInfo &MAI) {
   4748   return new AsmParser(SM, C, Out, MAI);
   4749 }
   4750